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