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