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