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