2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
13 #include <babeltrace2/babeltrace.h>
15 #define BT_COMP_LOG_SELF_COMP (ctx->self_comp)
16 #define BT_LOG_OUTPUT_LEVEL (ctx->log_level)
17 #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/TRANSLATE-TRACE-IR-TO-CTF-IR"
18 #include "logging/comp-logging.h"
20 #include "common/assert.h"
21 #include "common/common.h"
23 #include "fs-sink-ctf-meta.hpp"
24 #include "fs-sink.hpp"
25 #include "translate-trace-ir-to-ctf-ir.hpp"
27 struct field_path_elem
32 const bt_field_class
*ir_fc
;
35 struct fs_sink_ctf_field_class
*parent_fc
;
40 bt_logging_level log_level
;
41 bt_self_component
*self_comp
;
44 struct fs_sink_ctf_stream_class
*cur_sc
;
47 struct fs_sink_ctf_event_class
*cur_ec
;
49 bt_field_path_scope cur_scope
;
52 * Array of `struct field_path_elem` */
56 static inline struct field_path_elem
*cur_path_stack_at(struct ctx
*ctx
, uint64_t i
)
58 BT_ASSERT(i
< ctx
->cur_path
->len
);
59 return &bt_g_array_index(ctx
->cur_path
, struct field_path_elem
, i
);
62 static inline struct field_path_elem
*cur_path_stack_top(struct ctx
*ctx
)
64 BT_ASSERT(ctx
->cur_path
->len
> 0);
65 return cur_path_stack_at(ctx
, ctx
->cur_path
->len
- 1);
68 static inline bool is_reserved_member_name(const char *name
, const char *reserved_name
)
70 bool is_reserved
= false;
72 if (strcmp(name
, reserved_name
) == 0) {
77 if (name
[0] == '_' && strcmp(&name
[1], reserved_name
) == 0) {
86 static const char *reserved_tsdl_keywords
[] = {
87 "align", "callsite", "const", "char", "clock", "double", "enum",
88 "env", "event", "floating_point", "float", "integer", "int", "long",
89 "short", "signed", "stream", "string", "struct", "trace", "typealias",
90 "typedef", "unsigned", "variant", "void", "_Bool", "_Complex", "_Imaginary",
93 static inline bool ist_valid_identifier(const char *name
)
97 bool ist_valid
= true;
99 /* Make sure the name is not a reserved keyword */
100 for (i
= 0; i
< sizeof(reserved_tsdl_keywords
) / sizeof(*reserved_tsdl_keywords
); i
++) {
101 if (strcmp(name
, reserved_tsdl_keywords
[i
]) == 0) {
107 /* Make sure the name is not an empty string */
108 if (strlen(name
) == 0) {
113 /* Make sure the name starts with a letter or `_` */
114 if (!isalpha((unsigned char) name
[0]) && name
[0] != '_') {
119 /* Make sure the name only contains letters, digits, and `_` */
120 for (at
= name
; *at
!= '\0'; at
++) {
121 if (!isalnum((unsigned char) *at
) && *at
!= '_') {
131 static inline bool must_protect_identifier(const char *name
)
134 bool must_protect
= false;
136 /* Protect a reserved keyword */
137 for (i
= 0; i
< sizeof(reserved_tsdl_keywords
) / sizeof(*reserved_tsdl_keywords
); i
++) {
138 if (strcmp(name
, reserved_tsdl_keywords
[i
]) == 0) {
144 /* Protect an identifier which already starts with `_` */
145 if (name
[0] == '_') {
154 static inline int cur_path_stack_push(struct ctx
*ctx
, const char *name
, bool force_protect_name
,
155 const bt_field_class
*ir_fc
,
156 struct fs_sink_ctf_field_class
*parent_fc
)
159 struct field_path_elem
*field_path_elem
;
161 g_array_set_size(ctx
->cur_path
, ctx
->cur_path
->len
+ 1);
162 field_path_elem
= cur_path_stack_top(ctx
);
163 field_path_elem
->name
= g_string_new(NULL
);
166 if (force_protect_name
) {
167 g_string_assign(field_path_elem
->name
, "_");
170 g_string_append(field_path_elem
->name
, name
);
172 if (ctx
->cur_scope
== BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
) {
173 if (is_reserved_member_name(name
, "packet_size") ||
174 is_reserved_member_name(name
, "content_size") ||
175 is_reserved_member_name(name
, "timestamp_begin") ||
176 is_reserved_member_name(name
, "timestamp_end") ||
177 is_reserved_member_name(name
, "events_discarded") ||
178 is_reserved_member_name(name
, "packet_seq_num")) {
179 BT_COMP_LOGE("Unsupported reserved TSDL structure field class member "
180 "or variant field class option name: name=\"%s\"",
187 if (!ist_valid_identifier(field_path_elem
->name
->str
)) {
189 BT_COMP_LOGE("Unsupported non-TSDL structure field class member "
190 "or variant field class option name: name=\"%s\"",
191 field_path_elem
->name
->str
);
196 field_path_elem
->ir_fc
= ir_fc
;
197 field_path_elem
->parent_fc
= parent_fc
;
203 static inline void cur_path_stack_pop(struct ctx
*ctx
)
205 struct field_path_elem
*field_path_elem
;
207 BT_ASSERT(ctx
->cur_path
->len
> 0);
208 field_path_elem
= cur_path_stack_top(ctx
);
210 if (field_path_elem
->name
) {
211 g_string_free(field_path_elem
->name
, TRUE
);
212 field_path_elem
->name
= NULL
;
215 g_array_set_size(ctx
->cur_path
, ctx
->cur_path
->len
- 1);
219 * Creates a relative field ref (a single name) from IR field path
220 * `tgt_ir_field_path`.
222 * This function tries to locate the target field class recursively from
223 * the top to the bottom of the context's current path using only the
224 * target field class's own name. This is because many CTF reading tools
225 * do not support a relative field ref with more than one element, for
226 * example `prev_struct.len`.
228 * Returns a negative value if this resolving operation failed.
230 static int create_relative_field_ref(struct ctx
*ctx
, const bt_field_path
*tgt_ir_field_path
,
231 GString
*tgt_field_ref
,
232 struct fs_sink_ctf_field_class
**user_tgt_fc
)
235 struct fs_sink_ctf_field_class
*tgt_fc
= NULL
;
238 const char *tgt_fc_name
= NULL
;
239 struct field_path_elem
*field_path_elem
;
241 /* Get target field class's name */
242 switch (bt_field_path_get_root_scope(tgt_ir_field_path
)) {
243 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
:
244 BT_ASSERT(ctx
->cur_sc
);
245 tgt_fc
= ctx
->cur_sc
->packet_context_fc
;
247 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
:
248 BT_ASSERT(ctx
->cur_sc
);
249 tgt_fc
= ctx
->cur_sc
->event_common_context_fc
;
251 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
:
252 BT_ASSERT(ctx
->cur_ec
);
253 tgt_fc
= ctx
->cur_ec
->spec_context_fc
;
255 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
:
256 BT_ASSERT(ctx
->cur_ec
);
257 tgt_fc
= ctx
->cur_ec
->payload_fc
;
265 while (i
< bt_field_path_get_item_count(tgt_ir_field_path
)) {
266 const bt_field_path_item
*fp_item
=
267 bt_field_path_borrow_item_by_index_const(tgt_ir_field_path
, i
);
268 struct fs_sink_ctf_named_field_class
*named_fc
= NULL
;
273 if (bt_field_path_item_get_type(fp_item
) ==
274 BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT
) {
275 /* Not supported by CTF 1.8 */
280 switch (tgt_fc
->type
) {
281 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
282 BT_ASSERT(bt_field_path_item_get_type(fp_item
) == BT_FIELD_PATH_ITEM_TYPE_INDEX
);
283 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
284 fs_sink_ctf_field_class_as_struct(tgt_fc
),
285 bt_field_path_item_index_get_index(fp_item
));
287 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
288 BT_ASSERT(bt_field_path_item_get_type(fp_item
) == BT_FIELD_PATH_ITEM_TYPE_INDEX
);
289 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
290 fs_sink_ctf_field_class_as_variant(tgt_fc
),
291 bt_field_path_item_index_get_index(fp_item
));
293 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
294 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
296 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
297 fs_sink_ctf_field_class_as_array_base(tgt_fc
);
299 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
300 BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
);
301 tgt_fc
= array_base_fc
->elem_fc
;
309 tgt_fc
= named_fc
->fc
;
310 tgt_fc_name
= named_fc
->name
->str
;
316 BT_ASSERT(tgt_fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_INT
);
317 BT_ASSERT(tgt_fc_name
);
319 /* Find target field class having this name in current context */
320 for (si
= ctx
->cur_path
->len
- 1; si
>= 0; si
--) {
321 struct fs_sink_ctf_field_class
*fc
;
322 struct fs_sink_ctf_field_class_struct
*struct_fc
= NULL
;
323 struct fs_sink_ctf_field_class_variant
*var_fc
= NULL
;
324 struct fs_sink_ctf_named_field_class
*named_fc
;
327 field_path_elem
= cur_path_stack_at(ctx
, (uint64_t) si
);
328 fc
= field_path_elem
->parent_fc
;
330 /* Reached stack's bottom */
336 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
337 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
339 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
340 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
343 /* Not supported by TSDL 1.8 */
348 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
349 struct_fc
= fs_sink_ctf_field_class_as_struct(fc
);
350 len
= struct_fc
->members
->len
;
352 var_fc
= fs_sink_ctf_field_class_as_variant(fc
);
353 len
= var_fc
->options
->len
;
356 for (i
= 0; i
< len
; i
++) {
357 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
358 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
360 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
363 if (strcmp(named_fc
->name
->str
, tgt_fc_name
) == 0) {
364 if (named_fc
->fc
== tgt_fc
) {
365 g_string_assign(tgt_field_ref
, tgt_fc_name
);
368 *user_tgt_fc
= tgt_fc
;
372 * Using only the target field
373 * class's name, we're not
374 * reaching the target field
375 * class. This is not supported
391 * Creates an absolute field ref from IR field path `tgt_ir_field_path`.
393 * Returns a negative value if this resolving operation failed.
395 static int create_absolute_field_ref(struct ctx
*ctx
, const bt_field_path
*tgt_ir_field_path
,
396 GString
*tgt_field_ref
,
397 struct fs_sink_ctf_field_class
**user_tgt_fc
)
400 struct fs_sink_ctf_field_class
*fc
= NULL
;
403 switch (bt_field_path_get_root_scope(tgt_ir_field_path
)) {
404 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
:
405 BT_ASSERT(ctx
->cur_sc
);
406 fc
= ctx
->cur_sc
->packet_context_fc
;
407 g_string_assign(tgt_field_ref
, "stream.packet.context");
409 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
:
410 BT_ASSERT(ctx
->cur_sc
);
411 fc
= ctx
->cur_sc
->event_common_context_fc
;
412 g_string_assign(tgt_field_ref
, "stream.event.context");
414 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
:
415 BT_ASSERT(ctx
->cur_ec
);
416 fc
= ctx
->cur_ec
->spec_context_fc
;
417 g_string_assign(tgt_field_ref
, "event.context");
419 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
:
420 BT_ASSERT(ctx
->cur_ec
);
421 fc
= ctx
->cur_ec
->payload_fc
;
422 g_string_assign(tgt_field_ref
, "event.fields");
430 for (i
= 0; i
< bt_field_path_get_item_count(tgt_ir_field_path
); i
++) {
431 const bt_field_path_item
*fp_item
=
432 bt_field_path_borrow_item_by_index_const(tgt_ir_field_path
, i
);
433 struct fs_sink_ctf_named_field_class
*named_fc
= NULL
;
435 if (bt_field_path_item_get_type(fp_item
) != BT_FIELD_PATH_ITEM_TYPE_INDEX
) {
436 /* Not supported by TSDL 1.8 */
442 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
443 BT_ASSERT(bt_field_path_item_get_type(fp_item
) == BT_FIELD_PATH_ITEM_TYPE_INDEX
);
444 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
445 fs_sink_ctf_field_class_as_struct(fc
), bt_field_path_item_index_get_index(fp_item
));
447 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
448 BT_ASSERT(bt_field_path_item_get_type(fp_item
) == BT_FIELD_PATH_ITEM_TYPE_INDEX
);
449 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
450 fs_sink_ctf_field_class_as_variant(fc
),
451 bt_field_path_item_index_get_index(fp_item
));
458 g_string_append_c(tgt_field_ref
, '.');
459 g_string_append(tgt_field_ref
, named_fc
->name
->str
);
472 * Resolves a target field class located at `tgt_ir_field_path`, writing
473 * the resolved field ref to `tgt_field_ref` and setting
474 * `*create_before` according to whether or not the target field must be
475 * created immediately before (in which case `tgt_field_ref` is
478 static void resolve_field_class(struct ctx
*ctx
, const bt_field_path
*tgt_ir_field_path
,
479 GString
*tgt_field_ref
, bool *create_before
,
480 struct fs_sink_ctf_field_class
**user_tgt_fc
)
483 bt_field_path_scope tgt_scope
;
485 *create_before
= false;
487 if (!tgt_ir_field_path
) {
488 *create_before
= true;
492 tgt_scope
= bt_field_path_get_root_scope(tgt_ir_field_path
);
494 if (tgt_scope
== ctx
->cur_scope
) {
496 * Try, in this order:
498 * 1. Use a relative path, using only the target field
499 * class's name. This is what is the most commonly
500 * supported by popular CTF reading tools.
502 * 2. Use an absolute path. This could fail if there's
503 * an array field class from the current root's field
504 * class to the target field class.
506 * 3. Create the target field class before the
507 * requesting field class (fallback).
509 ret
= create_relative_field_ref(ctx
, tgt_ir_field_path
, tgt_field_ref
, user_tgt_fc
);
511 ret
= create_absolute_field_ref(ctx
, tgt_ir_field_path
, tgt_field_ref
, user_tgt_fc
);
513 *create_before
= true;
518 ret
= create_absolute_field_ref(ctx
, tgt_ir_field_path
, tgt_field_ref
, user_tgt_fc
);
520 /* It must always work in previous scopes */
528 static int translate_field_class(struct ctx
*ctx
);
530 static inline void append_to_parent_field_class(struct ctx
*ctx
, struct fs_sink_ctf_field_class
*fc
)
532 struct fs_sink_ctf_field_class
*parent_fc
= cur_path_stack_top(ctx
)->parent_fc
;
534 switch (parent_fc
->type
) {
535 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
536 fs_sink_ctf_field_class_struct_append_member(fs_sink_ctf_field_class_as_struct(parent_fc
),
537 cur_path_stack_top(ctx
)->name
->str
, fc
);
539 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
541 struct fs_sink_ctf_field_class_option
*opt_fc
=
542 fs_sink_ctf_field_class_as_option(parent_fc
);
544 BT_ASSERT(!opt_fc
->content_fc
);
545 opt_fc
->content_fc
= fc
;
546 opt_fc
->base
.alignment
= fc
->alignment
;
549 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
550 fs_sink_ctf_field_class_variant_append_option(fs_sink_ctf_field_class_as_variant(parent_fc
),
551 cur_path_stack_top(ctx
)->name
->str
, fc
);
553 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
554 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
556 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
557 fs_sink_ctf_field_class_as_array_base(parent_fc
);
559 BT_ASSERT(!array_base_fc
->elem_fc
);
560 array_base_fc
->elem_fc
= fc
;
561 array_base_fc
->base
.alignment
= fc
->alignment
;
569 static inline void update_parent_field_class_alignment(struct ctx
*ctx
, unsigned int alignment
)
571 struct fs_sink_ctf_field_class
*parent_fc
= cur_path_stack_top(ctx
)->parent_fc
;
573 switch (parent_fc
->type
) {
574 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
575 fs_sink_ctf_field_class_struct_align_at_least(fs_sink_ctf_field_class_as_struct(parent_fc
),
578 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
579 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
581 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
582 fs_sink_ctf_field_class_as_array_base(parent_fc
);
584 array_base_fc
->base
.alignment
= alignment
;
592 static inline int translate_structure_field_class_members(
593 struct ctx
*ctx
, struct fs_sink_ctf_field_class_struct
*struct_fc
, const bt_field_class
*ir_fc
)
598 for (i
= 0; i
< bt_field_class_structure_get_member_count(ir_fc
); i
++) {
599 const bt_field_class_structure_member
*member
;
601 const bt_field_class
*memb_ir_fc
;
603 member
= bt_field_class_structure_borrow_member_by_index_const(ir_fc
, i
);
604 name
= bt_field_class_structure_member_get_name(member
);
605 memb_ir_fc
= bt_field_class_structure_member_borrow_field_class_const(member
);
606 ret
= cur_path_stack_push(ctx
, name
, true, memb_ir_fc
, &struct_fc
->base
);
608 BT_COMP_LOGE("Cannot translate structure field class member: "
614 ret
= translate_field_class(ctx
);
616 BT_COMP_LOGE("Cannot translate structure field class member: "
622 cur_path_stack_pop(ctx
);
629 static inline int translate_structure_field_class(struct ctx
*ctx
)
632 struct fs_sink_ctf_field_class_struct
*fc
=
633 fs_sink_ctf_field_class_struct_create_empty(cur_path_stack_top(ctx
)->ir_fc
);
636 append_to_parent_field_class(ctx
, &fc
->base
);
637 ret
= translate_structure_field_class_members(ctx
, fc
, fc
->base
.ir_fc
);
642 update_parent_field_class_alignment(ctx
, fc
->base
.alignment
);
649 * This function protects a given variant FC option name (with the `_`
650 * prefix) if required. On success, `name_buf->str` contains the variant
651 * FC option name to use (original option name or protected if
654 * One of the goals of `sink.ctf.fs` is to write a CTF trace which is as
655 * close as possible to an original CTF trace as decoded by
658 * This scenario is valid in CTF 1.8:
670 * Once in trace IR, the enumeration FC mapping names and variant FC
671 * option names are kept as is. For this reason, we don't want to
672 * protect the variant FC option names here (by prepending `_`): this
673 * would make the variant FC option name and the enumeration FC mapping
676 * This scenario is also valid in CTF 1.8:
688 * Once in trace IR, the enumeration FC mapping names are kept as is,
689 * but the `_HELLO` variant FC option name becomes `HELLO` (unprotected
690 * for presentation, as recommended by CTF 1.8). When going back to
691 * TSDL, we need to protect `HELLO` so that it becomes `_HELLO` to match
692 * the corresponding enumeration FC mapping name.
694 * This scenario is also valid in CTF 1.8:
706 * Once in trace IR, the enumeration FC mapping names are kept as is,
707 * but the `__HELLO` variant FC option name becomes `_HELLO`
708 * (unprotected). When going back to TSDL, we need to protect `_HELLO`
709 * so that it becomes `__HELLO` to match the corresponding enumeration
712 * `src.ctf.fs` always uses the _same_ integer range sets for a selector
713 * FC mapping and a corresponding variant FC option. We can use that
714 * fact to find the original variant FC option names by matching variant
715 * FC options and enumeration FC mappings by range set.
717 static int maybe_protect_variant_option_name(const bt_field_class
*ir_var_fc
,
718 const bt_field_class
*ir_tag_fc
, uint64_t opt_i
,
723 bt_field_class_type ir_var_fc_type
;
724 const bt_integer_range_set_unsigned
*opt_ranges_unsigned
= NULL
;
725 const bt_integer_range_set_signed
*opt_ranges_signed
= NULL
;
726 const char *mapping_label
= NULL
;
727 const char *ir_opt_name
;
728 const bt_field_class_variant_option
*base_var_opt
;
729 bool force_protect
= false;
731 ir_var_fc_type
= bt_field_class_get_type(ir_var_fc
);
732 base_var_opt
= bt_field_class_variant_borrow_option_by_index_const(ir_var_fc
, opt_i
);
733 BT_ASSERT(base_var_opt
);
734 ir_opt_name
= bt_field_class_variant_option_get_name(base_var_opt
);
735 BT_ASSERT(ir_opt_name
);
738 * Check if the variant FC option name is required to be
739 * protected (reserved TSDL keyword or starts with `_`). In that
740 * case, the name of the selector FC mapping we find must match
741 * exactly the protected name.
743 force_protect
= must_protect_identifier(ir_opt_name
);
745 g_string_assign(name_buf
, "_");
746 g_string_append(name_buf
, ir_opt_name
);
748 g_string_assign(name_buf
, ir_opt_name
);
751 /* Borrow option's ranges */
752 if (ir_var_fc_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
) {
753 /* No ranges: we're done */
756 if (ir_var_fc_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
) {
757 const bt_field_class_variant_with_selector_field_integer_unsigned_option
*var_opt
=
758 bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const(
760 opt_ranges_unsigned
=
761 bt_field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const(
764 const bt_field_class_variant_with_selector_field_integer_signed_option
*var_opt
=
765 bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const(
768 bt_field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const(
772 /* Find corresponding mapping by range set in selector FC */
773 for (i
= 0; i
< bt_field_class_enumeration_get_mapping_count(ir_tag_fc
); i
++) {
774 if (ir_var_fc_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
) {
775 const bt_field_class_enumeration_mapping
*mapping_base
;
776 const bt_field_class_enumeration_unsigned_mapping
*mapping
;
777 const bt_integer_range_set_unsigned
*mapping_ranges
;
780 bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(ir_tag_fc
, i
);
782 bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(mapping
);
784 if (bt_integer_range_set_unsigned_is_equal(opt_ranges_unsigned
, mapping_ranges
)) {
785 /* We have a winner */
787 bt_field_class_enumeration_unsigned_mapping_as_mapping_const(mapping
);
788 mapping_label
= bt_field_class_enumeration_mapping_get_label(mapping_base
);
792 const bt_field_class_enumeration_mapping
*mapping_base
;
793 const bt_field_class_enumeration_signed_mapping
*mapping
;
794 const bt_integer_range_set_signed
*mapping_ranges
;
796 mapping
= bt_field_class_enumeration_signed_borrow_mapping_by_index_const(ir_tag_fc
, i
);
797 mapping_ranges
= bt_field_class_enumeration_signed_mapping_borrow_ranges_const(mapping
);
799 if (bt_integer_range_set_signed_is_equal(opt_ranges_signed
, mapping_ranges
)) {
800 /* We have a winner */
801 mapping_base
= bt_field_class_enumeration_signed_mapping_as_mapping_const(mapping
);
802 mapping_label
= bt_field_class_enumeration_mapping_get_label(mapping_base
);
808 if (!mapping_label
) {
809 /* Range set not found: invalid selector for CTF 1.8 */
815 * If the enumeration FC mapping name is not the same as the
816 * variant FC option name and we didn't protect already, try
817 * protecting the option name and check again.
819 if (strcmp(mapping_label
, name_buf
->str
) != 0) {
825 if (mapping_label
[0] == '\0') {
830 g_string_assign(name_buf
, "_");
831 g_string_append(name_buf
, ir_opt_name
);
833 if (strcmp(mapping_label
, name_buf
->str
) != 0) {
843 static inline int translate_option_field_class(struct ctx
*ctx
)
845 struct fs_sink_ctf_field_class_option
*fc
=
846 fs_sink_ctf_field_class_option_create_empty(cur_path_stack_top(ctx
)->ir_fc
);
847 const bt_field_class
*content_ir_fc
=
848 bt_field_class_option_borrow_field_class_const(fc
->base
.ir_fc
);
854 * CTF 1.8 does not support the option field class type. To
855 * write something anyway, this component translates this type
856 * to a variant field class where the options are:
858 * * An empty structure field class.
859 * * The optional field class itself.
861 * The "tag" is always generated/before in that case (an 8-bit
862 * unsigned enumeration field class).
864 append_to_parent_field_class(ctx
, &fc
->base
);
865 ret
= cur_path_stack_push(ctx
, NULL
, false, content_ir_fc
, &fc
->base
);
867 BT_COMP_LOGE_STR("Cannot translate option field class content.");
871 ret
= translate_field_class(ctx
);
873 BT_COMP_LOGE_STR("Cannot translate option field class content.");
877 cur_path_stack_pop(ctx
);
878 update_parent_field_class_alignment(ctx
, fc
->base
.alignment
);
884 static inline int translate_variant_field_class(struct ctx
*ctx
)
888 struct fs_sink_ctf_field_class_variant
*fc
=
889 fs_sink_ctf_field_class_variant_create_empty(cur_path_stack_top(ctx
)->ir_fc
);
890 bt_field_class_type ir_fc_type
;
891 const bt_field_path
*ir_selector_field_path
= NULL
;
892 struct fs_sink_ctf_field_class
*tgt_fc
= NULL
;
893 GString
*name_buf
= g_string_new(NULL
);
894 bt_value
*prot_opt_names
= bt_value_array_create();
899 BT_ASSERT(prot_opt_names
);
900 ir_fc_type
= bt_field_class_get_type(fc
->base
.ir_fc
);
901 opt_count
= bt_field_class_variant_get_option_count(fc
->base
.ir_fc
);
903 if (bt_field_class_type_is(ir_fc_type
, BT_FIELD_CLASS_TYPE_VARIANT_WITH_SELECTOR_FIELD
)) {
904 ir_selector_field_path
=
905 bt_field_class_variant_with_selector_field_borrow_selector_field_path_const(
907 BT_ASSERT(ir_selector_field_path
);
910 /* Resolve tag field class before appending to parent */
911 resolve_field_class(ctx
, ir_selector_field_path
, fc
->tag_ref
, &fc
->tag_is_before
, &tgt_fc
);
913 if (ir_selector_field_path
&& tgt_fc
) {
914 uint64_t mapping_count
;
915 uint64_t option_count
;
917 /* CTF 1.8: selector FC must be an enumeration FC */
918 bt_field_class_type type
= bt_field_class_get_type(tgt_fc
->ir_fc
);
920 if (!bt_field_class_type_is(type
, BT_FIELD_CLASS_TYPE_ENUMERATION
)) {
921 fc
->tag_is_before
= true;
926 * Call maybe_protect_variant_option_name() for each
927 * option below. In that case we also want selector FC
928 * to contain as many mappings as the variant FC has
931 mapping_count
= bt_field_class_enumeration_get_mapping_count(tgt_fc
->ir_fc
);
932 option_count
= bt_field_class_variant_get_option_count(fc
->base
.ir_fc
);
934 if (mapping_count
!= option_count
) {
935 fc
->tag_is_before
= true;
940 * No compatible selector field class for CTF 1.8:
941 * create the appropriate selector field class.
943 fc
->tag_is_before
= true;
949 * First pass: detect any option name clash with option name
950 * protection. In that case, we don't fail: just create the
951 * selector field class before the variant field class.
953 * After this, `prot_opt_names` contains the final option names,
954 * potentially protected if needed. They can still be invalid
955 * TSDL identifiers however; this will be checked by
956 * cur_path_stack_push().
958 for (i
= 0; i
< opt_count
; i
++) {
959 if (!fc
->tag_is_before
) {
960 BT_ASSERT(tgt_fc
->ir_fc
);
961 ret
= maybe_protect_variant_option_name(fc
->base
.ir_fc
, tgt_fc
->ir_fc
, i
, name_buf
);
963 fc
->tag_is_before
= true;
967 ret
= bt_value_array_append_string_element(prot_opt_names
, name_buf
->str
);
973 for (i
= 0; i
< opt_count
; i
++) {
975 const bt_value
*opt_name_a
=
976 bt_value_array_borrow_element_by_index_const(prot_opt_names
, i
);
978 for (j
= 0; j
< opt_count
; j
++) {
979 const bt_value
*opt_name_b
;
985 opt_name_b
= bt_value_array_borrow_element_by_index_const(prot_opt_names
, j
);
986 if (bt_value_is_equal(opt_name_a
, opt_name_b
)) {
988 * Variant FC option names are not
989 * unique when protected.
991 fc
->tag_is_before
= true;
992 goto append_to_parent
;
998 append_to_parent_field_class(ctx
, &fc
->base
);
1000 for (i
= 0; i
< opt_count
; i
++) {
1001 const bt_field_class_variant_option
*opt
;
1002 const bt_field_class
*opt_ir_fc
;
1003 const bt_value
*prot_opt_name_val
=
1004 bt_value_array_borrow_element_by_index_const(prot_opt_names
, i
);
1005 const char *prot_opt_name
= bt_value_string_get(prot_opt_name_val
);
1007 BT_ASSERT(prot_opt_name
);
1008 opt
= bt_field_class_variant_borrow_option_by_index_const(fc
->base
.ir_fc
, i
);
1009 opt_ir_fc
= bt_field_class_variant_option_borrow_field_class_const(opt
);
1012 * We don't ask cur_path_stack_push() to protect the
1013 * option name because it's already protected at this
1016 ret
= cur_path_stack_push(ctx
, prot_opt_name
, false, opt_ir_fc
, &fc
->base
);
1018 BT_COMP_LOGE("Cannot translate variant field class option: "
1024 ret
= translate_field_class(ctx
);
1026 BT_COMP_LOGE("Cannot translate variant field class option: "
1032 cur_path_stack_pop(ctx
);
1037 g_string_free(name_buf
, TRUE
);
1040 bt_value_put_ref(prot_opt_names
);
1044 static inline int translate_static_array_field_class(struct ctx
*ctx
)
1046 struct fs_sink_ctf_field_class_array
*fc
=
1047 fs_sink_ctf_field_class_array_create_empty(cur_path_stack_top(ctx
)->ir_fc
);
1048 const bt_field_class
*elem_ir_fc
=
1049 bt_field_class_array_borrow_element_field_class_const(fc
->base
.base
.ir_fc
);
1053 append_to_parent_field_class(ctx
, &fc
->base
.base
);
1054 ret
= cur_path_stack_push(ctx
, NULL
, false, elem_ir_fc
, &fc
->base
.base
);
1056 BT_COMP_LOGE_STR("Cannot translate static array field class element.");
1060 ret
= translate_field_class(ctx
);
1062 BT_COMP_LOGE_STR("Cannot translate static array field class element.");
1066 cur_path_stack_pop(ctx
);
1067 update_parent_field_class_alignment(ctx
, fc
->base
.base
.alignment
);
1073 static inline int translate_dynamic_array_field_class(struct ctx
*ctx
)
1075 struct fs_sink_ctf_field_class_sequence
*fc
=
1076 fs_sink_ctf_field_class_sequence_create_empty(cur_path_stack_top(ctx
)->ir_fc
);
1077 const bt_field_class
*elem_ir_fc
=
1078 bt_field_class_array_borrow_element_field_class_const(fc
->base
.base
.ir_fc
);
1083 /* Resolve length field class before appending to parent */
1084 if (bt_field_class_get_type(cur_path_stack_top(ctx
)->ir_fc
) ==
1085 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
) {
1086 resolve_field_class(
1088 bt_field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
1089 fc
->base
.base
.ir_fc
),
1090 fc
->length_ref
, &fc
->length_is_before
, NULL
);
1093 append_to_parent_field_class(ctx
, &fc
->base
.base
);
1094 ret
= cur_path_stack_push(ctx
, NULL
, false, elem_ir_fc
, &fc
->base
.base
);
1096 BT_COMP_LOGE_STR("Cannot translate dynamic array field class element.");
1100 ret
= translate_field_class(ctx
);
1102 BT_COMP_LOGE_STR("Cannot translate dynamic array field class element.");
1106 cur_path_stack_pop(ctx
);
1107 update_parent_field_class_alignment(ctx
, fc
->base
.base
.alignment
);
1113 static inline int translate_bool_field_class(struct ctx
*ctx
)
1115 struct fs_sink_ctf_field_class_bool
*fc
=
1116 fs_sink_ctf_field_class_bool_create(cur_path_stack_top(ctx
)->ir_fc
);
1119 append_to_parent_field_class(ctx
, &fc
->base
.base
);
1123 static inline int translate_bit_array_field_class(struct ctx
*ctx
)
1125 struct fs_sink_ctf_field_class_bit_array
*fc
=
1126 fs_sink_ctf_field_class_bit_array_create(cur_path_stack_top(ctx
)->ir_fc
);
1129 append_to_parent_field_class(ctx
, &fc
->base
);
1133 static inline int translate_integer_field_class(struct ctx
*ctx
)
1135 struct fs_sink_ctf_field_class_int
*fc
=
1136 fs_sink_ctf_field_class_int_create(cur_path_stack_top(ctx
)->ir_fc
);
1139 append_to_parent_field_class(ctx
, &fc
->base
.base
);
1143 static inline int translate_real_field_class(struct ctx
*ctx
)
1145 struct fs_sink_ctf_field_class_float
*fc
=
1146 fs_sink_ctf_field_class_float_create(cur_path_stack_top(ctx
)->ir_fc
);
1149 append_to_parent_field_class(ctx
, &fc
->base
.base
);
1153 static inline int translate_string_field_class(struct ctx
*ctx
)
1155 struct fs_sink_ctf_field_class_string
*fc
=
1156 fs_sink_ctf_field_class_string_create(cur_path_stack_top(ctx
)->ir_fc
);
1159 append_to_parent_field_class(ctx
, &fc
->base
);
1164 * Translates a field class, recursively.
1166 * The field class's IR field class, parent field class, and index
1167 * within its parent are in the context's current path's top element
1168 * (cur_path_stack_top()).
1170 static int translate_field_class(struct ctx
*ctx
)
1173 bt_field_class_type ir_fc_type
= bt_field_class_get_type(cur_path_stack_top(ctx
)->ir_fc
);
1175 if (ir_fc_type
== BT_FIELD_CLASS_TYPE_BOOL
) {
1176 ret
= translate_bool_field_class(ctx
);
1177 } else if (ir_fc_type
== BT_FIELD_CLASS_TYPE_BIT_ARRAY
) {
1178 ret
= translate_bit_array_field_class(ctx
);
1179 } else if (bt_field_class_type_is(ir_fc_type
, BT_FIELD_CLASS_TYPE_INTEGER
)) {
1180 ret
= translate_integer_field_class(ctx
);
1181 } else if (bt_field_class_type_is(ir_fc_type
, BT_FIELD_CLASS_TYPE_REAL
)) {
1182 ret
= translate_real_field_class(ctx
);
1183 } else if (ir_fc_type
== BT_FIELD_CLASS_TYPE_STRING
) {
1184 ret
= translate_string_field_class(ctx
);
1185 } else if (ir_fc_type
== BT_FIELD_CLASS_TYPE_STRUCTURE
) {
1186 ret
= translate_structure_field_class(ctx
);
1187 } else if (ir_fc_type
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
) {
1188 ret
= translate_static_array_field_class(ctx
);
1189 } else if (bt_field_class_type_is(ir_fc_type
, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
)) {
1190 ret
= translate_dynamic_array_field_class(ctx
);
1191 } else if (bt_field_class_type_is(ir_fc_type
, BT_FIELD_CLASS_TYPE_OPTION
)) {
1192 ret
= translate_option_field_class(ctx
);
1193 } else if (bt_field_class_type_is(ir_fc_type
, BT_FIELD_CLASS_TYPE_VARIANT
)) {
1194 ret
= translate_variant_field_class(ctx
);
1202 static int set_field_ref(struct fs_sink_ctf_field_class
*fc
, const char *fc_name
,
1203 struct fs_sink_ctf_field_class
*parent_fc
)
1206 GString
*field_ref
= NULL
;
1208 const char *tgt_type
;
1209 struct fs_sink_ctf_field_class_struct
*parent_struct_fc
=
1210 fs_sink_ctf_field_class_as_struct(parent_fc
);
1212 unsigned int suffix
= 0;
1214 if (!fc_name
|| !parent_fc
|| parent_fc
->type
!= FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
1221 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
1224 * CTF 1.8 does not support the option field class type.
1225 * To write something anyway, this component translates
1226 * this type to a variant field class where the options
1229 * * An empty structure field class.
1230 * * The optional field class itself.
1232 * Because the option field class becomes a CTF variant
1233 * field class, we use the term "tag" too here.
1235 * The "tag" is always generated/before in that case (an
1236 * 8-bit unsigned enumeration field class).
1238 struct fs_sink_ctf_field_class_option
*opt_fc
= fs_sink_ctf_field_class_as_option(fc
);
1240 field_ref
= opt_fc
->tag_ref
;
1245 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1247 struct fs_sink_ctf_field_class_sequence
*seq_fc
= fs_sink_ctf_field_class_as_sequence(fc
);
1249 field_ref
= seq_fc
->length_ref
;
1250 is_before
= seq_fc
->length_is_before
;
1254 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
1256 struct fs_sink_ctf_field_class_variant
*var_fc
= fs_sink_ctf_field_class_as_variant(fc
);
1258 field_ref
= var_fc
->tag_ref
;
1259 is_before
= var_fc
->tag_is_before
;
1267 BT_ASSERT(field_ref
);
1273 /* Initial field ref */
1274 g_string_printf(field_ref
, "__%s_%s", fc_name
, tgt_type
);
1277 * Make sure field ref does not clash with an existing field
1278 * class name within the same parent structure field class.
1281 bool name_ok
= true;
1283 for (i
= 0; i
< parent_struct_fc
->members
->len
; i
++) {
1284 struct fs_sink_ctf_named_field_class
*named_fc
=
1285 fs_sink_ctf_field_class_struct_borrow_member_by_index(parent_struct_fc
, i
);
1287 if (strcmp(field_ref
->str
, named_fc
->name
->str
) == 0) {
1295 /* No clash: we're done */
1299 /* Append suffix and try again */
1300 g_string_printf(field_ref
, "__%s_%s_%u", fc_name
, tgt_type
, suffix
);
1309 * This function recursively sets field refs of sequence and variant
1310 * field classes when they are immediately before, avoiding name clashes
1311 * with existing field class names.
1313 * It can fail at this point if, for example, a sequence field class of
1314 * which to set the length's field ref has something else than a
1315 * structure field class as its parent: in this case, there's no
1316 * location to place the length field class immediately before the
1317 * sequence field class.
1319 static int set_field_refs(struct fs_sink_ctf_field_class
* const fc
, const char *fc_name
,
1320 struct fs_sink_ctf_field_class
*parent_fc
)
1323 enum fs_sink_ctf_field_class_type fc_type
;
1329 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
1331 struct fs_sink_ctf_field_class_option
*opt_fc
= fs_sink_ctf_field_class_as_option(fc
);
1333 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
1338 ret
= set_field_refs(opt_fc
->content_fc
, NULL
, fc
);
1345 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
1346 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
1350 struct fs_sink_ctf_field_class_struct
*struct_fc
= NULL
;
1351 struct fs_sink_ctf_field_class_variant
*var_fc
= NULL
;
1352 struct fs_sink_ctf_named_field_class
*named_fc
;
1354 if (fc_type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
1355 struct_fc
= fs_sink_ctf_field_class_as_struct(fc
);
1356 len
= struct_fc
->members
->len
;
1358 var_fc
= fs_sink_ctf_field_class_as_variant(fc
);
1359 len
= var_fc
->options
->len
;
1360 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
1366 for (i
= 0; i
< len
; i
++) {
1367 if (fc_type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
1368 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
1370 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
1373 ret
= set_field_refs(named_fc
->fc
, named_fc
->name
->str
, fc
);
1381 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
1382 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1384 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
1385 fs_sink_ctf_field_class_as_array_base(fc
);
1387 if (fc_type
== FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
1388 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
1394 ret
= set_field_refs(array_base_fc
->elem_fc
, NULL
, fc
);
1410 * This function translates a root scope trace IR field class to
1411 * a CTF IR field class.
1413 * The resulting CTF IR field class is written to `*fc` so that it
1414 * exists as the parent object's (stream class or event class) true root
1415 * field class during the recursive translation for resolving purposes.
1416 * This is also why this function creates the empty structure field
1417 * class and then calls translate_structure_field_class_members() to
1420 static int translate_scope_field_class(struct ctx
*ctx
, bt_field_path_scope scope
,
1421 struct fs_sink_ctf_field_class
**fc
,
1422 const bt_field_class
*ir_fc
)
1430 BT_ASSERT(bt_field_class_get_type(ir_fc
) == BT_FIELD_CLASS_TYPE_STRUCTURE
);
1432 *fc
= &fs_sink_ctf_field_class_struct_create_empty(ir_fc
)->base
;
1434 ctx
->cur_scope
= scope
;
1435 BT_ASSERT(ctx
->cur_path
->len
== 0);
1436 ret
= cur_path_stack_push(ctx
, NULL
, false, ir_fc
, NULL
);
1438 BT_COMP_LOGE("Cannot translate scope structure field class: "
1445 translate_structure_field_class_members(ctx
, fs_sink_ctf_field_class_as_struct(*fc
), ir_fc
);
1447 BT_COMP_LOGE("Cannot translate scope structure field class: "
1453 cur_path_stack_pop(ctx
);
1455 /* Set field refs for preceding targets */
1456 ret
= set_field_refs(*fc
, NULL
, NULL
);
1462 static inline void ctx_init(struct ctx
*ctx
, struct fs_sink_comp
*fs_sink
)
1464 memset(ctx
, 0, sizeof(struct ctx
));
1465 ctx
->cur_path
= g_array_new(FALSE
, TRUE
, sizeof(struct field_path_elem
));
1466 BT_ASSERT(ctx
->cur_path
);
1467 ctx
->log_level
= fs_sink
->log_level
;
1468 ctx
->self_comp
= fs_sink
->self_comp
;
1471 static inline void ctx_fini(struct ctx
*ctx
)
1473 if (ctx
->cur_path
) {
1474 g_array_free(ctx
->cur_path
, TRUE
);
1475 ctx
->cur_path
= NULL
;
1479 static int translate_event_class(struct fs_sink_comp
*fs_sink
, struct fs_sink_ctf_stream_class
*sc
,
1480 const bt_event_class
*ir_ec
,
1481 struct fs_sink_ctf_event_class
**out_ec
)
1485 struct fs_sink_ctf_event_class
*ec
;
1490 ctx_init(&ctx
, fs_sink
);
1491 ec
= fs_sink_ctf_event_class_create(sc
, ir_ec
);
1495 ret
= translate_scope_field_class(
1496 &ctx
, BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
, &ec
->spec_context_fc
,
1497 bt_event_class_borrow_specific_context_field_class_const(ir_ec
));
1502 ret
= translate_scope_field_class(&ctx
, BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
, &ec
->payload_fc
,
1503 bt_event_class_borrow_payload_field_class_const(ir_ec
));
1514 int try_translate_event_class_trace_ir_to_ctf_ir(struct fs_sink_comp
*fs_sink
,
1515 struct fs_sink_ctf_stream_class
*sc
,
1516 const bt_event_class
*ir_ec
,
1517 struct fs_sink_ctf_event_class
**out_ec
)
1524 /* Check in hash table first */
1525 *out_ec
= (fs_sink_ctf_event_class
*) g_hash_table_lookup(sc
->event_classes_from_ir
, ir_ec
);
1526 if (G_LIKELY(*out_ec
)) {
1530 ret
= translate_event_class(fs_sink
, sc
, ir_ec
, out_ec
);
1536 static bool default_clock_class_name_exists(struct fs_sink_ctf_trace
*trace
, const char *name
)
1538 bool exists
= false;
1541 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1542 struct fs_sink_ctf_stream_class
*sc
=
1543 (fs_sink_ctf_stream_class
*) trace
->stream_classes
->pdata
[i
];
1545 if (sc
->default_clock_class_name
->len
== 0) {
1546 /* No default clock class */
1550 if (strcmp(sc
->default_clock_class_name
->str
, name
) == 0) {
1560 static void make_unique_default_clock_class_name(struct fs_sink_ctf_stream_class
*sc
)
1562 unsigned int suffix
= 0;
1564 std::string name
= "default";
1566 while (default_clock_class_name_exists(sc
->trace
, name
.c_str())) {
1567 name
= "default" + std::to_string(suffix
);
1571 g_string_assign(sc
->default_clock_class_name
, name
.c_str());
1574 static int translate_stream_class(struct fs_sink_comp
*fs_sink
, struct fs_sink_ctf_trace
*trace
,
1575 const bt_stream_class
*ir_sc
,
1576 struct fs_sink_ctf_stream_class
**out_sc
)
1583 ctx_init(&ctx
, fs_sink
);
1584 *out_sc
= fs_sink_ctf_stream_class_create(trace
, ir_sc
);
1587 /* Set default clock class's protected name, if any */
1588 if ((*out_sc
)->default_clock_class
) {
1589 const char *name
= bt_clock_class_get_name((*out_sc
)->default_clock_class
);
1592 /* Try original name, protected */
1593 g_string_assign((*out_sc
)->default_clock_class_name
, "");
1595 if (must_protect_identifier(name
)) {
1596 g_string_assign((*out_sc
)->default_clock_class_name
, "_");
1599 g_string_assign((*out_sc
)->default_clock_class_name
, name
);
1600 if (!ist_valid_identifier((*out_sc
)->default_clock_class_name
->str
)) {
1601 /* Invalid: create a new name */
1602 make_unique_default_clock_class_name(*out_sc
);
1605 /* No name: create a name */
1606 make_unique_default_clock_class_name(*out_sc
);
1610 ctx
.cur_sc
= *out_sc
;
1611 ret
= translate_scope_field_class(
1612 &ctx
, BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
, &(*out_sc
)->packet_context_fc
,
1613 bt_stream_class_borrow_packet_context_field_class_const(ir_sc
));
1618 if ((*out_sc
)->packet_context_fc
) {
1620 * Make sure the structure field class's alignment is
1621 * enough: 8 is what we use for our own special members
1622 * in the packet context.
1624 fs_sink_ctf_field_class_struct_align_at_least(
1625 fs_sink_ctf_field_class_as_struct((*out_sc
)->packet_context_fc
), 8);
1628 ret
= translate_scope_field_class(
1629 &ctx
, BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
, &(*out_sc
)->event_common_context_fc
,
1630 bt_stream_class_borrow_event_common_context_field_class_const(ir_sc
));
1638 fs_sink_ctf_stream_class_destroy(*out_sc
);
1646 int try_translate_stream_class_trace_ir_to_ctf_ir(struct fs_sink_comp
*fs_sink
,
1647 struct fs_sink_ctf_trace
*trace
,
1648 const bt_stream_class
*ir_sc
,
1649 struct fs_sink_ctf_stream_class
**out_sc
)
1657 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1658 *out_sc
= (fs_sink_ctf_stream_class
*) trace
->stream_classes
->pdata
[i
];
1660 if ((*out_sc
)->ir_sc
== ir_sc
) {
1665 ret
= translate_stream_class(fs_sink
, trace
, ir_sc
, out_sc
);
1671 struct fs_sink_ctf_trace
*translate_trace_trace_ir_to_ctf_ir(struct fs_sink_comp
*fs_sink
,
1672 const bt_trace
*ir_trace
)
1676 struct fs_sink_ctf_trace
*trace
= NULL
;
1678 /* Check that trace's environment is TSDL-compatible */
1679 count
= bt_trace_get_environment_entry_count(ir_trace
);
1680 for (i
= 0; i
< count
; i
++) {
1682 const bt_value
*val
;
1684 bt_trace_borrow_environment_entry_by_index_const(ir_trace
, i
, &name
, &val
);
1686 if (!ist_valid_identifier(name
)) {
1687 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, fs_sink
->log_level
, fs_sink
->self_comp
,
1688 "Unsupported trace class's environment entry name: "
1694 switch (bt_value_get_type(val
)) {
1695 case BT_VALUE_TYPE_SIGNED_INTEGER
:
1696 case BT_VALUE_TYPE_STRING
:
1699 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, fs_sink
->log_level
, fs_sink
->self_comp
,
1700 "Unsupported trace class's environment entry value type: "
1702 bt_common_value_type_string(bt_value_get_type(val
)));
1707 trace
= fs_sink_ctf_trace_create(ir_trace
);