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