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