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