fe0b6b4d9ed8db460cc9d5152d5e9a16805130a8
[babeltrace.git] / formats / ctf / metadata / ctf-visitor-generate-io-struct.c
1 /*
2 * ctf-visitor-generate-io-struct.c
3 *
4 * Common Trace Format Metadata Visitor (generate I/O structures).
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <assert.h>
32 #include <glib.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <babeltrace/babeltrace-internal.h>
36 #include <babeltrace/list.h>
37 #include <babeltrace/types.h>
38 #include <babeltrace/ctf/metadata.h>
39 #include <babeltrace/uuid.h>
40 #include <babeltrace/endian.h>
41 #include <babeltrace/ctf/events-internal.h>
42 #include "ctf-scanner.h"
43 #include "ctf-parser.h"
44 #include "ctf-ast.h"
45
46 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
47
48 #define _bt_list_first_entry(ptr, type, member) \
49 bt_list_entry((ptr)->next, type, member)
50
51 struct last_enum_value {
52 union {
53 int64_t s;
54 uint64_t u;
55 } u;
56 };
57
58 int opt_clock_force_correlate;
59
60 static
61 struct bt_declaration *ctf_type_specifier_list_visit(FILE *fd,
62 int depth, struct ctf_node *type_specifier_list,
63 struct declaration_scope *declaration_scope,
64 struct ctf_trace *trace);
65
66 static
67 int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node,
68 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace);
69
70 static
71 int is_unary_string(struct bt_list_head *head)
72 {
73 struct ctf_node *node;
74
75 bt_list_for_each_entry(node, head, siblings) {
76 if (node->type != NODE_UNARY_EXPRESSION)
77 return 0;
78 if (node->u.unary_expression.type != UNARY_STRING)
79 return 0;
80 }
81 return 1;
82 }
83
84 /*
85 * String returned must be freed by the caller using g_free.
86 */
87 static
88 char *concatenate_unary_strings(struct bt_list_head *head)
89 {
90 struct ctf_node *node;
91 GString *str;
92 int i = 0;
93
94 str = g_string_new("");
95 bt_list_for_each_entry(node, head, siblings) {
96 char *src_string;
97
98 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 bt_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 bt_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 = bt_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 = bt_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 bt_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 = bt_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 bt_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 (bt_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 bt_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 bt_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 (bt_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 bt_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 bt_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 = bt_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 bt_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 = bt_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 bt_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 bt_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 bt_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 bt_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 (bt_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 = bt_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 = bt_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 bt_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 bt_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 (bt_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 = bt_untagged_bt_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 = bt_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 = bt_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 bt_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 bt_enum_unsigned_insert(enum_declaration, start, end, q);
1051 }
1052 return 0;
1053 }
1054
1055 static
1056 struct bt_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 bt_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 bt_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 (bt_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 = bt_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 = bt_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 = bt_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 bt_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 bt_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 (void) g_string_free(str, TRUE);
1163 return NULL;
1164 }
1165 str_c = g_string_free(str, FALSE);
1166 id_q = g_quark_from_string(str_c);
1167 g_free(str_c);
1168 declaration = bt_lookup_declaration(id_q, declaration_scope);
1169 if (!declaration)
1170 return NULL;
1171 bt_declaration_ref(declaration);
1172 return declaration;
1173 }
1174
1175 /*
1176 * Returns 0/1 boolean, or < 0 on error.
1177 */
1178 static
1179 int get_boolean(FILE *fd, int depth, struct ctf_node *unary_expression)
1180 {
1181 if (unary_expression->type != NODE_UNARY_EXPRESSION) {
1182 fprintf(fd, "[error] %s: expecting unary expression\n",
1183 __func__);
1184 return -EINVAL;
1185 }
1186 switch (unary_expression->u.unary_expression.type) {
1187 case UNARY_UNSIGNED_CONSTANT:
1188 if (unary_expression->u.unary_expression.u.unsigned_constant == 0)
1189 return 0;
1190 else
1191 return 1;
1192 case UNARY_SIGNED_CONSTANT:
1193 if (unary_expression->u.unary_expression.u.signed_constant == 0)
1194 return 0;
1195 else
1196 return 1;
1197 case UNARY_STRING:
1198 if (!strcmp(unary_expression->u.unary_expression.u.string, "true"))
1199 return 1;
1200 else if (!strcmp(unary_expression->u.unary_expression.u.string, "TRUE"))
1201 return 1;
1202 else if (!strcmp(unary_expression->u.unary_expression.u.string, "false"))
1203 return 0;
1204 else if (!strcmp(unary_expression->u.unary_expression.u.string, "FALSE"))
1205 return 0;
1206 else {
1207 fprintf(fd, "[error] %s: unexpected string \"%s\"\n",
1208 __func__, unary_expression->u.unary_expression.u.string);
1209 return -EINVAL;
1210 }
1211 break;
1212 default:
1213 fprintf(fd, "[error] %s: unexpected unary expression type\n",
1214 __func__);
1215 return -EINVAL;
1216 }
1217
1218 }
1219
1220 static
1221 int get_trace_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression)
1222 {
1223 int byte_order;
1224
1225 if (unary_expression->u.unary_expression.type != UNARY_STRING) {
1226 fprintf(fd, "[error] %s: byte_order: expecting string\n",
1227 __func__);
1228 return -EINVAL;
1229 }
1230 if (!strcmp(unary_expression->u.unary_expression.u.string, "be"))
1231 byte_order = BIG_ENDIAN;
1232 else if (!strcmp(unary_expression->u.unary_expression.u.string, "le"))
1233 byte_order = LITTLE_ENDIAN;
1234 else {
1235 fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"be\" or \"le\".\n",
1236 __func__, unary_expression->u.unary_expression.u.string);
1237 return -EINVAL;
1238 }
1239 return byte_order;
1240 }
1241
1242 static
1243 int get_byte_order(FILE *fd, int depth, struct ctf_node *unary_expression,
1244 struct ctf_trace *trace)
1245 {
1246 int byte_order;
1247
1248 if (unary_expression->u.unary_expression.type != UNARY_STRING) {
1249 fprintf(fd, "[error] %s: byte_order: expecting string\n",
1250 __func__);
1251 return -EINVAL;
1252 }
1253 if (!strcmp(unary_expression->u.unary_expression.u.string, "native"))
1254 byte_order = trace->byte_order;
1255 else if (!strcmp(unary_expression->u.unary_expression.u.string, "network"))
1256 byte_order = BIG_ENDIAN;
1257 else if (!strcmp(unary_expression->u.unary_expression.u.string, "be"))
1258 byte_order = BIG_ENDIAN;
1259 else if (!strcmp(unary_expression->u.unary_expression.u.string, "le"))
1260 byte_order = LITTLE_ENDIAN;
1261 else {
1262 fprintf(fd, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
1263 __func__, unary_expression->u.unary_expression.u.string);
1264 return -EINVAL;
1265 }
1266 return byte_order;
1267 }
1268
1269 static
1270 struct bt_declaration *ctf_declaration_integer_visit(FILE *fd, int depth,
1271 struct bt_list_head *expressions,
1272 struct ctf_trace *trace)
1273 {
1274 struct ctf_node *expression;
1275 uint64_t alignment = 1, size = 0;
1276 int byte_order = trace->byte_order;
1277 int signedness = 0;
1278 int has_alignment = 0, has_size = 0;
1279 int base = 0;
1280 enum ctf_string_encoding encoding = CTF_STRING_NONE;
1281 struct ctf_clock *clock = NULL;
1282 struct declaration_integer *integer_declaration;
1283
1284 bt_list_for_each_entry(expression, expressions, siblings) {
1285 struct ctf_node *left, *right;
1286
1287 left = _bt_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings);
1288 right = _bt_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings);
1289 assert(left->u.unary_expression.type == UNARY_STRING);
1290 if (!strcmp(left->u.unary_expression.u.string, "signed")) {
1291 signedness = get_boolean(fd, depth, right);
1292 if (signedness < 0)
1293 return NULL;
1294 } else if (!strcmp(left->u.unary_expression.u.string, "byte_order")) {
1295 byte_order = get_byte_order(fd, depth, right, trace);
1296 if (byte_order < 0)
1297 return NULL;
1298 } else if (!strcmp(left->u.unary_expression.u.string, "size")) {
1299 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1300 fprintf(fd, "[error] %s: size: expecting unsigned constant\n",
1301 __func__);
1302 return NULL;
1303 }
1304 size = right->u.unary_expression.u.unsigned_constant;
1305 has_size = 1;
1306 } else if (!strcmp(left->u.unary_expression.u.string, "align")) {
1307 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1308 fprintf(fd, "[error] %s: align: expecting unsigned constant\n",
1309 __func__);
1310 return NULL;
1311 }
1312 alignment = right->u.unary_expression.u.unsigned_constant;
1313 /* Make sure alignment is a power of two */
1314 if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
1315 fprintf(fd, "[error] %s: align: expecting power of two\n",
1316 __func__);
1317 return NULL;
1318 }
1319 has_alignment = 1;
1320 } else if (!strcmp(left->u.unary_expression.u.string, "base")) {
1321 switch (right->u.unary_expression.type) {
1322 case UNARY_UNSIGNED_CONSTANT:
1323 switch (right->u.unary_expression.u.unsigned_constant) {
1324 case 2:
1325 case 8:
1326 case 10:
1327 case 16:
1328 base = right->u.unary_expression.u.unsigned_constant;
1329 break;
1330 default:
1331 fprintf(fd, "[error] %s: base not supported (%" PRIu64 ")\n",
1332 __func__, right->u.unary_expression.u.unsigned_constant);
1333 return NULL;
1334 }
1335 break;
1336 case UNARY_STRING:
1337 {
1338 char *s_right = concatenate_unary_strings(&expression->u.ctf_expression.right);
1339 if (!s_right) {
1340 fprintf(fd, "[error] %s: unexpected unary expression for integer base\n", __func__);
1341 g_free(s_right);
1342 return NULL;
1343 }
1344 if (!strcmp(s_right, "decimal") || !strcmp(s_right, "dec") || !strcmp(s_right, "d")
1345 || !strcmp(s_right, "i") || !strcmp(s_right, "u")) {
1346 base = 10;
1347 } else if (!strcmp(s_right, "hexadecimal") || !strcmp(s_right, "hex")
1348 || !strcmp(s_right, "x") || !strcmp(s_right, "X")
1349 || !strcmp(s_right, "p")) {
1350 base = 16;
1351 } else if (!strcmp(s_right, "octal") || !strcmp(s_right, "oct")
1352 || !strcmp(s_right, "o")) {
1353 base = 8;
1354 } else if (!strcmp(s_right, "binary") || !strcmp(s_right, "b")) {
1355 base = 2;
1356 } else {
1357 fprintf(fd, "[error] %s: unexpected expression for integer base (%s)\n", __func__, s_right);
1358 g_free(s_right);
1359 return NULL;
1360 }
1361
1362 g_free(s_right);
1363 break;
1364 }
1365 default:
1366 fprintf(fd, "[error] %s: base: expecting unsigned constant or unary string\n",
1367 __func__);
1368 return NULL;
1369 }
1370 } else if (!strcmp(left->u.unary_expression.u.string, "encoding")) {
1371 char *s_right;
1372
1373 if (right->u.unary_expression.type != UNARY_STRING) {
1374 fprintf(fd, "[error] %s: encoding: expecting unary string\n",
1375 __func__);
1376 return NULL;
1377 }
1378 s_right = concatenate_unary_strings(&expression->u.ctf_expression.right);
1379 if (!s_right) {
1380 fprintf(fd, "[error] %s: unexpected unary expression for integer base\n", __func__);
1381 g_free(s_right);
1382 return NULL;
1383 }
1384 if (!strcmp(s_right, "UTF8")
1385 || !strcmp(s_right, "utf8")
1386 || !strcmp(s_right, "utf-8")
1387 || !strcmp(s_right, "UTF-8"))
1388 encoding = CTF_STRING_UTF8;
1389 else if (!strcmp(s_right, "ASCII")
1390 || !strcmp(s_right, "ascii"))
1391 encoding = CTF_STRING_ASCII;
1392 else if (!strcmp(s_right, "none"))
1393 encoding = CTF_STRING_NONE;
1394 else {
1395 fprintf(fd, "[error] %s: unknown string encoding \"%s\"\n", __func__, s_right);
1396 g_free(s_right);
1397 return NULL;
1398 }
1399 g_free(s_right);
1400 } else if (!strcmp(left->u.unary_expression.u.string, "map")) {
1401 GQuark clock_name;
1402
1403 if (right->u.unary_expression.type != UNARY_STRING) {
1404 fprintf(fd, "[error] %s: map: expecting identifier\n",
1405 __func__);
1406 return NULL;
1407 }
1408 /* currently only support clock.name.value */
1409 clock_name = get_map_clock_name_value(&expression->u.ctf_expression.right);
1410 if (!clock_name) {
1411 char *s_right;
1412
1413 s_right = concatenate_unary_strings(&expression->u.ctf_expression.right);
1414 if (!s_right) {
1415 fprintf(fd, "[error] %s: unexpected unary expression for integer map\n", __func__);
1416 g_free(s_right);
1417 return NULL;
1418 }
1419 fprintf(fd, "[warning] %s: unknown map %s in integer declaration\n", __func__,
1420 s_right);
1421 g_free(s_right);
1422 continue;
1423 }
1424 clock = trace_clock_lookup(trace, clock_name);
1425 if (!clock) {
1426 fprintf(fd, "[error] %s: map: unable to find clock %s declaration\n",
1427 __func__, g_quark_to_string(clock_name));
1428 return NULL;
1429 }
1430 } else {
1431 fprintf(fd, "[warning] %s: unknown attribute name %s\n",
1432 __func__, left->u.unary_expression.u.string);
1433 /* Fall-through after warning */
1434 }
1435 }
1436 if (!has_size) {
1437 fprintf(fd, "[error] %s: missing size attribute\n", __func__);
1438 return NULL;
1439 }
1440 if (!has_alignment) {
1441 if (size % CHAR_BIT) {
1442 /* bit-packed alignment */
1443 alignment = 1;
1444 } else {
1445 /* byte-packed alignment */
1446 alignment = CHAR_BIT;
1447 }
1448 }
1449 integer_declaration = bt_integer_declaration_new(size,
1450 byte_order, signedness, alignment,
1451 base, encoding, clock);
1452 return &integer_declaration->p;
1453 }
1454
1455 static
1456 struct bt_declaration *ctf_declaration_floating_point_visit(FILE *fd, int depth,
1457 struct bt_list_head *expressions,
1458 struct ctf_trace *trace)
1459 {
1460 struct ctf_node *expression;
1461 uint64_t alignment = 1, exp_dig = 0, mant_dig = 0,
1462 byte_order = trace->byte_order;
1463 int has_alignment = 0, has_exp_dig = 0, has_mant_dig = 0;
1464 struct declaration_float *float_declaration;
1465
1466 bt_list_for_each_entry(expression, expressions, siblings) {
1467 struct ctf_node *left, *right;
1468
1469 left = _bt_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings);
1470 right = _bt_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings);
1471 assert(left->u.unary_expression.type == UNARY_STRING);
1472 if (!strcmp(left->u.unary_expression.u.string, "byte_order")) {
1473 byte_order = get_byte_order(fd, depth, right, trace);
1474 if (byte_order < 0)
1475 return NULL;
1476 } else if (!strcmp(left->u.unary_expression.u.string, "exp_dig")) {
1477 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1478 fprintf(fd, "[error] %s: exp_dig: expecting unsigned constant\n",
1479 __func__);
1480 return NULL;
1481 }
1482 exp_dig = right->u.unary_expression.u.unsigned_constant;
1483 has_exp_dig = 1;
1484 } else if (!strcmp(left->u.unary_expression.u.string, "mant_dig")) {
1485 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1486 fprintf(fd, "[error] %s: mant_dig: expecting unsigned constant\n",
1487 __func__);
1488 return NULL;
1489 }
1490 mant_dig = right->u.unary_expression.u.unsigned_constant;
1491 has_mant_dig = 1;
1492 } else if (!strcmp(left->u.unary_expression.u.string, "align")) {
1493 if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
1494 fprintf(fd, "[error] %s: align: expecting unsigned constant\n",
1495 __func__);
1496 return NULL;
1497 }
1498 alignment = right->u.unary_expression.u.unsigned_constant;
1499 /* Make sure alignment is a power of two */
1500 if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
1501 fprintf(fd, "[error] %s: align: expecting power of two\n",
1502 __func__);
1503 return NULL;
1504 }
1505 has_alignment = 1;
1506 } else {
1507 fprintf(fd, "[warning] %s: unknown attribute name %s\n",
1508 __func__, left->u.unary_expression.u.string);
1509 /* Fall-through after warning */
1510 }
1511 }
1512 if (!has_mant_dig) {
1513 fprintf(fd, "[error] %s: missing mant_dig attribute\n", __func__);
1514 return NULL;
1515 }
1516 if (!has_exp_dig) {
1517 fprintf(fd, "[error] %s: missing exp_dig attribute\n", __func__);
1518 return NULL;
1519 }
1520 if (!has_alignment) {
1521 if ((mant_dig + exp_dig) % CHAR_BIT) {
1522 /* bit-packed alignment */
1523 alignment = 1;
1524 } else {
1525 /* byte-packed alignment */
1526 alignment = CHAR_BIT;
1527 }
1528 }
1529 float_declaration = bt_float_declaration_new(mant_dig, exp_dig,
1530 byte_order, alignment);
1531 return &float_declaration->p;
1532 }
1533
1534 static
1535 struct bt_declaration *ctf_declaration_string_visit(FILE *fd, int depth,
1536 struct bt_list_head *expressions,
1537 struct ctf_trace *trace)
1538 {
1539 struct ctf_node *expression;
1540 const char *encoding_c = NULL;
1541 enum ctf_string_encoding encoding = CTF_STRING_UTF8;
1542 struct declaration_string *string_declaration;
1543
1544 bt_list_for_each_entry(expression, expressions, siblings) {
1545 struct ctf_node *left, *right;
1546
1547 left = _bt_list_first_entry(&expression->u.ctf_expression.left, struct ctf_node, siblings);
1548 right = _bt_list_first_entry(&expression->u.ctf_expression.right, struct ctf_node, siblings);
1549 assert(left->u.unary_expression.type == UNARY_STRING);
1550 if (!strcmp(left->u.unary_expression.u.string, "encoding")) {
1551 if (right->u.unary_expression.type != UNARY_STRING) {
1552 fprintf(fd, "[error] %s: encoding: expecting string\n",
1553 __func__);
1554 return NULL;
1555 }
1556 encoding_c = right->u.unary_expression.u.string;
1557 } else {
1558 fprintf(fd, "[warning] %s: unknown attribute name %s\n",
1559 __func__, left->u.unary_expression.u.string);
1560 /* Fall-through after warning */
1561 }
1562 }
1563 if (encoding_c && !strcmp(encoding_c, "ASCII"))
1564 encoding = CTF_STRING_ASCII;
1565 string_declaration = bt_string_declaration_new(encoding);
1566 return &string_declaration->p;
1567 }
1568
1569
1570 static
1571 struct bt_declaration *ctf_type_specifier_list_visit(FILE *fd,
1572 int depth, struct ctf_node *type_specifier_list,
1573 struct declaration_scope *declaration_scope,
1574 struct ctf_trace *trace)
1575 {
1576 struct ctf_node *first;
1577 struct ctf_node *node;
1578
1579 assert(type_specifier_list->type == NODE_TYPE_SPECIFIER_LIST);
1580
1581 first = _bt_list_first_entry(&type_specifier_list->u.type_specifier_list.head, struct ctf_node, siblings);
1582
1583 assert(first->type == NODE_TYPE_SPECIFIER);
1584
1585 node = first->u.type_specifier.node;
1586
1587 switch (first->u.type_specifier.type) {
1588 case TYPESPEC_FLOATING_POINT:
1589 return ctf_declaration_floating_point_visit(fd, depth,
1590 &node->u.floating_point.expressions, trace);
1591 case TYPESPEC_INTEGER:
1592 return ctf_declaration_integer_visit(fd, depth,
1593 &node->u.integer.expressions, trace);
1594 case TYPESPEC_STRING:
1595 return ctf_declaration_string_visit(fd, depth,
1596 &node->u.string.expressions, trace);
1597 case TYPESPEC_STRUCT:
1598 return ctf_declaration_struct_visit(fd, depth,
1599 node->u._struct.name,
1600 &node->u._struct.declaration_list,
1601 node->u._struct.has_body,
1602 &node->u._struct.min_align,
1603 declaration_scope,
1604 trace);
1605 case TYPESPEC_VARIANT:
1606 return ctf_declaration_variant_visit(fd, depth,
1607 node->u.variant.name,
1608 node->u.variant.choice,
1609 &node->u.variant.declaration_list,
1610 node->u.variant.has_body,
1611 declaration_scope,
1612 trace);
1613 case TYPESPEC_ENUM:
1614 return ctf_declaration_enum_visit(fd, depth,
1615 node->u._enum.enum_id,
1616 node->u._enum.container_type,
1617 &node->u._enum.enumerator_list,
1618 node->u._enum.has_body,
1619 declaration_scope,
1620 trace);
1621
1622 case TYPESPEC_VOID:
1623 case TYPESPEC_CHAR:
1624 case TYPESPEC_SHORT:
1625 case TYPESPEC_INT:
1626 case TYPESPEC_LONG:
1627 case TYPESPEC_FLOAT:
1628 case TYPESPEC_DOUBLE:
1629 case TYPESPEC_SIGNED:
1630 case TYPESPEC_UNSIGNED:
1631 case TYPESPEC_BOOL:
1632 case TYPESPEC_COMPLEX:
1633 case TYPESPEC_IMAGINARY:
1634 case TYPESPEC_CONST:
1635 case TYPESPEC_ID_TYPE:
1636 return ctf_declaration_type_specifier_visit(fd, depth,
1637 type_specifier_list, declaration_scope);
1638 default:
1639 fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) first->u.type_specifier.type);
1640 return NULL;
1641 }
1642 }
1643
1644 static
1645 int ctf_event_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_event_declaration *event, struct ctf_trace *trace)
1646 {
1647 int ret = 0;
1648
1649 switch (node->type) {
1650 case NODE_TYPEDEF:
1651 ret = ctf_typedef_visit(fd, depth + 1,
1652 event->declaration_scope,
1653 node->u._typedef.type_specifier_list,
1654 &node->u._typedef.type_declarators,
1655 trace);
1656 if (ret)
1657 return ret;
1658 break;
1659 case NODE_TYPEALIAS:
1660 ret = ctf_typealias_visit(fd, depth + 1,
1661 event->declaration_scope,
1662 node->u.typealias.target, node->u.typealias.alias,
1663 trace);
1664 if (ret)
1665 return ret;
1666 break;
1667 case NODE_CTF_EXPRESSION:
1668 {
1669 char *left;
1670
1671 left = concatenate_unary_strings(&node->u.ctf_expression.left);
1672 if (!strcmp(left, "name")) {
1673 char *right;
1674
1675 if (CTF_EVENT_FIELD_IS_SET(event, name)) {
1676 fprintf(fd, "[error] %s: name already declared in event declaration\n", __func__);
1677 ret = -EPERM;
1678 goto error;
1679 }
1680 right = concatenate_unary_strings(&node->u.ctf_expression.right);
1681 if (!right) {
1682 fprintf(fd, "[error] %s: unexpected unary expression for event name\n", __func__);
1683 ret = -EINVAL;
1684 goto error;
1685 }
1686 event->name = g_quark_from_string(right);
1687 g_free(right);
1688 CTF_EVENT_SET_FIELD(event, name);
1689 } else if (!strcmp(left, "id")) {
1690 if (CTF_EVENT_FIELD_IS_SET(event, id)) {
1691 fprintf(fd, "[error] %s: id already declared in event declaration\n", __func__);
1692 ret = -EPERM;
1693 goto error;
1694 }
1695 ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->id);
1696 if (ret) {
1697 fprintf(fd, "[error] %s: unexpected unary expression for event id\n", __func__);
1698 ret = -EINVAL;
1699 goto error;
1700 }
1701 CTF_EVENT_SET_FIELD(event, id);
1702 } else if (!strcmp(left, "stream_id")) {
1703 if (CTF_EVENT_FIELD_IS_SET(event, stream_id)) {
1704 fprintf(fd, "[error] %s: stream_id already declared in event declaration\n", __func__);
1705 ret = -EPERM;
1706 goto error;
1707 }
1708 ret = get_unary_unsigned(&node->u.ctf_expression.right, &event->stream_id);
1709 if (ret) {
1710 fprintf(fd, "[error] %s: unexpected unary expression for event stream_id\n", __func__);
1711 ret = -EINVAL;
1712 goto error;
1713 }
1714 event->stream = trace_stream_lookup(trace, event->stream_id);
1715 if (!event->stream) {
1716 fprintf(fd, "[error] %s: stream id %" PRIu64 " cannot be found\n", __func__, event->stream_id);
1717 ret = -EINVAL;
1718 goto error;
1719 }
1720 CTF_EVENT_SET_FIELD(event, stream_id);
1721 } else if (!strcmp(left, "context")) {
1722 struct bt_declaration *declaration;
1723
1724 if (event->context_decl) {
1725 fprintf(fd, "[error] %s: context already declared in event declaration\n", __func__);
1726 ret = -EINVAL;
1727 goto error;
1728 }
1729 declaration = ctf_type_specifier_list_visit(fd, depth,
1730 _bt_list_first_entry(&node->u.ctf_expression.right,
1731 struct ctf_node, siblings),
1732 event->declaration_scope, trace);
1733 if (!declaration) {
1734 ret = -EPERM;
1735 goto error;
1736 }
1737 if (declaration->id != CTF_TYPE_STRUCT) {
1738 ret = -EPERM;
1739 goto error;
1740 }
1741 event->context_decl = container_of(declaration, struct declaration_struct, p);
1742 } else if (!strcmp(left, "fields")) {
1743 struct bt_declaration *declaration;
1744
1745 if (event->fields_decl) {
1746 fprintf(fd, "[error] %s: fields already declared in event declaration\n", __func__);
1747 ret = -EINVAL;
1748 goto error;
1749 }
1750 declaration = ctf_type_specifier_list_visit(fd, depth,
1751 _bt_list_first_entry(&node->u.ctf_expression.right,
1752 struct ctf_node, siblings),
1753 event->declaration_scope, trace);
1754 if (!declaration) {
1755 ret = -EPERM;
1756 goto error;
1757 }
1758 if (declaration->id != CTF_TYPE_STRUCT) {
1759 ret = -EPERM;
1760 goto error;
1761 }
1762 event->fields_decl = container_of(declaration, struct declaration_struct, p);
1763 } else if (!strcmp(left, "loglevel")) {
1764 int64_t loglevel = -1;
1765
1766 if (CTF_EVENT_FIELD_IS_SET(event, loglevel)) {
1767 fprintf(fd, "[error] %s: loglevel already declared in event declaration\n", __func__);
1768 ret = -EPERM;
1769 goto error;
1770 }
1771 ret = get_unary_signed(&node->u.ctf_expression.right, &loglevel);
1772 if (ret) {
1773 fprintf(fd, "[error] %s: unexpected unary expression for event loglevel\n", __func__);
1774 ret = -EINVAL;
1775 goto error;
1776 }
1777 event->loglevel = (int) loglevel;
1778 CTF_EVENT_SET_FIELD(event, loglevel);
1779 } else if (!strcmp(left, "model.emf.uri")) {
1780 char *right;
1781
1782 if (CTF_EVENT_FIELD_IS_SET(event, model_emf_uri)) {
1783 fprintf(fd, "[error] %s: model.emf.uri already declared in event declaration\n", __func__);
1784 ret = -EPERM;
1785 goto error;
1786 }
1787 right = concatenate_unary_strings(&node->u.ctf_expression.right);
1788 if (!right) {
1789 fprintf(fd, "[error] %s: unexpected unary expression for event model.emf.uri\n", __func__);
1790 ret = -EINVAL;
1791 goto error;
1792 }
1793 event->model_emf_uri = g_quark_from_string(right);
1794 g_free(right);
1795 CTF_EVENT_SET_FIELD(event, model_emf_uri);
1796 } else {
1797 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in event declaration.\n", __func__, left);
1798 /* Fall-through after warning */
1799 }
1800 error:
1801 g_free(left);
1802 break;
1803 }
1804 default:
1805 return -EPERM;
1806 /* TODO: declaration specifier should be added. */
1807 }
1808
1809 return ret;
1810 }
1811
1812 static
1813 int ctf_event_visit(FILE *fd, int depth, struct ctf_node *node,
1814 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace)
1815 {
1816 int ret = 0;
1817 struct ctf_node *iter;
1818 struct ctf_event_declaration *event;
1819 struct bt_ctf_event_decl *event_decl;
1820
1821 event_decl = g_new0(struct bt_ctf_event_decl, 1);
1822 event = &event_decl->parent;
1823 event->declaration_scope = bt_new_declaration_scope(parent_declaration_scope);
1824 event->loglevel = -1;
1825 bt_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
1826 ret = ctf_event_declaration_visit(fd, depth + 1, iter, event, trace);
1827 if (ret)
1828 goto error;
1829 }
1830 if (!CTF_EVENT_FIELD_IS_SET(event, name)) {
1831 ret = -EPERM;
1832 fprintf(fd, "[error] %s: missing name field in event declaration\n", __func__);
1833 goto error;
1834 }
1835 if (!CTF_EVENT_FIELD_IS_SET(event, stream_id)) {
1836 /* Allow missing stream_id if there is only a single stream */
1837 switch (trace->streams->len) {
1838 case 0: /* Create stream if there was none. */
1839 ret = ctf_stream_visit(fd, depth, NULL, trace->root_declaration_scope, trace);
1840 if (ret)
1841 goto error;
1842 /* Fall-through */
1843 case 1:
1844 event->stream_id = 0;
1845 event->stream = trace_stream_lookup(trace, event->stream_id);
1846 break;
1847 default:
1848 ret = -EPERM;
1849 fprintf(fd, "[error] %s: missing stream_id field in event declaration\n", __func__);
1850 goto error;
1851 }
1852 }
1853 /* Allow only one event without id per stream */
1854 if (!CTF_EVENT_FIELD_IS_SET(event, id)
1855 && event->stream->events_by_id->len != 0) {
1856 ret = -EPERM;
1857 fprintf(fd, "[error] %s: missing id field in event declaration\n", __func__);
1858 goto error;
1859 }
1860 if (event->stream->events_by_id->len <= event->id)
1861 g_ptr_array_set_size(event->stream->events_by_id, event->id + 1);
1862 g_ptr_array_index(event->stream->events_by_id, event->id) = event;
1863 g_hash_table_insert(event->stream->event_quark_to_id,
1864 (gpointer) (unsigned long) event->name,
1865 &event->id);
1866 g_ptr_array_add(trace->event_declarations, event_decl);
1867 return 0;
1868
1869 error:
1870 if (event->fields_decl)
1871 bt_declaration_unref(&event->fields_decl->p);
1872 if (event->context_decl)
1873 bt_declaration_unref(&event->context_decl->p);
1874 bt_free_declaration_scope(event->declaration_scope);
1875 g_free(event_decl);
1876 return ret;
1877 }
1878
1879
1880 static
1881 int ctf_stream_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_stream_declaration *stream, struct ctf_trace *trace)
1882 {
1883 int ret = 0;
1884
1885 switch (node->type) {
1886 case NODE_TYPEDEF:
1887 ret = ctf_typedef_visit(fd, depth + 1,
1888 stream->declaration_scope,
1889 node->u._typedef.type_specifier_list,
1890 &node->u._typedef.type_declarators,
1891 trace);
1892 if (ret)
1893 return ret;
1894 break;
1895 case NODE_TYPEALIAS:
1896 ret = ctf_typealias_visit(fd, depth + 1,
1897 stream->declaration_scope,
1898 node->u.typealias.target, node->u.typealias.alias,
1899 trace);
1900 if (ret)
1901 return ret;
1902 break;
1903 case NODE_CTF_EXPRESSION:
1904 {
1905 char *left;
1906
1907 left = concatenate_unary_strings(&node->u.ctf_expression.left);
1908 if (!strcmp(left, "id")) {
1909 if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) {
1910 fprintf(fd, "[error] %s: id already declared in stream declaration\n", __func__);
1911 ret = -EPERM;
1912 goto error;
1913 }
1914 ret = get_unary_unsigned(&node->u.ctf_expression.right, &stream->stream_id);
1915 if (ret) {
1916 fprintf(fd, "[error] %s: unexpected unary expression for stream id\n", __func__);
1917 ret = -EINVAL;
1918 goto error;
1919 }
1920 CTF_STREAM_SET_FIELD(stream, stream_id);
1921 } else if (!strcmp(left, "event.header")) {
1922 struct bt_declaration *declaration;
1923
1924 if (stream->event_header_decl) {
1925 fprintf(fd, "[error] %s: event.header already declared in stream declaration\n", __func__);
1926 ret = -EINVAL;
1927 goto error;
1928 }
1929 declaration = ctf_type_specifier_list_visit(fd, depth,
1930 _bt_list_first_entry(&node->u.ctf_expression.right,
1931 struct ctf_node, siblings),
1932 stream->declaration_scope, trace);
1933 if (!declaration) {
1934 ret = -EPERM;
1935 goto error;
1936 }
1937 if (declaration->id != CTF_TYPE_STRUCT) {
1938 ret = -EPERM;
1939 goto error;
1940 }
1941 stream->event_header_decl = container_of(declaration, struct declaration_struct, p);
1942 } else if (!strcmp(left, "event.context")) {
1943 struct bt_declaration *declaration;
1944
1945 if (stream->event_context_decl) {
1946 fprintf(fd, "[error] %s: event.context already declared in stream declaration\n", __func__);
1947 ret = -EINVAL;
1948 goto error;
1949 }
1950 declaration = ctf_type_specifier_list_visit(fd, depth,
1951 _bt_list_first_entry(&node->u.ctf_expression.right,
1952 struct ctf_node, siblings),
1953 stream->declaration_scope, trace);
1954 if (!declaration) {
1955 ret = -EPERM;
1956 goto error;
1957 }
1958 if (declaration->id != CTF_TYPE_STRUCT) {
1959 ret = -EPERM;
1960 goto error;
1961 }
1962 stream->event_context_decl = container_of(declaration, struct declaration_struct, p);
1963 } else if (!strcmp(left, "packet.context")) {
1964 struct bt_declaration *declaration;
1965
1966 if (stream->packet_context_decl) {
1967 fprintf(fd, "[error] %s: packet.context already declared in stream declaration\n", __func__);
1968 ret = -EINVAL;
1969 goto error;
1970 }
1971 declaration = ctf_type_specifier_list_visit(fd, depth,
1972 _bt_list_first_entry(&node->u.ctf_expression.right,
1973 struct ctf_node, siblings),
1974 stream->declaration_scope, trace);
1975 if (!declaration) {
1976 ret = -EPERM;
1977 goto error;
1978 }
1979 if (declaration->id != CTF_TYPE_STRUCT) {
1980 ret = -EPERM;
1981 goto error;
1982 }
1983 stream->packet_context_decl = container_of(declaration, struct declaration_struct, p);
1984 } else {
1985 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in stream declaration.\n", __func__, left);
1986 /* Fall-through after warning */
1987 }
1988
1989 error:
1990 g_free(left);
1991 break;
1992 }
1993 default:
1994 return -EPERM;
1995 /* TODO: declaration specifier should be added. */
1996 }
1997
1998 return ret;
1999 }
2000
2001 static
2002 int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node,
2003 struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace)
2004 {
2005 int ret = 0;
2006 struct ctf_node *iter;
2007 struct ctf_stream_declaration *stream;
2008
2009 stream = g_new0(struct ctf_stream_declaration, 1);
2010 stream->declaration_scope = bt_new_declaration_scope(parent_declaration_scope);
2011 stream->events_by_id = g_ptr_array_new();
2012 stream->event_quark_to_id = g_hash_table_new(g_direct_hash, g_direct_equal);
2013 stream->streams = g_ptr_array_new();
2014 if (node) {
2015 bt_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
2016 ret = ctf_stream_declaration_visit(fd, depth + 1, iter, stream, trace);
2017 if (ret)
2018 goto error;
2019 }
2020 }
2021 if (CTF_STREAM_FIELD_IS_SET(stream, stream_id)) {
2022 /* check that packet header has stream_id field. */
2023 if (!trace->packet_header_decl
2024 || bt_struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("stream_id")) < 0) {
2025 ret = -EPERM;
2026 fprintf(fd, "[error] %s: missing stream_id field in packet header declaration, but stream_id attribute is declared for stream.\n", __func__);
2027 goto error;
2028 }
2029 } else {
2030 /* Allow only one id-less stream */
2031 if (trace->streams->len != 0) {
2032 ret = -EPERM;
2033 fprintf(fd, "[error] %s: missing id field in stream declaration\n", __func__);
2034 goto error;
2035 }
2036 stream->stream_id = 0;
2037 }
2038 if (trace->streams->len <= stream->stream_id)
2039 g_ptr_array_set_size(trace->streams, stream->stream_id + 1);
2040 g_ptr_array_index(trace->streams, stream->stream_id) = stream;
2041 stream->trace = trace;
2042
2043 return 0;
2044
2045 error:
2046 if (stream->event_header_decl)
2047 bt_declaration_unref(&stream->event_header_decl->p);
2048 if (stream->event_context_decl)
2049 bt_declaration_unref(&stream->event_context_decl->p);
2050 if (stream->packet_context_decl)
2051 bt_declaration_unref(&stream->packet_context_decl->p);
2052 g_ptr_array_free(stream->streams, TRUE);
2053 g_ptr_array_free(stream->events_by_id, TRUE);
2054 g_hash_table_destroy(stream->event_quark_to_id);
2055 bt_free_declaration_scope(stream->declaration_scope);
2056 g_free(stream);
2057 return ret;
2058 }
2059
2060 static
2061 int ctf_trace_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2062 {
2063 int ret = 0;
2064
2065 switch (node->type) {
2066 case NODE_TYPEDEF:
2067 ret = ctf_typedef_visit(fd, depth + 1,
2068 trace->declaration_scope,
2069 node->u._typedef.type_specifier_list,
2070 &node->u._typedef.type_declarators,
2071 trace);
2072 if (ret)
2073 return ret;
2074 break;
2075 case NODE_TYPEALIAS:
2076 ret = ctf_typealias_visit(fd, depth + 1,
2077 trace->declaration_scope,
2078 node->u.typealias.target, node->u.typealias.alias,
2079 trace);
2080 if (ret)
2081 return ret;
2082 break;
2083 case NODE_CTF_EXPRESSION:
2084 {
2085 char *left;
2086
2087 left = concatenate_unary_strings(&node->u.ctf_expression.left);
2088 if (!strcmp(left, "major")) {
2089 if (CTF_TRACE_FIELD_IS_SET(trace, major)) {
2090 fprintf(fd, "[error] %s: major already declared in trace declaration\n", __func__);
2091 ret = -EPERM;
2092 goto error;
2093 }
2094 ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->major);
2095 if (ret) {
2096 fprintf(fd, "[error] %s: unexpected unary expression for trace major number\n", __func__);
2097 ret = -EINVAL;
2098 goto error;
2099 }
2100 CTF_TRACE_SET_FIELD(trace, major);
2101 } else if (!strcmp(left, "minor")) {
2102 if (CTF_TRACE_FIELD_IS_SET(trace, minor)) {
2103 fprintf(fd, "[error] %s: minor already declared in trace declaration\n", __func__);
2104 ret = -EPERM;
2105 goto error;
2106 }
2107 ret = get_unary_unsigned(&node->u.ctf_expression.right, &trace->minor);
2108 if (ret) {
2109 fprintf(fd, "[error] %s: unexpected unary expression for trace minor number\n", __func__);
2110 ret = -EINVAL;
2111 goto error;
2112 }
2113 CTF_TRACE_SET_FIELD(trace, minor);
2114 } else if (!strcmp(left, "uuid")) {
2115 unsigned char uuid[BABELTRACE_UUID_LEN];
2116
2117 ret = get_unary_uuid(&node->u.ctf_expression.right, uuid);
2118 if (ret) {
2119 fprintf(fd, "[error] %s: unexpected unary expression for trace uuid\n", __func__);
2120 ret = -EINVAL;
2121 goto error;
2122 }
2123 if (CTF_TRACE_FIELD_IS_SET(trace, uuid)
2124 && babeltrace_uuid_compare(uuid, trace->uuid)) {
2125 fprintf(fd, "[error] %s: uuid mismatch\n", __func__);
2126 ret = -EPERM;
2127 goto error;
2128 } else {
2129 memcpy(trace->uuid, uuid, sizeof(uuid));
2130 }
2131 CTF_TRACE_SET_FIELD(trace, uuid);
2132 } else if (!strcmp(left, "byte_order")) {
2133 struct ctf_node *right;
2134 int byte_order;
2135
2136 right = _bt_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings);
2137 byte_order = get_trace_byte_order(fd, depth, right);
2138 if (byte_order < 0)
2139 return -EINVAL;
2140
2141 if (CTF_TRACE_FIELD_IS_SET(trace, byte_order)
2142 && byte_order != trace->byte_order) {
2143 fprintf(fd, "[error] %s: endianness mismatch\n", __func__);
2144 ret = -EPERM;
2145 goto error;
2146 } else {
2147 if (byte_order != trace->byte_order) {
2148 trace->byte_order = byte_order;
2149 /*
2150 * We need to restart
2151 * construction of the
2152 * intermediate representation.
2153 */
2154 trace->field_mask = 0;
2155 CTF_TRACE_SET_FIELD(trace, byte_order);
2156 ret = -EINTR;
2157 goto error;
2158 }
2159 }
2160 CTF_TRACE_SET_FIELD(trace, byte_order);
2161 } else if (!strcmp(left, "packet.header")) {
2162 struct bt_declaration *declaration;
2163
2164 if (trace->packet_header_decl) {
2165 fprintf(fd, "[error] %s: packet.header already declared in trace declaration\n", __func__);
2166 ret = -EINVAL;
2167 goto error;
2168 }
2169 declaration = ctf_type_specifier_list_visit(fd, depth,
2170 _bt_list_first_entry(&node->u.ctf_expression.right,
2171 struct ctf_node, siblings),
2172 trace->declaration_scope, trace);
2173 if (!declaration) {
2174 ret = -EPERM;
2175 goto error;
2176 }
2177 if (declaration->id != CTF_TYPE_STRUCT) {
2178 ret = -EPERM;
2179 goto error;
2180 }
2181 trace->packet_header_decl = container_of(declaration, struct declaration_struct, p);
2182 } else {
2183 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in trace declaration.\n", __func__, left);
2184 }
2185
2186 error:
2187 g_free(left);
2188 break;
2189 }
2190 default:
2191 return -EPERM;
2192 /* TODO: declaration specifier should be added. */
2193 }
2194
2195 return ret;
2196 }
2197
2198 static
2199 int ctf_trace_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2200 {
2201 int ret = 0;
2202 struct ctf_node *iter;
2203
2204 if (trace->declaration_scope)
2205 return -EEXIST;
2206 trace->declaration_scope = bt_new_declaration_scope(trace->root_declaration_scope);
2207 trace->streams = g_ptr_array_new();
2208 trace->event_declarations = g_ptr_array_new();
2209 bt_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
2210 ret = ctf_trace_declaration_visit(fd, depth + 1, iter, trace);
2211 if (ret)
2212 goto error;
2213 }
2214 if (!CTF_TRACE_FIELD_IS_SET(trace, major)) {
2215 ret = -EPERM;
2216 fprintf(fd, "[error] %s: missing major field in trace declaration\n", __func__);
2217 goto error;
2218 }
2219 if (!CTF_TRACE_FIELD_IS_SET(trace, minor)) {
2220 ret = -EPERM;
2221 fprintf(fd, "[error] %s: missing minor field in trace declaration\n", __func__);
2222 goto error;
2223 }
2224 if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
2225 ret = -EPERM;
2226 fprintf(fd, "[error] %s: missing byte_order field in trace declaration\n", __func__);
2227 goto error;
2228 }
2229
2230 if (!CTF_TRACE_FIELD_IS_SET(trace, byte_order)) {
2231 /* check that the packet header contains a "magic" field */
2232 if (!trace->packet_header_decl
2233 || bt_struct_declaration_lookup_field_index(trace->packet_header_decl, g_quark_from_static_string("magic")) < 0) {
2234 ret = -EPERM;
2235 fprintf(fd, "[error] %s: missing both byte_order and packet header magic number in trace declaration\n", __func__);
2236 goto error;
2237 }
2238 }
2239 return 0;
2240
2241 error:
2242 if (trace->packet_header_decl) {
2243 bt_declaration_unref(&trace->packet_header_decl->p);
2244 trace->packet_header_decl = NULL;
2245 }
2246 g_ptr_array_free(trace->streams, TRUE);
2247 g_ptr_array_free(trace->event_declarations, TRUE);
2248 bt_free_declaration_scope(trace->declaration_scope);
2249 trace->declaration_scope = NULL;
2250 return ret;
2251 }
2252
2253 static
2254 int ctf_clock_declaration_visit(FILE *fd, int depth, struct ctf_node *node,
2255 struct ctf_clock *clock, struct ctf_trace *trace)
2256 {
2257 int ret = 0;
2258
2259 switch (node->type) {
2260 case NODE_CTF_EXPRESSION:
2261 {
2262 char *left;
2263
2264 left = concatenate_unary_strings(&node->u.ctf_expression.left);
2265 if (!strcmp(left, "name")) {
2266 char *right;
2267
2268 if (CTF_CLOCK_FIELD_IS_SET(clock, name)) {
2269 fprintf(fd, "[error] %s: name already declared in clock declaration\n", __func__);
2270 ret = -EPERM;
2271 goto error;
2272 }
2273 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2274 if (!right) {
2275 fprintf(fd, "[error] %s: unexpected unary expression for clock name\n", __func__);
2276 ret = -EINVAL;
2277 goto error;
2278 }
2279 clock->name = g_quark_from_string(right);
2280 g_free(right);
2281 CTF_CLOCK_SET_FIELD(clock, name);
2282 } else if (!strcmp(left, "uuid")) {
2283 char *right;
2284
2285 if (clock->uuid) {
2286 fprintf(fd, "[error] %s: uuid already declared in clock declaration\n", __func__);
2287 ret = -EPERM;
2288 goto error;
2289 }
2290 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2291 if (!right) {
2292 fprintf(fd, "[error] %s: unexpected unary expression for clock uuid\n", __func__);
2293 ret = -EINVAL;
2294 goto error;
2295 }
2296 clock->uuid = g_quark_from_string(right);
2297 g_free(right);
2298 } else if (!strcmp(left, "description")) {
2299 char *right;
2300
2301 if (clock->description) {
2302 fprintf(fd, "[warning] %s: duplicated clock description\n", __func__);
2303 goto error; /* ret is 0, so not an actual error, just warn. */
2304 }
2305 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2306 if (!right) {
2307 fprintf(fd, "[warning] %s: unexpected unary expression for clock description\n", __func__);
2308 goto error; /* ret is 0, so not an actual error, just warn. */
2309 }
2310 clock->description = right;
2311 } else if (!strcmp(left, "freq")) {
2312 if (CTF_CLOCK_FIELD_IS_SET(clock, freq)) {
2313 fprintf(fd, "[error] %s: freq already declared in clock declaration\n", __func__);
2314 ret = -EPERM;
2315 goto error;
2316 }
2317 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->freq);
2318 if (ret) {
2319 fprintf(fd, "[error] %s: unexpected unary expression for clock freq\n", __func__);
2320 ret = -EINVAL;
2321 goto error;
2322 }
2323 CTF_CLOCK_SET_FIELD(clock, freq);
2324 } else if (!strcmp(left, "precision")) {
2325 if (clock->precision) {
2326 fprintf(fd, "[error] %s: precision already declared in clock declaration\n", __func__);
2327 ret = -EPERM;
2328 goto error;
2329 }
2330 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->precision);
2331 if (ret) {
2332 fprintf(fd, "[error] %s: unexpected unary expression for clock precision\n", __func__);
2333 ret = -EINVAL;
2334 goto error;
2335 }
2336 } else if (!strcmp(left, "offset_s")) {
2337 if (clock->offset_s) {
2338 fprintf(fd, "[error] %s: offset_s already declared in clock declaration\n", __func__);
2339 ret = -EPERM;
2340 goto error;
2341 }
2342 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->offset_s);
2343 if (ret) {
2344 fprintf(fd, "[error] %s: unexpected unary expression for clock offset_s\n", __func__);
2345 ret = -EINVAL;
2346 goto error;
2347 }
2348 } else if (!strcmp(left, "offset")) {
2349 if (clock->offset) {
2350 fprintf(fd, "[error] %s: offset already declared in clock declaration\n", __func__);
2351 ret = -EPERM;
2352 goto error;
2353 }
2354 ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->offset);
2355 if (ret) {
2356 fprintf(fd, "[error] %s: unexpected unary expression for clock offset\n", __func__);
2357 ret = -EINVAL;
2358 goto error;
2359 }
2360 } else if (!strcmp(left, "absolute")) {
2361 struct ctf_node *right;
2362
2363 right = _bt_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings);
2364 ret = get_boolean(fd, depth, right);
2365 if (ret < 0) {
2366 fprintf(fd, "[error] %s: unexpected \"absolute\" right member\n", __func__);
2367 ret = -EINVAL;
2368 goto error;
2369 }
2370 clock->absolute = ret;
2371 } else {
2372 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__, left);
2373 }
2374
2375 error:
2376 g_free(left);
2377 break;
2378 }
2379 default:
2380 return -EPERM;
2381 /* TODO: declaration specifier should be added. */
2382 }
2383
2384 return ret;
2385 }
2386
2387 static
2388 int ctf_clock_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2389 {
2390 int ret = 0;
2391 struct ctf_node *iter;
2392 struct ctf_clock *clock;
2393
2394 clock = g_new0(struct ctf_clock, 1);
2395 /* Default clock frequency is set to 1000000000 */
2396 clock->freq = 1000000000ULL;
2397 bt_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) {
2398 ret = ctf_clock_declaration_visit(fd, depth + 1, iter, clock, trace);
2399 if (ret)
2400 goto error;
2401 }
2402 if (opt_clock_force_correlate) {
2403 /*
2404 * User requested to forcibly correlate the clock
2405 * sources, even if we have no correlation
2406 * information.
2407 */
2408 if (!clock->absolute) {
2409 fprintf(fd, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2410 }
2411 clock->absolute = 1;
2412 }
2413 if (!CTF_CLOCK_FIELD_IS_SET(clock, name)) {
2414 ret = -EPERM;
2415 fprintf(fd, "[error] %s: missing name field in clock declaration\n", __func__);
2416 goto error;
2417 }
2418 if (g_hash_table_size(trace->clocks) > 0) {
2419 fprintf(fd, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
2420 ret = -EINVAL;
2421 goto error;
2422 }
2423 trace->single_clock = clock;
2424 g_hash_table_insert(trace->clocks, (gpointer) (unsigned long) clock->name, clock);
2425 return 0;
2426
2427 error:
2428 g_free(clock->description);
2429 g_free(clock);
2430 return ret;
2431 }
2432
2433 static
2434 void ctf_clock_default(FILE *fd, int depth, struct ctf_trace *trace)
2435 {
2436 struct ctf_clock *clock;
2437
2438 clock = g_new0(struct ctf_clock, 1);
2439 clock->name = g_quark_from_string("monotonic");
2440 clock->uuid = 0;
2441 clock->description = g_strdup("Default clock");
2442 /* Default clock frequency is set to 1000000000 */
2443 clock->freq = 1000000000ULL;
2444 if (opt_clock_force_correlate) {
2445 /*
2446 * User requested to forcibly correlate the clock
2447 * sources, even if we have no correlatation
2448 * information.
2449 */
2450 if (!clock->absolute) {
2451 fprintf(fd, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
2452 }
2453 clock->absolute = 1;
2454 } else {
2455 clock->absolute = 0; /* Not an absolute reference across traces */
2456 }
2457
2458 trace->single_clock = clock;
2459 g_hash_table_insert(trace->clocks, (gpointer) (unsigned long) clock->name, clock);
2460 }
2461
2462 static
2463 void clock_free(gpointer data)
2464 {
2465 struct ctf_clock *clock = data;
2466
2467 g_free(clock->description);
2468 g_free(clock);
2469 }
2470
2471 static
2472 int ctf_callsite_declaration_visit(FILE *fd, int depth, struct ctf_node *node,
2473 struct ctf_callsite *callsite, struct ctf_trace *trace)
2474 {
2475 int ret = 0;
2476
2477 switch (node->type) {
2478 case NODE_CTF_EXPRESSION:
2479 {
2480 char *left;
2481
2482 left = concatenate_unary_strings(&node->u.ctf_expression.left);
2483 if (!strcmp(left, "name")) {
2484 char *right;
2485
2486 if (CTF_CALLSITE_FIELD_IS_SET(callsite, name)) {
2487 fprintf(fd, "[error] %s: name already declared in callsite declaration\n", __func__);
2488 ret = -EPERM;
2489 goto error;
2490 }
2491 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2492 if (!right) {
2493 fprintf(fd, "[error] %s: unexpected unary expression for callsite name\n", __func__);
2494 ret = -EINVAL;
2495 goto error;
2496 }
2497 callsite->name = g_quark_from_string(right);
2498 g_free(right);
2499 CTF_CALLSITE_SET_FIELD(callsite, name);
2500 } else if (!strcmp(left, "func")) {
2501 char *right;
2502
2503 if (CTF_CALLSITE_FIELD_IS_SET(callsite, func)) {
2504 fprintf(fd, "[error] %s: func already declared in callsite declaration\n", __func__);
2505 ret = -EPERM;
2506 goto error;
2507 }
2508 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2509 if (!right) {
2510 fprintf(fd, "[error] %s: unexpected unary expression for callsite func\n", __func__);
2511 ret = -EINVAL;
2512 goto error;
2513 }
2514 callsite->func = right;
2515 CTF_CALLSITE_SET_FIELD(callsite, func);
2516 } else if (!strcmp(left, "file")) {
2517 char *right;
2518
2519 if (CTF_CALLSITE_FIELD_IS_SET(callsite, file)) {
2520 fprintf(fd, "[error] %s: file already declared in callsite declaration\n", __func__);
2521 ret = -EPERM;
2522 goto error;
2523 }
2524 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2525 if (!right) {
2526 fprintf(fd, "[error] %s: unexpected unary expression for callsite file\n", __func__);
2527 ret = -EINVAL;
2528 goto error;
2529 }
2530 callsite->file = right;
2531 CTF_CALLSITE_SET_FIELD(callsite, file);
2532 } else if (!strcmp(left, "line")) {
2533 if (CTF_CALLSITE_FIELD_IS_SET(callsite, line)) {
2534 fprintf(fd, "[error] %s: line already declared in callsite declaration\n", __func__);
2535 ret = -EPERM;
2536 goto error;
2537 }
2538 ret = get_unary_unsigned(&node->u.ctf_expression.right, &callsite->line);
2539 if (ret) {
2540 fprintf(fd, "[error] %s: unexpected unary expression for callsite line\n", __func__);
2541 ret = -EINVAL;
2542 goto error;
2543 }
2544 CTF_CALLSITE_SET_FIELD(callsite, line);
2545 } else if (!strcmp(left, "ip")) {
2546 if (CTF_CALLSITE_FIELD_IS_SET(callsite, ip)) {
2547 fprintf(fd, "[error] %s: ip already declared in callsite declaration\n", __func__);
2548 ret = -EPERM;
2549 goto error;
2550 }
2551 ret = get_unary_unsigned(&node->u.ctf_expression.right, &callsite->ip);
2552 if (ret) {
2553 fprintf(fd, "[error] %s: unexpected unary expression for callsite ip\n", __func__);
2554 ret = -EINVAL;
2555 goto error;
2556 }
2557 CTF_CALLSITE_SET_FIELD(callsite, ip);
2558 } else {
2559 fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in callsite declaration.\n", __func__, left);
2560 }
2561
2562 error:
2563 g_free(left);
2564 break;
2565 }
2566 default:
2567 return -EPERM;
2568 /* TODO: declaration specifier should be added. */
2569 }
2570
2571 return ret;
2572 }
2573
2574 static
2575 int ctf_callsite_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2576 {
2577 int ret = 0;
2578 struct ctf_node *iter;
2579 struct ctf_callsite *callsite;
2580 struct ctf_callsite_dups *cs_dups;
2581
2582 callsite = g_new0(struct ctf_callsite, 1);
2583 bt_list_for_each_entry(iter, &node->u.callsite.declaration_list, siblings) {
2584 ret = ctf_callsite_declaration_visit(fd, depth + 1, iter, callsite, trace);
2585 if (ret)
2586 goto error;
2587 }
2588 if (!CTF_CALLSITE_FIELD_IS_SET(callsite, name)) {
2589 ret = -EPERM;
2590 fprintf(fd, "[error] %s: missing name field in callsite declaration\n", __func__);
2591 goto error;
2592 }
2593 if (!CTF_CALLSITE_FIELD_IS_SET(callsite, func)) {
2594 ret = -EPERM;
2595 fprintf(fd, "[error] %s: missing func field in callsite declaration\n", __func__);
2596 goto error;
2597 }
2598 if (!CTF_CALLSITE_FIELD_IS_SET(callsite, file)) {
2599 ret = -EPERM;
2600 fprintf(fd, "[error] %s: missing file field in callsite declaration\n", __func__);
2601 goto error;
2602 }
2603 if (!CTF_CALLSITE_FIELD_IS_SET(callsite, line)) {
2604 ret = -EPERM;
2605 fprintf(fd, "[error] %s: missing line field in callsite declaration\n", __func__);
2606 goto error;
2607 }
2608
2609 cs_dups = g_hash_table_lookup(trace->callsites,
2610 (gpointer) (unsigned long) callsite->name);
2611 if (!cs_dups) {
2612 cs_dups = g_new0(struct ctf_callsite_dups, 1);
2613 BT_INIT_LIST_HEAD(&cs_dups->head);
2614 g_hash_table_insert(trace->callsites,
2615 (gpointer) (unsigned long) callsite->name, cs_dups);
2616 }
2617 bt_list_add_tail(&callsite->node, &cs_dups->head);
2618 return 0;
2619
2620 error:
2621 g_free(callsite->func);
2622 g_free(callsite->file);
2623 g_free(callsite);
2624 return ret;
2625 }
2626
2627 static
2628 void callsite_free(gpointer data)
2629 {
2630 struct ctf_callsite_dups *cs_dups = data;
2631 struct ctf_callsite *callsite, *cs_n;
2632
2633 bt_list_for_each_entry_safe(callsite, cs_n, &cs_dups->head, node) {
2634 g_free(callsite->func);
2635 g_free(callsite->file);
2636 g_free(callsite);
2637 }
2638 g_free(cs_dups);
2639 }
2640
2641 static
2642 int ctf_env_declaration_visit(FILE *fd, int depth, struct ctf_node *node,
2643 struct ctf_trace *trace)
2644 {
2645 int ret = 0;
2646 struct ctf_tracer_env *env = &trace->env;
2647
2648 switch (node->type) {
2649 case NODE_CTF_EXPRESSION:
2650 {
2651 char *left;
2652
2653 left = concatenate_unary_strings(&node->u.ctf_expression.left);
2654 if (!strcmp(left, "vpid")) {
2655 uint64_t v;
2656
2657 if (env->vpid != -1) {
2658 fprintf(fd, "[error] %s: vpid already declared in env declaration\n", __func__);
2659 goto error; /* ret is 0, so not an actual error, just warn. */
2660 }
2661 ret = get_unary_unsigned(&node->u.ctf_expression.right, &v);
2662 if (ret) {
2663 fprintf(fd, "[error] %s: unexpected unary expression for env vpid\n", __func__);
2664 goto error; /* ret is 0, so not an actual error, just warn. */
2665 }
2666 env->vpid = (int) v;
2667 printf_verbose("env.vpid = %d\n", env->vpid);
2668 } else if (!strcmp(left, "procname")) {
2669 char *right;
2670
2671 if (env->procname[0]) {
2672 fprintf(fd, "[warning] %s: duplicated env procname\n", __func__);
2673 goto error; /* ret is 0, so not an actual error, just warn. */
2674 }
2675 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2676 if (!right) {
2677 fprintf(fd, "[warning] %s: unexpected unary expression for env procname\n", __func__);
2678 goto error; /* ret is 0, so not an actual error, just warn. */
2679 }
2680 strncpy(env->procname, right, TRACER_ENV_LEN);
2681 env->procname[TRACER_ENV_LEN - 1] = '\0';
2682 printf_verbose("env.procname = \"%s\"\n", env->procname);
2683 g_free(right);
2684 } else if (!strcmp(left, "hostname")) {
2685 char *right;
2686
2687 if (env->hostname[0]) {
2688 fprintf(fd, "[warning] %s: duplicated env hostname\n", __func__);
2689 goto error; /* ret is 0, so not an actual error, just warn. */
2690 }
2691 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2692 if (!right) {
2693 fprintf(fd, "[warning] %s: unexpected unary expression for env hostname\n", __func__);
2694 goto error; /* ret is 0, so not an actual error, just warn. */
2695 }
2696 strncpy(env->hostname, right, TRACER_ENV_LEN);
2697 env->hostname[TRACER_ENV_LEN - 1] = '\0';
2698 printf_verbose("env.hostname = \"%s\"\n", env->hostname);
2699 g_free(right);
2700 } else if (!strcmp(left, "domain")) {
2701 char *right;
2702
2703 if (env->domain[0]) {
2704 fprintf(fd, "[warning] %s: duplicated env domain\n", __func__);
2705 goto error; /* ret is 0, so not an actual error, just warn. */
2706 }
2707 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2708 if (!right) {
2709 fprintf(fd, "[warning] %s: unexpected unary expression for env domain\n", __func__);
2710 goto error; /* ret is 0, so not an actual error, just warn. */
2711 }
2712 strncpy(env->domain, right, TRACER_ENV_LEN);
2713 env->domain[TRACER_ENV_LEN - 1] = '\0';
2714 printf_verbose("env.domain = \"%s\"\n", env->domain);
2715 g_free(right);
2716 } else if (!strcmp(left, "sysname")) {
2717 char *right;
2718
2719 if (env->sysname[0]) {
2720 fprintf(fd, "[warning] %s: duplicated env sysname\n", __func__);
2721 goto error; /* ret is 0, so not an actual error, just warn. */
2722 }
2723 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2724 if (!right) {
2725 fprintf(fd, "[warning] %s: unexpected unary expression for env sysname\n", __func__);
2726 goto error; /* ret is 0, so not an actual error, just warn. */
2727 }
2728 strncpy(env->sysname, right, TRACER_ENV_LEN);
2729 env->sysname[TRACER_ENV_LEN - 1] = '\0';
2730 printf_verbose("env.sysname = \"%s\"\n", env->sysname);
2731 g_free(right);
2732 } else if (!strcmp(left, "kernel_release")) {
2733 char *right;
2734
2735 if (env->release[0]) {
2736 fprintf(fd, "[warning] %s: duplicated env release\n", __func__);
2737 goto error; /* ret is 0, so not an actual error, just warn. */
2738 }
2739 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2740 if (!right) {
2741 fprintf(fd, "[warning] %s: unexpected unary expression for env release\n", __func__);
2742 goto error; /* ret is 0, so not an actual error, just warn. */
2743 }
2744 strncpy(env->release, right, TRACER_ENV_LEN);
2745 env->release[TRACER_ENV_LEN - 1] = '\0';
2746 printf_verbose("env.release = \"%s\"\n", env->release);
2747 g_free(right);
2748 } else if (!strcmp(left, "kernel_version")) {
2749 char *right;
2750
2751 if (env->version[0]) {
2752 fprintf(fd, "[warning] %s: duplicated env version\n", __func__);
2753 goto error; /* ret is 0, so not an actual error, just warn. */
2754 }
2755 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2756 if (!right) {
2757 fprintf(fd, "[warning] %s: unexpected unary expression for env version\n", __func__);
2758 goto error; /* ret is 0, so not an actual error, just warn. */
2759 }
2760 strncpy(env->version, right, TRACER_ENV_LEN);
2761 env->version[TRACER_ENV_LEN - 1] = '\0';
2762 printf_verbose("env.version = \"%s\"\n", env->version);
2763 g_free(right);
2764 } else {
2765 if (is_unary_string(&node->u.ctf_expression.right)) {
2766 char *right;
2767
2768 right = concatenate_unary_strings(&node->u.ctf_expression.right);
2769 printf_verbose("env.%s = \"%s\"\n", left, right);
2770 g_free(right);
2771 } else if (is_unary_unsigned(&node->u.ctf_expression.right)) {
2772 uint64_t v;
2773 int ret;
2774
2775 ret = get_unary_unsigned(&node->u.ctf_expression.right, &v);
2776 assert(ret == 0);
2777 printf_verbose("env.%s = %" PRIu64 "\n", left, v);
2778 } else if (is_unary_signed(&node->u.ctf_expression.right)) {
2779 int64_t v;
2780 int ret;
2781
2782 ret = get_unary_signed(&node->u.ctf_expression.right, &v);
2783 assert(ret == 0);
2784 printf_verbose("env.%s = %" PRId64 "\n", left, v);
2785 } else {
2786 printf_verbose("%s: attribute \"%s\" has unknown type.\n", __func__, left);
2787 }
2788 }
2789
2790 error:
2791 g_free(left);
2792 break;
2793 }
2794 default:
2795 return -EPERM;
2796 }
2797
2798 return ret;
2799 }
2800
2801 static
2802 int ctf_env_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2803 {
2804 int ret = 0;
2805 struct ctf_node *iter;
2806
2807 trace->env.vpid = -1;
2808 trace->env.procname[0] = '\0';
2809 trace->env.hostname[0] = '\0';
2810 trace->env.domain[0] = '\0';
2811 trace->env.sysname[0] = '\0';
2812 trace->env.release[0] = '\0';
2813 trace->env.version[0] = '\0';
2814 bt_list_for_each_entry(iter, &node->u.env.declaration_list, siblings) {
2815 ret = ctf_env_declaration_visit(fd, depth + 1, iter, trace);
2816 if (ret)
2817 goto error;
2818 }
2819 error:
2820 return 0;
2821 }
2822
2823 static
2824 int ctf_root_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
2825 {
2826 int ret = 0;
2827
2828 switch (node->type) {
2829 case NODE_TYPEDEF:
2830 ret = ctf_typedef_visit(fd, depth + 1,
2831 trace->root_declaration_scope,
2832 node->u._typedef.type_specifier_list,
2833 &node->u._typedef.type_declarators,
2834 trace);
2835 if (ret)
2836 return ret;
2837 break;
2838 case NODE_TYPEALIAS:
2839 ret = ctf_typealias_visit(fd, depth + 1,
2840 trace->root_declaration_scope,
2841 node->u.typealias.target, node->u.typealias.alias,
2842 trace);
2843 if (ret)
2844 return ret;
2845 break;
2846 case NODE_TYPE_SPECIFIER_LIST:
2847 {
2848 struct bt_declaration *declaration;
2849
2850 /*
2851 * Just add the type specifier to the root scope
2852 * declaration scope. Release local reference.
2853 */
2854 declaration = ctf_type_specifier_list_visit(fd, depth + 1,
2855 node, trace->root_declaration_scope, trace);
2856 if (!declaration)
2857 return -ENOMEM;
2858 bt_declaration_unref(declaration);
2859 break;
2860 }
2861 default:
2862 return -EPERM;
2863 }
2864
2865 return 0;
2866 }
2867
2868 int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node,
2869 struct ctf_trace *trace, int byte_order)
2870 {
2871 int ret = 0;
2872 struct ctf_node *iter;
2873 int env_clock_done = 0;
2874
2875 printf_verbose("CTF visitor: metadata construction...\n");
2876 trace->byte_order = byte_order;
2877 trace->clocks = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2878 NULL, clock_free);
2879 trace->callsites = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2880 NULL, callsite_free);
2881
2882 retry:
2883 trace->root_declaration_scope = bt_new_declaration_scope(NULL);
2884
2885 switch (node->type) {
2886 case NODE_ROOT:
2887 if (!env_clock_done) {
2888 /*
2889 * declarations need to query clock hash table,
2890 * so clock need to be treated first.
2891 */
2892 if (bt_list_empty(&node->u.root.clock)) {
2893 ctf_clock_default(fd, depth + 1, trace);
2894 } else {
2895 bt_list_for_each_entry(iter, &node->u.root.clock, siblings) {
2896 ret = ctf_clock_visit(fd, depth + 1, iter,
2897 trace);
2898 if (ret) {
2899 fprintf(fd, "[error] %s: clock declaration error\n", __func__);
2900 goto error;
2901 }
2902 }
2903 }
2904 env_clock_done = 1;
2905 }
2906 bt_list_for_each_entry(iter, &node->u.root.declaration_list,
2907 siblings) {
2908 ret = ctf_root_declaration_visit(fd, depth + 1, iter, trace);
2909 if (ret) {
2910 fprintf(fd, "[error] %s: root declaration error\n", __func__);
2911 goto error;
2912 }
2913 }
2914 bt_list_for_each_entry(iter, &node->u.root.trace, siblings) {
2915 ret = ctf_trace_visit(fd, depth + 1, iter, trace);
2916 if (ret == -EINTR) {
2917 bt_free_declaration_scope(trace->root_declaration_scope);
2918 /*
2919 * Need to restart creation of type
2920 * definitions, aliases and
2921 * trace header declarations.
2922 */
2923 goto retry;
2924 }
2925 if (ret) {
2926 fprintf(fd, "[error] %s: trace declaration error\n", __func__);
2927 goto error;
2928 }
2929 }
2930 bt_list_for_each_entry(iter, &node->u.root.callsite, siblings) {
2931 ret = ctf_callsite_visit(fd, depth + 1, iter,
2932 trace);
2933 if (ret) {
2934 fprintf(fd, "[error] %s: callsite declaration error\n", __func__);
2935 goto error;
2936 }
2937 }
2938 if (!trace->streams) {
2939 fprintf(fd, "[error] %s: missing trace declaration\n", __func__);
2940 ret = -EINVAL;
2941 goto error;
2942 }
2943 bt_list_for_each_entry(iter, &node->u.root.env, siblings) {
2944 ret = ctf_env_visit(fd, depth + 1, iter, trace);
2945 if (ret) {
2946 fprintf(fd, "[error] %s: env declaration error\n", __func__);
2947 goto error;
2948 }
2949 }
2950 bt_list_for_each_entry(iter, &node->u.root.stream, siblings) {
2951 ret = ctf_stream_visit(fd, depth + 1, iter,
2952 trace->root_declaration_scope, trace);
2953 if (ret) {
2954 fprintf(fd, "[error] %s: stream declaration error\n", __func__);
2955 goto error;
2956 }
2957 }
2958 bt_list_for_each_entry(iter, &node->u.root.event, siblings) {
2959 ret = ctf_event_visit(fd, depth + 1, iter,
2960 trace->root_declaration_scope, trace);
2961 if (ret) {
2962 fprintf(fd, "[error] %s: event declaration error\n", __func__);
2963 goto error;
2964 }
2965 }
2966 break;
2967 case NODE_UNKNOWN:
2968 default:
2969 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
2970 (int) node->type);
2971 ret = -EINVAL;
2972 goto error;
2973 }
2974 printf_verbose("done.\n");
2975 return ret;
2976
2977 error:
2978 bt_free_declaration_scope(trace->root_declaration_scope);
2979 g_hash_table_destroy(trace->callsites);
2980 g_hash_table_destroy(trace->clocks);
2981 return ret;
2982 }
2983
2984 int ctf_destroy_metadata(struct ctf_trace *trace)
2985 {
2986 int i;
2987 struct ctf_file_stream *metadata_stream;
2988
2989 if (trace->streams) {
2990 for (i = 0; i < trace->streams->len; i++) {
2991 struct ctf_stream_declaration *stream;
2992 int j;
2993
2994 stream = g_ptr_array_index(trace->streams, i);
2995 if (!stream)
2996 continue;
2997 for (j = 0; j < stream->streams->len; j++) {
2998 struct ctf_stream_definition *stream_def;
2999 int k;
3000
3001 stream_def = g_ptr_array_index(stream->streams, j);
3002 if (!stream_def)
3003 continue;
3004 for (k = 0; k < stream_def->events_by_id->len; k++) {
3005 struct ctf_event_definition *event;
3006
3007 event = g_ptr_array_index(stream_def->events_by_id, k);
3008 if (!event)
3009 continue;
3010 if (&event->event_fields->p)
3011 bt_definition_unref(&event->event_fields->p);
3012 if (&event->event_context->p)
3013 bt_definition_unref(&event->event_context->p);
3014 g_free(event);
3015 }
3016 if (&stream_def->trace_packet_header->p)
3017 bt_definition_unref(&stream_def->trace_packet_header->p);
3018 if (&stream_def->stream_event_header->p)
3019 bt_definition_unref(&stream_def->stream_event_header->p);
3020 if (&stream_def->stream_packet_context->p)
3021 bt_definition_unref(&stream_def->stream_packet_context->p);
3022 if (&stream_def->stream_event_context->p)
3023 bt_definition_unref(&stream_def->stream_event_context->p);
3024 g_ptr_array_free(stream_def->events_by_id, TRUE);
3025 g_free(stream_def);
3026 }
3027 if (stream->event_header_decl)
3028 bt_declaration_unref(&stream->event_header_decl->p);
3029 if (stream->event_context_decl)
3030 bt_declaration_unref(&stream->event_context_decl->p);
3031 if (stream->packet_context_decl)
3032 bt_declaration_unref(&stream->packet_context_decl->p);
3033 g_ptr_array_free(stream->streams, TRUE);
3034 g_ptr_array_free(stream->events_by_id, TRUE);
3035 g_hash_table_destroy(stream->event_quark_to_id);
3036 bt_free_declaration_scope(stream->declaration_scope);
3037 g_free(stream);
3038 }
3039 g_ptr_array_free(trace->streams, TRUE);
3040 }
3041
3042 if (trace->event_declarations) {
3043 for (i = 0; i < trace->event_declarations->len; i++) {
3044 struct bt_ctf_event_decl *event_decl;
3045 struct ctf_event_declaration *event;
3046
3047 event_decl = g_ptr_array_index(trace->event_declarations, i);
3048 if (event_decl->context_decl)
3049 g_ptr_array_free(event_decl->context_decl, TRUE);
3050 if (event_decl->fields_decl)
3051 g_ptr_array_free(event_decl->fields_decl, TRUE);
3052 if (event_decl->packet_header_decl)
3053 g_ptr_array_free(event_decl->packet_header_decl, TRUE);
3054 if (event_decl->event_context_decl)
3055 g_ptr_array_free(event_decl->event_context_decl, TRUE);
3056 if (event_decl->event_header_decl)
3057 g_ptr_array_free(event_decl->event_header_decl, TRUE);
3058 if (event_decl->packet_context_decl)
3059 g_ptr_array_free(event_decl->packet_context_decl, TRUE);
3060
3061 event = &event_decl->parent;
3062 if (event->fields_decl)
3063 bt_declaration_unref(&event->fields_decl->p);
3064 if (event->context_decl)
3065 bt_declaration_unref(&event->context_decl->p);
3066 bt_free_declaration_scope(event->declaration_scope);
3067
3068 g_free(event);
3069 }
3070 g_ptr_array_free(trace->event_declarations, TRUE);
3071 }
3072 if (trace->packet_header_decl)
3073 bt_declaration_unref(&trace->packet_header_decl->p);
3074
3075 bt_free_declaration_scope(trace->root_declaration_scope);
3076 bt_free_declaration_scope(trace->declaration_scope);
3077
3078 g_hash_table_destroy(trace->callsites);
3079 g_hash_table_destroy(trace->clocks);
3080
3081 metadata_stream = container_of(trace->metadata, struct ctf_file_stream, parent);
3082 g_free(metadata_stream);
3083
3084 return 0;
3085 }
This page took 0.160936 seconds and 4 git commands to generate.