Commit | Line | Data |
---|---|---|
e98a2d6e PP |
1 | /* |
2 | * ctf-visitor-generate-ir.c | |
3 | * | |
4 | * Common Trace Format metadata visitor (generates CTF IR objects). | |
5 | * | |
6 | * Based on older ctf-visitor-generate-io-struct.c. | |
7 | * | |
8 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7b33a0e0 | 9 | * Copyright 2015-2018 - Philippe Proulx <philippe.proulx@efficios.com> |
e98a2d6e PP |
10 | * |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice shall be included in | |
19 | * all copies or substantial portions of the Software. | |
20 | * | |
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
27 | * SOFTWARE. | |
28 | */ | |
29 | ||
37d21e07 PP |
30 | #define BT_LOG_TAG "PLUGIN-CTF-METADATA-IR-VISITOR" |
31 | #include "logging.h" | |
32 | ||
e98a2d6e PP |
33 | #include <stdio.h> |
34 | #include <unistd.h> | |
35 | #include <string.h> | |
c55a9f58 | 36 | #include <stdbool.h> |
e98a2d6e PP |
37 | #include <stdlib.h> |
38 | #include <ctype.h> | |
8b45963b | 39 | #include <babeltrace/assert-internal.h> |
e98a2d6e PP |
40 | #include <glib.h> |
41 | #include <inttypes.h> | |
42 | #include <errno.h> | |
8deee039 | 43 | #include <babeltrace/common-internal.h> |
3d9990ac PP |
44 | #include <babeltrace/compat/uuid-internal.h> |
45 | #include <babeltrace/endian-internal.h> | |
e0831b38 | 46 | #include <babeltrace/babeltrace.h> |
e98a2d6e | 47 | |
06a626b8 JG |
48 | #include "scanner.h" |
49 | #include "parser.h" | |
50 | #include "ast.h" | |
53ac3428 | 51 | #include "decoder.h" |
7b33a0e0 PP |
52 | #include "ctf-meta.h" |
53 | #include "ctf-meta-visitors.h" | |
e98a2d6e PP |
54 | |
55 | /* Bit value (left shift) */ | |
56 | #define _BV(_val) (1 << (_val)) | |
57 | ||
58 | /* Bit is set in a set of bits */ | |
59 | #define _IS_SET(_set, _mask) (*(_set) & (_mask)) | |
60 | ||
61 | /* Set bit in a set of bits */ | |
62 | #define _SET(_set, _mask) (*(_set) |= (_mask)) | |
63 | ||
7b33a0e0 PP |
64 | /* Try to push scope, or go to the `error` label */ |
65 | #define _TRY_PUSH_SCOPE_OR_GOTO_ERROR() \ | |
66 | do { \ | |
67 | ret = ctx_push_scope(ctx); \ | |
68 | if (ret) { \ | |
69 | BT_LOGE_STR("Cannot push scope."); \ | |
70 | goto error; \ | |
71 | } \ | |
72 | } while (0) | |
73 | ||
e98a2d6e PP |
74 | /* Bits for verifying existing attributes in various declarations */ |
75 | enum { | |
76 | _CLOCK_NAME_SET = _BV(0), | |
77 | _CLOCK_UUID_SET = _BV(1), | |
78 | _CLOCK_FREQ_SET = _BV(2), | |
79 | _CLOCK_PRECISION_SET = _BV(3), | |
80 | _CLOCK_OFFSET_S_SET = _BV(4), | |
81 | _CLOCK_OFFSET_SET = _BV(5), | |
82 | _CLOCK_ABSOLUTE_SET = _BV(6), | |
83 | _CLOCK_DESCRIPTION_SET = _BV(7), | |
84 | }; | |
85 | ||
86 | enum { | |
87 | _INTEGER_ALIGN_SET = _BV(0), | |
88 | _INTEGER_SIZE_SET = _BV(1), | |
89 | _INTEGER_BASE_SET = _BV(2), | |
90 | _INTEGER_ENCODING_SET = _BV(3), | |
91 | _INTEGER_BYTE_ORDER_SET = _BV(4), | |
92 | _INTEGER_SIGNED_SET = _BV(5), | |
93 | _INTEGER_MAP_SET = _BV(6), | |
94 | }; | |
95 | ||
96 | enum { | |
97 | _FLOAT_ALIGN_SET = _BV(0), | |
98 | _FLOAT_MANT_DIG_SET = _BV(1), | |
99 | _FLOAT_EXP_DIG_SET = _BV(2), | |
100 | _FLOAT_BYTE_ORDER_SET = _BV(3), | |
101 | }; | |
102 | ||
103 | enum { | |
104 | _STRING_ENCODING_SET = _BV(0), | |
105 | }; | |
106 | ||
107 | enum { | |
108 | _TRACE_MINOR_SET = _BV(0), | |
109 | _TRACE_MAJOR_SET = _BV(1), | |
110 | _TRACE_BYTE_ORDER_SET = _BV(2), | |
111 | _TRACE_UUID_SET = _BV(3), | |
112 | _TRACE_PACKET_HEADER_SET = _BV(4), | |
113 | }; | |
114 | ||
115 | enum { | |
116 | _STREAM_ID_SET = _BV(0), | |
117 | _STREAM_PACKET_CONTEXT_SET = _BV(1), | |
118 | _STREAM_EVENT_HEADER_SET = _BV(2), | |
119 | _STREAM_EVENT_CONTEXT_SET = _BV(3), | |
120 | }; | |
121 | ||
122 | enum { | |
123 | _EVENT_NAME_SET = _BV(0), | |
124 | _EVENT_ID_SET = _BV(1), | |
125 | _EVENT_MODEL_EMF_URI_SET = _BV(2), | |
126 | _EVENT_STREAM_ID_SET = _BV(3), | |
7b33a0e0 | 127 | _EVENT_LOG_LEVEL_SET = _BV(4), |
e98a2d6e PP |
128 | _EVENT_CONTEXT_SET = _BV(5), |
129 | _EVENT_FIELDS_SET = _BV(6), | |
130 | }; | |
131 | ||
c3c30b08 | 132 | enum loglevel { |
7b33a0e0 PP |
133 | LOG_LEVEL_EMERG = 0, |
134 | LOG_LEVEL_ALERT = 1, | |
135 | LOG_LEVEL_CRIT = 2, | |
136 | LOG_LEVEL_ERR = 3, | |
137 | LOG_LEVEL_WARNING = 4, | |
138 | LOG_LEVEL_NOTICE = 5, | |
139 | LOG_LEVEL_INFO = 6, | |
140 | LOG_LEVEL_DEBUG_SYSTEM = 7, | |
141 | LOG_LEVEL_DEBUG_PROGRAM = 8, | |
142 | LOG_LEVEL_DEBUG_PROCESS = 9, | |
143 | LOG_LEVEL_DEBUG_MODULE = 10, | |
144 | LOG_LEVEL_DEBUG_UNIT = 11, | |
145 | LOG_LEVEL_DEBUG_FUNCTION = 12, | |
146 | LOG_LEVEL_DEBUG_LINE = 13, | |
147 | LOG_LEVEL_DEBUG = 14, | |
c3c30b08 MD |
148 | _NR_LOGLEVELS = 15, |
149 | }; | |
150 | ||
939190b3 | 151 | /* Prefixes of class aliases */ |
e98a2d6e PP |
152 | #define _PREFIX_ALIAS 'a' |
153 | #define _PREFIX_ENUM 'e' | |
154 | #define _PREFIX_STRUCT 's' | |
155 | #define _PREFIX_VARIANT 'v' | |
156 | ||
157 | /* First entry in a BT list */ | |
939190b3 PP |
158 | #define _BT_LIST_FIRST_ENTRY(_ptr, _class, _member) \ |
159 | bt_list_entry((_ptr)->next, _class, _member) | |
e98a2d6e | 160 | |
87cb75e6 PP |
161 | #define _BT_LOGE_DUP_ATTR(_node, _attr, _entity) \ |
162 | _BT_LOGE_LINENO((_node)->lineno, \ | |
163 | "Duplicate attribute in %s: attr-name=\"%s\"", \ | |
164 | _entity, _attr) | |
165 | ||
166 | #define _BT_LOGE_NODE(_node, _msg, args...) \ | |
167 | _BT_LOGE_LINENO((_node)->lineno, _msg, ## args) | |
168 | ||
169 | #define _BT_LOGW_NODE(_node, _msg, args...) \ | |
170 | _BT_LOGW_LINENO((_node)->lineno, _msg, ## args) | |
171 | ||
172 | #define _BT_LOGV_NODE(_node, _msg, args...) \ | |
173 | _BT_LOGV_LINENO((_node)->lineno, _msg, ## args) | |
e98a2d6e PP |
174 | |
175 | /* | |
176 | * Declaration scope of a visitor context. This represents a TSDL | |
177 | * lexical scope, so that aliases and named structures, variants, | |
178 | * and enumerations may be registered and looked up hierarchically. | |
179 | */ | |
180 | struct ctx_decl_scope { | |
181 | /* | |
939190b3 | 182 | * Alias name to field class. |
e98a2d6e | 183 | * |
939190b3 | 184 | * GQuark -> struct ctf_field_class * (owned by this) |
e98a2d6e PP |
185 | */ |
186 | GHashTable *decl_map; | |
187 | ||
188 | /* Parent scope; NULL if this is the root declaration scope */ | |
189 | struct ctx_decl_scope *parent_scope; | |
190 | }; | |
191 | ||
192 | /* | |
1e649dff | 193 | * Visitor context (private). |
e98a2d6e PP |
194 | */ |
195 | struct ctx { | |
10b7a2e4 | 196 | /* Trace IR trace class being filled (owned by this) */ |
8eee8ea2 | 197 | bt_trace_class *trace_class; |
e98a2d6e | 198 | |
7b33a0e0 PP |
199 | /* CTF meta trace being filled (owned by this) */ |
200 | struct ctf_trace_class *ctf_tc; | |
201 | ||
202 | /* Current declaration scope (top of the stack) (owned by this) */ | |
e98a2d6e PP |
203 | struct ctx_decl_scope *current_scope; |
204 | ||
7b33a0e0 PP |
205 | /* True if trace declaration is visited */ |
206 | bool is_trace_visited; | |
e98a2d6e | 207 | |
7b33a0e0 | 208 | /* True if this is an LTTng trace */ |
f4421ff3 PP |
209 | bool is_lttng; |
210 | ||
53ac3428 PP |
211 | /* Config passed by the user */ |
212 | struct ctf_metadata_decoder_config decoder_config; | |
e98a2d6e PP |
213 | }; |
214 | ||
1e649dff PP |
215 | /* |
216 | * Visitor (public). | |
217 | */ | |
7b33a0e0 | 218 | struct ctf_visitor_generate_ir; |
1e649dff | 219 | |
e98a2d6e PP |
220 | /** |
221 | * Creates a new declaration scope. | |
222 | * | |
223 | * @param par_scope Parent scope (NULL if creating a root scope) | |
224 | * @returns New declaration scope, or NULL on error | |
225 | */ | |
226 | static | |
227 | struct ctx_decl_scope *ctx_decl_scope_create(struct ctx_decl_scope *par_scope) | |
228 | { | |
229 | struct ctx_decl_scope *scope; | |
230 | ||
231 | scope = g_new(struct ctx_decl_scope, 1); | |
232 | if (!scope) { | |
87cb75e6 | 233 | BT_LOGE_STR("Failed to allocate one declaration scope."); |
e98a2d6e PP |
234 | goto end; |
235 | } | |
236 | ||
237 | scope->decl_map = g_hash_table_new_full(g_direct_hash, g_direct_equal, | |
939190b3 | 238 | NULL, (GDestroyNotify) ctf_field_class_destroy); |
e98a2d6e PP |
239 | scope->parent_scope = par_scope; |
240 | ||
241 | end: | |
242 | return scope; | |
243 | } | |
244 | ||
245 | /** | |
246 | * Destroys a declaration scope. | |
247 | * | |
248 | * This function does not destroy the parent scope. | |
249 | * | |
250 | * @param scope Scope to destroy | |
251 | */ | |
252 | static | |
253 | void ctx_decl_scope_destroy(struct ctx_decl_scope *scope) | |
254 | { | |
255 | if (!scope) { | |
256 | goto end; | |
257 | } | |
258 | ||
259 | g_hash_table_destroy(scope->decl_map); | |
260 | g_free(scope); | |
261 | ||
262 | end: | |
263 | return; | |
264 | } | |
265 | ||
266 | /** | |
267 | * Returns the GQuark of a prefixed alias. | |
268 | * | |
269 | * @param prefix Prefix character | |
270 | * @param name Name | |
271 | * @returns Associated GQuark, or 0 on error | |
272 | */ | |
273 | static | |
274 | GQuark get_prefixed_named_quark(char prefix, const char *name) | |
275 | { | |
276 | GQuark qname = 0; | |
277 | ||
8b45963b | 278 | BT_ASSERT(name); |
e98a2d6e PP |
279 | |
280 | /* Prefix character + original string + '\0' */ | |
281 | char *prname = g_new(char, strlen(name) + 2); | |
282 | if (!prname) { | |
87cb75e6 | 283 | BT_LOGE_STR("Failed to allocate a string."); |
e98a2d6e PP |
284 | goto end; |
285 | } | |
286 | ||
287 | sprintf(prname, "%c%s", prefix, name); | |
288 | qname = g_quark_from_string(prname); | |
289 | g_free(prname); | |
290 | ||
291 | end: | |
292 | return qname; | |
293 | } | |
294 | ||
295 | /** | |
939190b3 | 296 | * Looks up a prefixed class alias within a declaration scope. |
e98a2d6e PP |
297 | * |
298 | * @param scope Declaration scope | |
299 | * @param prefix Prefix character | |
300 | * @param name Alias name | |
7b33a0e0 PP |
301 | * @param levels Number of levels to dig into (-1 means infinite) |
302 | * @param copy True to return a copy | |
303 | * @returns Declaration (owned by caller if \p copy is true), | |
304 | * or NULL if not found | |
e98a2d6e PP |
305 | */ |
306 | static | |
939190b3 | 307 | struct ctf_field_class *ctx_decl_scope_lookup_prefix_alias( |
7b33a0e0 PP |
308 | struct ctx_decl_scope *scope, char prefix, const char *name, |
309 | int levels, bool copy) | |
e98a2d6e PP |
310 | { |
311 | GQuark qname = 0; | |
312 | int cur_levels = 0; | |
939190b3 | 313 | struct ctf_field_class *decl = NULL; |
e98a2d6e PP |
314 | struct ctx_decl_scope *cur_scope = scope; |
315 | ||
8b45963b PP |
316 | BT_ASSERT(scope); |
317 | BT_ASSERT(name); | |
e98a2d6e PP |
318 | qname = get_prefixed_named_quark(prefix, name); |
319 | if (!qname) { | |
7b33a0e0 | 320 | goto end; |
e98a2d6e PP |
321 | } |
322 | ||
323 | if (levels < 0) { | |
324 | levels = INT_MAX; | |
325 | } | |
326 | ||
327 | while (cur_scope && cur_levels < levels) { | |
328 | decl = g_hash_table_lookup(cur_scope->decl_map, | |
71c7c95f | 329 | (gconstpointer) GUINT_TO_POINTER(qname)); |
e98a2d6e PP |
330 | if (decl) { |
331 | /* Caller's reference */ | |
7b33a0e0 | 332 | if (copy) { |
939190b3 | 333 | decl = ctf_field_class_copy(decl); |
7b33a0e0 PP |
334 | BT_ASSERT(decl); |
335 | } | |
336 | ||
337 | goto end; | |
e98a2d6e PP |
338 | } |
339 | ||
340 | cur_scope = cur_scope->parent_scope; | |
341 | cur_levels++; | |
342 | } | |
343 | ||
7b33a0e0 | 344 | end: |
e98a2d6e | 345 | return decl; |
e98a2d6e PP |
346 | } |
347 | ||
348 | /** | |
939190b3 | 349 | * Looks up a class alias within a declaration scope. |
e98a2d6e PP |
350 | * |
351 | * @param scope Declaration scope | |
352 | * @param name Alias name | |
7b33a0e0 PP |
353 | * @param levels Number of levels to dig into (-1 means infinite) |
354 | * @param copy True to return a copy | |
355 | * @returns Declaration (owned by caller if \p copy is true), | |
356 | * or NULL if not found | |
e98a2d6e PP |
357 | */ |
358 | static | |
939190b3 | 359 | struct ctf_field_class *ctx_decl_scope_lookup_alias( |
7b33a0e0 PP |
360 | struct ctx_decl_scope *scope, const char *name, int levels, |
361 | bool copy) | |
e98a2d6e PP |
362 | { |
363 | return ctx_decl_scope_lookup_prefix_alias(scope, _PREFIX_ALIAS, | |
7b33a0e0 | 364 | name, levels, copy); |
e98a2d6e PP |
365 | } |
366 | ||
367 | /** | |
368 | * Looks up an enumeration within a declaration scope. | |
369 | * | |
370 | * @param scope Declaration scope | |
371 | * @param name Enumeration name | |
7b33a0e0 PP |
372 | * @param levels Number of levels to dig into (-1 means infinite) |
373 | * @param copy True to return a copy | |
374 | * @returns Declaration (owned by caller if \p copy is true), | |
375 | * or NULL if not found | |
e98a2d6e PP |
376 | */ |
377 | static | |
939190b3 | 378 | struct ctf_field_class_enum *ctx_decl_scope_lookup_enum( |
7b33a0e0 PP |
379 | struct ctx_decl_scope *scope, const char *name, int levels, |
380 | bool copy) | |
e98a2d6e | 381 | { |
7b33a0e0 PP |
382 | return (void *) ctx_decl_scope_lookup_prefix_alias(scope, _PREFIX_ENUM, |
383 | name, levels, copy); | |
e98a2d6e PP |
384 | } |
385 | ||
386 | /** | |
387 | * Looks up a structure within a declaration scope. | |
388 | * | |
389 | * @param scope Declaration scope | |
390 | * @param name Structure name | |
7b33a0e0 PP |
391 | * @param levels Number of levels to dig into (-1 means infinite) |
392 | * @param copy True to return a copy | |
393 | * @returns Declaration (owned by caller if \p copy is true), | |
394 | * or NULL if not found | |
e98a2d6e PP |
395 | */ |
396 | static | |
939190b3 | 397 | struct ctf_field_class_struct *ctx_decl_scope_lookup_struct( |
7b33a0e0 PP |
398 | struct ctx_decl_scope *scope, const char *name, int levels, |
399 | bool copy) | |
e98a2d6e | 400 | { |
7b33a0e0 PP |
401 | return (void *) ctx_decl_scope_lookup_prefix_alias(scope, |
402 | _PREFIX_STRUCT, name, levels, copy); | |
e98a2d6e PP |
403 | } |
404 | ||
405 | /** | |
406 | * Looks up a variant within a declaration scope. | |
407 | * | |
408 | * @param scope Declaration scope | |
409 | * @param name Variant name | |
7b33a0e0 PP |
410 | * @param levels Number of levels to dig into (-1 means infinite) |
411 | * @param copy True to return a copy | |
412 | * @returns Declaration (owned by caller if \p copy is true), | |
413 | * or NULL if not found | |
e98a2d6e PP |
414 | */ |
415 | static | |
939190b3 | 416 | struct ctf_field_class_variant *ctx_decl_scope_lookup_variant( |
7b33a0e0 PP |
417 | struct ctx_decl_scope *scope, const char *name, int levels, |
418 | bool copy) | |
e98a2d6e | 419 | { |
7b33a0e0 PP |
420 | return (void *) ctx_decl_scope_lookup_prefix_alias(scope, |
421 | _PREFIX_VARIANT, name, levels, copy); | |
e98a2d6e PP |
422 | } |
423 | ||
424 | /** | |
939190b3 | 425 | * Registers a prefixed class alias within a declaration scope. |
e98a2d6e PP |
426 | * |
427 | * @param scope Declaration scope | |
428 | * @param prefix Prefix character | |
429 | * @param name Alias name (non-NULL) | |
939190b3 | 430 | * @param decl Field class to register (copied) |
e98a2d6e PP |
431 | * @returns 0 if registration went okay, negative value otherwise |
432 | */ | |
433 | static | |
434 | int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope *scope, | |
939190b3 | 435 | char prefix, const char *name, struct ctf_field_class *decl) |
e98a2d6e PP |
436 | { |
437 | int ret = 0; | |
438 | GQuark qname = 0; | |
e98a2d6e | 439 | |
8b45963b PP |
440 | BT_ASSERT(scope); |
441 | BT_ASSERT(name); | |
442 | BT_ASSERT(decl); | |
e98a2d6e PP |
443 | qname = get_prefixed_named_quark(prefix, name); |
444 | if (!qname) { | |
445 | ret = -ENOMEM; | |
7b33a0e0 | 446 | goto end; |
e98a2d6e PP |
447 | } |
448 | ||
449 | /* Make sure alias does not exist in local scope */ | |
7b33a0e0 PP |
450 | if (ctx_decl_scope_lookup_prefix_alias(scope, prefix, name, 1, |
451 | false)) { | |
e98a2d6e | 452 | ret = -EEXIST; |
7b33a0e0 | 453 | goto end; |
e98a2d6e PP |
454 | } |
455 | ||
939190b3 | 456 | decl = ctf_field_class_copy(decl); |
7b33a0e0 PP |
457 | BT_ASSERT(decl); |
458 | g_hash_table_insert(scope->decl_map, GUINT_TO_POINTER(qname), decl); | |
e98a2d6e | 459 | |
7b33a0e0 | 460 | end: |
e98a2d6e PP |
461 | return ret; |
462 | } | |
463 | ||
464 | /** | |
939190b3 | 465 | * Registers a class alias within a declaration scope. |
e98a2d6e PP |
466 | * |
467 | * @param scope Declaration scope | |
468 | * @param name Alias name (non-NULL) | |
939190b3 | 469 | * @param decl Field class to register (copied) |
e98a2d6e PP |
470 | * @returns 0 if registration went okay, negative value otherwise |
471 | */ | |
472 | static | |
473 | int ctx_decl_scope_register_alias(struct ctx_decl_scope *scope, | |
939190b3 | 474 | const char *name, struct ctf_field_class *decl) |
e98a2d6e PP |
475 | { |
476 | return ctx_decl_scope_register_prefix_alias(scope, _PREFIX_ALIAS, | |
7b33a0e0 | 477 | name, (void *) decl); |
e98a2d6e PP |
478 | } |
479 | ||
480 | /** | |
481 | * Registers an enumeration declaration within a declaration scope. | |
482 | * | |
483 | * @param scope Declaration scope | |
484 | * @param name Enumeration name (non-NULL) | |
939190b3 | 485 | * @param decl Enumeration field class to register (copied) |
e98a2d6e PP |
486 | * @returns 0 if registration went okay, negative value otherwise |
487 | */ | |
488 | static | |
489 | int ctx_decl_scope_register_enum(struct ctx_decl_scope *scope, | |
939190b3 | 490 | const char *name, struct ctf_field_class_enum *decl) |
e98a2d6e PP |
491 | { |
492 | return ctx_decl_scope_register_prefix_alias(scope, _PREFIX_ENUM, | |
7b33a0e0 | 493 | name, (void *) decl); |
e98a2d6e PP |
494 | } |
495 | ||
496 | /** | |
497 | * Registers a structure declaration within a declaration scope. | |
498 | * | |
499 | * @param scope Declaration scope | |
500 | * @param name Structure name (non-NULL) | |
939190b3 | 501 | * @param decl Structure field class to register (copied) |
e98a2d6e PP |
502 | * @returns 0 if registration went okay, negative value otherwise |
503 | */ | |
504 | static | |
505 | int ctx_decl_scope_register_struct(struct ctx_decl_scope *scope, | |
939190b3 | 506 | const char *name, struct ctf_field_class_struct *decl) |
e98a2d6e PP |
507 | { |
508 | return ctx_decl_scope_register_prefix_alias(scope, _PREFIX_STRUCT, | |
7b33a0e0 | 509 | name, (void *) decl); |
e98a2d6e PP |
510 | } |
511 | ||
512 | /** | |
513 | * Registers a variant declaration within a declaration scope. | |
514 | * | |
515 | * @param scope Declaration scope | |
516 | * @param name Variant name (non-NULL) | |
939190b3 | 517 | * @param decl Variant field class to register |
e98a2d6e PP |
518 | * @returns 0 if registration went okay, negative value otherwise |
519 | */ | |
520 | static | |
521 | int ctx_decl_scope_register_variant(struct ctx_decl_scope *scope, | |
939190b3 | 522 | const char *name, struct ctf_field_class_variant *decl) |
e98a2d6e PP |
523 | { |
524 | return ctx_decl_scope_register_prefix_alias(scope, _PREFIX_VARIANT, | |
7b33a0e0 | 525 | name, (void *) decl); |
e98a2d6e PP |
526 | } |
527 | ||
91fd6f6a PP |
528 | /** |
529 | * Destroys a visitor context. | |
530 | * | |
531 | * @param ctx Visitor context to destroy | |
532 | */ | |
533 | static | |
534 | void ctx_destroy(struct ctx *ctx) | |
535 | { | |
536 | struct ctx_decl_scope *scope; | |
91fd6f6a PP |
537 | |
538 | if (!ctx) { | |
539 | goto end; | |
540 | } | |
541 | ||
542 | scope = ctx->current_scope; | |
543 | ||
7b33a0e0 PP |
544 | /* |
545 | * Destroy all scopes, from current one to the root scope. | |
546 | */ | |
91fd6f6a PP |
547 | while (scope) { |
548 | struct ctx_decl_scope *parent_scope = scope->parent_scope; | |
549 | ||
550 | ctx_decl_scope_destroy(scope); | |
551 | scope = parent_scope; | |
552 | } | |
553 | ||
8c6884d9 | 554 | bt_trace_class_put_ref(ctx->trace_class); |
91fd6f6a | 555 | |
7b33a0e0 PP |
556 | if (ctx->ctf_tc) { |
557 | ctf_trace_class_destroy(ctx->ctf_tc); | |
558 | } | |
559 | ||
91fd6f6a PP |
560 | g_free(ctx); |
561 | ||
562 | end: | |
563 | return; | |
564 | } | |
565 | ||
e98a2d6e PP |
566 | /** |
567 | * Creates a new visitor context. | |
568 | * | |
569 | * @param trace Associated trace | |
e98a2d6e PP |
570 | * @returns New visitor context, or NULL on error |
571 | */ | |
572 | static | |
e5e71bf8 | 573 | struct ctx *ctx_create(bt_self_component_source *self_comp, |
10b7a2e4 | 574 | const struct ctf_metadata_decoder_config *decoder_config) |
e98a2d6e PP |
575 | { |
576 | struct ctx *ctx = NULL; | |
e98a2d6e | 577 | |
8b45963b | 578 | BT_ASSERT(decoder_config); |
53ac3428 | 579 | |
1e649dff | 580 | ctx = g_new0(struct ctx, 1); |
e98a2d6e | 581 | if (!ctx) { |
87cb75e6 | 582 | BT_LOGE_STR("Failed to allocate one visitor context."); |
e98a2d6e PP |
583 | goto error; |
584 | } | |
585 | ||
e5e71bf8 PP |
586 | if (self_comp) { |
587 | ctx->trace_class = bt_trace_class_create( | |
588 | bt_self_component_source_as_self_component(self_comp)); | |
589 | if (!ctx->trace_class) { | |
590 | BT_LOGE_STR("Cannot create empty trace class."); | |
591 | goto error; | |
592 | } | |
7b33a0e0 PP |
593 | } |
594 | ||
595 | ctx->ctf_tc = ctf_trace_class_create(); | |
596 | if (!ctx->ctf_tc) { | |
597 | BT_LOGE_STR("Cannot create CTF trace class."); | |
e98a2d6e PP |
598 | goto error; |
599 | } | |
600 | ||
7b33a0e0 PP |
601 | /* Root declaration scope */ |
602 | ctx->current_scope = ctx_decl_scope_create(NULL); | |
603 | if (!ctx->current_scope) { | |
604 | BT_LOGE_STR("Cannot create declaration scope."); | |
e98a2d6e PP |
605 | goto error; |
606 | } | |
607 | ||
53ac3428 | 608 | ctx->decoder_config = *decoder_config; |
7b33a0e0 | 609 | goto end; |
e98a2d6e PP |
610 | |
611 | error: | |
91fd6f6a | 612 | ctx_destroy(ctx); |
7b33a0e0 PP |
613 | ctx = NULL; |
614 | ||
615 | end: | |
616 | return ctx; | |
e98a2d6e PP |
617 | } |
618 | ||
e98a2d6e PP |
619 | /** |
620 | * Pushes a new declaration scope on top of a visitor context's | |
621 | * declaration scope stack. | |
622 | * | |
623 | * @param ctx Visitor context | |
624 | * @returns 0 on success, or a negative value on error | |
625 | */ | |
626 | static | |
627 | int ctx_push_scope(struct ctx *ctx) | |
628 | { | |
629 | int ret = 0; | |
630 | struct ctx_decl_scope *new_scope; | |
631 | ||
8b45963b | 632 | BT_ASSERT(ctx); |
e98a2d6e PP |
633 | new_scope = ctx_decl_scope_create(ctx->current_scope); |
634 | if (!new_scope) { | |
87cb75e6 | 635 | BT_LOGE_STR("Cannot create declaration scope."); |
e98a2d6e PP |
636 | ret = -ENOMEM; |
637 | goto end; | |
638 | } | |
639 | ||
640 | ctx->current_scope = new_scope; | |
641 | ||
642 | end: | |
643 | return ret; | |
644 | } | |
645 | ||
646 | static | |
647 | void ctx_pop_scope(struct ctx *ctx) | |
648 | { | |
649 | struct ctx_decl_scope *parent_scope = NULL; | |
650 | ||
8b45963b | 651 | BT_ASSERT(ctx); |
e98a2d6e PP |
652 | |
653 | if (!ctx->current_scope) { | |
654 | goto end; | |
655 | } | |
656 | ||
657 | parent_scope = ctx->current_scope->parent_scope; | |
658 | ctx_decl_scope_destroy(ctx->current_scope); | |
659 | ctx->current_scope = parent_scope; | |
660 | ||
661 | end: | |
662 | return; | |
663 | } | |
664 | ||
665 | static | |
939190b3 PP |
666 | int visit_field_class_specifier_list(struct ctx *ctx, struct ctf_node *ts_list, |
667 | struct ctf_field_class **decl); | |
e98a2d6e | 668 | |
a153db48 PP |
669 | static |
670 | char *remove_underscores_from_field_ref(const char *field_ref) | |
671 | { | |
672 | const char *in_ch; | |
673 | char *out_ch; | |
674 | char *ret; | |
675 | enum { | |
676 | UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE, | |
677 | UNDERSCORE_REMOVE_STATE_DO_NOT_REMOVE_NEXT_UNDERSCORE, | |
678 | } state = UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE; | |
679 | ||
8b45963b | 680 | BT_ASSERT(field_ref); |
a153db48 PP |
681 | ret = calloc(strlen(field_ref) + 1, 1); |
682 | if (!ret) { | |
683 | BT_LOGE("Failed to allocate a string: size=%zu", | |
684 | strlen(field_ref) + 1); | |
685 | goto end; | |
686 | } | |
687 | ||
688 | in_ch = field_ref; | |
689 | out_ch = ret; | |
690 | ||
691 | while (*in_ch != '\0') { | |
692 | switch (*in_ch) { | |
693 | case ' ': | |
694 | case '\t': | |
695 | /* Remove whitespace */ | |
696 | in_ch++; | |
697 | continue; | |
698 | case '_': | |
699 | if (state == UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE) { | |
700 | in_ch++; | |
701 | state = UNDERSCORE_REMOVE_STATE_DO_NOT_REMOVE_NEXT_UNDERSCORE; | |
702 | continue; | |
703 | } | |
704 | ||
705 | goto copy; | |
706 | case '.': | |
707 | state = UNDERSCORE_REMOVE_STATE_REMOVE_NEXT_UNDERSCORE; | |
708 | goto copy; | |
709 | default: | |
710 | state = UNDERSCORE_REMOVE_STATE_DO_NOT_REMOVE_NEXT_UNDERSCORE; | |
711 | goto copy; | |
712 | } | |
713 | ||
714 | copy: | |
715 | *out_ch = *in_ch; | |
716 | in_ch++; | |
717 | out_ch++; | |
718 | } | |
719 | ||
720 | end: | |
721 | return ret; | |
722 | } | |
723 | ||
e98a2d6e PP |
724 | static |
725 | int is_unary_string(struct bt_list_head *head) | |
726 | { | |
727 | int ret = TRUE; | |
728 | struct ctf_node *node; | |
729 | ||
730 | bt_list_for_each_entry(node, head, siblings) { | |
731 | if (node->type != NODE_UNARY_EXPRESSION) { | |
732 | ret = FALSE; | |
733 | } | |
734 | ||
735 | if (node->u.unary_expression.type != UNARY_STRING) { | |
736 | ret = FALSE; | |
737 | } | |
738 | } | |
739 | ||
740 | return ret; | |
741 | } | |
742 | ||
743 | static | |
744 | char *concatenate_unary_strings(struct bt_list_head *head) | |
745 | { | |
746 | int i = 0; | |
747 | GString *str; | |
748 | struct ctf_node *node; | |
749 | ||
750 | str = g_string_new(NULL); | |
8b45963b | 751 | BT_ASSERT(str); |
e98a2d6e PP |
752 | |
753 | bt_list_for_each_entry(node, head, siblings) { | |
754 | char *src_string; | |
755 | ||
756 | if ( | |
757 | node->type != NODE_UNARY_EXPRESSION || | |
758 | node->u.unary_expression.type != UNARY_STRING || | |
759 | !( | |
760 | ( | |
761 | node->u.unary_expression.link != | |
762 | UNARY_LINK_UNKNOWN | |
763 | ) ^ (i == 0) | |
764 | ) | |
765 | ) { | |
766 | goto error; | |
767 | } | |
768 | ||
769 | switch (node->u.unary_expression.link) { | |
770 | case UNARY_DOTLINK: | |
771 | g_string_append(str, "."); | |
772 | break; | |
773 | case UNARY_ARROWLINK: | |
774 | g_string_append(str, "->"); | |
775 | break; | |
776 | case UNARY_DOTDOTDOT: | |
777 | g_string_append(str, "..."); | |
778 | break; | |
779 | default: | |
780 | break; | |
781 | } | |
782 | ||
783 | src_string = node->u.unary_expression.u.string; | |
784 | g_string_append(str, src_string); | |
785 | i++; | |
786 | } | |
787 | ||
788 | /* Destroys the container, returns the underlying string */ | |
789 | return g_string_free(str, FALSE); | |
790 | ||
791 | error: | |
792 | /* This always returns NULL */ | |
793 | return g_string_free(str, TRUE); | |
794 | } | |
795 | ||
796 | static | |
797 | const char *get_map_clock_name_value(struct bt_list_head *head) | |
798 | { | |
799 | int i = 0; | |
800 | struct ctf_node *node; | |
801 | const char *name = NULL; | |
802 | ||
803 | bt_list_for_each_entry(node, head, siblings) { | |
804 | char *src_string; | |
805 | int uexpr_type = node->u.unary_expression.type; | |
806 | int uexpr_link = node->u.unary_expression.link; | |
807 | int cond = node->type != NODE_UNARY_EXPRESSION || | |
808 | uexpr_type != UNARY_STRING || | |
809 | !((uexpr_link != UNARY_LINK_UNKNOWN) ^ (i == 0)); | |
810 | if (cond) { | |
811 | goto error; | |
812 | } | |
813 | ||
814 | /* Needs to be chained with . */ | |
815 | switch (node->u.unary_expression.link) { | |
816 | case UNARY_DOTLINK: | |
817 | break; | |
818 | case UNARY_ARROWLINK: | |
819 | case UNARY_DOTDOTDOT: | |
820 | goto error; | |
821 | default: | |
822 | break; | |
823 | } | |
824 | ||
825 | src_string = node->u.unary_expression.u.string; | |
826 | ||
827 | switch (i) { | |
828 | case 0: | |
829 | if (strcmp("clock", src_string)) { | |
830 | goto error; | |
831 | } | |
832 | break; | |
833 | case 1: | |
834 | name = src_string; | |
835 | break; | |
836 | case 2: | |
837 | if (strcmp("value", src_string)) { | |
838 | goto error; | |
839 | } | |
840 | break; | |
841 | default: | |
842 | /* Extra identifier, unknown */ | |
843 | goto error; | |
844 | } | |
845 | ||
846 | i++; | |
847 | } | |
848 | ||
849 | return name; | |
850 | ||
851 | error: | |
852 | return NULL; | |
853 | } | |
854 | ||
855 | static | |
856 | int is_unary_unsigned(struct bt_list_head *head) | |
857 | { | |
858 | int ret = TRUE; | |
859 | struct ctf_node *node; | |
860 | ||
861 | bt_list_for_each_entry(node, head, siblings) { | |
862 | if (node->type != NODE_UNARY_EXPRESSION) { | |
863 | ret = FALSE; | |
864 | } | |
865 | ||
866 | if (node->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { | |
867 | ret = FALSE; | |
868 | } | |
869 | } | |
870 | ||
871 | return ret; | |
872 | } | |
873 | ||
874 | static | |
875 | int get_unary_unsigned(struct bt_list_head *head, uint64_t *value) | |
876 | { | |
877 | int i = 0; | |
878 | int ret = 0; | |
879 | struct ctf_node *node; | |
880 | ||
8fc063a2 PP |
881 | *value = 0; |
882 | ||
d82c602e JG |
883 | if (bt_list_empty(head)) { |
884 | ret = -1; | |
885 | goto end; | |
886 | } | |
887 | ||
e98a2d6e PP |
888 | bt_list_for_each_entry(node, head, siblings) { |
889 | int uexpr_type = node->u.unary_expression.type; | |
890 | int uexpr_link = node->u.unary_expression.link; | |
891 | int cond = node->type != NODE_UNARY_EXPRESSION || | |
892 | uexpr_type != UNARY_UNSIGNED_CONSTANT || | |
893 | uexpr_link != UNARY_LINK_UNKNOWN || i != 0; | |
894 | if (cond) { | |
87cb75e6 | 895 | _BT_LOGE_NODE(node, "Invalid constant unsigned integer."); |
e98a2d6e PP |
896 | ret = -EINVAL; |
897 | goto end; | |
898 | } | |
899 | ||
900 | *value = node->u.unary_expression.u.unsigned_constant; | |
901 | i++; | |
902 | } | |
903 | ||
904 | end: | |
905 | return ret; | |
906 | } | |
907 | ||
908 | static | |
909 | int is_unary_signed(struct bt_list_head *head) | |
910 | { | |
911 | int ret = TRUE; | |
912 | struct ctf_node *node; | |
913 | ||
914 | bt_list_for_each_entry(node, head, siblings) { | |
915 | if (node->type != NODE_UNARY_EXPRESSION) { | |
916 | ret = FALSE; | |
917 | } | |
918 | ||
919 | if (node->u.unary_expression.type != UNARY_SIGNED_CONSTANT) { | |
920 | ret = FALSE; | |
921 | } | |
922 | } | |
923 | ||
924 | return ret; | |
925 | } | |
926 | ||
927 | static | |
928 | int get_unary_signed(struct bt_list_head *head, int64_t *value) | |
929 | { | |
930 | int i = 0; | |
931 | int ret = 0; | |
932 | struct ctf_node *node; | |
933 | ||
934 | bt_list_for_each_entry(node, head, siblings) { | |
935 | int uexpr_type = node->u.unary_expression.type; | |
936 | int uexpr_link = node->u.unary_expression.link; | |
937 | int cond = node->type != NODE_UNARY_EXPRESSION || | |
e98a2d6e PP |
938 | (uexpr_type != UNARY_UNSIGNED_CONSTANT && |
939 | uexpr_type != UNARY_SIGNED_CONSTANT) || | |
940 | uexpr_link != UNARY_LINK_UNKNOWN || i != 0; | |
941 | if (cond) { | |
942 | ret = -EINVAL; | |
943 | goto end; | |
944 | } | |
945 | ||
69610492 | 946 | switch (uexpr_type) { |
e98a2d6e PP |
947 | case UNARY_UNSIGNED_CONSTANT: |
948 | *value = (int64_t) | |
949 | node->u.unary_expression.u.unsigned_constant; | |
950 | break; | |
951 | case UNARY_SIGNED_CONSTANT: | |
952 | *value = node->u.unary_expression.u.signed_constant; | |
953 | break; | |
954 | default: | |
955 | ret = -EINVAL; | |
956 | goto end; | |
957 | } | |
958 | ||
959 | i++; | |
960 | } | |
961 | ||
962 | end: | |
963 | return ret; | |
964 | } | |
965 | ||
966 | static | |
967 | int get_unary_uuid(struct bt_list_head *head, unsigned char *uuid) | |
968 | { | |
969 | int i = 0; | |
970 | int ret = 0; | |
971 | struct ctf_node *node; | |
972 | ||
973 | bt_list_for_each_entry(node, head, siblings) { | |
974 | int uexpr_type = node->u.unary_expression.type; | |
975 | int uexpr_link = node->u.unary_expression.link; | |
976 | const char *src_string; | |
977 | ||
978 | if (node->type != NODE_UNARY_EXPRESSION || | |
979 | uexpr_type != UNARY_STRING || | |
980 | uexpr_link != UNARY_LINK_UNKNOWN || | |
981 | i != 0) { | |
982 | ret = -EINVAL; | |
983 | goto end; | |
984 | } | |
985 | ||
986 | src_string = node->u.unary_expression.u.string; | |
987 | ret = bt_uuid_parse(src_string, uuid); | |
988 | if (ret) { | |
87cb75e6 PP |
989 | _BT_LOGE_NODE(node, |
990 | "Cannot parse UUID: uuid=\"%s\"", src_string); | |
e98a2d6e PP |
991 | goto end; |
992 | } | |
993 | } | |
994 | ||
995 | end: | |
996 | return ret; | |
997 | } | |
998 | ||
999 | static | |
55314f2a | 1000 | int get_boolean(struct ctf_node *unary_expr) |
e98a2d6e PP |
1001 | { |
1002 | int ret = 0; | |
1003 | ||
1004 | if (unary_expr->type != NODE_UNARY_EXPRESSION) { | |
87cb75e6 PP |
1005 | _BT_LOGE_NODE(unary_expr, |
1006 | "Expecting unary expression: node-type=%d", | |
37d21e07 | 1007 | unary_expr->type); |
e98a2d6e PP |
1008 | ret = -EINVAL; |
1009 | goto end; | |
1010 | } | |
1011 | ||
1012 | switch (unary_expr->u.unary_expression.type) { | |
1013 | case UNARY_UNSIGNED_CONSTANT: | |
1014 | ret = (unary_expr->u.unary_expression.u.unsigned_constant != 0); | |
1015 | break; | |
1016 | case UNARY_SIGNED_CONSTANT: | |
1017 | ret = (unary_expr->u.unary_expression.u.signed_constant != 0); | |
1018 | break; | |
1019 | case UNARY_STRING: | |
1020 | { | |
1021 | const char *str = unary_expr->u.unary_expression.u.string; | |
1022 | ||
1023 | if (!strcmp(str, "true") || !strcmp(str, "TRUE")) { | |
1024 | ret = TRUE; | |
1025 | } else if (!strcmp(str, "false") || !strcmp(str, "FALSE")) { | |
1026 | ret = FALSE; | |
1027 | } else { | |
87cb75e6 PP |
1028 | _BT_LOGE_NODE(unary_expr, |
1029 | "Unexpected boolean value: value=\"%s\"", str); | |
e98a2d6e PP |
1030 | ret = -EINVAL; |
1031 | goto end; | |
1032 | } | |
1033 | break; | |
1034 | } | |
1035 | default: | |
87cb75e6 PP |
1036 | _BT_LOGE_NODE(unary_expr, |
1037 | "Unexpected unary expression type: node-type=%d", | |
37d21e07 | 1038 | unary_expr->u.unary_expression.type); |
e98a2d6e PP |
1039 | ret = -EINVAL; |
1040 | goto end; | |
1041 | } | |
1042 | ||
1043 | end: | |
1044 | return ret; | |
1045 | } | |
1046 | ||
1047 | static | |
7b33a0e0 | 1048 | enum ctf_byte_order byte_order_from_unary_expr(struct ctf_node *unary_expr) |
e98a2d6e PP |
1049 | { |
1050 | const char *str; | |
7b33a0e0 | 1051 | enum ctf_byte_order bo = -1; |
e98a2d6e PP |
1052 | |
1053 | if (unary_expr->u.unary_expression.type != UNARY_STRING) { | |
87cb75e6 PP |
1054 | _BT_LOGE_NODE(unary_expr, |
1055 | "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`."); | |
e98a2d6e PP |
1056 | goto end; |
1057 | } | |
1058 | ||
1059 | str = unary_expr->u.unary_expression.u.string; | |
1060 | ||
1061 | if (!strcmp(str, "be") || !strcmp(str, "network")) { | |
7b33a0e0 | 1062 | bo = CTF_BYTE_ORDER_BIG; |
e98a2d6e | 1063 | } else if (!strcmp(str, "le")) { |
7b33a0e0 | 1064 | bo = CTF_BYTE_ORDER_LITTLE; |
e98a2d6e | 1065 | } else if (!strcmp(str, "native")) { |
7b33a0e0 | 1066 | bo = CTF_BYTE_ORDER_DEFAULT; |
e98a2d6e | 1067 | } else { |
87cb75e6 PP |
1068 | _BT_LOGE_NODE(unary_expr, |
1069 | "Unexpected \"byte_order\" attribute value: " | |
37d21e07 | 1070 | "expecting `be`, `le`, `network`, or `native`: value=\"%s\"", |
e98a2d6e PP |
1071 | str); |
1072 | goto end; | |
1073 | } | |
1074 | ||
1075 | end: | |
1076 | return bo; | |
1077 | } | |
1078 | ||
1079 | static | |
7b33a0e0 PP |
1080 | enum ctf_byte_order get_real_byte_order(struct ctx *ctx, |
1081 | struct ctf_node *uexpr) | |
e98a2d6e | 1082 | { |
7b33a0e0 | 1083 | enum ctf_byte_order bo = byte_order_from_unary_expr(uexpr); |
e98a2d6e | 1084 | |
7b33a0e0 PP |
1085 | if (bo == CTF_BYTE_ORDER_DEFAULT) { |
1086 | bo = ctx->ctf_tc->default_byte_order; | |
e98a2d6e PP |
1087 | } |
1088 | ||
1089 | return bo; | |
1090 | } | |
1091 | ||
1092 | static | |
1093 | int is_align_valid(uint64_t align) | |
1094 | { | |
7b33a0e0 | 1095 | return (align != 0) && !(align & (align - UINT64_C(1))); |
e98a2d6e PP |
1096 | } |
1097 | ||
1098 | static | |
939190b3 | 1099 | int get_class_specifier_name(struct ctx *ctx, struct ctf_node *cls_specifier, |
7b33a0e0 | 1100 | GString *str) |
e98a2d6e PP |
1101 | { |
1102 | int ret = 0; | |
1103 | ||
939190b3 PP |
1104 | if (cls_specifier->type != NODE_TYPE_SPECIFIER) { |
1105 | _BT_LOGE_NODE(cls_specifier, | |
87cb75e6 | 1106 | "Unexpected node type: node-type=%d", |
939190b3 | 1107 | cls_specifier->type); |
e98a2d6e PP |
1108 | ret = -EINVAL; |
1109 | goto end; | |
1110 | } | |
1111 | ||
939190b3 | 1112 | switch (cls_specifier->u.field_class_specifier.type) { |
e98a2d6e PP |
1113 | case TYPESPEC_VOID: |
1114 | g_string_append(str, "void"); | |
1115 | break; | |
1116 | case TYPESPEC_CHAR: | |
1117 | g_string_append(str, "char"); | |
1118 | break; | |
1119 | case TYPESPEC_SHORT: | |
1120 | g_string_append(str, "short"); | |
1121 | break; | |
1122 | case TYPESPEC_INT: | |
1123 | g_string_append(str, "int"); | |
1124 | break; | |
1125 | case TYPESPEC_LONG: | |
1126 | g_string_append(str, "long"); | |
1127 | break; | |
1128 | case TYPESPEC_FLOAT: | |
1129 | g_string_append(str, "float"); | |
1130 | break; | |
1131 | case TYPESPEC_DOUBLE: | |
1132 | g_string_append(str, "double"); | |
1133 | break; | |
1134 | case TYPESPEC_SIGNED: | |
1135 | g_string_append(str, "signed"); | |
1136 | break; | |
1137 | case TYPESPEC_UNSIGNED: | |
1138 | g_string_append(str, "unsigned"); | |
1139 | break; | |
1140 | case TYPESPEC_BOOL: | |
1141 | g_string_append(str, "bool"); | |
1142 | break; | |
1143 | case TYPESPEC_COMPLEX: | |
1144 | g_string_append(str, "_Complex"); | |
1145 | break; | |
1146 | case TYPESPEC_IMAGINARY: | |
1147 | g_string_append(str, "_Imaginary"); | |
1148 | break; | |
1149 | case TYPESPEC_CONST: | |
1150 | g_string_append(str, "const"); | |
1151 | break; | |
1152 | case TYPESPEC_ID_TYPE: | |
939190b3 | 1153 | if (cls_specifier->u.field_class_specifier.id_type) { |
e98a2d6e | 1154 | g_string_append(str, |
939190b3 | 1155 | cls_specifier->u.field_class_specifier.id_type); |
e98a2d6e PP |
1156 | } |
1157 | break; | |
1158 | case TYPESPEC_STRUCT: | |
1159 | { | |
939190b3 | 1160 | struct ctf_node *node = cls_specifier->u.field_class_specifier.node; |
e98a2d6e PP |
1161 | |
1162 | if (!node->u._struct.name) { | |
939190b3 | 1163 | _BT_LOGE_NODE(node, "Unexpected empty structure field class name."); |
e98a2d6e PP |
1164 | ret = -EINVAL; |
1165 | goto end; | |
1166 | } | |
1167 | ||
1168 | g_string_append(str, "struct "); | |
1169 | g_string_append(str, node->u._struct.name); | |
1170 | break; | |
1171 | } | |
1172 | case TYPESPEC_VARIANT: | |
1173 | { | |
939190b3 | 1174 | struct ctf_node *node = cls_specifier->u.field_class_specifier.node; |
e98a2d6e PP |
1175 | |
1176 | if (!node->u.variant.name) { | |
939190b3 | 1177 | _BT_LOGE_NODE(node, "Unexpected empty variant field class name."); |
e98a2d6e PP |
1178 | ret = -EINVAL; |
1179 | goto end; | |
1180 | } | |
1181 | ||
1182 | g_string_append(str, "variant "); | |
1183 | g_string_append(str, node->u.variant.name); | |
1184 | break; | |
1185 | } | |
1186 | case TYPESPEC_ENUM: | |
1187 | { | |
939190b3 | 1188 | struct ctf_node *node = cls_specifier->u.field_class_specifier.node; |
e98a2d6e PP |
1189 | |
1190 | if (!node->u._enum.enum_id) { | |
87cb75e6 | 1191 | _BT_LOGE_NODE(node, |
939190b3 | 1192 | "Unexpected empty enumeration field class (`enum`) name."); |
e98a2d6e PP |
1193 | ret = -EINVAL; |
1194 | goto end; | |
1195 | } | |
1196 | ||
1197 | g_string_append(str, "enum "); | |
1198 | g_string_append(str, node->u._enum.enum_id); | |
1199 | break; | |
1200 | } | |
1201 | case TYPESPEC_FLOATING_POINT: | |
1202 | case TYPESPEC_INTEGER: | |
1203 | case TYPESPEC_STRING: | |
1204 | default: | |
939190b3 PP |
1205 | _BT_LOGE_NODE(cls_specifier->u.field_class_specifier.node, |
1206 | "Unexpected field class specifier type: %d", | |
1207 | cls_specifier->u.field_class_specifier.type); | |
e98a2d6e PP |
1208 | ret = -EINVAL; |
1209 | goto end; | |
1210 | } | |
1211 | ||
1212 | end: | |
1213 | return ret; | |
1214 | } | |
1215 | ||
1216 | static | |
939190b3 PP |
1217 | int get_class_specifier_list_name(struct ctx *ctx, |
1218 | struct ctf_node *cls_specifier_list, GString *str) | |
e98a2d6e | 1219 | { |
502b0402 | 1220 | int ret = 0; |
e98a2d6e PP |
1221 | struct ctf_node *iter; |
1222 | int alias_item_nr = 0; | |
1223 | struct bt_list_head *head = | |
939190b3 | 1224 | &cls_specifier_list->u.field_class_specifier_list.head; |
e98a2d6e PP |
1225 | |
1226 | bt_list_for_each_entry(iter, head, siblings) { | |
1227 | if (alias_item_nr != 0) { | |
1228 | g_string_append(str, " "); | |
1229 | } | |
1230 | ||
1231 | alias_item_nr++; | |
939190b3 | 1232 | ret = get_class_specifier_name(ctx, iter, str); |
e98a2d6e PP |
1233 | if (ret) { |
1234 | goto end; | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | end: | |
1239 | return ret; | |
1240 | } | |
1241 | ||
1242 | static | |
939190b3 PP |
1243 | GQuark create_class_alias_identifier(struct ctx *ctx, |
1244 | struct ctf_node *cls_specifier_list, | |
1245 | struct ctf_node *node_field_class_declarator) | |
e98a2d6e PP |
1246 | { |
1247 | int ret; | |
1248 | char *str_c; | |
1249 | GString *str; | |
1250 | GQuark qalias = 0; | |
1251 | struct ctf_node *iter; | |
1252 | struct bt_list_head *pointers = | |
939190b3 | 1253 | &node_field_class_declarator->u.field_class_declarator.pointers; |
e98a2d6e PP |
1254 | |
1255 | str = g_string_new(""); | |
939190b3 | 1256 | ret = get_class_specifier_list_name(ctx, cls_specifier_list, str); |
e98a2d6e PP |
1257 | if (ret) { |
1258 | g_string_free(str, TRUE); | |
1259 | goto end; | |
1260 | } | |
1261 | ||
1262 | bt_list_for_each_entry(iter, pointers, siblings) { | |
1263 | g_string_append(str, " *"); | |
1264 | ||
1265 | if (iter->u.pointer.const_qualifier) { | |
1266 | g_string_append(str, " const"); | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | str_c = g_string_free(str, FALSE); | |
1271 | qalias = g_quark_from_string(str_c); | |
1272 | g_free(str_c); | |
1273 | ||
1274 | end: | |
1275 | return qalias; | |
1276 | } | |
1277 | ||
1278 | static | |
939190b3 PP |
1279 | int visit_field_class_declarator(struct ctx *ctx, |
1280 | struct ctf_node *cls_specifier_list, | |
1281 | GQuark *field_name, struct ctf_node *node_field_class_declarator, | |
1282 | struct ctf_field_class **field_decl, | |
1283 | struct ctf_field_class *nested_decl) | |
e98a2d6e PP |
1284 | { |
1285 | /* | |
1286 | * During this whole function, nested_decl is always OURS, | |
1287 | * whereas field_decl is an output which we create, but | |
1288 | * belongs to the caller (it is moved). | |
1289 | */ | |
e98a2d6e PP |
1290 | int ret = 0; |
1291 | *field_decl = NULL; | |
1292 | ||
939190b3 PP |
1293 | /* Validate field class declarator node */ |
1294 | if (node_field_class_declarator) { | |
1295 | if (node_field_class_declarator->u.field_class_declarator.type == | |
e98a2d6e | 1296 | TYPEDEC_UNKNOWN) { |
939190b3 PP |
1297 | _BT_LOGE_NODE(node_field_class_declarator, |
1298 | "Unexpected field class declarator type: type=%d", | |
1299 | node_field_class_declarator->u.field_class_declarator.type); | |
e98a2d6e PP |
1300 | ret = -EINVAL; |
1301 | goto error; | |
1302 | } | |
1303 | ||
1304 | /* TODO: GCC bitfields not supported yet */ | |
939190b3 | 1305 | if (node_field_class_declarator->u.field_class_declarator.bitfield_len != |
e98a2d6e | 1306 | NULL) { |
939190b3 | 1307 | _BT_LOGE_NODE(node_field_class_declarator, |
87cb75e6 | 1308 | "GCC bitfields are not supported as of this version."); |
e98a2d6e PP |
1309 | ret = -EPERM; |
1310 | goto error; | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | /* Find the right nested declaration if not provided */ | |
1315 | if (!nested_decl) { | |
1316 | struct bt_list_head *pointers = | |
939190b3 | 1317 | &node_field_class_declarator->u.field_class_declarator.pointers; |
e98a2d6e | 1318 | |
939190b3 | 1319 | if (node_field_class_declarator && !bt_list_empty(pointers)) { |
e98a2d6e | 1320 | GQuark qalias; |
e98a2d6e PP |
1321 | |
1322 | /* | |
1323 | * If we have a pointer declarator, it HAS to | |
939190b3 PP |
1324 | * be present in the field class aliases (else |
1325 | * fail). | |
e98a2d6e | 1326 | */ |
939190b3 PP |
1327 | qalias = create_class_alias_identifier(ctx, |
1328 | cls_specifier_list, node_field_class_declarator); | |
e98a2d6e PP |
1329 | nested_decl = |
1330 | ctx_decl_scope_lookup_alias(ctx->current_scope, | |
7b33a0e0 | 1331 | g_quark_to_string(qalias), -1, true); |
e98a2d6e | 1332 | if (!nested_decl) { |
939190b3 PP |
1333 | _BT_LOGE_NODE(node_field_class_declarator, |
1334 | "Cannot find class alias: name=\"%s\"", | |
e98a2d6e PP |
1335 | g_quark_to_string(qalias)); |
1336 | ret = -EINVAL; | |
1337 | goto error; | |
1338 | } | |
1339 | ||
af0c18e3 | 1340 | if (nested_decl->type == CTF_FIELD_CLASS_TYPE_INT) { |
7b33a0e0 | 1341 | /* Pointer: force integer's base to 16 */ |
939190b3 | 1342 | struct ctf_field_class_int *int_fc = |
7b33a0e0 | 1343 | (void *) nested_decl; |
e98a2d6e | 1344 | |
939190b3 PP |
1345 | int_fc->disp_base = |
1346 | BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL; | |
e98a2d6e PP |
1347 | } |
1348 | } else { | |
939190b3 PP |
1349 | ret = visit_field_class_specifier_list(ctx, |
1350 | cls_specifier_list, &nested_decl); | |
e98a2d6e | 1351 | if (ret) { |
8b45963b | 1352 | BT_ASSERT(!nested_decl); |
e98a2d6e PP |
1353 | goto error; |
1354 | } | |
1355 | } | |
1356 | } | |
1357 | ||
8b45963b | 1358 | BT_ASSERT(nested_decl); |
e98a2d6e | 1359 | |
939190b3 | 1360 | if (!node_field_class_declarator) { |
7b33a0e0 PP |
1361 | *field_decl = nested_decl; |
1362 | nested_decl = NULL; | |
e98a2d6e PP |
1363 | goto end; |
1364 | } | |
1365 | ||
939190b3 PP |
1366 | if (node_field_class_declarator->u.field_class_declarator.type == TYPEDEC_ID) { |
1367 | if (node_field_class_declarator->u.field_class_declarator.u.id) { | |
e98a2d6e | 1368 | const char *id = |
939190b3 | 1369 | node_field_class_declarator->u.field_class_declarator.u.id; |
e98a2d6e | 1370 | |
a153db48 PP |
1371 | if (id[0] == '_') { |
1372 | id++; | |
1373 | } | |
1374 | ||
e98a2d6e PP |
1375 | *field_name = g_quark_from_string(id); |
1376 | } else { | |
1377 | *field_name = 0; | |
1378 | } | |
1379 | ||
7b33a0e0 PP |
1380 | *field_decl = nested_decl; |
1381 | nested_decl = NULL; | |
e98a2d6e PP |
1382 | goto end; |
1383 | } else { | |
1384 | struct ctf_node *first; | |
939190b3 PP |
1385 | struct ctf_field_class *decl = NULL; |
1386 | struct ctf_field_class *outer_field_decl = NULL; | |
e98a2d6e | 1387 | struct bt_list_head *length = |
939190b3 PP |
1388 | &node_field_class_declarator-> |
1389 | u.field_class_declarator.u.nested.length; | |
e98a2d6e PP |
1390 | |
1391 | /* Create array/sequence, pass nested_decl as child */ | |
1392 | if (bt_list_empty(length)) { | |
939190b3 | 1393 | _BT_LOGE_NODE(node_field_class_declarator, |
87cb75e6 | 1394 | "Expecting length field reference or value."); |
e98a2d6e PP |
1395 | ret = -EINVAL; |
1396 | goto error; | |
1397 | } | |
1398 | ||
1399 | first = _BT_LIST_FIRST_ENTRY(length, struct ctf_node, siblings); | |
1400 | if (first->type != NODE_UNARY_EXPRESSION) { | |
87cb75e6 PP |
1401 | _BT_LOGE_NODE(first, |
1402 | "Unexpected node type: node-type=%d", | |
1403 | first->type); | |
e98a2d6e PP |
1404 | ret = -EINVAL; |
1405 | goto error; | |
1406 | } | |
1407 | ||
1408 | switch (first->u.unary_expression.type) { | |
1409 | case UNARY_UNSIGNED_CONSTANT: | |
1410 | { | |
939190b3 | 1411 | struct ctf_field_class_array *array_decl = NULL; |
7b33a0e0 | 1412 | |
939190b3 | 1413 | array_decl = ctf_field_class_array_create(); |
7b33a0e0 PP |
1414 | BT_ASSERT(array_decl); |
1415 | array_decl->length = | |
1416 | first->u.unary_expression.u.unsigned_constant; | |
939190b3 | 1417 | array_decl->base.elem_fc = nested_decl; |
7b33a0e0 PP |
1418 | nested_decl = NULL; |
1419 | decl = (void *) array_decl; | |
e98a2d6e PP |
1420 | break; |
1421 | } | |
1422 | case UNARY_STRING: | |
1423 | { | |
1424 | /* Lookup unsigned integer definition, create seq. */ | |
939190b3 | 1425 | struct ctf_field_class_sequence *seq_decl = NULL; |
e98a2d6e PP |
1426 | char *length_name = concatenate_unary_strings(length); |
1427 | ||
1428 | if (!length_name) { | |
939190b3 | 1429 | _BT_LOGE_NODE(node_field_class_declarator, |
87cb75e6 | 1430 | "Cannot concatenate unary strings."); |
e98a2d6e PP |
1431 | ret = -EINVAL; |
1432 | goto error; | |
1433 | } | |
1434 | ||
7b33a0e0 PP |
1435 | if (strncmp(length_name, "env.", 4) == 0) { |
1436 | /* This is, in fact, an array */ | |
1437 | const char *env_entry_name = &length_name[4]; | |
1438 | struct ctf_trace_class_env_entry *env_entry = | |
1439 | ctf_trace_class_borrow_env_entry_by_name( | |
1440 | ctx->ctf_tc, env_entry_name); | |
939190b3 | 1441 | struct ctf_field_class_array *array_decl; |
7b33a0e0 PP |
1442 | |
1443 | if (!env_entry) { | |
939190b3 | 1444 | _BT_LOGE_NODE(node_field_class_declarator, |
7b33a0e0 PP |
1445 | "Cannot find environment entry: " |
1446 | "name=\"%s\"", env_entry_name); | |
1447 | ret = -EINVAL; | |
1448 | goto error; | |
1449 | } | |
1450 | ||
1451 | if (env_entry->type != CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT) { | |
939190b3 | 1452 | _BT_LOGE_NODE(node_field_class_declarator, |
7b33a0e0 PP |
1453 | "Wrong environment entry type " |
1454 | "(expecting integer): " | |
1455 | "name=\"%s\"", env_entry_name); | |
1456 | ret = -EINVAL; | |
1457 | goto error; | |
1458 | } | |
1459 | ||
1460 | if (env_entry->value.i < 0) { | |
939190b3 | 1461 | _BT_LOGE_NODE(node_field_class_declarator, |
7b33a0e0 PP |
1462 | "Invalid, negative array length: " |
1463 | "env-entry-name=\"%s\", " | |
1464 | "value=%" PRId64, | |
1465 | env_entry_name, | |
1466 | env_entry->value.i); | |
1467 | ret = -EINVAL; | |
1468 | goto error; | |
1469 | } | |
1470 | ||
939190b3 | 1471 | array_decl = ctf_field_class_array_create(); |
7b33a0e0 PP |
1472 | BT_ASSERT(array_decl); |
1473 | array_decl->length = | |
1474 | (uint64_t) env_entry->value.i; | |
939190b3 | 1475 | array_decl->base.elem_fc = nested_decl; |
7b33a0e0 PP |
1476 | nested_decl = NULL; |
1477 | decl = (void *) array_decl; | |
1478 | } else { | |
1479 | char *length_name_no_underscore = | |
1480 | remove_underscores_from_field_ref( | |
1481 | length_name); | |
1482 | if (!length_name_no_underscore) { | |
1483 | /* | |
1484 | * remove_underscores_from_field_ref() | |
1485 | * logs errors | |
1486 | */ | |
1487 | ret = -EINVAL; | |
1488 | goto error; | |
1489 | } | |
939190b3 | 1490 | seq_decl = ctf_field_class_sequence_create(); |
7b33a0e0 | 1491 | BT_ASSERT(seq_decl); |
939190b3 | 1492 | seq_decl->base.elem_fc = nested_decl; |
7b33a0e0 PP |
1493 | nested_decl = NULL; |
1494 | g_string_assign(seq_decl->length_ref, | |
1495 | length_name_no_underscore); | |
1496 | free(length_name_no_underscore); | |
1497 | decl = (void *) seq_decl; | |
e98a2d6e PP |
1498 | } |
1499 | ||
7b33a0e0 | 1500 | g_free(length_name); |
e98a2d6e PP |
1501 | break; |
1502 | } | |
1503 | default: | |
1504 | ret = -EINVAL; | |
1505 | goto error; | |
1506 | } | |
1507 | ||
8b45963b PP |
1508 | BT_ASSERT(!nested_decl); |
1509 | BT_ASSERT(decl); | |
1510 | BT_ASSERT(!*field_decl); | |
e98a2d6e PP |
1511 | |
1512 | /* | |
1513 | * At this point, we found the next nested declaration. | |
1514 | * We currently own this (and lost the ownership of | |
1515 | * nested_decl in the meantime). Pass this next | |
1516 | * nested declaration as the content of the outer | |
1517 | * container, MOVING its ownership. | |
1518 | */ | |
939190b3 | 1519 | ret = visit_field_class_declarator(ctx, cls_specifier_list, |
e98a2d6e | 1520 | field_name, |
939190b3 PP |
1521 | node_field_class_declarator-> |
1522 | u.field_class_declarator.u.nested.field_class_declarator, | |
e98a2d6e PP |
1523 | &outer_field_decl, decl); |
1524 | decl = NULL; | |
1525 | if (ret) { | |
8b45963b | 1526 | BT_ASSERT(!outer_field_decl); |
e98a2d6e PP |
1527 | ret = -EINVAL; |
1528 | goto error; | |
1529 | } | |
1530 | ||
8b45963b | 1531 | BT_ASSERT(outer_field_decl); |
7b33a0e0 PP |
1532 | *field_decl = outer_field_decl; |
1533 | outer_field_decl = NULL; | |
e98a2d6e PP |
1534 | } |
1535 | ||
8b45963b | 1536 | BT_ASSERT(*field_decl); |
7b33a0e0 | 1537 | goto end; |
e98a2d6e PP |
1538 | |
1539 | error: | |
939190b3 | 1540 | ctf_field_class_destroy(*field_decl); |
7b33a0e0 | 1541 | *field_decl = NULL; |
e98a2d6e | 1542 | |
7b33a0e0 PP |
1543 | if (ret >= 0) { |
1544 | ret = -1; | |
1545 | } | |
1546 | ||
1547 | end: | |
939190b3 | 1548 | ctf_field_class_destroy(nested_decl); |
7b33a0e0 | 1549 | nested_decl = NULL; |
e98a2d6e PP |
1550 | return ret; |
1551 | } | |
1552 | ||
1553 | static | |
1554 | int visit_struct_decl_field(struct ctx *ctx, | |
939190b3 PP |
1555 | struct ctf_field_class_struct *struct_decl, |
1556 | struct ctf_node *cls_specifier_list, | |
1557 | struct bt_list_head *field_class_declarators) | |
e98a2d6e PP |
1558 | { |
1559 | int ret = 0; | |
1560 | struct ctf_node *iter; | |
939190b3 | 1561 | struct ctf_field_class *field_decl = NULL; |
e98a2d6e | 1562 | |
939190b3 | 1563 | bt_list_for_each_entry(iter, field_class_declarators, siblings) { |
e98a2d6e PP |
1564 | field_decl = NULL; |
1565 | GQuark qfield_name; | |
1566 | const char *field_name; | |
e98a2d6e | 1567 | |
939190b3 | 1568 | ret = visit_field_class_declarator(ctx, cls_specifier_list, |
e98a2d6e PP |
1569 | &qfield_name, iter, &field_decl, NULL); |
1570 | if (ret) { | |
8b45963b | 1571 | BT_ASSERT(!field_decl); |
939190b3 PP |
1572 | _BT_LOGE_NODE(cls_specifier_list, |
1573 | "Cannot visit field class declarator: ret=%d", ret); | |
e98a2d6e PP |
1574 | goto error; |
1575 | } | |
1576 | ||
8b45963b | 1577 | BT_ASSERT(field_decl); |
e98a2d6e PP |
1578 | field_name = g_quark_to_string(qfield_name); |
1579 | ||
1580 | /* Check if field with same name already exists */ | |
939190b3 | 1581 | if (ctf_field_class_struct_borrow_member_by_name( |
7b33a0e0 | 1582 | struct_decl, field_name)) { |
939190b3 PP |
1583 | _BT_LOGE_NODE(cls_specifier_list, |
1584 | "Duplicate field in structure field class: " | |
37d21e07 | 1585 | "field-name=\"%s\"", field_name); |
e98a2d6e PP |
1586 | ret = -EINVAL; |
1587 | goto error; | |
1588 | } | |
1589 | ||
1590 | /* Add field to structure */ | |
939190b3 | 1591 | ctf_field_class_struct_append_member(struct_decl, |
7b33a0e0 PP |
1592 | field_name, field_decl); |
1593 | field_decl = NULL; | |
e98a2d6e PP |
1594 | } |
1595 | ||
1596 | return 0; | |
1597 | ||
1598 | error: | |
939190b3 | 1599 | ctf_field_class_destroy(field_decl); |
7b33a0e0 | 1600 | field_decl = NULL; |
e98a2d6e PP |
1601 | return ret; |
1602 | } | |
1603 | ||
1604 | static | |
1605 | int visit_variant_decl_field(struct ctx *ctx, | |
939190b3 PP |
1606 | struct ctf_field_class_variant *variant_decl, |
1607 | struct ctf_node *cls_specifier_list, | |
1608 | struct bt_list_head *field_class_declarators) | |
e98a2d6e PP |
1609 | { |
1610 | int ret = 0; | |
1611 | struct ctf_node *iter; | |
939190b3 | 1612 | struct ctf_field_class *field_decl = NULL; |
e98a2d6e | 1613 | |
939190b3 | 1614 | bt_list_for_each_entry(iter, field_class_declarators, siblings) { |
e98a2d6e PP |
1615 | field_decl = NULL; |
1616 | GQuark qfield_name; | |
1617 | const char *field_name; | |
e98a2d6e | 1618 | |
939190b3 | 1619 | ret = visit_field_class_declarator(ctx, cls_specifier_list, |
e98a2d6e PP |
1620 | &qfield_name, iter, &field_decl, NULL); |
1621 | if (ret) { | |
8b45963b | 1622 | BT_ASSERT(!field_decl); |
939190b3 PP |
1623 | _BT_LOGE_NODE(cls_specifier_list, |
1624 | "Cannot visit field class declarator: ret=%d", ret); | |
e98a2d6e PP |
1625 | goto error; |
1626 | } | |
1627 | ||
8b45963b | 1628 | BT_ASSERT(field_decl); |
e98a2d6e PP |
1629 | field_name = g_quark_to_string(qfield_name); |
1630 | ||
1631 | /* Check if field with same name already exists */ | |
939190b3 | 1632 | if (ctf_field_class_variant_borrow_option_by_name( |
7b33a0e0 | 1633 | variant_decl, field_name)) { |
939190b3 PP |
1634 | _BT_LOGE_NODE(cls_specifier_list, |
1635 | "Duplicate field in variant field class: " | |
37d21e07 | 1636 | "field-name=\"%s\"", field_name); |
e98a2d6e PP |
1637 | ret = -EINVAL; |
1638 | goto error; | |
1639 | } | |
1640 | ||
1641 | /* Add field to structure */ | |
939190b3 | 1642 | ctf_field_class_variant_append_option(variant_decl, |
7b33a0e0 PP |
1643 | field_name, field_decl); |
1644 | field_decl = NULL; | |
e98a2d6e PP |
1645 | } |
1646 | ||
1647 | return 0; | |
1648 | ||
1649 | error: | |
939190b3 | 1650 | ctf_field_class_destroy(field_decl); |
7b33a0e0 | 1651 | field_decl = NULL; |
e98a2d6e PP |
1652 | return ret; |
1653 | } | |
1654 | ||
1655 | static | |
939190b3 PP |
1656 | int visit_field_class_def(struct ctx *ctx, struct ctf_node *cls_specifier_list, |
1657 | struct bt_list_head *field_class_declarators) | |
e98a2d6e PP |
1658 | { |
1659 | int ret = 0; | |
1660 | GQuark qidentifier; | |
1661 | struct ctf_node *iter; | |
939190b3 | 1662 | struct ctf_field_class *class_decl = NULL; |
e98a2d6e | 1663 | |
939190b3 PP |
1664 | bt_list_for_each_entry(iter, field_class_declarators, siblings) { |
1665 | ret = visit_field_class_declarator(ctx, cls_specifier_list, | |
1666 | &qidentifier, iter, &class_decl, NULL); | |
e98a2d6e | 1667 | if (ret) { |
87cb75e6 | 1668 | _BT_LOGE_NODE(iter, |
939190b3 | 1669 | "Cannot visit field class declarator: ret=%d", ret); |
e98a2d6e PP |
1670 | ret = -EINVAL; |
1671 | goto end; | |
1672 | } | |
1673 | ||
939190b3 | 1674 | /* Do not allow field class def and alias of untagged variants */ |
af0c18e3 | 1675 | if (class_decl->type == CTF_FIELD_CLASS_TYPE_VARIANT) { |
939190b3 PP |
1676 | struct ctf_field_class_variant *var_fc = |
1677 | (void *) class_decl; | |
7b33a0e0 | 1678 | |
939190b3 | 1679 | if (var_fc->tag_path.path->len == 0) { |
87cb75e6 | 1680 | _BT_LOGE_NODE(iter, |
939190b3 | 1681 | "Type definition of untagged variant field class is not allowed."); |
e98a2d6e PP |
1682 | ret = -EPERM; |
1683 | goto end; | |
1684 | } | |
1685 | } | |
1686 | ||
1687 | ret = ctx_decl_scope_register_alias(ctx->current_scope, | |
939190b3 | 1688 | g_quark_to_string(qidentifier), class_decl); |
e98a2d6e | 1689 | if (ret) { |
87cb75e6 | 1690 | _BT_LOGE_NODE(iter, |
939190b3 | 1691 | "Cannot register field class alias: name=\"%s\"", |
e98a2d6e PP |
1692 | g_quark_to_string(qidentifier)); |
1693 | goto end; | |
1694 | } | |
1695 | } | |
1696 | ||
1697 | end: | |
939190b3 PP |
1698 | ctf_field_class_destroy(class_decl); |
1699 | class_decl = NULL; | |
e98a2d6e PP |
1700 | return ret; |
1701 | } | |
1702 | ||
1703 | static | |
939190b3 | 1704 | int visit_field_class_alias(struct ctx *ctx, struct ctf_node *target, |
7b33a0e0 | 1705 | struct ctf_node *alias) |
e98a2d6e PP |
1706 | { |
1707 | int ret = 0; | |
1708 | GQuark qalias; | |
1709 | struct ctf_node *node; | |
1710 | GQuark qdummy_field_name; | |
939190b3 | 1711 | struct ctf_field_class *class_decl = NULL; |
e98a2d6e | 1712 | |
939190b3 PP |
1713 | /* Create target field class */ |
1714 | if (bt_list_empty(&target->u.field_class_alias_target.field_class_declarators)) { | |
e98a2d6e PP |
1715 | node = NULL; |
1716 | } else { | |
1717 | node = _BT_LIST_FIRST_ENTRY( | |
939190b3 | 1718 | &target->u.field_class_alias_target.field_class_declarators, |
e98a2d6e PP |
1719 | struct ctf_node, siblings); |
1720 | } | |
1721 | ||
939190b3 PP |
1722 | ret = visit_field_class_declarator(ctx, |
1723 | target->u.field_class_alias_target.field_class_specifier_list, | |
1724 | &qdummy_field_name, node, &class_decl, NULL); | |
e98a2d6e | 1725 | if (ret) { |
939190b3 | 1726 | BT_ASSERT(!class_decl); |
87cb75e6 | 1727 | _BT_LOGE_NODE(node, |
939190b3 | 1728 | "Cannot visit field class declarator: ret=%d", ret); |
e98a2d6e PP |
1729 | goto end; |
1730 | } | |
1731 | ||
939190b3 | 1732 | /* Do not allow field class def and alias of untagged variants */ |
af0c18e3 | 1733 | if (class_decl->type == CTF_FIELD_CLASS_TYPE_VARIANT) { |
939190b3 | 1734 | struct ctf_field_class_variant *var_fc = (void *) class_decl; |
7b33a0e0 | 1735 | |
939190b3 | 1736 | if (var_fc->tag_path.path->len == 0) { |
f8992f93 | 1737 | _BT_LOGE_NODE(target, |
939190b3 | 1738 | "Type definition of untagged variant field class is not allowed."); |
e98a2d6e PP |
1739 | ret = -EPERM; |
1740 | goto end; | |
1741 | } | |
1742 | } | |
1743 | ||
1744 | /* | |
1745 | * The semantic validator does not check whether the target is | |
1746 | * abstract or not (if it has an identifier). Check it here. | |
1747 | */ | |
1748 | if (qdummy_field_name != 0) { | |
f8992f93 | 1749 | _BT_LOGE_NODE(target, |
87cb75e6 | 1750 | "Expecting empty identifier: id=\"%s\"", |
37d21e07 | 1751 | g_quark_to_string(qdummy_field_name)); |
e98a2d6e PP |
1752 | ret = -EINVAL; |
1753 | goto end; | |
1754 | } | |
1755 | ||
1756 | /* Create alias identifier */ | |
939190b3 | 1757 | node = _BT_LIST_FIRST_ENTRY(&alias->u.field_class_alias_name.field_class_declarators, |
e98a2d6e | 1758 | struct ctf_node, siblings); |
939190b3 PP |
1759 | qalias = create_class_alias_identifier(ctx, |
1760 | alias->u.field_class_alias_name.field_class_specifier_list, node); | |
e98a2d6e | 1761 | ret = ctx_decl_scope_register_alias(ctx->current_scope, |
939190b3 | 1762 | g_quark_to_string(qalias), class_decl); |
e98a2d6e | 1763 | if (ret) { |
87cb75e6 | 1764 | _BT_LOGE_NODE(node, |
939190b3 | 1765 | "Cannot register class alias: name=\"%s\"", |
e98a2d6e PP |
1766 | g_quark_to_string(qalias)); |
1767 | goto end; | |
1768 | } | |
1769 | ||
1770 | end: | |
939190b3 PP |
1771 | ctf_field_class_destroy(class_decl); |
1772 | class_decl = NULL; | |
e98a2d6e PP |
1773 | return ret; |
1774 | } | |
1775 | ||
1776 | static | |
1777 | int visit_struct_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, | |
939190b3 | 1778 | struct ctf_field_class_struct *struct_decl) |
e98a2d6e PP |
1779 | { |
1780 | int ret = 0; | |
1781 | ||
1782 | switch (entry_node->type) { | |
1783 | case NODE_TYPEDEF: | |
939190b3 PP |
1784 | ret = visit_field_class_def(ctx, |
1785 | entry_node->u.field_class_def.field_class_specifier_list, | |
1786 | &entry_node->u.field_class_def.field_class_declarators); | |
e98a2d6e | 1787 | if (ret) { |
87cb75e6 | 1788 | _BT_LOGE_NODE(entry_node, |
939190b3 | 1789 | "Cannot add field class found in structure field class: ret=%d", |
37d21e07 | 1790 | ret); |
e98a2d6e PP |
1791 | goto end; |
1792 | } | |
1793 | break; | |
1794 | case NODE_TYPEALIAS: | |
939190b3 PP |
1795 | ret = visit_field_class_alias(ctx, entry_node->u.field_class_alias.target, |
1796 | entry_node->u.field_class_alias.alias); | |
e98a2d6e | 1797 | if (ret) { |
87cb75e6 | 1798 | _BT_LOGE_NODE(entry_node, |
939190b3 | 1799 | "Cannot add field class alias found in structure field class: ret=%d", |
37d21e07 | 1800 | ret); |
e98a2d6e PP |
1801 | goto end; |
1802 | } | |
1803 | break; | |
1804 | case NODE_STRUCT_OR_VARIANT_DECLARATION: | |
1805 | /* Field */ | |
1806 | ret = visit_struct_decl_field(ctx, struct_decl, | |
1807 | entry_node->u.struct_or_variant_declaration. | |
939190b3 | 1808 | field_class_specifier_list, |
e98a2d6e | 1809 | &entry_node->u.struct_or_variant_declaration. |
939190b3 | 1810 | field_class_declarators); |
e98a2d6e PP |
1811 | if (ret) { |
1812 | goto end; | |
1813 | } | |
1814 | break; | |
1815 | default: | |
87cb75e6 PP |
1816 | _BT_LOGE_NODE(entry_node, |
1817 | "Unexpected node type: node-type=%d", entry_node->type); | |
e98a2d6e PP |
1818 | ret = -EINVAL; |
1819 | goto end; | |
1820 | } | |
1821 | ||
1822 | end: | |
1823 | return ret; | |
1824 | } | |
1825 | ||
1826 | static | |
1827 | int visit_variant_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, | |
939190b3 | 1828 | struct ctf_field_class_variant *variant_decl) |
e98a2d6e PP |
1829 | { |
1830 | int ret = 0; | |
1831 | ||
1832 | switch (entry_node->type) { | |
1833 | case NODE_TYPEDEF: | |
939190b3 PP |
1834 | ret = visit_field_class_def(ctx, |
1835 | entry_node->u.field_class_def.field_class_specifier_list, | |
1836 | &entry_node->u.field_class_def.field_class_declarators); | |
e98a2d6e | 1837 | if (ret) { |
87cb75e6 | 1838 | _BT_LOGE_NODE(entry_node, |
939190b3 | 1839 | "Cannot add field class found in variant field class: ret=%d", |
37d21e07 | 1840 | ret); |
e98a2d6e PP |
1841 | goto end; |
1842 | } | |
1843 | break; | |
1844 | case NODE_TYPEALIAS: | |
939190b3 PP |
1845 | ret = visit_field_class_alias(ctx, entry_node->u.field_class_alias.target, |
1846 | entry_node->u.field_class_alias.alias); | |
e98a2d6e | 1847 | if (ret) { |
87cb75e6 | 1848 | _BT_LOGE_NODE(entry_node, |
939190b3 | 1849 | "Cannot add field class alias found in variant field class: ret=%d", |
37d21e07 | 1850 | ret); |
e98a2d6e PP |
1851 | goto end; |
1852 | } | |
1853 | break; | |
1854 | case NODE_STRUCT_OR_VARIANT_DECLARATION: | |
1855 | /* Field */ | |
1856 | ret = visit_variant_decl_field(ctx, variant_decl, | |
1857 | entry_node->u.struct_or_variant_declaration. | |
939190b3 | 1858 | field_class_specifier_list, |
e98a2d6e | 1859 | &entry_node->u.struct_or_variant_declaration. |
939190b3 | 1860 | field_class_declarators); |
e98a2d6e PP |
1861 | if (ret) { |
1862 | goto end; | |
1863 | } | |
1864 | break; | |
1865 | default: | |
87cb75e6 PP |
1866 | _BT_LOGE_NODE(entry_node, |
1867 | "Unexpected node type: node-type=%d", | |
37d21e07 | 1868 | entry_node->type); |
e98a2d6e PP |
1869 | ret = -EINVAL; |
1870 | goto end; | |
1871 | } | |
1872 | ||
1873 | end: | |
1874 | return ret; | |
1875 | } | |
1876 | ||
1877 | static | |
1878 | int visit_struct_decl(struct ctx *ctx, const char *name, | |
7b33a0e0 PP |
1879 | struct bt_list_head *decl_list, int has_body, |
1880 | struct bt_list_head *min_align, | |
939190b3 | 1881 | struct ctf_field_class_struct **struct_decl) |
e98a2d6e PP |
1882 | { |
1883 | int ret = 0; | |
1884 | ||
7b33a0e0 | 1885 | BT_ASSERT(struct_decl); |
e98a2d6e PP |
1886 | *struct_decl = NULL; |
1887 | ||
1888 | /* For named struct (without body), lookup in declaration scope */ | |
1889 | if (!has_body) { | |
e98a2d6e | 1890 | if (!name) { |
939190b3 | 1891 | BT_LOGE_STR("Bodyless structure field class: missing name."); |
e98a2d6e PP |
1892 | ret = -EPERM; |
1893 | goto error; | |
1894 | } | |
1895 | ||
1896 | *struct_decl = ctx_decl_scope_lookup_struct(ctx->current_scope, | |
7b33a0e0 | 1897 | name, -1, true); |
e98a2d6e | 1898 | if (!*struct_decl) { |
939190b3 | 1899 | BT_LOGE("Cannot find structure field class: name=\"struct %s\"", |
37d21e07 | 1900 | name); |
e98a2d6e PP |
1901 | ret = -EINVAL; |
1902 | goto error; | |
1903 | } | |
e98a2d6e PP |
1904 | } else { |
1905 | struct ctf_node *entry_node; | |
1906 | uint64_t min_align_value = 0; | |
1907 | ||
1908 | if (name) { | |
7b33a0e0 PP |
1909 | if (ctx_decl_scope_lookup_struct( |
1910 | ctx->current_scope, name, 1, false)) { | |
939190b3 | 1911 | BT_LOGE("Structure field class already declared in local scope: " |
37d21e07 | 1912 | "name=\"struct %s\"", name); |
e98a2d6e PP |
1913 | ret = -EINVAL; |
1914 | goto error; | |
1915 | } | |
1916 | } | |
1917 | ||
1918 | if (!bt_list_empty(min_align)) { | |
1919 | ret = get_unary_unsigned(min_align, &min_align_value); | |
1920 | if (ret) { | |
939190b3 | 1921 | BT_LOGE("Unexpected unary expression for structure field class's `align` attribute: " |
37d21e07 | 1922 | "ret=%d", ret); |
e98a2d6e PP |
1923 | goto error; |
1924 | } | |
1925 | } | |
1926 | ||
939190b3 | 1927 | *struct_decl = ctf_field_class_struct_create(); |
7b33a0e0 | 1928 | BT_ASSERT(*struct_decl); |
e98a2d6e | 1929 | |
539fd01b | 1930 | if (min_align_value != 0) { |
7b33a0e0 | 1931 | (*struct_decl)->base.alignment = min_align_value; |
539fd01b JG |
1932 | } |
1933 | ||
7b33a0e0 | 1934 | _TRY_PUSH_SCOPE_OR_GOTO_ERROR(); |
e98a2d6e PP |
1935 | |
1936 | bt_list_for_each_entry(entry_node, decl_list, siblings) { | |
1937 | ret = visit_struct_decl_entry(ctx, entry_node, | |
1938 | *struct_decl); | |
1939 | if (ret) { | |
87cb75e6 | 1940 | _BT_LOGE_NODE(entry_node, |
939190b3 | 1941 | "Cannot visit structure field class entry: " |
87cb75e6 | 1942 | "ret=%d", ret); |
e98a2d6e PP |
1943 | ctx_pop_scope(ctx); |
1944 | goto error; | |
1945 | } | |
1946 | } | |
1947 | ||
1948 | ctx_pop_scope(ctx); | |
1949 | ||
1950 | if (name) { | |
1951 | ret = ctx_decl_scope_register_struct(ctx->current_scope, | |
1952 | name, *struct_decl); | |
1953 | if (ret) { | |
939190b3 | 1954 | BT_LOGE("Cannot register structure field class in declaration scope: " |
37d21e07 | 1955 | "name=\"struct %s\", ret=%d", name, ret); |
e98a2d6e PP |
1956 | goto error; |
1957 | } | |
1958 | } | |
1959 | } | |
1960 | ||
1961 | return 0; | |
1962 | ||
1963 | error: | |
939190b3 | 1964 | ctf_field_class_destroy((void *) *struct_decl); |
7b33a0e0 | 1965 | *struct_decl = NULL; |
e98a2d6e PP |
1966 | return ret; |
1967 | } | |
1968 | ||
1969 | static | |
1970 | int visit_variant_decl(struct ctx *ctx, const char *name, | |
1971 | const char *tag, struct bt_list_head *decl_list, | |
939190b3 | 1972 | int has_body, struct ctf_field_class_variant **variant_decl) |
e98a2d6e PP |
1973 | { |
1974 | int ret = 0; | |
939190b3 | 1975 | struct ctf_field_class_variant *untagged_variant_decl = NULL; |
e98a2d6e | 1976 | |
7b33a0e0 | 1977 | BT_ASSERT(variant_decl); |
e98a2d6e PP |
1978 | *variant_decl = NULL; |
1979 | ||
1980 | /* For named variant (without body), lookup in declaration scope */ | |
1981 | if (!has_body) { | |
e98a2d6e | 1982 | if (!name) { |
939190b3 | 1983 | BT_LOGE_STR("Bodyless variant field class: missing name."); |
e98a2d6e PP |
1984 | ret = -EPERM; |
1985 | goto error; | |
1986 | } | |
1987 | ||
1988 | untagged_variant_decl = | |
1989 | ctx_decl_scope_lookup_variant(ctx->current_scope, | |
7b33a0e0 | 1990 | name, -1, true); |
e98a2d6e | 1991 | if (!untagged_variant_decl) { |
939190b3 | 1992 | BT_LOGE("Cannot find variant field class: name=\"variant %s\"", |
37d21e07 | 1993 | name); |
e98a2d6e PP |
1994 | ret = -EINVAL; |
1995 | goto error; | |
1996 | } | |
e98a2d6e PP |
1997 | } else { |
1998 | struct ctf_node *entry_node; | |
1999 | ||
2000 | if (name) { | |
7b33a0e0 PP |
2001 | if (ctx_decl_scope_lookup_variant(ctx->current_scope, |
2002 | name, 1, false)) { | |
939190b3 | 2003 | BT_LOGE("Variant field class already declared in local scope: " |
37d21e07 | 2004 | "name=\"variant %s\"", name); |
e98a2d6e PP |
2005 | ret = -EINVAL; |
2006 | goto error; | |
2007 | } | |
2008 | } | |
2009 | ||
939190b3 | 2010 | untagged_variant_decl = ctf_field_class_variant_create(); |
7b33a0e0 PP |
2011 | BT_ASSERT(untagged_variant_decl); |
2012 | _TRY_PUSH_SCOPE_OR_GOTO_ERROR(); | |
e98a2d6e PP |
2013 | |
2014 | bt_list_for_each_entry(entry_node, decl_list, siblings) { | |
2015 | ret = visit_variant_decl_entry(ctx, entry_node, | |
2016 | untagged_variant_decl); | |
2017 | if (ret) { | |
87cb75e6 | 2018 | _BT_LOGE_NODE(entry_node, |
939190b3 | 2019 | "Cannot visit variant field class entry: " |
87cb75e6 | 2020 | "ret=%d", ret); |
e98a2d6e PP |
2021 | ctx_pop_scope(ctx); |
2022 | goto error; | |
2023 | } | |
2024 | } | |
2025 | ||
2026 | ctx_pop_scope(ctx); | |
2027 | ||
2028 | if (name) { | |
2029 | ret = ctx_decl_scope_register_variant( | |
2030 | ctx->current_scope, name, | |
2031 | untagged_variant_decl); | |
2032 | if (ret) { | |
939190b3 | 2033 | BT_LOGE("Cannot register variant field class in declaration scope: " |
37d21e07 | 2034 | "name=\"variant %s\", ret=%d", name, ret); |
e98a2d6e PP |
2035 | goto error; |
2036 | } | |
2037 | } | |
2038 | } | |
2039 | ||
2040 | /* | |
2041 | * If tagged, create tagged variant and return; otherwise | |
2042 | * return untagged variant. | |
2043 | */ | |
2044 | if (!tag) { | |
7b33a0e0 PP |
2045 | *variant_decl = untagged_variant_decl; |
2046 | untagged_variant_decl = NULL; | |
e98a2d6e PP |
2047 | } else { |
2048 | /* | |
2049 | * At this point, we have a fresh untagged variant; nobody | |
2050 | * else owns it. Set its tag now. | |
2051 | */ | |
a153db48 PP |
2052 | char *tag_no_underscore = |
2053 | remove_underscores_from_field_ref(tag); | |
2054 | ||
2055 | if (!tag_no_underscore) { | |
2056 | /* remove_underscores_from_field_ref() logs errors */ | |
2057 | goto error; | |
2058 | } | |
2059 | ||
7b33a0e0 PP |
2060 | g_string_assign(untagged_variant_decl->tag_ref, |
2061 | tag_no_underscore); | |
a153db48 | 2062 | free(tag_no_underscore); |
7b33a0e0 PP |
2063 | *variant_decl = untagged_variant_decl; |
2064 | untagged_variant_decl = NULL; | |
e98a2d6e PP |
2065 | } |
2066 | ||
8b45963b PP |
2067 | BT_ASSERT(!untagged_variant_decl); |
2068 | BT_ASSERT(*variant_decl); | |
e98a2d6e PP |
2069 | return 0; |
2070 | ||
2071 | error: | |
939190b3 | 2072 | ctf_field_class_destroy((void *) untagged_variant_decl); |
7b33a0e0 | 2073 | untagged_variant_decl = NULL; |
939190b3 | 2074 | ctf_field_class_destroy((void *) *variant_decl); |
7b33a0e0 | 2075 | *variant_decl = NULL; |
e98a2d6e PP |
2076 | return ret; |
2077 | } | |
2078 | ||
7b33a0e0 PP |
2079 | struct uori { |
2080 | bool is_signed; | |
2081 | union { | |
2082 | uint64_t u; | |
2083 | uint64_t i; | |
2084 | } value; | |
2085 | }; | |
2086 | ||
e98a2d6e PP |
2087 | static |
2088 | int visit_enum_decl_entry(struct ctx *ctx, struct ctf_node *enumerator, | |
939190b3 | 2089 | struct ctf_field_class_enum *enum_decl, struct uori *last) |
e98a2d6e PP |
2090 | { |
2091 | int ret = 0; | |
2092 | int nr_vals = 0; | |
2093 | struct ctf_node *iter; | |
7b33a0e0 PP |
2094 | struct uori start = { |
2095 | .is_signed = false, | |
2096 | .value.u = 0, | |
2097 | }; | |
2098 | struct uori end = { | |
2099 | .is_signed = false, | |
2100 | .value.u = 0, | |
2101 | }; | |
e98a2d6e | 2102 | const char *label = enumerator->u.enumerator.id; |
792defbe | 2103 | const char *effective_label = label; |
e98a2d6e PP |
2104 | struct bt_list_head *values = &enumerator->u.enumerator.values; |
2105 | ||
2106 | bt_list_for_each_entry(iter, values, siblings) { | |
7b33a0e0 | 2107 | struct uori *target; |
e98a2d6e PP |
2108 | |
2109 | if (iter->type != NODE_UNARY_EXPRESSION) { | |
87cb75e6 | 2110 | _BT_LOGE_NODE(iter, |
939190b3 | 2111 | "Wrong expression for enumeration field class label: " |
37d21e07 | 2112 | "node-type=%d, label=\"%s\"", iter->type, |
e98a2d6e PP |
2113 | label); |
2114 | ret = -EINVAL; | |
2115 | goto error; | |
2116 | } | |
2117 | ||
2118 | if (nr_vals == 0) { | |
2119 | target = &start; | |
2120 | } else { | |
2121 | target = &end; | |
2122 | } | |
2123 | ||
2124 | switch (iter->u.unary_expression.type) { | |
2125 | case UNARY_SIGNED_CONSTANT: | |
7b33a0e0 PP |
2126 | target->is_signed = true; |
2127 | target->value.i = | |
2128 | iter->u.unary_expression.u.signed_constant; | |
e98a2d6e PP |
2129 | break; |
2130 | case UNARY_UNSIGNED_CONSTANT: | |
7b33a0e0 PP |
2131 | target->is_signed = false; |
2132 | target->value.u = | |
e98a2d6e PP |
2133 | iter->u.unary_expression.u.unsigned_constant; |
2134 | break; | |
2135 | default: | |
87cb75e6 | 2136 | _BT_LOGE_NODE(iter, |
939190b3 | 2137 | "Invalid enumeration field class entry: " |
37d21e07 PP |
2138 | "expecting constant signed or unsigned integer: " |
2139 | "node-type=%d, label=\"%s\"", | |
2140 | iter->u.unary_expression.type, label); | |
e98a2d6e PP |
2141 | ret = -EINVAL; |
2142 | goto error; | |
2143 | } | |
2144 | ||
2145 | if (nr_vals > 1) { | |
87cb75e6 | 2146 | _BT_LOGE_NODE(iter, |
939190b3 | 2147 | "Invalid enumeration field class entry: label=\"%s\"", |
e98a2d6e PP |
2148 | label); |
2149 | ret = -EINVAL; | |
2150 | goto error; | |
2151 | } | |
2152 | ||
2153 | nr_vals++; | |
2154 | } | |
2155 | ||
2156 | if (nr_vals == 0) { | |
2157 | start = *last; | |
2158 | } | |
2159 | ||
2160 | if (nr_vals <= 1) { | |
2161 | end = start; | |
2162 | } | |
2163 | ||
7b33a0e0 PP |
2164 | if (end.is_signed) { |
2165 | last->value.i = end.value.i + 1; | |
2166 | } else { | |
2167 | last->value.u = end.value.u + 1; | |
2168 | } | |
e98a2d6e | 2169 | |
792defbe PP |
2170 | if (label[0] == '_') { |
2171 | /* | |
2172 | * Strip the first underscore of any enumeration field | |
939190b3 PP |
2173 | * class's label in case this enumeration FC is used as |
2174 | * a variant FC tag later. The variant FC choice names | |
792defbe PP |
2175 | * could also start with `_`, in which case the prefix |
2176 | * is removed, and it the resulting choice name needs to | |
2177 | * match tag labels. | |
2178 | */ | |
2179 | effective_label = &label[1]; | |
2180 | } | |
2181 | ||
939190b3 | 2182 | ctf_field_class_enum_append_mapping(enum_decl, effective_label, |
7b33a0e0 | 2183 | start.value.u, end.value.u); |
e98a2d6e PP |
2184 | return 0; |
2185 | ||
2186 | error: | |
2187 | return ret; | |
2188 | } | |
2189 | ||
2190 | static | |
2191 | int visit_enum_decl(struct ctx *ctx, const char *name, | |
939190b3 | 2192 | struct ctf_node *container_cls, |
7b33a0e0 | 2193 | struct bt_list_head *enumerator_list, |
939190b3 | 2194 | int has_body, struct ctf_field_class_enum **enum_decl) |
e98a2d6e PP |
2195 | { |
2196 | int ret = 0; | |
2197 | GQuark qdummy_id; | |
939190b3 | 2198 | struct ctf_field_class_int *integer_decl = NULL; |
e98a2d6e | 2199 | |
7b33a0e0 | 2200 | BT_ASSERT(enum_decl); |
e98a2d6e PP |
2201 | *enum_decl = NULL; |
2202 | ||
2203 | /* For named enum (without body), lookup in declaration scope */ | |
2204 | if (!has_body) { | |
e98a2d6e | 2205 | if (!name) { |
939190b3 | 2206 | BT_LOGE_STR("Bodyless enumeration field class: missing name."); |
e98a2d6e PP |
2207 | ret = -EPERM; |
2208 | goto error; | |
2209 | } | |
2210 | ||
2211 | *enum_decl = ctx_decl_scope_lookup_enum(ctx->current_scope, | |
7b33a0e0 | 2212 | name, -1, true); |
e98a2d6e | 2213 | if (!*enum_decl) { |
939190b3 | 2214 | BT_LOGE("Cannot find enumeration field class: " |
37d21e07 | 2215 | "name=\"enum %s\"", name); |
e98a2d6e PP |
2216 | ret = -EINVAL; |
2217 | goto error; | |
2218 | } | |
e98a2d6e PP |
2219 | } else { |
2220 | struct ctf_node *iter; | |
7b33a0e0 PP |
2221 | struct uori last_value = { |
2222 | .is_signed = false, | |
2223 | .value.u = 0, | |
2224 | }; | |
e98a2d6e PP |
2225 | |
2226 | if (name) { | |
7b33a0e0 PP |
2227 | if (ctx_decl_scope_lookup_enum(ctx->current_scope, |
2228 | name, 1, false)) { | |
939190b3 | 2229 | BT_LOGE("Enumeration field class already declared in local scope: " |
37d21e07 | 2230 | "name=\"enum %s\"", name); |
e98a2d6e PP |
2231 | ret = -EINVAL; |
2232 | goto error; | |
2233 | } | |
2234 | } | |
2235 | ||
939190b3 | 2236 | if (!container_cls) { |
7b33a0e0 PP |
2237 | integer_decl = (void *) ctx_decl_scope_lookup_alias( |
2238 | ctx->current_scope, "int", -1, true); | |
e98a2d6e | 2239 | if (!integer_decl) { |
939190b3 | 2240 | BT_LOGE_STR("Cannot find implicit `int` field class alias for enumeration field class."); |
e98a2d6e PP |
2241 | ret = -EINVAL; |
2242 | goto error; | |
2243 | } | |
2244 | } else { | |
939190b3 | 2245 | ret = visit_field_class_declarator(ctx, container_cls, |
7b33a0e0 PP |
2246 | &qdummy_id, NULL, (void *) &integer_decl, |
2247 | NULL); | |
e98a2d6e | 2248 | if (ret) { |
8b45963b | 2249 | BT_ASSERT(!integer_decl); |
e98a2d6e PP |
2250 | ret = -EINVAL; |
2251 | goto error; | |
2252 | } | |
2253 | } | |
2254 | ||
8b45963b | 2255 | BT_ASSERT(integer_decl); |
e98a2d6e | 2256 | |
af0c18e3 | 2257 | if (integer_decl->base.base.type != CTF_FIELD_CLASS_TYPE_INT) { |
939190b3 | 2258 | BT_LOGE("Container field class for enumeration field class is not an integer field class: " |
af0c18e3 | 2259 | "fc-type=%d", integer_decl->base.base.type); |
e98a2d6e PP |
2260 | ret = -EINVAL; |
2261 | goto error; | |
2262 | } | |
2263 | ||
939190b3 | 2264 | *enum_decl = ctf_field_class_enum_create(); |
7b33a0e0 PP |
2265 | BT_ASSERT(*enum_decl); |
2266 | (*enum_decl)->base.base.base.alignment = | |
2267 | integer_decl->base.base.alignment; | |
939190b3 | 2268 | ctf_field_class_int_copy_content((void *) *enum_decl, |
7b33a0e0 PP |
2269 | (void *) integer_decl); |
2270 | last_value.is_signed = (*enum_decl)->base.is_signed; | |
e98a2d6e PP |
2271 | |
2272 | bt_list_for_each_entry(iter, enumerator_list, siblings) { | |
2273 | ret = visit_enum_decl_entry(ctx, iter, *enum_decl, | |
7b33a0e0 | 2274 | &last_value); |
e98a2d6e | 2275 | if (ret) { |
87cb75e6 | 2276 | _BT_LOGE_NODE(iter, |
939190b3 | 2277 | "Cannot visit enumeration field class entry: " |
87cb75e6 | 2278 | "ret=%d", ret); |
e98a2d6e PP |
2279 | goto error; |
2280 | } | |
2281 | } | |
2282 | ||
2283 | if (name) { | |
2284 | ret = ctx_decl_scope_register_enum(ctx->current_scope, | |
2285 | name, *enum_decl); | |
2286 | if (ret) { | |
939190b3 | 2287 | BT_LOGE("Cannot register enumeration field class in declaration scope: " |
87cb75e6 | 2288 | "ret=%d", ret); |
e98a2d6e PP |
2289 | goto error; |
2290 | } | |
2291 | } | |
2292 | } | |
2293 | ||
7b33a0e0 | 2294 | goto end; |
e98a2d6e PP |
2295 | |
2296 | error: | |
939190b3 | 2297 | ctf_field_class_destroy((void *) *enum_decl); |
7b33a0e0 | 2298 | *enum_decl = NULL; |
e98a2d6e | 2299 | |
7b33a0e0 | 2300 | end: |
939190b3 | 2301 | ctf_field_class_destroy((void *) integer_decl); |
7b33a0e0 | 2302 | integer_decl = NULL; |
e98a2d6e PP |
2303 | return ret; |
2304 | } | |
2305 | ||
2306 | static | |
939190b3 PP |
2307 | int visit_field_class_specifier(struct ctx *ctx, |
2308 | struct ctf_node *cls_specifier_list, | |
2309 | struct ctf_field_class **decl) | |
e98a2d6e PP |
2310 | { |
2311 | int ret = 0; | |
2312 | GString *str = NULL; | |
e98a2d6e PP |
2313 | |
2314 | *decl = NULL; | |
2315 | str = g_string_new(""); | |
939190b3 | 2316 | ret = get_class_specifier_list_name(ctx, cls_specifier_list, str); |
e98a2d6e | 2317 | if (ret) { |
939190b3 PP |
2318 | _BT_LOGE_NODE(cls_specifier_list, |
2319 | "Cannot get field class specifier list's name: ret=%d", ret); | |
e98a2d6e PP |
2320 | goto error; |
2321 | } | |
2322 | ||
7b33a0e0 PP |
2323 | *decl = ctx_decl_scope_lookup_alias(ctx->current_scope, str->str, -1, |
2324 | true); | |
e98a2d6e | 2325 | if (!*decl) { |
939190b3 PP |
2326 | _BT_LOGE_NODE(cls_specifier_list, |
2327 | "Cannot find field class alias: name=\"%s\"", str->str); | |
e98a2d6e PP |
2328 | ret = -EINVAL; |
2329 | goto error; | |
2330 | } | |
2331 | ||
7b33a0e0 | 2332 | goto end; |
e98a2d6e PP |
2333 | |
2334 | error: | |
939190b3 | 2335 | ctf_field_class_destroy(*decl); |
7b33a0e0 PP |
2336 | *decl = NULL; |
2337 | ||
2338 | end: | |
e98a2d6e | 2339 | if (str) { |
7b33a0e0 | 2340 | g_string_free(str, TRUE); |
e98a2d6e PP |
2341 | } |
2342 | ||
e98a2d6e PP |
2343 | return ret; |
2344 | } | |
2345 | ||
2346 | static | |
2347 | int visit_integer_decl(struct ctx *ctx, | |
7b33a0e0 | 2348 | struct bt_list_head *expressions, |
939190b3 | 2349 | struct ctf_field_class_int **integer_decl) |
e98a2d6e PP |
2350 | { |
2351 | int set = 0; | |
2352 | int ret = 0; | |
7b33a0e0 | 2353 | int signedness = 0; |
e98a2d6e PP |
2354 | struct ctf_node *expression; |
2355 | uint64_t alignment = 0, size = 0; | |
35fa110e | 2356 | struct ctf_clock_class *mapped_clock_class = NULL; |
7b33a0e0 | 2357 | enum ctf_encoding encoding = CTF_ENCODING_NONE; |
ee78f405 | 2358 | bt_field_class_integer_preferred_display_base base = |
939190b3 | 2359 | BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL; |
7b33a0e0 | 2360 | enum ctf_byte_order byte_order = ctx->ctf_tc->default_byte_order; |
e98a2d6e PP |
2361 | |
2362 | *integer_decl = NULL; | |
2363 | ||
2364 | bt_list_for_each_entry(expression, expressions, siblings) { | |
2365 | struct ctf_node *left, *right; | |
2366 | ||
2367 | left = _BT_LIST_FIRST_ENTRY(&expression->u.ctf_expression.left, | |
2368 | struct ctf_node, siblings); | |
2369 | right = _BT_LIST_FIRST_ENTRY( | |
2370 | &expression->u.ctf_expression.right, struct ctf_node, | |
2371 | siblings); | |
2372 | ||
2373 | if (left->u.unary_expression.type != UNARY_STRING) { | |
87cb75e6 PP |
2374 | _BT_LOGE_NODE(left, |
2375 | "Unexpected unary expression type: type=%d", | |
2376 | left->u.unary_expression.type); | |
e98a2d6e PP |
2377 | ret = -EINVAL; |
2378 | goto error; | |
2379 | } | |
2380 | ||
2381 | if (!strcmp(left->u.unary_expression.u.string, "signed")) { | |
2382 | if (_IS_SET(&set, _INTEGER_SIGNED_SET)) { | |
87cb75e6 | 2383 | _BT_LOGE_DUP_ATTR(left, "signed", |
939190b3 | 2384 | "integer field class"); |
e98a2d6e PP |
2385 | ret = -EPERM; |
2386 | goto error; | |
2387 | } | |
2388 | ||
55314f2a | 2389 | signedness = get_boolean(right); |
e98a2d6e | 2390 | if (signedness < 0) { |
87cb75e6 | 2391 | _BT_LOGE_NODE(right, |
939190b3 | 2392 | "Invalid boolean value for integer field class's `signed` attribute: " |
87cb75e6 | 2393 | "ret=%d", ret); |
e98a2d6e PP |
2394 | ret = -EINVAL; |
2395 | goto error; | |
2396 | } | |
2397 | ||
2398 | _SET(&set, _INTEGER_SIGNED_SET); | |
2399 | } else if (!strcmp(left->u.unary_expression.u.string, | |
2400 | "byte_order")) { | |
2401 | if (_IS_SET(&set, _INTEGER_BYTE_ORDER_SET)) { | |
87cb75e6 | 2402 | _BT_LOGE_DUP_ATTR(left, "byte_order", |
939190b3 | 2403 | "integer field class"); |
e98a2d6e PP |
2404 | ret = -EPERM; |
2405 | goto error; | |
2406 | } | |
2407 | ||
2408 | byte_order = get_real_byte_order(ctx, right); | |
7b33a0e0 | 2409 | if (byte_order == -1) { |
87cb75e6 | 2410 | _BT_LOGE_NODE(right, |
939190b3 | 2411 | "Invalid `byte_order` attribute in integer field class: " |
37d21e07 | 2412 | "ret=%d", ret); |
e98a2d6e PP |
2413 | ret = -EINVAL; |
2414 | goto error; | |
2415 | } | |
2416 | ||
2417 | _SET(&set, _INTEGER_BYTE_ORDER_SET); | |
2418 | } else if (!strcmp(left->u.unary_expression.u.string, "size")) { | |
2419 | if (_IS_SET(&set, _INTEGER_SIZE_SET)) { | |
87cb75e6 | 2420 | _BT_LOGE_DUP_ATTR(left, "size", |
939190b3 | 2421 | "integer field class"); |
e98a2d6e PP |
2422 | ret = -EPERM; |
2423 | goto error; | |
2424 | } | |
2425 | ||
2426 | if (right->u.unary_expression.type != | |
2427 | UNARY_UNSIGNED_CONSTANT) { | |
87cb75e6 | 2428 | _BT_LOGE_NODE(right, |
939190b3 | 2429 | "Invalid `size` attribute in integer field class: " |
37d21e07 PP |
2430 | "expecting unsigned constant integer: " |
2431 | "node-type=%d", | |
2432 | right->u.unary_expression.type); | |
e98a2d6e PP |
2433 | ret = -EINVAL; |
2434 | goto error; | |
2435 | } | |
2436 | ||
2437 | size = right->u.unary_expression.u.unsigned_constant; | |
2438 | if (size == 0) { | |
87cb75e6 | 2439 | _BT_LOGE_NODE(right, |
939190b3 | 2440 | "Invalid `size` attribute in integer field class: " |
37d21e07 PP |
2441 | "expecting positive constant integer: " |
2442 | "size=%" PRIu64, size); | |
e98a2d6e PP |
2443 | ret = -EINVAL; |
2444 | goto error; | |
2445 | } else if (size > 64) { | |
87cb75e6 | 2446 | _BT_LOGE_NODE(right, |
939190b3 | 2447 | "Invalid `size` attribute in integer field class: " |
37d21e07 PP |
2448 | "integer fields over 64 bits are not supported as of this version: " |
2449 | "size=%" PRIu64, size); | |
e98a2d6e PP |
2450 | ret = -EINVAL; |
2451 | goto error; | |
2452 | } | |
2453 | ||
2454 | _SET(&set, _INTEGER_SIZE_SET); | |
2455 | } else if (!strcmp(left->u.unary_expression.u.string, | |
2456 | "align")) { | |
2457 | if (_IS_SET(&set, _INTEGER_ALIGN_SET)) { | |
87cb75e6 | 2458 | _BT_LOGE_DUP_ATTR(left, "align", |
939190b3 | 2459 | "integer field class"); |
e98a2d6e PP |
2460 | ret = -EPERM; |
2461 | goto error; | |
2462 | } | |
2463 | ||
2464 | if (right->u.unary_expression.type != | |
2465 | UNARY_UNSIGNED_CONSTANT) { | |
87cb75e6 | 2466 | _BT_LOGE_NODE(right, |
939190b3 | 2467 | "Invalid `align` attribute in integer field class: " |
37d21e07 PP |
2468 | "expecting unsigned constant integer: " |
2469 | "node-type=%d", | |
2470 | right->u.unary_expression.type); | |
e98a2d6e PP |
2471 | ret = -EINVAL; |
2472 | goto error; | |
2473 | } | |
2474 | ||
2475 | alignment = | |
2476 | right->u.unary_expression.u.unsigned_constant; | |
2477 | if (!is_align_valid(alignment)) { | |
87cb75e6 | 2478 | _BT_LOGE_NODE(right, |
939190b3 | 2479 | "Invalid `align` attribute in integer field class: " |
37d21e07 PP |
2480 | "expecting power of two: " |
2481 | "align=%" PRIu64, alignment); | |
e98a2d6e PP |
2482 | ret = -EINVAL; |
2483 | goto error; | |
2484 | } | |
2485 | ||
2486 | _SET(&set, _INTEGER_ALIGN_SET); | |
2487 | } else if (!strcmp(left->u.unary_expression.u.string, "base")) { | |
2488 | if (_IS_SET(&set, _INTEGER_BASE_SET)) { | |
87cb75e6 | 2489 | _BT_LOGE_DUP_ATTR(left, "base", |
939190b3 | 2490 | "integer field class"); |
e98a2d6e PP |
2491 | ret = -EPERM; |
2492 | goto error; | |
2493 | } | |
2494 | ||
2495 | switch (right->u.unary_expression.type) { | |
2496 | case UNARY_UNSIGNED_CONSTANT: | |
2497 | { | |
2498 | uint64_t constant = right->u.unary_expression. | |
2499 | u.unsigned_constant; | |
2500 | ||
2501 | switch (constant) { | |
2502 | case 2: | |
939190b3 | 2503 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY; |
e98a2d6e PP |
2504 | break; |
2505 | case 8: | |
939190b3 | 2506 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL; |
e98a2d6e PP |
2507 | break; |
2508 | case 10: | |
939190b3 | 2509 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL; |
e98a2d6e PP |
2510 | break; |
2511 | case 16: | |
939190b3 | 2512 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL; |
e98a2d6e PP |
2513 | break; |
2514 | default: | |
87cb75e6 | 2515 | _BT_LOGE_NODE(right, |
939190b3 | 2516 | "Invalid `base` attribute in integer field class: " |
37d21e07 | 2517 | "base=%" PRIu64, |
e98a2d6e PP |
2518 | right->u.unary_expression.u.unsigned_constant); |
2519 | ret = -EINVAL; | |
2520 | goto error; | |
2521 | } | |
2522 | break; | |
2523 | } | |
2524 | case UNARY_STRING: | |
2525 | { | |
2526 | char *s_right = concatenate_unary_strings( | |
2527 | &expression->u.ctf_expression.right); | |
2528 | if (!s_right) { | |
87cb75e6 | 2529 | _BT_LOGE_NODE(right, |
939190b3 | 2530 | "Unexpected unary expression for integer field class's `base` attribute."); |
e98a2d6e PP |
2531 | ret = -EINVAL; |
2532 | goto error; | |
2533 | } | |
2534 | ||
2535 | if (!strcmp(s_right, "decimal") || | |
2536 | !strcmp(s_right, "dec") || | |
2537 | !strcmp(s_right, "d") || | |
2538 | !strcmp(s_right, "i") || | |
2539 | !strcmp(s_right, "u")) { | |
939190b3 | 2540 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL; |
e98a2d6e PP |
2541 | } else if (!strcmp(s_right, "hexadecimal") || |
2542 | !strcmp(s_right, "hex") || | |
2543 | !strcmp(s_right, "x") || | |
2544 | !strcmp(s_right, "X") || | |
2545 | !strcmp(s_right, "p")) { | |
939190b3 | 2546 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL; |
e98a2d6e PP |
2547 | } else if (!strcmp(s_right, "octal") || |
2548 | !strcmp(s_right, "oct") || | |
2549 | !strcmp(s_right, "o")) { | |
939190b3 | 2550 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL; |
e98a2d6e PP |
2551 | } else if (!strcmp(s_right, "binary") || |
2552 | !strcmp(s_right, "b")) { | |
939190b3 | 2553 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY; |
e98a2d6e | 2554 | } else { |
87cb75e6 | 2555 | _BT_LOGE_NODE(right, |
939190b3 | 2556 | "Unexpected unary expression for integer field class's `base` attribute: " |
37d21e07 | 2557 | "base=\"%s\"", s_right); |
e98a2d6e PP |
2558 | g_free(s_right); |
2559 | ret = -EINVAL; | |
2560 | goto error; | |
2561 | } | |
2562 | ||
2563 | g_free(s_right); | |
2564 | break; | |
2565 | } | |
2566 | default: | |
87cb75e6 | 2567 | _BT_LOGE_NODE(right, |
939190b3 | 2568 | "Invalid `base` attribute in integer field class: " |
37d21e07 | 2569 | "expecting unsigned constant integer or unary string."); |
e98a2d6e PP |
2570 | ret = -EINVAL; |
2571 | goto error; | |
2572 | } | |
2573 | ||
2574 | _SET(&set, _INTEGER_BASE_SET); | |
2575 | } else if (!strcmp(left->u.unary_expression.u.string, | |
2576 | "encoding")) { | |
2577 | char *s_right; | |
2578 | ||
2579 | if (_IS_SET(&set, _INTEGER_ENCODING_SET)) { | |
87cb75e6 | 2580 | _BT_LOGE_DUP_ATTR(left, "encoding", |
939190b3 | 2581 | "integer field class"); |
e98a2d6e PP |
2582 | ret = -EPERM; |
2583 | goto error; | |
2584 | } | |
2585 | ||
2586 | if (right->u.unary_expression.type != UNARY_STRING) { | |
87cb75e6 | 2587 | _BT_LOGE_NODE(right, |
939190b3 | 2588 | "Invalid `encoding` attribute in integer field class: " |
37d21e07 | 2589 | "expecting unary string."); |
e98a2d6e PP |
2590 | ret = -EINVAL; |
2591 | goto error; | |
2592 | } | |
2593 | ||
2594 | s_right = concatenate_unary_strings( | |
2595 | &expression->u.ctf_expression.right); | |
2596 | if (!s_right) { | |
87cb75e6 | 2597 | _BT_LOGE_NODE(right, |
939190b3 | 2598 | "Unexpected unary expression for integer field class's `encoding` attribute."); |
e98a2d6e PP |
2599 | ret = -EINVAL; |
2600 | goto error; | |
2601 | } | |
2602 | ||
2603 | if (!strcmp(s_right, "UTF8") || | |
2604 | !strcmp(s_right, "utf8") || | |
2605 | !strcmp(s_right, "utf-8") || | |
7b33a0e0 PP |
2606 | !strcmp(s_right, "UTF-8") || |
2607 | !strcmp(s_right, "ASCII") || | |
e98a2d6e | 2608 | !strcmp(s_right, "ascii")) { |
7b33a0e0 | 2609 | encoding = CTF_ENCODING_UTF8; |
e98a2d6e | 2610 | } else if (!strcmp(s_right, "none")) { |
7b33a0e0 | 2611 | encoding = CTF_ENCODING_NONE; |
e98a2d6e | 2612 | } else { |
87cb75e6 | 2613 | _BT_LOGE_NODE(right, |
939190b3 | 2614 | "Invalid `encoding` attribute in integer field class: " |
37d21e07 | 2615 | "unknown encoding: encoding=\"%s\"", |
e98a2d6e PP |
2616 | s_right); |
2617 | g_free(s_right); | |
2618 | ret = -EINVAL; | |
2619 | goto error; | |
2620 | } | |
2621 | ||
2622 | g_free(s_right); | |
2623 | _SET(&set, _INTEGER_ENCODING_SET); | |
2624 | } else if (!strcmp(left->u.unary_expression.u.string, "map")) { | |
2625 | const char *clock_name; | |
2626 | ||
2627 | if (_IS_SET(&set, _INTEGER_MAP_SET)) { | |
87cb75e6 | 2628 | _BT_LOGE_DUP_ATTR(left, "map", |
939190b3 | 2629 | "integer field class"); |
e98a2d6e PP |
2630 | ret = -EPERM; |
2631 | goto error; | |
2632 | } | |
2633 | ||
2634 | if (right->u.unary_expression.type != UNARY_STRING) { | |
87cb75e6 | 2635 | _BT_LOGE_NODE(right, |
939190b3 | 2636 | "Invalid `map` attribute in integer field class: " |
37d21e07 | 2637 | "expecting unary string."); |
e98a2d6e PP |
2638 | ret = -EINVAL; |
2639 | goto error; | |
2640 | } | |
2641 | ||
2642 | clock_name = | |
2643 | get_map_clock_name_value( | |
2644 | &expression->u.ctf_expression.right); | |
2645 | if (!clock_name) { | |
2646 | char *s_right = concatenate_unary_strings( | |
2647 | &expression->u.ctf_expression.right); | |
2648 | ||
2649 | if (!s_right) { | |
87cb75e6 | 2650 | _BT_LOGE_NODE(right, |
939190b3 | 2651 | "Unexpected unary expression for integer field class's `map` attribute."); |
e98a2d6e PP |
2652 | ret = -EINVAL; |
2653 | goto error; | |
2654 | } | |
2655 | ||
87cb75e6 | 2656 | _BT_LOGE_NODE(right, |
939190b3 | 2657 | "Invalid `map` attribute in integer field class: " |
37d21e07 | 2658 | "cannot find clock class at this point: name=\"%s\"", |
e98a2d6e PP |
2659 | s_right); |
2660 | _SET(&set, _INTEGER_MAP_SET); | |
2661 | g_free(s_right); | |
2662 | continue; | |
2663 | } | |
2664 | ||
7b33a0e0 PP |
2665 | mapped_clock_class = |
2666 | ctf_trace_class_borrow_clock_class_by_name( | |
2667 | ctx->ctf_tc, clock_name); | |
2668 | if (!mapped_clock_class) { | |
87cb75e6 | 2669 | _BT_LOGE_NODE(right, |
939190b3 | 2670 | "Invalid `map` attribute in integer field class: " |
37d21e07 | 2671 | "cannot find clock class at this point: name=\"%s\"", |
e98a2d6e PP |
2672 | clock_name); |
2673 | ret = -EINVAL; | |
2674 | goto error; | |
2675 | } | |
2676 | ||
2677 | _SET(&set, _INTEGER_MAP_SET); | |
2678 | } else { | |
87cb75e6 | 2679 | _BT_LOGW_NODE(left, |
939190b3 | 2680 | "Unknown attribute in integer field class: " |
37d21e07 | 2681 | "attr-name=\"%s\"", |
e98a2d6e PP |
2682 | left->u.unary_expression.u.string); |
2683 | } | |
2684 | } | |
2685 | ||
2686 | if (!_IS_SET(&set, _INTEGER_SIZE_SET)) { | |
939190b3 | 2687 | BT_LOGE_STR("Missing `size` attribute in integer field class."); |
e98a2d6e PP |
2688 | ret = -EPERM; |
2689 | goto error; | |
2690 | } | |
2691 | ||
2692 | if (!_IS_SET(&set, _INTEGER_ALIGN_SET)) { | |
2693 | if (size % CHAR_BIT) { | |
2694 | /* Bit-packed alignment */ | |
2695 | alignment = 1; | |
2696 | } else { | |
2697 | /* Byte-packed alignment */ | |
2698 | alignment = CHAR_BIT; | |
2699 | } | |
2700 | } | |
2701 | ||
939190b3 | 2702 | *integer_decl = ctf_field_class_int_create(); |
7b33a0e0 PP |
2703 | BT_ASSERT(*integer_decl); |
2704 | (*integer_decl)->base.base.alignment = alignment; | |
2705 | (*integer_decl)->base.byte_order = byte_order; | |
2706 | (*integer_decl)->base.size = size; | |
2707 | (*integer_decl)->is_signed = (signedness > 0); | |
2708 | (*integer_decl)->disp_base = base; | |
2709 | (*integer_decl)->encoding = encoding; | |
4b70020d | 2710 | (*integer_decl)->mapped_clock_class = mapped_clock_class; |
e98a2d6e PP |
2711 | return 0; |
2712 | ||
2713 | error: | |
939190b3 | 2714 | ctf_field_class_destroy((void *) *integer_decl); |
7b33a0e0 | 2715 | *integer_decl = NULL; |
e98a2d6e PP |
2716 | return ret; |
2717 | } | |
2718 | ||
2719 | static | |
2720 | int visit_floating_point_number_decl(struct ctx *ctx, | |
7b33a0e0 | 2721 | struct bt_list_head *expressions, |
939190b3 | 2722 | struct ctf_field_class_float **float_decl) |
e98a2d6e PP |
2723 | { |
2724 | int set = 0; | |
2725 | int ret = 0; | |
2726 | struct ctf_node *expression; | |
2727 | uint64_t alignment = 1, exp_dig = 0, mant_dig = 0; | |
7b33a0e0 | 2728 | enum ctf_byte_order byte_order = ctx->ctf_tc->default_byte_order; |
e98a2d6e PP |
2729 | |
2730 | *float_decl = NULL; | |
2731 | ||
2732 | bt_list_for_each_entry(expression, expressions, siblings) { | |
2733 | struct ctf_node *left, *right; | |
2734 | ||
2735 | left = _BT_LIST_FIRST_ENTRY(&expression->u.ctf_expression.left, | |
2736 | struct ctf_node, siblings); | |
2737 | right = _BT_LIST_FIRST_ENTRY( | |
2738 | &expression->u.ctf_expression.right, struct ctf_node, | |
2739 | siblings); | |
2740 | ||
2741 | if (left->u.unary_expression.type != UNARY_STRING) { | |
87cb75e6 PP |
2742 | _BT_LOGE_NODE(left, |
2743 | "Unexpected unary expression type: type=%d", | |
2744 | left->u.unary_expression.type); | |
e98a2d6e PP |
2745 | ret = -EINVAL; |
2746 | goto error; | |
2747 | } | |
2748 | ||
2749 | if (!strcmp(left->u.unary_expression.u.string, "byte_order")) { | |
2750 | if (_IS_SET(&set, _FLOAT_BYTE_ORDER_SET)) { | |
87cb75e6 | 2751 | _BT_LOGE_DUP_ATTR(left, "byte_order", |
939190b3 | 2752 | "floating point number field class"); |
e98a2d6e PP |
2753 | ret = -EPERM; |
2754 | goto error; | |
2755 | } | |
2756 | ||
2757 | byte_order = get_real_byte_order(ctx, right); | |
7b33a0e0 | 2758 | if (byte_order == -1) { |
87cb75e6 | 2759 | _BT_LOGE_NODE(right, |
939190b3 | 2760 | "Invalid `byte_order` attribute in floating point number field class: " |
37d21e07 | 2761 | "ret=%d", ret); |
e98a2d6e PP |
2762 | ret = -EINVAL; |
2763 | goto error; | |
2764 | } | |
2765 | ||
2766 | _SET(&set, _FLOAT_BYTE_ORDER_SET); | |
2767 | } else if (!strcmp(left->u.unary_expression.u.string, | |
2768 | "exp_dig")) { | |
2769 | if (_IS_SET(&set, _FLOAT_EXP_DIG_SET)) { | |
87cb75e6 | 2770 | _BT_LOGE_DUP_ATTR(left, "exp_dig", |
939190b3 | 2771 | "floating point number field class"); |
e98a2d6e PP |
2772 | ret = -EPERM; |
2773 | goto error; | |
2774 | } | |
2775 | ||
2776 | if (right->u.unary_expression.type != | |
2777 | UNARY_UNSIGNED_CONSTANT) { | |
87cb75e6 | 2778 | _BT_LOGE_NODE(right, |
939190b3 | 2779 | "Invalid `exp_dig` attribute in floating point number field class: " |
37d21e07 PP |
2780 | "expecting unsigned constant integer: " |
2781 | "node-type=%d", | |
2782 | right->u.unary_expression.type); | |
e98a2d6e PP |
2783 | ret = -EINVAL; |
2784 | goto error; | |
2785 | } | |
2786 | ||
2787 | exp_dig = right->u.unary_expression.u.unsigned_constant; | |
2788 | _SET(&set, _FLOAT_EXP_DIG_SET); | |
2789 | } else if (!strcmp(left->u.unary_expression.u.string, | |
2790 | "mant_dig")) { | |
2791 | if (_IS_SET(&set, _FLOAT_MANT_DIG_SET)) { | |
87cb75e6 | 2792 | _BT_LOGE_DUP_ATTR(left, "mant_dig", |
939190b3 | 2793 | "floating point number field class"); |
e98a2d6e PP |
2794 | ret = -EPERM; |
2795 | goto error; | |
2796 | } | |
2797 | ||
2798 | if (right->u.unary_expression.type != | |
2799 | UNARY_UNSIGNED_CONSTANT) { | |
87cb75e6 | 2800 | _BT_LOGE_NODE(right, |
939190b3 | 2801 | "Invalid `mant_dig` attribute in floating point number field class: " |
37d21e07 PP |
2802 | "expecting unsigned constant integer: " |
2803 | "node-type=%d", | |
2804 | right->u.unary_expression.type); | |
e98a2d6e PP |
2805 | ret = -EINVAL; |
2806 | goto error; | |
2807 | } | |
2808 | ||
2809 | mant_dig = right->u.unary_expression.u. | |
2810 | unsigned_constant; | |
2811 | _SET(&set, _FLOAT_MANT_DIG_SET); | |
2812 | } else if (!strcmp(left->u.unary_expression.u.string, | |
2813 | "align")) { | |
2814 | if (_IS_SET(&set, _FLOAT_ALIGN_SET)) { | |
87cb75e6 | 2815 | _BT_LOGE_DUP_ATTR(left, "align", |
939190b3 | 2816 | "floating point number field class"); |
e98a2d6e PP |
2817 | ret = -EPERM; |
2818 | goto error; | |
2819 | } | |
2820 | ||
2821 | if (right->u.unary_expression.type != | |
2822 | UNARY_UNSIGNED_CONSTANT) { | |
87cb75e6 | 2823 | _BT_LOGE_NODE(right, |
939190b3 | 2824 | "Invalid `align` attribute in floating point number field class: " |
37d21e07 PP |
2825 | "expecting unsigned constant integer: " |
2826 | "node-type=%d", | |
2827 | right->u.unary_expression.type); | |
e98a2d6e PP |
2828 | ret = -EINVAL; |
2829 | goto error; | |
2830 | } | |
2831 | ||
2832 | alignment = right->u.unary_expression.u. | |
2833 | unsigned_constant; | |
2834 | ||
2835 | if (!is_align_valid(alignment)) { | |
87cb75e6 | 2836 | _BT_LOGE_NODE(right, |
939190b3 | 2837 | "Invalid `align` attribute in floating point number field class: " |
37d21e07 PP |
2838 | "expecting power of two: " |
2839 | "align=%" PRIu64, alignment); | |
e98a2d6e PP |
2840 | ret = -EINVAL; |
2841 | goto error; | |
2842 | } | |
2843 | ||
2844 | _SET(&set, _FLOAT_ALIGN_SET); | |
2845 | } else { | |
87cb75e6 | 2846 | _BT_LOGW_NODE(left, |
939190b3 | 2847 | "Unknown attribute in floating point number field class: " |
37d21e07 | 2848 | "attr-name=\"%s\"", |
e98a2d6e PP |
2849 | left->u.unary_expression.u.string); |
2850 | } | |
2851 | } | |
2852 | ||
2853 | if (!_IS_SET(&set, _FLOAT_MANT_DIG_SET)) { | |
939190b3 | 2854 | BT_LOGE_STR("Missing `mant_dig` attribute in floating point number field class."); |
e98a2d6e PP |
2855 | ret = -EPERM; |
2856 | goto error; | |
2857 | } | |
2858 | ||
2859 | if (!_IS_SET(&set, _FLOAT_EXP_DIG_SET)) { | |
939190b3 | 2860 | BT_LOGE_STR("Missing `exp_dig` attribute in floating point number field class."); |
e98a2d6e PP |
2861 | ret = -EPERM; |
2862 | goto error; | |
2863 | } | |
2864 | ||
7b33a0e0 PP |
2865 | if (mant_dig != 24 && mant_dig != 53) { |
2866 | BT_LOGE_STR("`mant_dig` attribute: expecting 24 or 53."); | |
2867 | ret = -EPERM; | |
2868 | goto error; | |
2869 | } | |
2870 | ||
2871 | if (mant_dig == 24 && exp_dig != 8) { | |
2872 | BT_LOGE_STR("`exp_dig` attribute: expecting 8 because `mant_dig` is 24."); | |
2873 | ret = -EPERM; | |
2874 | goto error; | |
2875 | } | |
2876 | ||
2877 | if (mant_dig == 53 && exp_dig != 11) { | |
2878 | BT_LOGE_STR("`exp_dig` attribute: expecting 11 because `mant_dig` is 53."); | |
2879 | ret = -EPERM; | |
2880 | goto error; | |
2881 | } | |
2882 | ||
e98a2d6e PP |
2883 | if (!_IS_SET(&set, _INTEGER_ALIGN_SET)) { |
2884 | if ((mant_dig + exp_dig) % CHAR_BIT) { | |
2885 | /* Bit-packed alignment */ | |
2886 | alignment = 1; | |
2887 | } else { | |
2888 | /* Byte-packed alignment */ | |
2889 | alignment = CHAR_BIT; | |
2890 | } | |
2891 | } | |
2892 | ||
939190b3 | 2893 | *float_decl = ctf_field_class_float_create(); |
7b33a0e0 PP |
2894 | BT_ASSERT(*float_decl); |
2895 | (*float_decl)->base.base.alignment = alignment; | |
2896 | (*float_decl)->base.byte_order = byte_order; | |
2897 | (*float_decl)->base.size = mant_dig + exp_dig; | |
e98a2d6e PP |
2898 | return 0; |
2899 | ||
2900 | error: | |
939190b3 | 2901 | ctf_field_class_destroy((void *) *float_decl); |
7b33a0e0 | 2902 | *float_decl = NULL; |
e98a2d6e PP |
2903 | return ret; |
2904 | } | |
2905 | ||
2906 | static | |
2907 | int visit_string_decl(struct ctx *ctx, | |
7b33a0e0 | 2908 | struct bt_list_head *expressions, |
939190b3 | 2909 | struct ctf_field_class_string **string_decl) |
e98a2d6e PP |
2910 | { |
2911 | int set = 0; | |
2912 | int ret = 0; | |
2913 | struct ctf_node *expression; | |
7b33a0e0 | 2914 | enum ctf_encoding encoding = CTF_ENCODING_UTF8; |
e98a2d6e PP |
2915 | |
2916 | *string_decl = NULL; | |
2917 | ||
2918 | bt_list_for_each_entry(expression, expressions, siblings) { | |
2919 | struct ctf_node *left, *right; | |
2920 | ||
2921 | left = _BT_LIST_FIRST_ENTRY(&expression->u.ctf_expression.left, | |
2922 | struct ctf_node, siblings); | |
2923 | right = _BT_LIST_FIRST_ENTRY( | |
2924 | &expression->u.ctf_expression.right, struct ctf_node, | |
2925 | siblings); | |
2926 | ||
2927 | if (left->u.unary_expression.type != UNARY_STRING) { | |
87cb75e6 PP |
2928 | _BT_LOGE_NODE(left, |
2929 | "Unexpected unary expression type: type=%d", | |
2930 | left->u.unary_expression.type); | |
e98a2d6e PP |
2931 | ret = -EINVAL; |
2932 | goto error; | |
2933 | } | |
2934 | ||
2935 | if (!strcmp(left->u.unary_expression.u.string, "encoding")) { | |
2936 | char *s_right; | |
2937 | ||
2938 | if (_IS_SET(&set, _STRING_ENCODING_SET)) { | |
87cb75e6 | 2939 | _BT_LOGE_DUP_ATTR(left, "encoding", |
939190b3 | 2940 | "string field class"); |
e98a2d6e PP |
2941 | ret = -EPERM; |
2942 | goto error; | |
2943 | } | |
2944 | ||
2945 | if (right->u.unary_expression.type != UNARY_STRING) { | |
87cb75e6 | 2946 | _BT_LOGE_NODE(right, |
939190b3 | 2947 | "Invalid `encoding` attribute in string field class: " |
37d21e07 | 2948 | "expecting unary string."); |
e98a2d6e PP |
2949 | ret = -EINVAL; |
2950 | goto error; | |
2951 | } | |
2952 | ||
2953 | s_right = concatenate_unary_strings( | |
2954 | &expression->u.ctf_expression.right); | |
2955 | if (!s_right) { | |
87cb75e6 | 2956 | _BT_LOGE_NODE(right, |
939190b3 | 2957 | "Unexpected unary expression for string field class's `encoding` attribute."); |
e98a2d6e PP |
2958 | ret = -EINVAL; |
2959 | goto error; | |
2960 | } | |
2961 | ||
2962 | if (!strcmp(s_right, "UTF8") || | |
2963 | !strcmp(s_right, "utf8") || | |
2964 | !strcmp(s_right, "utf-8") || | |
7b33a0e0 PP |
2965 | !strcmp(s_right, "UTF-8") || |
2966 | !strcmp(s_right, "ASCII") || | |
e98a2d6e | 2967 | !strcmp(s_right, "ascii")) { |
7b33a0e0 | 2968 | encoding = CTF_ENCODING_UTF8; |
e98a2d6e | 2969 | } else if (!strcmp(s_right, "none")) { |
7b33a0e0 | 2970 | encoding = CTF_ENCODING_NONE; |
e98a2d6e | 2971 | } else { |
87cb75e6 | 2972 | _BT_LOGE_NODE(right, |
939190b3 | 2973 | "Invalid `encoding` attribute in string field class: " |
37d21e07 | 2974 | "unknown encoding: encoding=\"%s\"", |
e98a2d6e PP |
2975 | s_right); |
2976 | g_free(s_right); | |
2977 | ret = -EINVAL; | |
2978 | goto error; | |
2979 | } | |
2980 | ||
2981 | g_free(s_right); | |
2982 | _SET(&set, _STRING_ENCODING_SET); | |
2983 | } else { | |
87cb75e6 | 2984 | _BT_LOGW_NODE(left, |
939190b3 | 2985 | "Unknown attribute in string field class: " |
37d21e07 | 2986 | "attr-name=\"%s\"", |
e98a2d6e PP |
2987 | left->u.unary_expression.u.string); |
2988 | } | |
2989 | } | |
2990 | ||
939190b3 | 2991 | *string_decl = ctf_field_class_string_create(); |
7b33a0e0 PP |
2992 | BT_ASSERT(*string_decl); |
2993 | (*string_decl)->encoding = encoding; | |
e98a2d6e PP |
2994 | return 0; |
2995 | ||
2996 | error: | |
939190b3 | 2997 | ctf_field_class_destroy((void *) *string_decl); |
7b33a0e0 | 2998 | *string_decl = NULL; |
e98a2d6e PP |
2999 | return ret; |
3000 | } | |
3001 | ||
3002 | static | |
939190b3 PP |
3003 | int visit_field_class_specifier_list(struct ctx *ctx, |
3004 | struct ctf_node *ts_list, struct ctf_field_class **decl) | |
e98a2d6e PP |
3005 | { |
3006 | int ret = 0; | |
3007 | struct ctf_node *first, *node; | |
3008 | ||
3009 | *decl = NULL; | |
3010 | ||
3011 | if (ts_list->type != NODE_TYPE_SPECIFIER_LIST) { | |
87cb75e6 PP |
3012 | _BT_LOGE_NODE(ts_list, |
3013 | "Unexpected node type: node-type=%d", ts_list->type); | |
e98a2d6e PP |
3014 | ret = -EINVAL; |
3015 | goto error; | |
3016 | } | |
3017 | ||
939190b3 | 3018 | first = _BT_LIST_FIRST_ENTRY(&ts_list->u.field_class_specifier_list.head, |
e98a2d6e PP |
3019 | struct ctf_node, siblings); |
3020 | if (first->type != NODE_TYPE_SPECIFIER) { | |
87cb75e6 PP |
3021 | _BT_LOGE_NODE(first, |
3022 | "Unexpected node type: node-type=%d", first->type); | |
e98a2d6e PP |
3023 | ret = -EINVAL; |
3024 | goto error; | |
3025 | } | |
3026 | ||
939190b3 | 3027 | node = first->u.field_class_specifier.node; |
e98a2d6e | 3028 | |
939190b3 | 3029 | switch (first->u.field_class_specifier.type) { |
e98a2d6e PP |
3030 | case TYPESPEC_INTEGER: |
3031 | ret = visit_integer_decl(ctx, &node->u.integer.expressions, | |
7b33a0e0 | 3032 | (void *) decl); |
e98a2d6e | 3033 | if (ret) { |
8b45963b | 3034 | BT_ASSERT(!*decl); |
e98a2d6e PP |
3035 | goto error; |
3036 | } | |
3037 | break; | |
3038 | case TYPESPEC_FLOATING_POINT: | |
3039 | ret = visit_floating_point_number_decl(ctx, | |
7b33a0e0 | 3040 | &node->u.floating_point.expressions, (void *) decl); |
e98a2d6e | 3041 | if (ret) { |
8b45963b | 3042 | BT_ASSERT(!*decl); |
e98a2d6e PP |
3043 | goto error; |
3044 | } | |
3045 | break; | |
3046 | case TYPESPEC_STRING: | |
3047 | ret = visit_string_decl(ctx, | |
7b33a0e0 | 3048 | &node->u.string.expressions, (void *) decl); |
e98a2d6e | 3049 | if (ret) { |
8b45963b | 3050 | BT_ASSERT(!*decl); |
e98a2d6e PP |
3051 | goto error; |
3052 | } | |
3053 | break; | |
3054 | case TYPESPEC_STRUCT: | |
3055 | ret = visit_struct_decl(ctx, node->u._struct.name, | |
3056 | &node->u._struct.declaration_list, | |
3057 | node->u._struct.has_body, | |
7b33a0e0 | 3058 | &node->u._struct.min_align, (void *) decl); |
e98a2d6e | 3059 | if (ret) { |
8b45963b | 3060 | BT_ASSERT(!*decl); |
e98a2d6e PP |
3061 | goto error; |
3062 | } | |
3063 | break; | |
3064 | case TYPESPEC_VARIANT: | |
3065 | ret = visit_variant_decl(ctx, node->u.variant.name, | |
3066 | node->u.variant.choice, | |
3067 | &node->u.variant.declaration_list, | |
7b33a0e0 | 3068 | node->u.variant.has_body, (void *) decl); |
e98a2d6e | 3069 | if (ret) { |
8b45963b | 3070 | BT_ASSERT(!*decl); |
e98a2d6e PP |
3071 | goto error; |
3072 | } | |
3073 | break; | |
3074 | case TYPESPEC_ENUM: | |
3075 | ret = visit_enum_decl(ctx, node->u._enum.enum_id, | |
939190b3 | 3076 | node->u._enum.container_field_class, |
e98a2d6e | 3077 | &node->u._enum.enumerator_list, |
7b33a0e0 | 3078 | node->u._enum.has_body, (void *) decl); |
e98a2d6e | 3079 | if (ret) { |
8b45963b | 3080 | BT_ASSERT(!*decl); |
e98a2d6e PP |
3081 | goto error; |
3082 | } | |
3083 | break; | |
3084 | case TYPESPEC_VOID: | |
3085 | case TYPESPEC_CHAR: | |
3086 | case TYPESPEC_SHORT: | |
3087 | case TYPESPEC_INT: | |
3088 | case TYPESPEC_LONG: | |
3089 | case TYPESPEC_FLOAT: | |
3090 | case TYPESPEC_DOUBLE: | |
3091 | case TYPESPEC_SIGNED: | |
3092 | case TYPESPEC_UNSIGNED: | |
3093 | case TYPESPEC_BOOL: | |
3094 | case TYPESPEC_COMPLEX: | |
3095 | case TYPESPEC_IMAGINARY: | |
3096 | case TYPESPEC_CONST: | |
3097 | case TYPESPEC_ID_TYPE: | |
939190b3 | 3098 | ret = visit_field_class_specifier(ctx, ts_list, decl); |
e98a2d6e | 3099 | if (ret) { |
87cb75e6 | 3100 | _BT_LOGE_NODE(first, |
939190b3 | 3101 | "Cannot visit field class specifier: ret=%d", |
87cb75e6 | 3102 | ret); |
8b45963b | 3103 | BT_ASSERT(!*decl); |
e98a2d6e PP |
3104 | goto error; |
3105 | } | |
3106 | break; | |
3107 | default: | |
87cb75e6 | 3108 | _BT_LOGE_NODE(first, |
939190b3 PP |
3109 | "Unexpected field class specifier type: node-type=%d", |
3110 | first->u.field_class_specifier.type); | |
e98a2d6e PP |
3111 | ret = -EINVAL; |
3112 | goto error; | |
3113 | } | |
3114 | ||
8b45963b | 3115 | BT_ASSERT(*decl); |
e98a2d6e PP |
3116 | return 0; |
3117 | ||
3118 | error: | |
939190b3 | 3119 | ctf_field_class_destroy((void *) *decl); |
7b33a0e0 | 3120 | *decl = NULL; |
e98a2d6e PP |
3121 | return ret; |
3122 | } | |
3123 | ||
3124 | static | |
3125 | int visit_event_decl_entry(struct ctx *ctx, struct ctf_node *node, | |
7b33a0e0 PP |
3126 | struct ctf_event_class *event_class, uint64_t *stream_id, |
3127 | int *set) | |
e98a2d6e PP |
3128 | { |
3129 | int ret = 0; | |
3130 | char *left = NULL; | |
e98a2d6e PP |
3131 | |
3132 | switch (node->type) { | |
3133 | case NODE_TYPEDEF: | |
939190b3 PP |
3134 | ret = visit_field_class_def(ctx, node->u.field_class_def.field_class_specifier_list, |
3135 | &node->u.field_class_def.field_class_declarators); | |
e98a2d6e | 3136 | if (ret) { |
87cb75e6 | 3137 | _BT_LOGE_NODE(node, |
939190b3 | 3138 | "Cannot add field class found in event class."); |
e98a2d6e PP |
3139 | goto error; |
3140 | } | |
3141 | break; | |
3142 | case NODE_TYPEALIAS: | |
939190b3 PP |
3143 | ret = visit_field_class_alias(ctx, node->u.field_class_alias.target, |
3144 | node->u.field_class_alias.alias); | |
e98a2d6e | 3145 | if (ret) { |
87cb75e6 | 3146 | _BT_LOGE_NODE(node, |
939190b3 | 3147 | "Cannot add field class alias found in event class."); |
e98a2d6e PP |
3148 | goto error; |
3149 | } | |
3150 | break; | |
3151 | case NODE_CTF_EXPRESSION: | |
3152 | { | |
3153 | left = concatenate_unary_strings(&node->u.ctf_expression.left); | |
3154 | if (!left) { | |
87cb75e6 | 3155 | _BT_LOGE_NODE(node, "Cannot concatenate unary strings."); |
e98a2d6e PP |
3156 | ret = -EINVAL; |
3157 | goto error; | |
3158 | } | |
3159 | ||
3160 | if (!strcmp(left, "name")) { | |
3161 | /* This is already known at this stage */ | |
3162 | if (_IS_SET(set, _EVENT_NAME_SET)) { | |
7b33a0e0 | 3163 | _BT_LOGE_DUP_ATTR(node, "name", "event class"); |
e98a2d6e PP |
3164 | ret = -EPERM; |
3165 | goto error; | |
3166 | } | |
3167 | ||
3168 | _SET(set, _EVENT_NAME_SET); | |
3169 | } else if (!strcmp(left, "id")) { | |
8e1cfe98 | 3170 | int64_t id = -1; |
e98a2d6e PP |
3171 | |
3172 | if (_IS_SET(set, _EVENT_ID_SET)) { | |
7b33a0e0 | 3173 | _BT_LOGE_DUP_ATTR(node, "id", "event class"); |
e98a2d6e PP |
3174 | ret = -EPERM; |
3175 | goto error; | |
3176 | } | |
3177 | ||
3178 | ret = get_unary_unsigned(&node->u.ctf_expression.right, | |
3179 | (uint64_t *) &id); | |
5eae0c3c JG |
3180 | /* Only read "id" if get_unary_unsigned() succeeded. */ |
3181 | if (ret || (!ret && id < 0)) { | |
87cb75e6 PP |
3182 | _BT_LOGE_NODE(node, |
3183 | "Unexpected unary expression for event class's `id` attribute."); | |
e98a2d6e PP |
3184 | ret = -EINVAL; |
3185 | goto error; | |
3186 | } | |
3187 | ||
7b33a0e0 | 3188 | event_class->id = id; |
e98a2d6e PP |
3189 | _SET(set, _EVENT_ID_SET); |
3190 | } else if (!strcmp(left, "stream_id")) { | |
3191 | if (_IS_SET(set, _EVENT_STREAM_ID_SET)) { | |
87cb75e6 | 3192 | _BT_LOGE_DUP_ATTR(node, "stream_id", |
37d21e07 | 3193 | "event class"); |
e98a2d6e PP |
3194 | ret = -EPERM; |
3195 | goto error; | |
3196 | } | |
3197 | ||
3198 | ret = get_unary_unsigned(&node->u.ctf_expression.right, | |
7b33a0e0 PP |
3199 | stream_id); |
3200 | ||
5eae0c3c JG |
3201 | /* |
3202 | * Only read "stream_id" if get_unary_unsigned() | |
3203 | * succeeded. | |
3204 | */ | |
3205 | if (ret || (!ret && *stream_id < 0)) { | |
87cb75e6 PP |
3206 | _BT_LOGE_NODE(node, |
3207 | "Unexpected unary expression for event class's `stream_id` attribute."); | |
e98a2d6e PP |
3208 | ret = -EINVAL; |
3209 | goto error; | |
3210 | } | |
3211 | ||
3212 | _SET(set, _EVENT_STREAM_ID_SET); | |
3213 | } else if (!strcmp(left, "context")) { | |
3214 | if (_IS_SET(set, _EVENT_CONTEXT_SET)) { | |
87cb75e6 PP |
3215 | _BT_LOGE_NODE(node, |
3216 | "Duplicate `context` entry in event class."); | |
e98a2d6e PP |
3217 | ret = -EPERM; |
3218 | goto error; | |
3219 | } | |
3220 | ||
939190b3 | 3221 | ret = visit_field_class_specifier_list(ctx, |
e98a2d6e PP |
3222 | _BT_LIST_FIRST_ENTRY( |
3223 | &node->u.ctf_expression.right, | |
3224 | struct ctf_node, siblings), | |
939190b3 | 3225 | &event_class->spec_context_fc); |
e98a2d6e | 3226 | if (ret) { |
87cb75e6 | 3227 | _BT_LOGE_NODE(node, |
939190b3 | 3228 | "Cannot create event class's context field class."); |
e98a2d6e PP |
3229 | goto error; |
3230 | } | |
3231 | ||
939190b3 | 3232 | BT_ASSERT(event_class->spec_context_fc); |
e98a2d6e PP |
3233 | _SET(set, _EVENT_CONTEXT_SET); |
3234 | } else if (!strcmp(left, "fields")) { | |
3235 | if (_IS_SET(set, _EVENT_FIELDS_SET)) { | |
87cb75e6 PP |
3236 | _BT_LOGE_NODE(node, |
3237 | "Duplicate `fields` entry in event class."); | |
e98a2d6e PP |
3238 | ret = -EPERM; |
3239 | goto error; | |
3240 | } | |
3241 | ||
939190b3 | 3242 | ret = visit_field_class_specifier_list(ctx, |
e98a2d6e PP |
3243 | _BT_LIST_FIRST_ENTRY( |
3244 | &node->u.ctf_expression.right, | |
3245 | struct ctf_node, siblings), | |
939190b3 | 3246 | &event_class->payload_fc); |
e98a2d6e | 3247 | if (ret) { |
87cb75e6 | 3248 | _BT_LOGE_NODE(node, |
939190b3 | 3249 | "Cannot create event class's payload field class."); |
e98a2d6e PP |
3250 | goto error; |
3251 | } | |
3252 | ||
939190b3 | 3253 | BT_ASSERT(event_class->payload_fc); |
e98a2d6e PP |
3254 | _SET(set, _EVENT_FIELDS_SET); |
3255 | } else if (!strcmp(left, "loglevel")) { | |
c3c30b08 | 3256 | uint64_t loglevel_value; |
ee78f405 | 3257 | bt_event_class_log_level log_level = -1; |
e98a2d6e | 3258 | |
7b33a0e0 | 3259 | if (_IS_SET(set, _EVENT_LOG_LEVEL_SET)) { |
87cb75e6 | 3260 | _BT_LOGE_DUP_ATTR(node, "loglevel", |
37d21e07 | 3261 | "event class"); |
e98a2d6e PP |
3262 | ret = -EPERM; |
3263 | goto error; | |
3264 | } | |
3265 | ||
3266 | ret = get_unary_unsigned(&node->u.ctf_expression.right, | |
c3c30b08 | 3267 | &loglevel_value); |
e98a2d6e | 3268 | if (ret) { |
87cb75e6 PP |
3269 | _BT_LOGE_NODE(node, |
3270 | "Unexpected unary expression for event class's `loglevel` attribute."); | |
e98a2d6e PP |
3271 | ret = -EINVAL; |
3272 | goto error; | |
3273 | } | |
9cf5d083 PP |
3274 | |
3275 | switch (loglevel_value) { | |
3276 | case 0: | |
839d52a5 | 3277 | log_level = BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY; |
9cf5d083 PP |
3278 | break; |
3279 | case 1: | |
839d52a5 | 3280 | log_level = BT_EVENT_CLASS_LOG_LEVEL_ALERT; |
9cf5d083 PP |
3281 | break; |
3282 | case 2: | |
839d52a5 | 3283 | log_level = BT_EVENT_CLASS_LOG_LEVEL_CRITICAL; |
9cf5d083 PP |
3284 | break; |
3285 | case 3: | |
839d52a5 | 3286 | log_level = BT_EVENT_CLASS_LOG_LEVEL_ERROR; |
9cf5d083 PP |
3287 | break; |
3288 | case 4: | |
839d52a5 | 3289 | log_level = BT_EVENT_CLASS_LOG_LEVEL_WARNING; |
9cf5d083 PP |
3290 | break; |
3291 | case 5: | |
839d52a5 | 3292 | log_level = BT_EVENT_CLASS_LOG_LEVEL_NOTICE; |
9cf5d083 PP |
3293 | break; |
3294 | case 6: | |
839d52a5 | 3295 | log_level = BT_EVENT_CLASS_LOG_LEVEL_INFO; |
9cf5d083 PP |
3296 | break; |
3297 | case 7: | |
839d52a5 | 3298 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM; |
9cf5d083 PP |
3299 | break; |
3300 | case 8: | |
839d52a5 | 3301 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM; |
9cf5d083 PP |
3302 | break; |
3303 | case 9: | |
839d52a5 | 3304 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS; |
9cf5d083 PP |
3305 | break; |
3306 | case 10: | |
839d52a5 | 3307 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE; |
9cf5d083 PP |
3308 | break; |
3309 | case 11: | |
839d52a5 | 3310 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT; |
9cf5d083 PP |
3311 | break; |
3312 | case 12: | |
839d52a5 | 3313 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION; |
9cf5d083 PP |
3314 | break; |
3315 | case 13: | |
839d52a5 | 3316 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE; |
9cf5d083 PP |
3317 | break; |
3318 | case 14: | |
839d52a5 | 3319 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG; |
9cf5d083 PP |
3320 | break; |
3321 | default: | |
3322 | _BT_LOGW_NODE(node, "Not setting event class's log level because its value is unknown: " | |
3323 | "log-level=%" PRIu64, loglevel_value); | |
c3c30b08 | 3324 | } |
9cf5d083 | 3325 | |
7b33a0e0 PP |
3326 | if (log_level != -1) { |
3327 | event_class->log_level = log_level; | |
c3c30b08 | 3328 | } |
9cf5d083 | 3329 | |
7b33a0e0 | 3330 | _SET(set, _EVENT_LOG_LEVEL_SET); |
e98a2d6e PP |
3331 | } else if (!strcmp(left, "model.emf.uri")) { |
3332 | char *right; | |
3333 | ||
3334 | if (_IS_SET(set, _EVENT_MODEL_EMF_URI_SET)) { | |
87cb75e6 PP |
3335 | _BT_LOGE_DUP_ATTR(node, "model.emf.uri", |
3336 | "event class"); | |
e98a2d6e PP |
3337 | ret = -EPERM; |
3338 | goto error; | |
3339 | } | |
3340 | ||
3341 | right = concatenate_unary_strings( | |
3342 | &node->u.ctf_expression.right); | |
3343 | if (!right) { | |
87cb75e6 PP |
3344 | _BT_LOGE_NODE(node, |
3345 | "Unexpected unary expression for event class's `model.emf.uri` attribute."); | |
e98a2d6e PP |
3346 | ret = -EINVAL; |
3347 | goto error; | |
3348 | } | |
3349 | ||
9cf5d083 PP |
3350 | if (strlen(right) == 0) { |
3351 | _BT_LOGW_NODE(node, | |
3352 | "Not setting event class's EMF URI because it's empty."); | |
3353 | } else { | |
7b33a0e0 PP |
3354 | g_string_assign(event_class->emf_uri, |
3355 | right); | |
87cb75e6 | 3356 | } |
9cf5d083 | 3357 | |
e98a2d6e PP |
3358 | g_free(right); |
3359 | _SET(set, _EVENT_MODEL_EMF_URI_SET); | |
3360 | } else { | |
87cb75e6 PP |
3361 | _BT_LOGW_NODE(node, |
3362 | "Unknown attribute in event class: " | |
37d21e07 | 3363 | "attr-name=\"%s\"", left); |
e98a2d6e PP |
3364 | } |
3365 | ||
3366 | g_free(left); | |
3367 | left = NULL; | |
3368 | break; | |
3369 | } | |
3370 | default: | |
3371 | ret = -EPERM; | |
3372 | goto error; | |
3373 | } | |
3374 | ||
7b33a0e0 | 3375 | goto end; |
e98a2d6e PP |
3376 | |
3377 | error: | |
3378 | if (left) { | |
3379 | g_free(left); | |
3380 | } | |
3381 | ||
7b33a0e0 | 3382 | end: |
e98a2d6e PP |
3383 | return ret; |
3384 | } | |
3385 | ||
3386 | static | |
3387 | char *get_event_decl_name(struct ctx *ctx, struct ctf_node *node) | |
3388 | { | |
3389 | char *left = NULL; | |
3390 | char *name = NULL; | |
3391 | struct ctf_node *iter; | |
3392 | struct bt_list_head *decl_list = &node->u.event.declaration_list; | |
3393 | ||
3394 | bt_list_for_each_entry(iter, decl_list, siblings) { | |
3395 | if (iter->type != NODE_CTF_EXPRESSION) { | |
3396 | continue; | |
3397 | } | |
3398 | ||
3399 | left = concatenate_unary_strings(&iter->u.ctf_expression.left); | |
3400 | if (!left) { | |
87cb75e6 PP |
3401 | _BT_LOGE_NODE(iter, |
3402 | "Cannot concatenate unary strings."); | |
e98a2d6e PP |
3403 | goto error; |
3404 | } | |
3405 | ||
3406 | if (!strcmp(left, "name")) { | |
3407 | name = concatenate_unary_strings( | |
3408 | &iter->u.ctf_expression.right); | |
3409 | if (!name) { | |
87cb75e6 PP |
3410 | _BT_LOGE_NODE(iter, |
3411 | "Unexpected unary expression for event class's `name` attribute."); | |
e98a2d6e PP |
3412 | goto error; |
3413 | } | |
3414 | } | |
3415 | ||
3416 | g_free(left); | |
3417 | left = NULL; | |
3418 | ||
3419 | if (name) { | |
3420 | break; | |
3421 | } | |
3422 | } | |
3423 | ||
3424 | return name; | |
3425 | ||
3426 | error: | |
3427 | g_free(left); | |
e98a2d6e PP |
3428 | return NULL; |
3429 | } | |
3430 | ||
3431 | static | |
3432 | int visit_event_decl(struct ctx *ctx, struct ctf_node *node) | |
3433 | { | |
3434 | int ret = 0; | |
3435 | int set = 0; | |
e98a2d6e | 3436 | struct ctf_node *iter; |
7b33a0e0 | 3437 | uint64_t stream_id = 0; |
e98a2d6e | 3438 | char *event_name = NULL; |
7b33a0e0 PP |
3439 | struct ctf_event_class *event_class = NULL; |
3440 | struct ctf_stream_class *stream_class = NULL; | |
e98a2d6e | 3441 | struct bt_list_head *decl_list = &node->u.event.declaration_list; |
1e649dff | 3442 | bool pop_scope = false; |
e98a2d6e PP |
3443 | |
3444 | if (node->visited) { | |
3445 | goto end; | |
3446 | } | |
3447 | ||
3448 | node->visited = TRUE; | |
3449 | event_name = get_event_decl_name(ctx, node); | |
3450 | if (!event_name) { | |
87cb75e6 PP |
3451 | _BT_LOGE_NODE(node, |
3452 | "Missing `name` attribute in event class."); | |
e98a2d6e PP |
3453 | ret = -EPERM; |
3454 | goto error; | |
3455 | } | |
3456 | ||
7b33a0e0 PP |
3457 | event_class = ctf_event_class_create(); |
3458 | BT_ASSERT(event_class); | |
3459 | g_string_assign(event_class->name, event_name); | |
3460 | _TRY_PUSH_SCOPE_OR_GOTO_ERROR(); | |
1e649dff PP |
3461 | pop_scope = true; |
3462 | ||
e98a2d6e PP |
3463 | bt_list_for_each_entry(iter, decl_list, siblings) { |
3464 | ret = visit_event_decl_entry(ctx, iter, event_class, | |
3465 | &stream_id, &set); | |
3466 | if (ret) { | |
87cb75e6 PP |
3467 | _BT_LOGE_NODE(iter, "Cannot visit event class's entry: " |
3468 | "ret=%d", ret); | |
e98a2d6e PP |
3469 | goto error; |
3470 | } | |
3471 | } | |
3472 | ||
3473 | if (!_IS_SET(&set, _EVENT_STREAM_ID_SET)) { | |
1e649dff PP |
3474 | /* |
3475 | * Allow missing stream_id if there is only a single | |
3476 | * stream class. | |
3477 | */ | |
7b33a0e0 | 3478 | switch (ctx->ctf_tc->stream_classes->len) { |
e98a2d6e | 3479 | case 0: |
1e649dff | 3480 | /* Create implicit stream class if there's none */ |
9ea34460 | 3481 | stream_id = 0; |
7b33a0e0 PP |
3482 | stream_class = ctf_stream_class_create(); |
3483 | BT_ASSERT(stream_class); | |
3484 | stream_class->id = stream_id; | |
3485 | g_ptr_array_add(ctx->ctf_tc->stream_classes, | |
3486 | stream_class); | |
3487 | stream_class = stream_class; | |
e98a2d6e PP |
3488 | break; |
3489 | case 1: | |
1e649dff | 3490 | /* Single stream class: get its ID */ |
7b33a0e0 PP |
3491 | stream_class = ctx->ctf_tc->stream_classes->pdata[0]; |
3492 | stream_id = stream_class->id; | |
e98a2d6e PP |
3493 | break; |
3494 | default: | |
87cb75e6 PP |
3495 | _BT_LOGE_NODE(node, |
3496 | "Missing `stream_id` attribute in event class."); | |
e98a2d6e PP |
3497 | ret = -EPERM; |
3498 | goto error; | |
3499 | } | |
3500 | } | |
3501 | ||
1e649dff | 3502 | /* We have the stream ID now; get the stream class if found */ |
e98a2d6e | 3503 | if (!stream_class) { |
7b33a0e0 PP |
3504 | stream_class = ctf_trace_class_borrow_stream_class_by_id( |
3505 | ctx->ctf_tc, stream_id); | |
1e649dff | 3506 | if (!stream_class) { |
87cb75e6 PP |
3507 | _BT_LOGE_NODE(node, |
3508 | "Cannot find stream class at this point: " | |
37d21e07 | 3509 | "id=%" PRId64, stream_id); |
1e649dff PP |
3510 | ret = -EINVAL; |
3511 | goto error; | |
3512 | } | |
e98a2d6e PP |
3513 | } |
3514 | ||
8b45963b | 3515 | BT_ASSERT(stream_class); |
1e649dff | 3516 | |
e98a2d6e PP |
3517 | if (!_IS_SET(&set, _EVENT_ID_SET)) { |
3518 | /* Allow only one event without ID per stream */ | |
7b33a0e0 | 3519 | if (stream_class->event_classes->len != 0) { |
87cb75e6 PP |
3520 | _BT_LOGE_NODE(node, |
3521 | "Missing `id` attribute in event class."); | |
e98a2d6e PP |
3522 | ret = -EPERM; |
3523 | goto error; | |
3524 | } | |
3525 | ||
3526 | /* Automatic ID */ | |
7b33a0e0 | 3527 | event_class->id = 0; |
e98a2d6e PP |
3528 | } |
3529 | ||
7b33a0e0 PP |
3530 | if (ctf_stream_class_borrow_event_class_by_id(stream_class, |
3531 | event_class->id)) { | |
87cb75e6 PP |
3532 | _BT_LOGE_NODE(node, |
3533 | "Duplicate event class (same ID) in the same stream class: " | |
7b33a0e0 | 3534 | "id=%" PRId64, event_class->id); |
e98a2d6e PP |
3535 | ret = -EEXIST; |
3536 | goto error; | |
3537 | } | |
3538 | ||
7b33a0e0 PP |
3539 | ctf_stream_class_append_event_class(stream_class, event_class); |
3540 | event_class = NULL; | |
1e649dff | 3541 | goto end; |
e98a2d6e PP |
3542 | |
3543 | error: | |
7b33a0e0 PP |
3544 | ctf_event_class_destroy(event_class); |
3545 | event_class = NULL; | |
3546 | ||
3547 | if (ret >= 0) { | |
3548 | ret = -1; | |
3549 | } | |
e98a2d6e | 3550 | |
1e649dff PP |
3551 | end: |
3552 | if (pop_scope) { | |
3553 | ctx_pop_scope(ctx); | |
3554 | } | |
e98a2d6e | 3555 | |
7b33a0e0 PP |
3556 | if (event_name) { |
3557 | g_free(event_name); | |
3558 | } | |
3559 | ||
e98a2d6e PP |
3560 | return ret; |
3561 | } | |
3562 | ||
4adc5a41 | 3563 | static |
a0a4aa52 | 3564 | int auto_map_field_to_trace_clock_class(struct ctx *ctx, |
939190b3 | 3565 | struct ctf_field_class *fc) |
4adc5a41 | 3566 | { |
35fa110e | 3567 | struct ctf_clock_class *clock_class_to_map_to = NULL; |
939190b3 | 3568 | struct ctf_field_class_int *int_fc = (void *) fc; |
4adc5a41 | 3569 | int ret = 0; |
7b33a0e0 PP |
3570 | uint64_t clock_class_count; |
3571 | ||
939190b3 | 3572 | if (!fc) { |
7b33a0e0 PP |
3573 | goto end; |
3574 | } | |
4adc5a41 | 3575 | |
af0c18e3 PP |
3576 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
3577 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
4adc5a41 PP |
3578 | goto end; |
3579 | } | |
3580 | ||
939190b3 | 3581 | if (int_fc->mapped_clock_class) { |
7b33a0e0 | 3582 | /* Already mapped */ |
4adc5a41 PP |
3583 | goto end; |
3584 | } | |
3585 | ||
7b33a0e0 | 3586 | clock_class_count = ctx->ctf_tc->clock_classes->len; |
a0a4aa52 PP |
3587 | |
3588 | switch (clock_class_count) { | |
3589 | case 0: | |
3590 | /* | |
7b33a0e0 PP |
3591 | * No clock class exists in the trace at this point. Create an |
3592 | * implicit one at 1 GHz, named `default`, and use this clock | |
3593 | * class. | |
a0a4aa52 | 3594 | */ |
35fa110e | 3595 | clock_class_to_map_to = ctf_clock_class_create(); |
7b33a0e0 | 3596 | BT_ASSERT(clock_class_to_map_to); |
35fa110e PP |
3597 | clock_class_to_map_to->frequency = UINT64_C(1000000000); |
3598 | g_string_assign(clock_class_to_map_to->name, "default"); | |
7b33a0e0 PP |
3599 | BT_ASSERT(ret == 0); |
3600 | g_ptr_array_add(ctx->ctf_tc->clock_classes, | |
4b70020d | 3601 | clock_class_to_map_to); |
a0a4aa52 PP |
3602 | break; |
3603 | case 1: | |
3604 | /* | |
7b33a0e0 PP |
3605 | * Only one clock class exists in the trace at this point: use |
3606 | * this one. | |
a0a4aa52 | 3607 | */ |
4b70020d | 3608 | clock_class_to_map_to = ctx->ctf_tc->clock_classes->pdata[0]; |
a0a4aa52 PP |
3609 | break; |
3610 | default: | |
3611 | /* | |
7b33a0e0 PP |
3612 | * Timestamp field not mapped to a clock class and there's more |
3613 | * than one clock class in the trace: this is an error. | |
a0a4aa52 PP |
3614 | */ |
3615 | BT_LOGE_STR("Timestamp field found with no mapped clock class, " | |
3616 | "but there's more than one clock class in the trace at this point."); | |
3617 | ret = -1; | |
4adc5a41 PP |
3618 | goto end; |
3619 | } | |
3620 | ||
8b45963b | 3621 | BT_ASSERT(clock_class_to_map_to); |
4b70020d | 3622 | int_fc->mapped_clock_class = clock_class_to_map_to; |
4adc5a41 | 3623 | |
a0a4aa52 | 3624 | end: |
a0a4aa52 PP |
3625 | return ret; |
3626 | } | |
4adc5a41 | 3627 | |
a0a4aa52 PP |
3628 | static |
3629 | int auto_map_fields_to_trace_clock_class(struct ctx *ctx, | |
939190b3 | 3630 | struct ctf_field_class *root_fc, const char *field_name) |
a0a4aa52 PP |
3631 | { |
3632 | int ret = 0; | |
7b33a0e0 | 3633 | uint64_t i, count; |
939190b3 PP |
3634 | struct ctf_field_class_struct *struct_fc = (void *) root_fc; |
3635 | struct ctf_field_class_variant *var_fc = (void *) root_fc; | |
4adc5a41 | 3636 | |
939190b3 | 3637 | if (!root_fc) { |
a0a4aa52 PP |
3638 | goto end; |
3639 | } | |
4adc5a41 | 3640 | |
af0c18e3 PP |
3641 | if (root_fc->type != CTF_FIELD_CLASS_TYPE_STRUCT && |
3642 | root_fc->type != CTF_FIELD_CLASS_TYPE_VARIANT) { | |
a0a4aa52 PP |
3643 | goto end; |
3644 | } | |
3645 | ||
af0c18e3 | 3646 | if (root_fc->type == CTF_FIELD_CLASS_TYPE_STRUCT) { |
939190b3 | 3647 | count = struct_fc->members->len; |
a0a4aa52 | 3648 | } else { |
939190b3 | 3649 | count = var_fc->options->len; |
a0a4aa52 PP |
3650 | } |
3651 | ||
a0a4aa52 | 3652 | for (i = 0; i < count; i++) { |
939190b3 | 3653 | struct ctf_named_field_class *named_fc = NULL; |
a0a4aa52 | 3654 | |
af0c18e3 | 3655 | if (root_fc->type == CTF_FIELD_CLASS_TYPE_STRUCT) { |
939190b3 PP |
3656 | named_fc = ctf_field_class_struct_borrow_member_by_index( |
3657 | struct_fc, i); | |
af0c18e3 | 3658 | } else if (root_fc->type == CTF_FIELD_CLASS_TYPE_VARIANT) { |
939190b3 PP |
3659 | named_fc = ctf_field_class_variant_borrow_option_by_index( |
3660 | var_fc, i); | |
a0a4aa52 PP |
3661 | } |
3662 | ||
939190b3 | 3663 | if (strcmp(named_fc->name->str, field_name) == 0) { |
7b33a0e0 | 3664 | ret = auto_map_field_to_trace_clock_class(ctx, |
939190b3 | 3665 | named_fc->fc); |
a0a4aa52 PP |
3666 | if (ret) { |
3667 | BT_LOGE("Cannot automatically map field to trace's clock class: " | |
3668 | "field-name=\"%s\"", field_name); | |
a0a4aa52 | 3669 | goto end; |
4adc5a41 PP |
3670 | } |
3671 | } | |
3672 | ||
939190b3 | 3673 | ret = auto_map_fields_to_trace_clock_class(ctx, named_fc->fc, |
7b33a0e0 | 3674 | field_name); |
a0a4aa52 | 3675 | if (ret) { |
939190b3 | 3676 | BT_LOGE("Cannot automatically map structure or variant field class's fields to trace's clock class: " |
a0a4aa52 | 3677 | "field-name=\"%s\", root-field-name=\"%s\"", |
939190b3 | 3678 | field_name, named_fc->name->str); |
a0a4aa52 PP |
3679 | goto end; |
3680 | } | |
4adc5a41 PP |
3681 | } |
3682 | ||
3683 | end: | |
4adc5a41 PP |
3684 | return ret; |
3685 | } | |
3686 | ||
e98a2d6e PP |
3687 | static |
3688 | int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node, | |
7b33a0e0 | 3689 | struct ctf_stream_class *stream_class, int *set) |
e98a2d6e PP |
3690 | { |
3691 | int ret = 0; | |
3692 | char *left = NULL; | |
e98a2d6e PP |
3693 | |
3694 | switch (node->type) { | |
3695 | case NODE_TYPEDEF: | |
939190b3 PP |
3696 | ret = visit_field_class_def(ctx, node->u.field_class_def.field_class_specifier_list, |
3697 | &node->u.field_class_def.field_class_declarators); | |
e98a2d6e | 3698 | if (ret) { |
87cb75e6 | 3699 | _BT_LOGE_NODE(node, |
939190b3 | 3700 | "Cannot add field class found in stream class."); |
e98a2d6e PP |
3701 | goto error; |
3702 | } | |
3703 | break; | |
3704 | case NODE_TYPEALIAS: | |
939190b3 PP |
3705 | ret = visit_field_class_alias(ctx, node->u.field_class_alias.target, |
3706 | node->u.field_class_alias.alias); | |
e98a2d6e | 3707 | if (ret) { |
87cb75e6 | 3708 | _BT_LOGE_NODE(node, |
939190b3 | 3709 | "Cannot add field class alias found in stream class."); |
e98a2d6e PP |
3710 | goto error; |
3711 | } | |
3712 | break; | |
3713 | case NODE_CTF_EXPRESSION: | |
3714 | { | |
3715 | left = concatenate_unary_strings(&node->u.ctf_expression.left); | |
3716 | if (!left) { | |
87cb75e6 | 3717 | _BT_LOGE_NODE(node, "Cannot concatenate unary strings."); |
e98a2d6e PP |
3718 | ret = -EINVAL; |
3719 | goto error; | |
3720 | } | |
3721 | ||
3722 | if (!strcmp(left, "id")) { | |
3723 | int64_t id; | |
e98a2d6e PP |
3724 | |
3725 | if (_IS_SET(set, _STREAM_ID_SET)) { | |
87cb75e6 PP |
3726 | _BT_LOGE_DUP_ATTR(node, "id", |
3727 | "stream declaration"); | |
e98a2d6e PP |
3728 | ret = -EPERM; |
3729 | goto error; | |
3730 | } | |
3731 | ||
3732 | ret = get_unary_unsigned(&node->u.ctf_expression.right, | |
3733 | (uint64_t *) &id); | |
7b33a0e0 | 3734 | |
5eae0c3c JG |
3735 | /* Only read "id" if get_unary_unsigned() succeeded. */ |
3736 | if (ret || (!ret && id < 0)) { | |
87cb75e6 PP |
3737 | _BT_LOGE_NODE(node, |
3738 | "Unexpected unary expression for stream class's `id` attribute."); | |
e98a2d6e PP |
3739 | ret = -EINVAL; |
3740 | goto error; | |
3741 | } | |
3742 | ||
7b33a0e0 PP |
3743 | if (ctf_trace_class_borrow_stream_class_by_id( |
3744 | ctx->ctf_tc, id)) { | |
87cb75e6 PP |
3745 | _BT_LOGE_NODE(node, |
3746 | "Duplicate stream class (same ID): id=%" PRId64, | |
e98a2d6e PP |
3747 | id); |
3748 | ret = -EEXIST; | |
3749 | goto error; | |
3750 | } | |
3751 | ||
7b33a0e0 | 3752 | stream_class->id = id; |
e98a2d6e PP |
3753 | _SET(set, _STREAM_ID_SET); |
3754 | } else if (!strcmp(left, "event.header")) { | |
3755 | if (_IS_SET(set, _STREAM_EVENT_HEADER_SET)) { | |
87cb75e6 PP |
3756 | _BT_LOGE_NODE(node, |
3757 | "Duplicate `event.header` entry in stream class."); | |
e98a2d6e PP |
3758 | ret = -EPERM; |
3759 | goto error; | |
3760 | } | |
3761 | ||
939190b3 | 3762 | ret = visit_field_class_specifier_list(ctx, |
e98a2d6e PP |
3763 | _BT_LIST_FIRST_ENTRY( |
3764 | &node->u.ctf_expression.right, | |
3765 | struct ctf_node, siblings), | |
939190b3 | 3766 | &stream_class->event_header_fc); |
e98a2d6e | 3767 | if (ret) { |
87cb75e6 | 3768 | _BT_LOGE_NODE(node, |
939190b3 | 3769 | "Cannot create stream class's event header field class."); |
e98a2d6e PP |
3770 | goto error; |
3771 | } | |
3772 | ||
939190b3 | 3773 | BT_ASSERT(stream_class->event_header_fc); |
4adc5a41 | 3774 | ret = auto_map_fields_to_trace_clock_class(ctx, |
939190b3 | 3775 | stream_class->event_header_fc, "timestamp"); |
4adc5a41 PP |
3776 | if (ret) { |
3777 | _BT_LOGE_NODE(node, | |
939190b3 | 3778 | "Cannot automatically map specific event header field class fields named `timestamp` to trace's clock class."); |
4adc5a41 PP |
3779 | goto error; |
3780 | } | |
e98a2d6e | 3781 | |
e98a2d6e PP |
3782 | _SET(set, _STREAM_EVENT_HEADER_SET); |
3783 | } else if (!strcmp(left, "event.context")) { | |
3784 | if (_IS_SET(set, _STREAM_EVENT_CONTEXT_SET)) { | |
87cb75e6 PP |
3785 | _BT_LOGE_NODE(node, |
3786 | "Duplicate `event.context` entry in stream class."); | |
e98a2d6e PP |
3787 | ret = -EPERM; |
3788 | goto error; | |
3789 | } | |
3790 | ||
939190b3 | 3791 | ret = visit_field_class_specifier_list(ctx, |
e98a2d6e PP |
3792 | _BT_LIST_FIRST_ENTRY( |
3793 | &node->u.ctf_expression.right, | |
3794 | struct ctf_node, siblings), | |
939190b3 | 3795 | &stream_class->event_common_context_fc); |
e98a2d6e | 3796 | if (ret) { |
87cb75e6 | 3797 | _BT_LOGE_NODE(node, |
939190b3 | 3798 | "Cannot create stream class's event context field class."); |
e98a2d6e PP |
3799 | goto error; |
3800 | } | |
3801 | ||
939190b3 | 3802 | BT_ASSERT(stream_class->event_common_context_fc); |
e98a2d6e PP |
3803 | _SET(set, _STREAM_EVENT_CONTEXT_SET); |
3804 | } else if (!strcmp(left, "packet.context")) { | |
3805 | if (_IS_SET(set, _STREAM_PACKET_CONTEXT_SET)) { | |
87cb75e6 PP |
3806 | _BT_LOGE_NODE(node, |
3807 | "Duplicate `packet.context` entry in stream class."); | |
e98a2d6e PP |
3808 | ret = -EPERM; |
3809 | goto error; | |
3810 | } | |
3811 | ||
939190b3 | 3812 | ret = visit_field_class_specifier_list(ctx, |
e98a2d6e PP |
3813 | _BT_LIST_FIRST_ENTRY( |
3814 | &node->u.ctf_expression.right, | |
3815 | struct ctf_node, siblings), | |
939190b3 | 3816 | &stream_class->packet_context_fc); |
e98a2d6e | 3817 | if (ret) { |
87cb75e6 | 3818 | _BT_LOGE_NODE(node, |
939190b3 | 3819 | "Cannot create stream class's packet context field class."); |
e98a2d6e PP |
3820 | goto error; |
3821 | } | |
3822 | ||
939190b3 | 3823 | BT_ASSERT(stream_class->packet_context_fc); |
4adc5a41 | 3824 | ret = auto_map_fields_to_trace_clock_class(ctx, |
939190b3 | 3825 | stream_class->packet_context_fc, |
7b33a0e0 | 3826 | "timestamp_begin"); |
a0a4aa52 PP |
3827 | if (ret) { |
3828 | _BT_LOGE_NODE(node, | |
939190b3 | 3829 | "Cannot automatically map specific packet context field class fields named `timestamp_begin` to trace's clock class."); |
a0a4aa52 PP |
3830 | goto error; |
3831 | } | |
3832 | ||
3833 | ret = auto_map_fields_to_trace_clock_class(ctx, | |
939190b3 | 3834 | stream_class->packet_context_fc, |
7b33a0e0 | 3835 | "timestamp_end"); |
4adc5a41 PP |
3836 | if (ret) { |
3837 | _BT_LOGE_NODE(node, | |
939190b3 | 3838 | "Cannot automatically map specific packet context field class fields named `timestamp_end` to trace's clock class."); |
4adc5a41 PP |
3839 | goto error; |
3840 | } | |
e98a2d6e | 3841 | |
e98a2d6e PP |
3842 | _SET(set, _STREAM_PACKET_CONTEXT_SET); |
3843 | } else { | |
87cb75e6 PP |
3844 | _BT_LOGW_NODE(node, |
3845 | "Unknown attribute in stream class: " | |
37d21e07 | 3846 | "attr-name=\"%s\"", left); |
e98a2d6e PP |
3847 | } |
3848 | ||
3849 | g_free(left); | |
3850 | left = NULL; | |
3851 | break; | |
3852 | } | |
3853 | ||
3854 | default: | |
3855 | ret = -EPERM; | |
3856 | goto error; | |
3857 | } | |
3858 | ||
3859 | return 0; | |
3860 | ||
3861 | error: | |
3862 | g_free(left); | |
e98a2d6e PP |
3863 | return ret; |
3864 | } | |
3865 | ||
3866 | static | |
3867 | int visit_stream_decl(struct ctx *ctx, struct ctf_node *node) | |
3868 | { | |
e98a2d6e PP |
3869 | int set = 0; |
3870 | int ret = 0; | |
3871 | struct ctf_node *iter; | |
7b33a0e0 | 3872 | struct ctf_stream_class *stream_class = NULL; |
e98a2d6e PP |
3873 | struct bt_list_head *decl_list = &node->u.stream.declaration_list; |
3874 | ||
3875 | if (node->visited) { | |
3876 | goto end; | |
3877 | } | |
3878 | ||
3879 | node->visited = TRUE; | |
7b33a0e0 PP |
3880 | stream_class = ctf_stream_class_create(); |
3881 | BT_ASSERT(stream_class); | |
3882 | _TRY_PUSH_SCOPE_OR_GOTO_ERROR(); | |
e98a2d6e PP |
3883 | |
3884 | bt_list_for_each_entry(iter, decl_list, siblings) { | |
3885 | ret = visit_stream_decl_entry(ctx, iter, stream_class, &set); | |
3886 | if (ret) { | |
87cb75e6 PP |
3887 | _BT_LOGE_NODE(iter, |
3888 | "Cannot visit stream class's entry: " | |
3889 | "ret=%d", ret); | |
e98a2d6e PP |
3890 | ctx_pop_scope(ctx); |
3891 | goto error; | |
3892 | } | |
3893 | } | |
3894 | ||
3895 | ctx_pop_scope(ctx); | |
3896 | ||
3897 | if (_IS_SET(&set, _STREAM_ID_SET)) { | |
7b33a0e0 | 3898 | /* Check that packet header has `stream_id` field */ |
939190b3 | 3899 | struct ctf_named_field_class *named_fc = NULL; |
e98a2d6e | 3900 | |
939190b3 | 3901 | if (!ctx->ctf_tc->packet_header_fc) { |
87cb75e6 PP |
3902 | _BT_LOGE_NODE(node, |
3903 | "Stream class has a `id` attribute, " | |
939190b3 | 3904 | "but trace has no packet header field class."); |
e98a2d6e PP |
3905 | goto error; |
3906 | } | |
3907 | ||
939190b3 PP |
3908 | named_fc = ctf_field_class_struct_borrow_member_by_name( |
3909 | (void *) ctx->ctf_tc->packet_header_fc, "stream_id"); | |
3910 | if (!named_fc) { | |
87cb75e6 PP |
3911 | _BT_LOGE_NODE(node, |
3912 | "Stream class has a `id` attribute, " | |
939190b3 | 3913 | "but trace's packet header field class has no `stream_id` field."); |
e98a2d6e PP |
3914 | goto error; |
3915 | } | |
3916 | ||
af0c18e3 PP |
3917 | if (named_fc->fc->type != CTF_FIELD_CLASS_TYPE_INT && |
3918 | named_fc->fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
87cb75e6 PP |
3919 | _BT_LOGE_NODE(node, |
3920 | "Stream class has a `id` attribute, " | |
939190b3 | 3921 | "but trace's packet header field class's `stream_id` field is not an integer field class."); |
e98a2d6e PP |
3922 | goto error; |
3923 | } | |
e98a2d6e PP |
3924 | } else { |
3925 | /* Allow only _one_ ID-less stream */ | |
7b33a0e0 | 3926 | if (ctx->ctf_tc->stream_classes->len != 0) { |
87cb75e6 PP |
3927 | _BT_LOGE_NODE(node, |
3928 | "Missing `id` attribute in stream class as there's more than one stream class in the trace."); | |
e98a2d6e PP |
3929 | ret = -EPERM; |
3930 | goto error; | |
3931 | } | |
3932 | ||
3933 | /* Automatic ID: 0 */ | |
7b33a0e0 | 3934 | stream_class->id = 0; |
e98a2d6e PP |
3935 | } |
3936 | ||
1e649dff PP |
3937 | /* |
3938 | * Make sure that this stream class's ID is currently unique in | |
3939 | * the trace. | |
3940 | */ | |
7b33a0e0 PP |
3941 | if (ctf_trace_class_borrow_stream_class_by_id(ctx->ctf_tc, |
3942 | stream_class->id)) { | |
87cb75e6 PP |
3943 | _BT_LOGE_NODE(node, |
3944 | "Duplicate stream class (same ID): id=%" PRId64, | |
7b33a0e0 | 3945 | stream_class->id); |
1e649dff PP |
3946 | ret = -EINVAL; |
3947 | goto error; | |
3948 | } | |
3949 | ||
7b33a0e0 | 3950 | g_ptr_array_add(ctx->ctf_tc->stream_classes, stream_class); |
e98a2d6e | 3951 | stream_class = NULL; |
1e649dff | 3952 | goto end; |
e98a2d6e PP |
3953 | |
3954 | error: | |
7b33a0e0 PP |
3955 | ctf_stream_class_destroy(stream_class); |
3956 | stream_class = NULL; | |
e98a2d6e | 3957 | |
1e649dff | 3958 | end: |
e98a2d6e PP |
3959 | return ret; |
3960 | } | |
3961 | ||
3962 | static | |
3963 | int visit_trace_decl_entry(struct ctx *ctx, struct ctf_node *node, int *set) | |
3964 | { | |
3965 | int ret = 0; | |
3966 | char *left = NULL; | |
7b33a0e0 | 3967 | uint64_t val; |
e98a2d6e PP |
3968 | |
3969 | switch (node->type) { | |
3970 | case NODE_TYPEDEF: | |
939190b3 PP |
3971 | ret = visit_field_class_def(ctx, node->u.field_class_def.field_class_specifier_list, |
3972 | &node->u.field_class_def.field_class_declarators); | |
e98a2d6e | 3973 | if (ret) { |
87cb75e6 | 3974 | _BT_LOGE_NODE(node, |
939190b3 | 3975 | "Cannot add field class found in trace (`trace` block)."); |
e98a2d6e PP |
3976 | goto error; |
3977 | } | |
3978 | break; | |
3979 | case NODE_TYPEALIAS: | |
939190b3 PP |
3980 | ret = visit_field_class_alias(ctx, node->u.field_class_alias.target, |
3981 | node->u.field_class_alias.alias); | |
e98a2d6e | 3982 | if (ret) { |
87cb75e6 | 3983 | _BT_LOGE_NODE(node, |
939190b3 | 3984 | "Cannot add field class alias found in trace (`trace` block)."); |
e98a2d6e PP |
3985 | goto error; |
3986 | } | |
3987 | break; | |
3988 | case NODE_CTF_EXPRESSION: | |
3989 | { | |
3990 | left = concatenate_unary_strings(&node->u.ctf_expression.left); | |
3991 | if (!left) { | |
87cb75e6 | 3992 | _BT_LOGE_NODE(node, "Cannot concatenate unary strings."); |
e98a2d6e PP |
3993 | ret = -EINVAL; |
3994 | goto error; | |
3995 | } | |
3996 | ||
3997 | if (!strcmp(left, "major")) { | |
3998 | if (_IS_SET(set, _TRACE_MAJOR_SET)) { | |
87cb75e6 | 3999 | _BT_LOGE_DUP_ATTR(node, "major", "trace"); |
e98a2d6e PP |
4000 | ret = -EPERM; |
4001 | goto error; | |
4002 | } | |
4003 | ||
4004 | ret = get_unary_unsigned(&node->u.ctf_expression.right, | |
7b33a0e0 | 4005 | &val); |
e98a2d6e | 4006 | if (ret) { |
87cb75e6 PP |
4007 | _BT_LOGE_NODE(node, |
4008 | "Unexpected unary expression for trace's `major` attribute."); | |
e98a2d6e PP |
4009 | ret = -EINVAL; |
4010 | goto error; | |
4011 | } | |
4012 | ||
7b33a0e0 PP |
4013 | if (val != 1) { |
4014 | _BT_LOGE_NODE(node, | |
4015 | "Invalid trace's `minor` attribute: expecting 1."); | |
4016 | goto error; | |
4017 | } | |
4018 | ||
4019 | ctx->ctf_tc->major = val; | |
e98a2d6e PP |
4020 | _SET(set, _TRACE_MAJOR_SET); |
4021 | } else if (!strcmp(left, "minor")) { | |
4022 | if (_IS_SET(set, _TRACE_MINOR_SET)) { | |
87cb75e6 | 4023 | _BT_LOGE_DUP_ATTR(node, "minor", "trace"); |
e98a2d6e PP |
4024 | ret = -EPERM; |
4025 | goto error; | |
4026 | } | |
4027 | ||
4028 | ret = get_unary_unsigned(&node->u.ctf_expression.right, | |
7b33a0e0 | 4029 | &val); |
e98a2d6e | 4030 | if (ret) { |
87cb75e6 PP |
4031 | _BT_LOGE_NODE(node, |
4032 | "Unexpected unary expression for trace's `minor` attribute."); | |
e98a2d6e PP |
4033 | ret = -EINVAL; |
4034 | goto error; | |
4035 | } | |
4036 | ||
7b33a0e0 PP |
4037 | if (val != 8) { |
4038 | _BT_LOGE_NODE(node, | |
4039 | "Invalid trace's `minor` attribute: expecting 8."); | |
4040 | goto error; | |
4041 | } | |
4042 | ||
4043 | ctx->ctf_tc->minor = val; | |
e98a2d6e PP |
4044 | _SET(set, _TRACE_MINOR_SET); |
4045 | } else if (!strcmp(left, "uuid")) { | |
4046 | if (_IS_SET(set, _TRACE_UUID_SET)) { | |
87cb75e6 | 4047 | _BT_LOGE_DUP_ATTR(node, "uuid", "trace"); |
e98a2d6e PP |
4048 | ret = -EPERM; |
4049 | goto error; | |
4050 | } | |
4051 | ||
4052 | ret = get_unary_uuid(&node->u.ctf_expression.right, | |
7b33a0e0 | 4053 | ctx->ctf_tc->uuid); |
e98a2d6e | 4054 | if (ret) { |
87cb75e6 PP |
4055 | _BT_LOGE_NODE(node, |
4056 | "Invalid trace's `uuid` attribute."); | |
e98a2d6e PP |
4057 | goto error; |
4058 | } | |
4059 | ||
7b33a0e0 | 4060 | ctx->ctf_tc->is_uuid_set = true; |
e98a2d6e PP |
4061 | _SET(set, _TRACE_UUID_SET); |
4062 | } else if (!strcmp(left, "byte_order")) { | |
7b33a0e0 | 4063 | /* Default byte order is already known at this stage */ |
e98a2d6e | 4064 | if (_IS_SET(set, _TRACE_BYTE_ORDER_SET)) { |
87cb75e6 | 4065 | _BT_LOGE_DUP_ATTR(node, "byte_order", |
37d21e07 | 4066 | "trace"); |
e98a2d6e PP |
4067 | ret = -EPERM; |
4068 | goto error; | |
4069 | } | |
4070 | ||
7b33a0e0 | 4071 | BT_ASSERT(ctx->ctf_tc->default_byte_order != -1); |
e98a2d6e PP |
4072 | _SET(set, _TRACE_BYTE_ORDER_SET); |
4073 | } else if (!strcmp(left, "packet.header")) { | |
4074 | if (_IS_SET(set, _TRACE_PACKET_HEADER_SET)) { | |
87cb75e6 PP |
4075 | _BT_LOGE_NODE(node, |
4076 | "Duplicate `packet.header` entry in trace."); | |
e98a2d6e PP |
4077 | ret = -EPERM; |
4078 | goto error; | |
4079 | } | |
4080 | ||
939190b3 | 4081 | ret = visit_field_class_specifier_list(ctx, |
e98a2d6e PP |
4082 | _BT_LIST_FIRST_ENTRY( |
4083 | &node->u.ctf_expression.right, | |
4084 | struct ctf_node, siblings), | |
939190b3 | 4085 | &ctx->ctf_tc->packet_header_fc); |
e98a2d6e | 4086 | if (ret) { |
87cb75e6 | 4087 | _BT_LOGE_NODE(node, |
939190b3 | 4088 | "Cannot create trace's packet header field class."); |
e98a2d6e PP |
4089 | goto error; |
4090 | } | |
4091 | ||
939190b3 | 4092 | BT_ASSERT(ctx->ctf_tc->packet_header_fc); |
e98a2d6e PP |
4093 | _SET(set, _TRACE_PACKET_HEADER_SET); |
4094 | } else { | |
87cb75e6 PP |
4095 | _BT_LOGW_NODE(node, |
4096 | "Unknown attribute in stream class: " | |
37d21e07 | 4097 | "attr-name=\"%s\"", left); |
e98a2d6e PP |
4098 | } |
4099 | ||
4100 | g_free(left); | |
4101 | left = NULL; | |
4102 | break; | |
4103 | } | |
4104 | default: | |
87cb75e6 | 4105 | _BT_LOGE_NODE(node, "Unknown expression in trace."); |
e98a2d6e PP |
4106 | ret = -EINVAL; |
4107 | goto error; | |
4108 | } | |
4109 | ||
4110 | return 0; | |
4111 | ||
4112 | error: | |
4113 | g_free(left); | |
e98a2d6e PP |
4114 | return ret; |
4115 | } | |
4116 | ||
4117 | static | |
4118 | int visit_trace_decl(struct ctx *ctx, struct ctf_node *node) | |
4119 | { | |
4120 | int ret = 0; | |
4121 | int set = 0; | |
4122 | struct ctf_node *iter; | |
4123 | struct bt_list_head *decl_list = &node->u.trace.declaration_list; | |
4124 | ||
4125 | if (node->visited) { | |
4126 | goto end; | |
4127 | } | |
4128 | ||
4129 | node->visited = TRUE; | |
4130 | ||
4131 | if (ctx->is_trace_visited) { | |
87cb75e6 | 4132 | _BT_LOGE_NODE(node, "Duplicate trace (`trace` block)."); |
e98a2d6e PP |
4133 | ret = -EEXIST; |
4134 | goto error; | |
4135 | } | |
4136 | ||
7b33a0e0 | 4137 | _TRY_PUSH_SCOPE_OR_GOTO_ERROR(); |
e98a2d6e PP |
4138 | |
4139 | bt_list_for_each_entry(iter, decl_list, siblings) { | |
4140 | ret = visit_trace_decl_entry(ctx, iter, &set); | |
4141 | if (ret) { | |
87cb75e6 PP |
4142 | _BT_LOGE_NODE(iter, "Cannot visit trace's entry (`trace` block): " |
4143 | "ret=%d", ret); | |
e98a2d6e PP |
4144 | ctx_pop_scope(ctx); |
4145 | goto error; | |
4146 | } | |
4147 | } | |
4148 | ||
4149 | ctx_pop_scope(ctx); | |
4150 | ||
4151 | if (!_IS_SET(&set, _TRACE_MAJOR_SET)) { | |
87cb75e6 PP |
4152 | _BT_LOGE_NODE(node, |
4153 | "Missing `major` attribute in trace (`trace` block)."); | |
e98a2d6e PP |
4154 | ret = -EPERM; |
4155 | goto error; | |
4156 | } | |
4157 | ||
4158 | if (!_IS_SET(&set, _TRACE_MINOR_SET)) { | |
87cb75e6 PP |
4159 | _BT_LOGE_NODE(node, |
4160 | "Missing `minor` attribute in trace (`trace` block)."); | |
e98a2d6e PP |
4161 | ret = -EPERM; |
4162 | goto error; | |
4163 | } | |
4164 | ||
4165 | if (!_IS_SET(&set, _TRACE_BYTE_ORDER_SET)) { | |
87cb75e6 PP |
4166 | _BT_LOGE_NODE(node, |
4167 | "Missing `byte_order` attribute in trace (`trace` block)."); | |
e98a2d6e PP |
4168 | ret = -EPERM; |
4169 | goto error; | |
4170 | } | |
4171 | ||
7b33a0e0 | 4172 | ctx->is_trace_visited = true; |
e98a2d6e PP |
4173 | |
4174 | end: | |
4175 | return 0; | |
4176 | ||
4177 | error: | |
4178 | return ret; | |
4179 | } | |
4180 | ||
4181 | static | |
4182 | int visit_env(struct ctx *ctx, struct ctf_node *node) | |
4183 | { | |
4184 | int ret = 0; | |
4185 | char *left = NULL; | |
4186 | struct ctf_node *entry_node; | |
4187 | struct bt_list_head *decl_list = &node->u.env.declaration_list; | |
4188 | ||
4189 | if (node->visited) { | |
4190 | goto end; | |
4191 | } | |
4192 | ||
4193 | node->visited = TRUE; | |
4194 | ||
4195 | bt_list_for_each_entry(entry_node, decl_list, siblings) { | |
4196 | struct bt_list_head *right_head = | |
4197 | &entry_node->u.ctf_expression.right; | |
4198 | ||
4199 | if (entry_node->type != NODE_CTF_EXPRESSION) { | |
87cb75e6 PP |
4200 | _BT_LOGE_NODE(entry_node, |
4201 | "Wrong expression in environment entry: " | |
37d21e07 | 4202 | "node-type=%d", entry_node->type); |
e98a2d6e PP |
4203 | ret = -EPERM; |
4204 | goto error; | |
4205 | } | |
4206 | ||
4207 | left = concatenate_unary_strings( | |
4208 | &entry_node->u.ctf_expression.left); | |
4209 | if (!left) { | |
87cb75e6 PP |
4210 | _BT_LOGE_NODE(entry_node, |
4211 | "Cannot get environment entry's name."); | |
e98a2d6e PP |
4212 | ret = -EINVAL; |
4213 | goto error; | |
4214 | } | |
4215 | ||
4216 | if (is_unary_string(right_head)) { | |
4217 | char *right = concatenate_unary_strings(right_head); | |
4218 | ||
4219 | if (!right) { | |
87cb75e6 PP |
4220 | _BT_LOGE_NODE(entry_node, |
4221 | "Unexpected unary expression for environment entry's value: " | |
37d21e07 | 4222 | "name=\"%s\"", left); |
e98a2d6e PP |
4223 | ret = -EINVAL; |
4224 | goto error; | |
4225 | } | |
4226 | ||
a0a4aa52 PP |
4227 | if (strcmp(left, "tracer_name") == 0) { |
4228 | if (strncmp(right, "lttng", 5) == 0) { | |
4229 | BT_LOGI("Detected LTTng trace from `%s` environment value: " | |
4230 | "tracer-name=\"%s\"", | |
4231 | left, right); | |
7b33a0e0 | 4232 | ctx->is_lttng = true; |
f4421ff3 PP |
4233 | } |
4234 | } | |
4235 | ||
7b33a0e0 PP |
4236 | ctf_trace_class_append_env_entry(ctx->ctf_tc, |
4237 | left, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR, | |
4238 | right, 0); | |
e98a2d6e | 4239 | g_free(right); |
e98a2d6e PP |
4240 | } else if (is_unary_unsigned(right_head) || |
4241 | is_unary_signed(right_head)) { | |
4242 | int64_t v; | |
4243 | ||
4244 | if (is_unary_unsigned(right_head)) { | |
4245 | ret = get_unary_unsigned(right_head, | |
4246 | (uint64_t *) &v); | |
4247 | } else { | |
4248 | ret = get_unary_signed(right_head, &v); | |
4249 | } | |
4250 | if (ret) { | |
87cb75e6 PP |
4251 | _BT_LOGE_NODE(entry_node, |
4252 | "Unexpected unary expression for environment entry's value: " | |
37d21e07 | 4253 | "name=\"%s\"", left); |
e98a2d6e PP |
4254 | ret = -EINVAL; |
4255 | goto error; | |
4256 | } | |
4257 | ||
7b33a0e0 PP |
4258 | ctf_trace_class_append_env_entry(ctx->ctf_tc, |
4259 | left, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT, | |
4260 | NULL, v); | |
e98a2d6e | 4261 | } else { |
87cb75e6 PP |
4262 | _BT_LOGW_NODE(entry_node, |
4263 | "Environment entry has unknown type: " | |
37d21e07 | 4264 | "name=\"%s\"", left); |
e98a2d6e PP |
4265 | } |
4266 | ||
4267 | g_free(left); | |
4268 | left = NULL; | |
4269 | } | |
4270 | ||
4271 | end: | |
4272 | return 0; | |
4273 | ||
4274 | error: | |
4275 | g_free(left); | |
e98a2d6e PP |
4276 | return ret; |
4277 | } | |
4278 | ||
4279 | static | |
4280 | int set_trace_byte_order(struct ctx *ctx, struct ctf_node *trace_node) | |
4281 | { | |
4282 | int ret = 0; | |
4283 | int set = 0; | |
4284 | char *left = NULL; | |
4285 | struct ctf_node *node; | |
4286 | struct bt_list_head *decl_list = &trace_node->u.trace.declaration_list; | |
4287 | ||
4288 | bt_list_for_each_entry(node, decl_list, siblings) { | |
4289 | if (node->type == NODE_CTF_EXPRESSION) { | |
4290 | struct ctf_node *right_node; | |
4291 | ||
4292 | left = concatenate_unary_strings( | |
4293 | &node->u.ctf_expression.left); | |
4294 | if (!left) { | |
87cb75e6 PP |
4295 | _BT_LOGE_NODE(node, |
4296 | "Cannot concatenate unary strings."); | |
e98a2d6e PP |
4297 | ret = -EINVAL; |
4298 | goto error; | |
4299 | } | |
4300 | ||
4301 | if (!strcmp(left, "byte_order")) { | |
7b33a0e0 | 4302 | enum ctf_byte_order bo; |
e98a2d6e PP |
4303 | |
4304 | if (_IS_SET(&set, _TRACE_BYTE_ORDER_SET)) { | |
87cb75e6 | 4305 | _BT_LOGE_DUP_ATTR(node, "byte_order", |
37d21e07 | 4306 | "trace"); |
e98a2d6e PP |
4307 | ret = -EPERM; |
4308 | goto error; | |
4309 | } | |
4310 | ||
4311 | _SET(&set, _TRACE_BYTE_ORDER_SET); | |
4312 | right_node = _BT_LIST_FIRST_ENTRY( | |
4313 | &node->u.ctf_expression.right, | |
4314 | struct ctf_node, siblings); | |
55314f2a | 4315 | bo = byte_order_from_unary_expr(right_node); |
7b33a0e0 | 4316 | if (bo == -1) { |
87cb75e6 PP |
4317 | _BT_LOGE_NODE(node, |
4318 | "Invalid `byte_order` attribute in trace (`trace` block): " | |
4319 | "expecting `le`, `be`, or `network`."); | |
e98a2d6e PP |
4320 | ret = -EINVAL; |
4321 | goto error; | |
7b33a0e0 | 4322 | } else if (bo == CTF_BYTE_ORDER_DEFAULT) { |
87cb75e6 PP |
4323 | _BT_LOGE_NODE(node, |
4324 | "Invalid `byte_order` attribute in trace (`trace` block): " | |
37d21e07 | 4325 | "cannot be set to `native` here."); |
e98a2d6e PP |
4326 | ret = -EPERM; |
4327 | goto error; | |
4328 | } | |
4329 | ||
7b33a0e0 | 4330 | ctx->ctf_tc->default_byte_order = bo; |
e98a2d6e PP |
4331 | } |
4332 | ||
4333 | g_free(left); | |
4334 | left = NULL; | |
4335 | } | |
4336 | } | |
4337 | ||
4338 | if (!_IS_SET(&set, _TRACE_BYTE_ORDER_SET)) { | |
87cb75e6 PP |
4339 | _BT_LOGE_NODE(trace_node, |
4340 | "Missing `byte_order` attribute in trace (`trace` block)."); | |
e98a2d6e PP |
4341 | ret = -EINVAL; |
4342 | goto error; | |
4343 | } | |
4344 | ||
4345 | return 0; | |
4346 | ||
4347 | error: | |
4348 | g_free(left); | |
e98a2d6e PP |
4349 | return ret; |
4350 | } | |
4351 | ||
4352 | static | |
4353 | int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, | |
35fa110e | 4354 | struct ctf_clock_class *clock, int *set, int64_t *offset_seconds, |
7b33a0e0 | 4355 | uint64_t *offset_cycles) |
e98a2d6e PP |
4356 | { |
4357 | int ret = 0; | |
4358 | char *left = NULL; | |
4359 | ||
4360 | if (entry_node->type != NODE_CTF_EXPRESSION) { | |
87cb75e6 PP |
4361 | _BT_LOGE_NODE(entry_node, |
4362 | "Unexpected node type: node-type=%d", | |
4363 | entry_node->type); | |
e98a2d6e PP |
4364 | ret = -EPERM; |
4365 | goto error; | |
4366 | } | |
4367 | ||
4368 | left = concatenate_unary_strings(&entry_node->u.ctf_expression.left); | |
4369 | if (!left) { | |
87cb75e6 | 4370 | _BT_LOGE_NODE(entry_node, "Cannot concatenate unary strings."); |
e98a2d6e PP |
4371 | ret = -EINVAL; |
4372 | goto error; | |
4373 | } | |
4374 | ||
4375 | if (!strcmp(left, "name")) { | |
4376 | char *right; | |
4377 | ||
4378 | if (_IS_SET(set, _CLOCK_NAME_SET)) { | |
87cb75e6 | 4379 | _BT_LOGE_DUP_ATTR(entry_node, "name", "clock class"); |
e98a2d6e PP |
4380 | ret = -EPERM; |
4381 | goto error; | |
4382 | } | |
4383 | ||
4384 | right = concatenate_unary_strings( | |
4385 | &entry_node->u.ctf_expression.right); | |
4386 | if (!right) { | |
87cb75e6 PP |
4387 | _BT_LOGE_NODE(entry_node, |
4388 | "Unexpected unary expression for clock class's `name` attribute."); | |
e98a2d6e PP |
4389 | ret = -EINVAL; |
4390 | goto error; | |
4391 | } | |
4392 | ||
35fa110e | 4393 | g_string_assign(clock->name, right); |
e98a2d6e PP |
4394 | g_free(right); |
4395 | _SET(set, _CLOCK_NAME_SET); | |
4396 | } else if (!strcmp(left, "uuid")) { | |
7b33a0e0 | 4397 | uint8_t uuid[BABELTRACE_UUID_LEN]; |
e98a2d6e PP |
4398 | |
4399 | if (_IS_SET(set, _CLOCK_UUID_SET)) { | |
87cb75e6 | 4400 | _BT_LOGE_DUP_ATTR(entry_node, "uuid", "clock class"); |
e98a2d6e PP |
4401 | ret = -EPERM; |
4402 | goto error; | |
4403 | } | |
4404 | ||
4405 | ret = get_unary_uuid(&entry_node->u.ctf_expression.right, uuid); | |
4406 | if (ret) { | |
87cb75e6 PP |
4407 | _BT_LOGE_NODE(entry_node, |
4408 | "Invalid clock class's `uuid` attribute."); | |
e98a2d6e PP |
4409 | goto error; |
4410 | } | |
4411 | ||
35fa110e PP |
4412 | clock->has_uuid = true; |
4413 | memcpy(&clock->uuid[0], uuid, 16); | |
e98a2d6e PP |
4414 | _SET(set, _CLOCK_UUID_SET); |
4415 | } else if (!strcmp(left, "description")) { | |
4416 | char *right; | |
4417 | ||
4418 | if (_IS_SET(set, _CLOCK_DESCRIPTION_SET)) { | |
87cb75e6 PP |
4419 | _BT_LOGE_DUP_ATTR(entry_node, "description", |
4420 | "clock class"); | |
e98a2d6e PP |
4421 | ret = -EPERM; |
4422 | goto error; | |
4423 | } | |
4424 | ||
4425 | right = concatenate_unary_strings( | |
4426 | &entry_node->u.ctf_expression.right); | |
4427 | if (!right) { | |
87cb75e6 PP |
4428 | _BT_LOGE_NODE(entry_node, |
4429 | "Unexpected unary expression for clock class's `description` attribute."); | |
e98a2d6e PP |
4430 | ret = -EINVAL; |
4431 | goto error; | |
4432 | } | |
4433 | ||
35fa110e | 4434 | g_string_assign(clock->description, right); |
e98a2d6e PP |
4435 | g_free(right); |
4436 | _SET(set, _CLOCK_DESCRIPTION_SET); | |
4437 | } else if (!strcmp(left, "freq")) { | |
7b33a0e0 | 4438 | uint64_t freq = UINT64_C(-1); |
e98a2d6e PP |
4439 | |
4440 | if (_IS_SET(set, _CLOCK_FREQ_SET)) { | |
87cb75e6 | 4441 | _BT_LOGE_DUP_ATTR(entry_node, "freq", "clock class"); |
e98a2d6e PP |
4442 | ret = -EPERM; |
4443 | goto error; | |
4444 | } | |
4445 | ||
4446 | ret = get_unary_unsigned( | |
4447 | &entry_node->u.ctf_expression.right, &freq); | |
4448 | if (ret) { | |
87cb75e6 PP |
4449 | _BT_LOGE_NODE(entry_node, |
4450 | "Unexpected unary expression for clock class's `freq` attribute."); | |
e98a2d6e PP |
4451 | ret = -EINVAL; |
4452 | goto error; | |
4453 | } | |
4454 | ||
7b33a0e0 | 4455 | if (freq == UINT64_C(-1) || freq == 0) { |
e31e3b2a PP |
4456 | _BT_LOGE_NODE(entry_node, |
4457 | "Invalid clock class frequency: freq=%" PRIu64, | |
4458 | freq); | |
4459 | ret = -EINVAL; | |
4460 | goto error; | |
4461 | } | |
4462 | ||
35fa110e | 4463 | clock->frequency = freq; |
e98a2d6e PP |
4464 | _SET(set, _CLOCK_FREQ_SET); |
4465 | } else if (!strcmp(left, "precision")) { | |
4466 | uint64_t precision; | |
4467 | ||
4468 | if (_IS_SET(set, _CLOCK_PRECISION_SET)) { | |
87cb75e6 PP |
4469 | _BT_LOGE_DUP_ATTR(entry_node, "precision", |
4470 | "clock class"); | |
e98a2d6e PP |
4471 | ret = -EPERM; |
4472 | goto error; | |
4473 | } | |
4474 | ||
4475 | ret = get_unary_unsigned( | |
4476 | &entry_node->u.ctf_expression.right, &precision); | |
4477 | if (ret) { | |
87cb75e6 PP |
4478 | _BT_LOGE_NODE(entry_node, |
4479 | "Unexpected unary expression for clock class's `precision` attribute."); | |
e98a2d6e PP |
4480 | ret = -EINVAL; |
4481 | goto error; | |
4482 | } | |
4483 | ||
35fa110e | 4484 | clock->precision = precision; |
e98a2d6e PP |
4485 | _SET(set, _CLOCK_PRECISION_SET); |
4486 | } else if (!strcmp(left, "offset_s")) { | |
e98a2d6e | 4487 | if (_IS_SET(set, _CLOCK_OFFSET_S_SET)) { |
87cb75e6 PP |
4488 | _BT_LOGE_DUP_ATTR(entry_node, "offset_s", |
4489 | "clock class"); | |
e98a2d6e PP |
4490 | ret = -EPERM; |
4491 | goto error; | |
4492 | } | |
4493 | ||
69610492 | 4494 | ret = get_unary_signed( |
7b33a0e0 | 4495 | &entry_node->u.ctf_expression.right, offset_seconds); |
e98a2d6e | 4496 | if (ret) { |
87cb75e6 PP |
4497 | _BT_LOGE_NODE(entry_node, |
4498 | "Unexpected unary expression for clock class's `offset_s` attribute."); | |
e98a2d6e PP |
4499 | ret = -EINVAL; |
4500 | goto error; | |
4501 | } | |
4502 | ||
e98a2d6e PP |
4503 | _SET(set, _CLOCK_OFFSET_S_SET); |
4504 | } else if (!strcmp(left, "offset")) { | |
e98a2d6e | 4505 | if (_IS_SET(set, _CLOCK_OFFSET_SET)) { |
87cb75e6 | 4506 | _BT_LOGE_DUP_ATTR(entry_node, "offset", "clock class"); |
e98a2d6e PP |
4507 | ret = -EPERM; |
4508 | goto error; | |
4509 | } | |
4510 | ||
7b33a0e0 PP |
4511 | ret = get_unary_unsigned( |
4512 | &entry_node->u.ctf_expression.right, offset_cycles); | |
e98a2d6e | 4513 | if (ret) { |
87cb75e6 PP |
4514 | _BT_LOGE_NODE(entry_node, |
4515 | "Unexpected unary expression for clock class's `offset` attribute."); | |
e98a2d6e PP |
4516 | ret = -EINVAL; |
4517 | goto error; | |
4518 | } | |
4519 | ||
e98a2d6e PP |
4520 | _SET(set, _CLOCK_OFFSET_SET); |
4521 | } else if (!strcmp(left, "absolute")) { | |
4522 | struct ctf_node *right; | |
4523 | ||
4524 | if (_IS_SET(set, _CLOCK_ABSOLUTE_SET)) { | |
87cb75e6 PP |
4525 | _BT_LOGE_DUP_ATTR(entry_node, "absolute", |
4526 | "clock class"); | |
e98a2d6e PP |
4527 | ret = -EPERM; |
4528 | goto error; | |
4529 | } | |
4530 | ||
4531 | right = _BT_LIST_FIRST_ENTRY( | |
4532 | &entry_node->u.ctf_expression.right, | |
4533 | struct ctf_node, siblings); | |
55314f2a | 4534 | ret = get_boolean(right); |
e98a2d6e | 4535 | if (ret < 0) { |
87cb75e6 PP |
4536 | _BT_LOGE_NODE(entry_node, |
4537 | "Unexpected unary expression for clock class's `absolute` attribute."); | |
e98a2d6e PP |
4538 | ret = -EINVAL; |
4539 | goto error; | |
4540 | } | |
4541 | ||
35fa110e | 4542 | clock->is_absolute = ret; |
e98a2d6e PP |
4543 | _SET(set, _CLOCK_ABSOLUTE_SET); |
4544 | } else { | |
87cb75e6 PP |
4545 | _BT_LOGW_NODE(entry_node, |
4546 | "Unknown attribute in clock class: attr-name=\"%s\"", | |
e98a2d6e PP |
4547 | left); |
4548 | } | |
4549 | ||
4550 | g_free(left); | |
4551 | left = NULL; | |
e98a2d6e PP |
4552 | return 0; |
4553 | ||
4554 | error: | |
4555 | g_free(left); | |
e98a2d6e PP |
4556 | return ret; |
4557 | } | |
4558 | ||
7b33a0e0 PP |
4559 | static inline |
4560 | uint64_t cycles_from_ns(uint64_t frequency, uint64_t ns) | |
92540773 | 4561 | { |
7b33a0e0 | 4562 | uint64_t cycles; |
92540773 JD |
4563 | |
4564 | /* 1GHz */ | |
7b33a0e0 | 4565 | if (frequency == UINT64_C(1000000000)) { |
92540773 JD |
4566 | cycles = ns; |
4567 | } else { | |
4568 | cycles = (uint64_t) (((double) ns * (double) frequency) / 1e9); | |
4569 | } | |
4570 | ||
4571 | return cycles; | |
4572 | } | |
4573 | ||
4574 | static | |
7b33a0e0 PP |
4575 | void calibrate_clock_class_offsets(int64_t *offset_seconds, |
4576 | uint64_t *offset_cycles, uint64_t freq) | |
4577 | { | |
4578 | if (*offset_cycles >= freq) { | |
4579 | const uint64_t s_in_offset_cycles = *offset_cycles / freq; | |
4580 | ||
4581 | *offset_seconds += (int64_t) s_in_offset_cycles; | |
4582 | *offset_cycles -= (s_in_offset_cycles * freq); | |
4583 | } | |
4584 | } | |
4585 | ||
4586 | static | |
9e550e5f | 4587 | void apply_clock_class_offset(struct ctx *ctx, |
35fa110e | 4588 | struct ctf_clock_class *clock) |
92540773 | 4589 | { |
92540773 | 4590 | uint64_t freq; |
7b33a0e0 PP |
4591 | int64_t offset_s_to_apply = ctx->decoder_config.clock_class_offset_s; |
4592 | uint64_t offset_ns_to_apply; | |
4593 | int64_t cur_offset_s; | |
4594 | uint64_t cur_offset_cycles; | |
92540773 | 4595 | |
7b33a0e0 PP |
4596 | if (ctx->decoder_config.clock_class_offset_s == 0 && |
4597 | ctx->decoder_config.clock_class_offset_ns == 0) { | |
92540773 JD |
4598 | goto end; |
4599 | } | |
4600 | ||
7b33a0e0 PP |
4601 | /* Transfer nanoseconds to seconds as much as possible */ |
4602 | if (ctx->decoder_config.clock_class_offset_ns < 0) { | |
4603 | const int64_t abs_ns = -ctx->decoder_config.clock_class_offset_ns; | |
4604 | const int64_t abs_extra_s = abs_ns / INT64_C(1000000000) + 1; | |
4605 | const int64_t extra_s = -abs_extra_s; | |
4606 | const int64_t offset_ns = ctx->decoder_config.clock_class_offset_ns - | |
4607 | (extra_s * INT64_C(1000000000)); | |
4608 | ||
4609 | BT_ASSERT(offset_ns > 0); | |
4610 | offset_ns_to_apply = (uint64_t) offset_ns; | |
4611 | offset_s_to_apply += extra_s; | |
4612 | } else { | |
4613 | const int64_t extra_s = ctx->decoder_config.clock_class_offset_ns / | |
4614 | INT64_C(1000000000); | |
4615 | const int64_t offset_ns = ctx->decoder_config.clock_class_offset_ns - | |
4616 | (extra_s * INT64_C(1000000000)); | |
4617 | ||
4618 | BT_ASSERT(offset_ns >= 0); | |
4619 | offset_ns_to_apply = (uint64_t) offset_ns; | |
4620 | offset_s_to_apply += extra_s; | |
92540773 JD |
4621 | } |
4622 | ||
35fa110e PP |
4623 | freq = clock->frequency; |
4624 | cur_offset_s = clock->offset_seconds; | |
4625 | cur_offset_cycles = clock->offset_cycles; | |
7b33a0e0 PP |
4626 | |
4627 | /* Apply offsets */ | |
4628 | cur_offset_s += offset_s_to_apply; | |
4629 | cur_offset_cycles += cycles_from_ns(freq, offset_ns_to_apply); | |
4630 | ||
4631 | /* | |
4632 | * Recalibrate offsets because the part in cycles can be greater | |
4633 | * than the frequency at this point. | |
4634 | */ | |
4635 | calibrate_clock_class_offsets(&cur_offset_s, &cur_offset_cycles, freq); | |
4636 | ||
4637 | /* Set final offsets */ | |
35fa110e PP |
4638 | clock->offset_seconds = cur_offset_s; |
4639 | clock->offset_cycles = cur_offset_cycles; | |
92540773 JD |
4640 | |
4641 | end: | |
7b33a0e0 | 4642 | return; |
92540773 JD |
4643 | } |
4644 | ||
4645 | static | |
1e649dff | 4646 | int visit_clock_decl(struct ctx *ctx, struct ctf_node *clock_node) |
e98a2d6e PP |
4647 | { |
4648 | int ret = 0; | |
4649 | int set = 0; | |
35fa110e | 4650 | struct ctf_clock_class *clock; |
e98a2d6e PP |
4651 | struct ctf_node *entry_node; |
4652 | struct bt_list_head *decl_list = &clock_node->u.clock.declaration_list; | |
f4421ff3 | 4653 | const char *clock_class_name; |
7b33a0e0 PP |
4654 | int64_t offset_seconds = 0; |
4655 | uint64_t offset_cycles = 0; | |
4656 | uint64_t freq; | |
e98a2d6e PP |
4657 | |
4658 | if (clock_node->visited) { | |
4659 | return 0; | |
4660 | } | |
4661 | ||
4662 | clock_node->visited = TRUE; | |
15260cc8 PP |
4663 | |
4664 | /* CTF 1.8's default frequency for a clock class is 1 GHz */ | |
35fa110e | 4665 | clock = ctf_clock_class_create(); |
e98a2d6e | 4666 | if (!clock) { |
87cb75e6 PP |
4667 | _BT_LOGE_NODE(clock_node, |
4668 | "Cannot create default clock class."); | |
e98a2d6e | 4669 | ret = -ENOMEM; |
7b33a0e0 PP |
4670 | goto end; |
4671 | } | |
4672 | ||
e98a2d6e | 4673 | bt_list_for_each_entry(entry_node, decl_list, siblings) { |
7b33a0e0 PP |
4674 | ret = visit_clock_decl_entry(ctx, entry_node, clock, &set, |
4675 | &offset_seconds, &offset_cycles); | |
e98a2d6e | 4676 | if (ret) { |
87cb75e6 PP |
4677 | _BT_LOGE_NODE(entry_node, |
4678 | "Cannot visit clock class's entry: ret=%d", | |
4679 | ret); | |
7b33a0e0 | 4680 | goto end; |
e98a2d6e PP |
4681 | } |
4682 | } | |
4683 | ||
4684 | if (!_IS_SET(&set, _CLOCK_NAME_SET)) { | |
87cb75e6 PP |
4685 | _BT_LOGE_NODE(clock_node, |
4686 | "Missing `name` attribute in clock class."); | |
e98a2d6e | 4687 | ret = -EPERM; |
7b33a0e0 | 4688 | goto end; |
e98a2d6e PP |
4689 | } |
4690 | ||
35fa110e | 4691 | clock_class_name = clock->name->str; |
8b45963b | 4692 | BT_ASSERT(clock_class_name); |
f4421ff3 PP |
4693 | if (ctx->is_lttng && strcmp(clock_class_name, "monotonic") == 0) { |
4694 | /* | |
4695 | * Old versions of LTTng forgot to set its clock class | |
4696 | * as absolute, even if it is. This is important because | |
b09a5592 | 4697 | * it's a condition to be able to sort messages |
f4421ff3 PP |
4698 | * from different sources. |
4699 | */ | |
35fa110e | 4700 | clock->is_absolute = true; |
e98a2d6e PP |
4701 | } |
4702 | ||
7b33a0e0 PP |
4703 | /* |
4704 | * Adjust offsets so that the part in cycles is less than the | |
4705 | * frequency (move to the part in seconds). | |
4706 | */ | |
35fa110e | 4707 | freq = clock->frequency; |
7b33a0e0 | 4708 | calibrate_clock_class_offsets(&offset_seconds, &offset_cycles, freq); |
35fa110e PP |
4709 | BT_ASSERT(offset_cycles < clock->frequency); |
4710 | clock->offset_seconds = offset_seconds; | |
4711 | clock->offset_cycles = offset_cycles; | |
7b33a0e0 | 4712 | apply_clock_class_offset(ctx, clock); |
4b70020d | 4713 | g_ptr_array_add(ctx->ctf_tc->clock_classes, clock); |
35fa110e | 4714 | clock = NULL; |
e98a2d6e | 4715 | |
7b33a0e0 | 4716 | end: |
35fa110e PP |
4717 | if (clock) { |
4718 | ctf_clock_class_destroy(clock); | |
4719 | } | |
4720 | ||
e98a2d6e PP |
4721 | return ret; |
4722 | } | |
4723 | ||
4724 | static | |
4725 | int visit_root_decl(struct ctx *ctx, struct ctf_node *root_decl_node) | |
4726 | { | |
4727 | int ret = 0; | |
4728 | ||
4729 | if (root_decl_node->visited) { | |
4730 | goto end; | |
4731 | } | |
4732 | ||
4733 | root_decl_node->visited = TRUE; | |
4734 | ||
4735 | switch (root_decl_node->type) { | |
4736 | case NODE_TYPEDEF: | |
939190b3 PP |
4737 | ret = visit_field_class_def(ctx, |
4738 | root_decl_node->u.field_class_def.field_class_specifier_list, | |
4739 | &root_decl_node->u.field_class_def.field_class_declarators); | |
e98a2d6e | 4740 | if (ret) { |
87cb75e6 | 4741 | _BT_LOGE_NODE(root_decl_node, |
939190b3 | 4742 | "Cannot add field class found in root scope."); |
e98a2d6e PP |
4743 | goto end; |
4744 | } | |
4745 | break; | |
4746 | case NODE_TYPEALIAS: | |
939190b3 PP |
4747 | ret = visit_field_class_alias(ctx, root_decl_node->u.field_class_alias.target, |
4748 | root_decl_node->u.field_class_alias.alias); | |
e98a2d6e | 4749 | if (ret) { |
87cb75e6 | 4750 | _BT_LOGE_NODE(root_decl_node, |
939190b3 | 4751 | "Cannot add field class alias found in root scope."); |
e98a2d6e PP |
4752 | goto end; |
4753 | } | |
4754 | break; | |
4755 | case NODE_TYPE_SPECIFIER_LIST: | |
4756 | { | |
939190b3 | 4757 | struct ctf_field_class *decl = NULL; |
e98a2d6e PP |
4758 | |
4759 | /* | |
939190b3 | 4760 | * Just add the field class specifier to the root |
e98a2d6e PP |
4761 | * declaration scope. Put local reference. |
4762 | */ | |
939190b3 | 4763 | ret = visit_field_class_specifier_list(ctx, root_decl_node, &decl); |
e98a2d6e | 4764 | if (ret) { |
87cb75e6 | 4765 | _BT_LOGE_NODE(root_decl_node, |
939190b3 | 4766 | "Cannot visit root scope's field class: " |
87cb75e6 | 4767 | "ret=%d", ret); |
8b45963b | 4768 | BT_ASSERT(!decl); |
e98a2d6e PP |
4769 | goto end; |
4770 | } | |
4771 | ||
939190b3 | 4772 | ctf_field_class_destroy(decl); |
7b33a0e0 | 4773 | decl = NULL; |
e98a2d6e PP |
4774 | break; |
4775 | } | |
4776 | default: | |
87cb75e6 PP |
4777 | _BT_LOGE_NODE(root_decl_node, |
4778 | "Unexpected node type: node-type=%d", | |
4779 | root_decl_node->type); | |
e98a2d6e PP |
4780 | ret = -EPERM; |
4781 | goto end; | |
4782 | } | |
4783 | ||
4784 | end: | |
4785 | return ret; | |
4786 | } | |
4787 | ||
1e649dff | 4788 | BT_HIDDEN |
55314f2a | 4789 | struct ctf_visitor_generate_ir *ctf_visitor_generate_ir_create( |
e5e71bf8 | 4790 | bt_self_component_source *self_comp, |
10b7a2e4 | 4791 | const struct ctf_metadata_decoder_config *decoder_config) |
e98a2d6e | 4792 | { |
e98a2d6e | 4793 | struct ctx *ctx = NULL; |
e98a2d6e | 4794 | |
1e649dff | 4795 | /* Create visitor's context */ |
e5e71bf8 | 4796 | ctx = ctx_create(self_comp, decoder_config); |
e98a2d6e | 4797 | if (!ctx) { |
37d21e07 | 4798 | BT_LOGE_STR("Cannot create visitor's context."); |
e98a2d6e PP |
4799 | goto error; |
4800 | } | |
4801 | ||
1e649dff PP |
4802 | goto end; |
4803 | ||
4804 | error: | |
4805 | ctx_destroy(ctx); | |
4806 | ctx = NULL; | |
4807 | ||
4808 | end: | |
1e649dff PP |
4809 | return (void *) ctx; |
4810 | } | |
4811 | ||
4812 | BT_HIDDEN | |
4813 | void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor) | |
4814 | { | |
4815 | ctx_destroy((void *) visitor); | |
4816 | } | |
4817 | ||
4818 | BT_HIDDEN | |
8eee8ea2 | 4819 | bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class( |
1e649dff PP |
4820 | struct ctf_visitor_generate_ir *visitor) |
4821 | { | |
4822 | struct ctx *ctx = (void *) visitor; | |
4823 | ||
8b45963b | 4824 | BT_ASSERT(ctx); |
e5e71bf8 PP |
4825 | |
4826 | if (ctx->trace_class) { | |
4827 | bt_trace_class_get_ref(ctx->trace_class); | |
4828 | } | |
4829 | ||
10b7a2e4 | 4830 | return ctx->trace_class; |
1e649dff PP |
4831 | } |
4832 | ||
7b33a0e0 PP |
4833 | BT_HIDDEN |
4834 | struct ctf_trace_class *ctf_visitor_generate_ir_borrow_ctf_trace_class( | |
4835 | struct ctf_visitor_generate_ir *visitor) | |
4836 | { | |
4837 | struct ctx *ctx = (void *) visitor; | |
4838 | ||
4839 | BT_ASSERT(ctx); | |
4840 | BT_ASSERT(ctx->ctf_tc); | |
4841 | return ctx->ctf_tc; | |
4842 | } | |
4843 | ||
1e649dff PP |
4844 | BT_HIDDEN |
4845 | int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor, | |
4846 | struct ctf_node *node) | |
4847 | { | |
4848 | int ret = 0; | |
4849 | struct ctx *ctx = (void *) visitor; | |
4850 | ||
37d21e07 | 4851 | BT_LOGI_STR("Visiting metadata's AST to generate CTF IR objects."); |
1e649dff | 4852 | |
e98a2d6e PP |
4853 | switch (node->type) { |
4854 | case NODE_ROOT: | |
4855 | { | |
4856 | struct ctf_node *iter; | |
7b33a0e0 | 4857 | bool got_trace_decl = false; |
e98a2d6e PP |
4858 | |
4859 | /* | |
1e649dff | 4860 | * The first thing we need is the native byte order of |
939190b3 | 4861 | * the trace block, because early class aliases can have |
1e649dff PP |
4862 | * a `byte_order` attribute set to `native`. If we don't |
4863 | * have the native byte order yet, and we don't have any | |
4864 | * trace block yet, then fail with EINCOMPLETE. | |
e98a2d6e | 4865 | */ |
7b33a0e0 | 4866 | if (ctx->ctf_tc->default_byte_order == -1) { |
1e649dff PP |
4867 | bt_list_for_each_entry(iter, &node->u.root.trace, siblings) { |
4868 | if (got_trace_decl) { | |
87cb75e6 PP |
4869 | _BT_LOGE_NODE(node, |
4870 | "Duplicate trace (`trace` block)."); | |
1e649dff PP |
4871 | ret = -1; |
4872 | goto end; | |
4873 | } | |
e98a2d6e | 4874 | |
1e649dff PP |
4875 | ret = set_trace_byte_order(ctx, iter); |
4876 | if (ret) { | |
87cb75e6 PP |
4877 | _BT_LOGE_NODE(node, |
4878 | "Cannot set trace's native byte order: " | |
37d21e07 | 4879 | "ret=%d", ret); |
1e649dff PP |
4880 | goto end; |
4881 | } | |
4882 | ||
7b33a0e0 | 4883 | got_trace_decl = true; |
e98a2d6e PP |
4884 | } |
4885 | ||
1e649dff | 4886 | if (!got_trace_decl) { |
87cb75e6 | 4887 | BT_LOGD_STR("Incomplete AST: need trace (`trace` block)."); |
1e649dff PP |
4888 | ret = -EINCOMPLETE; |
4889 | goto end; | |
4890 | } | |
e98a2d6e PP |
4891 | } |
4892 | ||
7b33a0e0 PP |
4893 | BT_ASSERT(ctx->ctf_tc->default_byte_order == CTF_BYTE_ORDER_LITTLE || |
4894 | ctx->ctf_tc->default_byte_order == CTF_BYTE_ORDER_BIG); | |
8b45963b | 4895 | BT_ASSERT(ctx->current_scope && |
7b33a0e0 | 4896 | ctx->current_scope->parent_scope == NULL); |
e98a2d6e | 4897 | |
f4421ff3 PP |
4898 | /* Environment */ |
4899 | bt_list_for_each_entry(iter, &node->u.root.env, siblings) { | |
4900 | ret = visit_env(ctx, iter); | |
4901 | if (ret) { | |
87cb75e6 PP |
4902 | _BT_LOGE_NODE(iter, |
4903 | "Cannot visit trace's environment (`env` block) entry: " | |
37d21e07 | 4904 | "ret=%d", ret); |
f4421ff3 PP |
4905 | goto end; |
4906 | } | |
4907 | } | |
4908 | ||
8b45963b | 4909 | BT_ASSERT(ctx->current_scope && |
f4421ff3 PP |
4910 | ctx->current_scope->parent_scope == NULL); |
4911 | ||
e98a2d6e | 4912 | /* |
f4421ff3 | 4913 | * Visit clock blocks. |
e98a2d6e PP |
4914 | */ |
4915 | bt_list_for_each_entry(iter, &node->u.root.clock, siblings) { | |
1e649dff | 4916 | ret = visit_clock_decl(ctx, iter); |
e98a2d6e | 4917 | if (ret) { |
87cb75e6 PP |
4918 | _BT_LOGE_NODE(iter, |
4919 | "Cannot visit clock class: ret=%d", | |
e98a2d6e | 4920 | ret); |
1e649dff | 4921 | goto end; |
e98a2d6e PP |
4922 | } |
4923 | } | |
4924 | ||
8b45963b | 4925 | BT_ASSERT(ctx->current_scope && |
1e649dff PP |
4926 | ctx->current_scope->parent_scope == NULL); |
4927 | ||
e98a2d6e PP |
4928 | /* |
4929 | * Visit root declarations next, as they can be used by any | |
4930 | * following entity. | |
4931 | */ | |
4932 | bt_list_for_each_entry(iter, &node->u.root.declaration_list, | |
4933 | siblings) { | |
4934 | ret = visit_root_decl(ctx, iter); | |
4935 | if (ret) { | |
87cb75e6 PP |
4936 | _BT_LOGE_NODE(iter, |
4937 | "Cannot visit root entry: ret=%d", | |
e98a2d6e | 4938 | ret); |
1e649dff | 4939 | goto end; |
e98a2d6e PP |
4940 | } |
4941 | } | |
4942 | ||
8b45963b | 4943 | BT_ASSERT(ctx->current_scope && |
1e649dff PP |
4944 | ctx->current_scope->parent_scope == NULL); |
4945 | ||
4946 | /* Callsite blocks are not supported */ | |
e98a2d6e | 4947 | bt_list_for_each_entry(iter, &node->u.root.callsite, siblings) { |
87cb75e6 PP |
4948 | _BT_LOGW_NODE(iter, |
4949 | "\"callsite\" blocks are not supported as of this version."); | |
e98a2d6e PP |
4950 | } |
4951 | ||
8b45963b | 4952 | BT_ASSERT(ctx->current_scope && |
1e649dff PP |
4953 | ctx->current_scope->parent_scope == NULL); |
4954 | ||
e98a2d6e PP |
4955 | /* Trace */ |
4956 | bt_list_for_each_entry(iter, &node->u.root.trace, siblings) { | |
4957 | ret = visit_trace_decl(ctx, iter); | |
4958 | if (ret) { | |
87cb75e6 PP |
4959 | _BT_LOGE_NODE(iter, |
4960 | "Cannot visit trace (`trace` block): " | |
37d21e07 | 4961 | "ret=%d", ret); |
1e649dff | 4962 | goto end; |
e98a2d6e PP |
4963 | } |
4964 | } | |
4965 | ||
8b45963b | 4966 | BT_ASSERT(ctx->current_scope && |
1e649dff PP |
4967 | ctx->current_scope->parent_scope == NULL); |
4968 | ||
e98a2d6e PP |
4969 | /* Streams */ |
4970 | bt_list_for_each_entry(iter, &node->u.root.stream, siblings) { | |
4971 | ret = visit_stream_decl(ctx, iter); | |
4972 | if (ret) { | |
87cb75e6 PP |
4973 | _BT_LOGE_NODE(iter, |
4974 | "Cannot visit stream class: ret=%d", | |
37d21e07 | 4975 | ret); |
1e649dff | 4976 | goto end; |
e98a2d6e PP |
4977 | } |
4978 | } | |
4979 | ||
8b45963b | 4980 | BT_ASSERT(ctx->current_scope && |
1e649dff PP |
4981 | ctx->current_scope->parent_scope == NULL); |
4982 | ||
e98a2d6e PP |
4983 | /* Events */ |
4984 | bt_list_for_each_entry(iter, &node->u.root.event, siblings) { | |
4985 | ret = visit_event_decl(ctx, iter); | |
4986 | if (ret) { | |
87cb75e6 PP |
4987 | _BT_LOGE_NODE(iter, |
4988 | "Cannot visit event class: ret=%d", | |
37d21e07 | 4989 | ret); |
1e649dff | 4990 | goto end; |
e98a2d6e PP |
4991 | } |
4992 | } | |
1e649dff | 4993 | |
8b45963b | 4994 | BT_ASSERT(ctx->current_scope && |
1e649dff | 4995 | ctx->current_scope->parent_scope == NULL); |
e98a2d6e PP |
4996 | break; |
4997 | } | |
e98a2d6e | 4998 | default: |
87cb75e6 PP |
4999 | _BT_LOGE_NODE(node, |
5000 | "Unexpected node type: node-type=%d", | |
37d21e07 | 5001 | node->type); |
e98a2d6e | 5002 | ret = -EINVAL; |
1e649dff | 5003 | goto end; |
e98a2d6e PP |
5004 | } |
5005 | ||
7b33a0e0 PP |
5006 | /* Update default clock classes */ |
5007 | ret = ctf_trace_class_update_default_clock_classes(ctx->ctf_tc); | |
5008 | if (ret) { | |
5009 | ret = -EINVAL; | |
5010 | goto end; | |
5011 | } | |
5012 | ||
7b33a0e0 PP |
5013 | /* Update trace class meanings */ |
5014 | ret = ctf_trace_class_update_meanings(ctx->ctf_tc); | |
5015 | if (ret) { | |
5016 | ret = -EINVAL; | |
5017 | goto end; | |
5018 | } | |
5019 | ||
5020 | /* Update text arrays and sequences */ | |
5021 | ret = ctf_trace_class_update_text_array_sequence(ctx->ctf_tc); | |
5022 | if (ret) { | |
5023 | ret = -EINVAL; | |
5024 | goto end; | |
5025 | } | |
5026 | ||
5027 | /* Resolve sequence lengths and variant tags */ | |
939190b3 | 5028 | ret = ctf_trace_class_resolve_field_classes(ctx->ctf_tc); |
7b33a0e0 PP |
5029 | if (ret) { |
5030 | ret = -EINVAL; | |
5031 | goto end; | |
5032 | } | |
5033 | ||
e5e71bf8 PP |
5034 | if (ctx->trace_class) { |
5035 | /* | |
5036 | * Update "in IR" for field classes. | |
5037 | * | |
5038 | * If we have no IR trace class, then we'll have no way | |
5039 | * to create IR fields anyway, so we leave all the | |
5040 | * `in_ir` members false. | |
5041 | */ | |
5042 | ret = ctf_trace_class_update_in_ir(ctx->ctf_tc); | |
5043 | if (ret) { | |
5044 | ret = -EINVAL; | |
5045 | goto end; | |
5046 | } | |
7b33a0e0 PP |
5047 | } |
5048 | ||
5049 | /* Update saved value indexes */ | |
5050 | ret = ctf_trace_class_update_value_storing_indexes(ctx->ctf_tc); | |
e98a2d6e | 5051 | if (ret) { |
7b33a0e0 PP |
5052 | ret = -EINVAL; |
5053 | goto end; | |
5054 | } | |
5055 | ||
5056 | /* Validate what we have so far */ | |
5057 | ret = ctf_trace_class_validate(ctx->ctf_tc); | |
5058 | if (ret) { | |
5059 | ret = -EINVAL; | |
5060 | goto end; | |
5061 | } | |
5062 | ||
e5e71bf8 PP |
5063 | if (ctx->trace_class) { |
5064 | /* Copy new CTF metadata -> new IR metadata */ | |
5065 | ret = ctf_trace_class_translate(ctx->trace_class, ctx->ctf_tc); | |
5066 | if (ret) { | |
5067 | ret = -EINVAL; | |
5068 | goto end; | |
5069 | } | |
e98a2d6e PP |
5070 | } |
5071 | ||
1e649dff | 5072 | end: |
e98a2d6e PP |
5073 | return ret; |
5074 | } |