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