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