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