2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_COMP_LOG_SELF_COMP (ctx->self_comp)
24 #define BT_LOG_OUTPUT_LEVEL (ctx->log_level)
25 #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/TRANSLATE-TRACE-IR-TO-CTF-IR"
26 #include "logging/comp-logging.h"
28 #include <babeltrace2/babeltrace.h>
29 #include "common/macros.h"
30 #include "common/common.h"
31 #include "common/assert.h"
38 #include "fs-sink-ctf-meta.h"
40 struct field_path_elem
{
41 uint64_t index_in_parent
;
45 const bt_field_class
*ir_fc
;
48 struct fs_sink_ctf_field_class
*parent_fc
;
52 bt_logging_level log_level
;
53 bt_self_component
*self_comp
;
56 struct fs_sink_ctf_stream_class
*cur_sc
;
59 struct fs_sink_ctf_event_class
*cur_ec
;
61 bt_field_path_scope cur_scope
;
64 * Array of `struct field_path_elem` */
69 struct field_path_elem
*cur_path_stack_at(struct ctx
*ctx
, uint64_t i
)
71 BT_ASSERT(i
< ctx
->cur_path
->len
);
72 return &g_array_index(ctx
->cur_path
, struct field_path_elem
, i
);
76 struct field_path_elem
*cur_path_stack_top(struct ctx
*ctx
)
78 BT_ASSERT(ctx
->cur_path
->len
> 0);
79 return cur_path_stack_at(ctx
, ctx
->cur_path
->len
- 1);
83 bool is_reserved_member_name(const char *name
, const char *reserved_name
)
85 bool is_reserved
= false;
87 if (strcmp(name
, reserved_name
) == 0) {
92 if (name
[0] == '_' && strcmp(&name
[1], reserved_name
) == 0) {
101 static const char *reserved_tsdl_keywords
[] = {
133 bool ist_valid_identifier(const char *name
)
137 bool ist_valid
= true;
139 /* Make sure the name is not a reserved keyword */
140 for (i
= 0; i
< sizeof(reserved_tsdl_keywords
) / sizeof(*reserved_tsdl_keywords
);
142 if (strcmp(name
, reserved_tsdl_keywords
[i
]) == 0) {
148 /* Make sure the name is not an empty string */
149 if (strlen(name
) == 0) {
154 /* Make sure the name starts with a letter or `_` */
155 if (!isalpha(name
[0]) && name
[0] != '_') {
160 /* Make sure the name only contains letters, digits, and `_` */
161 for (at
= name
; *at
!= '\0'; at
++) {
162 if (!isalnum(*at
) && *at
!= '_') {
173 bool must_protect_identifier(const char *name
)
176 bool must_protect
= false;
178 /* Protect a reserved keyword */
179 for (i
= 0; i
< sizeof(reserved_tsdl_keywords
) / sizeof(*reserved_tsdl_keywords
);
181 if (strcmp(name
, reserved_tsdl_keywords
[i
]) == 0) {
187 /* Protect an identifier which already starts with `_` */
188 if (name
[0] == '_') {
198 int cur_path_stack_push(struct ctx
*ctx
,
199 uint64_t index_in_parent
, const char *name
,
200 bool force_protect_name
, const bt_field_class
*ir_fc
,
201 struct fs_sink_ctf_field_class
*parent_fc
)
204 struct field_path_elem
*field_path_elem
;
206 g_array_set_size(ctx
->cur_path
, ctx
->cur_path
->len
+ 1);
207 field_path_elem
= cur_path_stack_top(ctx
);
208 field_path_elem
->index_in_parent
= index_in_parent
;
209 field_path_elem
->name
= g_string_new(NULL
);
212 if (force_protect_name
) {
213 g_string_assign(field_path_elem
->name
, "_");
216 g_string_append(field_path_elem
->name
, name
);
218 if (ctx
->cur_scope
== BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
) {
219 if (is_reserved_member_name(name
, "packet_size") ||
220 is_reserved_member_name(name
, "content_size") ||
221 is_reserved_member_name(name
, "timestamp_begin") ||
222 is_reserved_member_name(name
, "timestamp_end") ||
223 is_reserved_member_name(name
, "events_discarded") ||
224 is_reserved_member_name(name
, "packet_seq_num")) {
225 BT_COMP_LOGE("Unsupported reserved TSDL structure field class member "
226 "or variant field class option name: name=\"%s\"",
233 if (!ist_valid_identifier(field_path_elem
->name
->str
)) {
235 BT_COMP_LOGE("Unsupported non-TSDL structure field class member "
236 "or variant field class option name: name=\"%s\"",
237 field_path_elem
->name
->str
);
242 field_path_elem
->ir_fc
= ir_fc
;
243 field_path_elem
->parent_fc
= parent_fc
;
250 void cur_path_stack_pop(struct ctx
*ctx
)
252 struct field_path_elem
*field_path_elem
;
254 BT_ASSERT(ctx
->cur_path
->len
> 0);
255 field_path_elem
= cur_path_stack_top(ctx
);
257 if (field_path_elem
->name
) {
258 g_string_free(field_path_elem
->name
, TRUE
);
259 field_path_elem
->name
= NULL
;
262 g_array_set_size(ctx
->cur_path
, ctx
->cur_path
->len
- 1);
266 * Creates a relative field ref (a single name) from IR field path
267 * `tgt_ir_field_path`.
269 * This function tries to locate the target field class recursively from
270 * the top to the bottom of the context's current path using only the
271 * target field class's own name. This is because many CTF reading tools
272 * do not support a relative field ref with more than one element, for
273 * example `prev_struct.len`.
275 * Returns a negative value if this resolving operation failed.
278 int create_relative_field_ref(struct ctx
*ctx
,
279 const bt_field_path
*tgt_ir_field_path
, GString
*tgt_field_ref
,
280 struct fs_sink_ctf_field_class
**user_tgt_fc
)
283 struct fs_sink_ctf_field_class
*tgt_fc
= NULL
;
286 const char *tgt_fc_name
= NULL
;
287 struct field_path_elem
*field_path_elem
;
289 /* Get target field class's name */
290 switch (bt_field_path_get_root_scope(tgt_ir_field_path
)) {
291 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
:
292 BT_ASSERT(ctx
->cur_sc
);
293 tgt_fc
= ctx
->cur_sc
->packet_context_fc
;
295 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
:
296 BT_ASSERT(ctx
->cur_sc
);
297 tgt_fc
= ctx
->cur_sc
->event_common_context_fc
;
299 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
:
300 BT_ASSERT(ctx
->cur_ec
);
301 tgt_fc
= ctx
->cur_ec
->spec_context_fc
;
303 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
:
304 BT_ASSERT(ctx
->cur_ec
);
305 tgt_fc
= ctx
->cur_ec
->payload_fc
;
313 while (i
< bt_field_path_get_item_count(tgt_ir_field_path
)) {
314 const bt_field_path_item
*fp_item
=
315 bt_field_path_borrow_item_by_index_const(
316 tgt_ir_field_path
, i
);
317 struct fs_sink_ctf_named_field_class
*named_fc
= NULL
;
322 if (bt_field_path_item_get_type(fp_item
) ==
323 BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT
) {
324 /* Not supported by CTF 1.8 */
329 switch (tgt_fc
->type
) {
330 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
331 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
332 BT_FIELD_PATH_ITEM_TYPE_INDEX
);
333 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
335 bt_field_path_item_index_get_index(fp_item
));
337 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
338 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
339 BT_FIELD_PATH_ITEM_TYPE_INDEX
);
340 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
342 bt_field_path_item_index_get_index(fp_item
));
344 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
345 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
347 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
350 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
351 BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
);
352 tgt_fc
= array_base_fc
->elem_fc
;
360 tgt_fc
= named_fc
->fc
;
361 tgt_fc_name
= named_fc
->name
->str
;
367 BT_ASSERT(tgt_fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_INT
);
368 BT_ASSERT(tgt_fc_name
);
370 /* Find target field class having this name in current context */
371 for (si
= ctx
->cur_path
->len
- 1; si
>= 0; si
--) {
372 struct fs_sink_ctf_field_class
*fc
;
373 struct fs_sink_ctf_field_class_struct
*struct_fc
= NULL
;
374 struct fs_sink_ctf_field_class_variant
*var_fc
= NULL
;
375 struct fs_sink_ctf_named_field_class
*named_fc
;
378 field_path_elem
= cur_path_stack_at(ctx
, (uint64_t) si
);
379 fc
= field_path_elem
->parent_fc
;
381 /* Reached stack's bottom */
387 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
388 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
390 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
391 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
394 /* Not supported by TSDL 1.8 */
399 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
400 struct_fc
= (void *) fc
;
401 len
= struct_fc
->members
->len
;
403 var_fc
= (void *) fc
;
404 len
= var_fc
->options
->len
;
407 for (i
= 0; i
< len
; i
++) {
408 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
409 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
412 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
416 if (strcmp(named_fc
->name
->str
, tgt_fc_name
) == 0) {
417 if (named_fc
->fc
== tgt_fc
) {
418 g_string_assign(tgt_field_ref
,
422 *user_tgt_fc
= tgt_fc
;
426 * Using only the target field
427 * class's name, we're not
428 * reaching the target field
429 * class. This is not supported
445 * Creates an absolute field ref from IR field path `tgt_ir_field_path`.
447 * Returns a negative value if this resolving operation failed.
450 int create_absolute_field_ref(struct ctx
*ctx
,
451 const bt_field_path
*tgt_ir_field_path
, GString
*tgt_field_ref
,
452 struct fs_sink_ctf_field_class
**user_tgt_fc
)
455 struct fs_sink_ctf_field_class
*fc
= NULL
;
458 switch (bt_field_path_get_root_scope(tgt_ir_field_path
)) {
459 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
:
460 BT_ASSERT(ctx
->cur_sc
);
461 fc
= ctx
->cur_sc
->packet_context_fc
;
462 g_string_assign(tgt_field_ref
, "stream.packet.context");
464 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
:
465 BT_ASSERT(ctx
->cur_sc
);
466 fc
= ctx
->cur_sc
->event_common_context_fc
;
467 g_string_assign(tgt_field_ref
, "stream.event.context");
469 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
:
470 BT_ASSERT(ctx
->cur_ec
);
471 fc
= ctx
->cur_ec
->spec_context_fc
;
472 g_string_assign(tgt_field_ref
, "event.context");
474 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
:
475 BT_ASSERT(ctx
->cur_ec
);
476 fc
= ctx
->cur_ec
->payload_fc
;
477 g_string_assign(tgt_field_ref
, "event.fields");
485 for (i
= 0; i
< bt_field_path_get_item_count(tgt_ir_field_path
); i
++) {
486 const bt_field_path_item
*fp_item
=
487 bt_field_path_borrow_item_by_index_const(
488 tgt_ir_field_path
, i
);
489 struct fs_sink_ctf_named_field_class
*named_fc
= NULL
;
491 if (bt_field_path_item_get_type(fp_item
) !=
492 BT_FIELD_PATH_ITEM_TYPE_INDEX
) {
493 /* Not supported by TSDL 1.8 */
499 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
500 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
501 BT_FIELD_PATH_ITEM_TYPE_INDEX
);
502 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
504 bt_field_path_item_index_get_index(fp_item
));
506 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
507 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
508 BT_FIELD_PATH_ITEM_TYPE_INDEX
);
509 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
511 bt_field_path_item_index_get_index(fp_item
));
518 g_string_append_c(tgt_field_ref
, '.');
519 g_string_append(tgt_field_ref
, named_fc
->name
->str
);
532 * Resolves a target field class located at `tgt_ir_field_path`, writing
533 * the resolved field ref to `tgt_field_ref` and setting
534 * `*create_before` according to whether or not the target field must be
535 * created immediately before (in which case `tgt_field_ref` is
539 void resolve_field_class(struct ctx
*ctx
,
540 const bt_field_path
*tgt_ir_field_path
,
541 GString
*tgt_field_ref
, bool *create_before
,
542 struct fs_sink_ctf_field_class
**user_tgt_fc
)
545 bt_field_path_scope tgt_scope
;
547 *create_before
= false;
549 if (!tgt_ir_field_path
) {
550 *create_before
= true;
554 tgt_scope
= bt_field_path_get_root_scope(tgt_ir_field_path
);
556 if (tgt_scope
== ctx
->cur_scope
) {
558 * Try, in this order:
560 * 1. Use a relative path, using only the target field
561 * class's name. This is what is the most commonly
562 * supported by popular CTF reading tools.
564 * 2. Use an absolute path. This could fail if there's
565 * an array field class from the current root's field
566 * class to the target field class.
568 * 3. Create the target field class before the
569 * requesting field class (fallback).
571 ret
= create_relative_field_ref(ctx
, tgt_ir_field_path
,
572 tgt_field_ref
, user_tgt_fc
);
574 ret
= create_absolute_field_ref(ctx
, tgt_ir_field_path
,
575 tgt_field_ref
, user_tgt_fc
);
577 *create_before
= true;
582 ret
= create_absolute_field_ref(ctx
, tgt_ir_field_path
,
583 tgt_field_ref
, user_tgt_fc
);
585 /* It must always work in previous scopes */
594 int translate_field_class(struct ctx
*ctx
);
597 void append_to_parent_field_class(struct ctx
*ctx
,
598 struct fs_sink_ctf_field_class
*fc
)
600 struct fs_sink_ctf_field_class
*parent_fc
=
601 cur_path_stack_top(ctx
)->parent_fc
;
603 switch (parent_fc
->type
) {
604 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
605 fs_sink_ctf_field_class_struct_append_member((void *) parent_fc
,
606 cur_path_stack_top(ctx
)->name
->str
, fc
);
608 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
610 struct fs_sink_ctf_field_class_option
*opt_fc
=
613 BT_ASSERT(!opt_fc
->content_fc
);
614 opt_fc
->content_fc
= fc
;
615 opt_fc
->base
.alignment
= fc
->alignment
;
618 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
619 fs_sink_ctf_field_class_variant_append_option((void *) parent_fc
,
620 cur_path_stack_top(ctx
)->name
->str
, fc
);
622 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
623 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
625 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
628 BT_ASSERT(!array_base_fc
->elem_fc
);
629 array_base_fc
->elem_fc
= fc
;
630 array_base_fc
->base
.alignment
= fc
->alignment
;
639 void update_parent_field_class_alignment(struct ctx
*ctx
,
640 unsigned int alignment
)
642 struct fs_sink_ctf_field_class
*parent_fc
=
643 cur_path_stack_top(ctx
)->parent_fc
;
645 switch (parent_fc
->type
) {
646 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
647 fs_sink_ctf_field_class_struct_align_at_least(
648 (void *) parent_fc
, alignment
);
650 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
651 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
653 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
656 array_base_fc
->base
.alignment
= alignment
;
665 int translate_structure_field_class_members(struct ctx
*ctx
,
666 struct fs_sink_ctf_field_class_struct
*struct_fc
,
667 const bt_field_class
*ir_fc
)
672 for (i
= 0; i
< bt_field_class_structure_get_member_count(ir_fc
); i
++) {
673 const bt_field_class_structure_member
*member
;
675 const bt_field_class
*memb_ir_fc
;
678 bt_field_class_structure_borrow_member_by_index_const(
680 name
= bt_field_class_structure_member_get_name(member
);
681 memb_ir_fc
= bt_field_class_structure_member_borrow_field_class_const(
683 ret
= cur_path_stack_push(ctx
, i
, name
, true, memb_ir_fc
,
686 BT_COMP_LOGE("Cannot translate structure field class member: "
687 "name=\"%s\"", name
);
691 ret
= translate_field_class(ctx
);
693 BT_COMP_LOGE("Cannot translate structure field class member: "
694 "name=\"%s\"", name
);
698 cur_path_stack_pop(ctx
);
706 int translate_structure_field_class(struct ctx
*ctx
)
709 struct fs_sink_ctf_field_class_struct
*fc
=
710 fs_sink_ctf_field_class_struct_create_empty(
711 cur_path_stack_top(ctx
)->ir_fc
,
712 cur_path_stack_top(ctx
)->index_in_parent
);
715 append_to_parent_field_class(ctx
, (void *) fc
);
716 ret
= translate_structure_field_class_members(ctx
, fc
, fc
->base
.ir_fc
);
721 update_parent_field_class_alignment(ctx
, fc
->base
.alignment
);
728 * This function protects a given variant FC option name (with the `_`
729 * prefix) if required. On success, `name_buf->str` contains the variant
730 * FC option name to use (original option name or protected if
733 * One of the goals of `sink.ctf.fs` is to write a CTF trace which is as
734 * close as possible to an original CTF trace as decoded by
737 * This scenario is valid in CTF 1.8:
749 * Once in trace IR, the enumeration FC mapping names and variant FC
750 * option names are kept as is. For this reason, we don't want to
751 * protect the variant FC option names here (by prepending `_`): this
752 * would make the variant FC option name and the enumeration FC mapping
755 * This scenario is also valid in CTF 1.8:
767 * Once in trace IR, the enumeration FC mapping names are kept as is,
768 * but the `_HELLO` variant FC option name becomes `HELLO` (unprotected
769 * for presentation, as recommended by CTF 1.8). When going back to
770 * TSDL, we need to protect `HELLO` so that it becomes `_HELLO` to match
771 * the corresponding enumeration FC mapping name.
773 * This scenario is also valid in CTF 1.8:
785 * Once in trace IR, the enumeration FC mapping names are kept as is,
786 * but the `__HELLO` variant FC option name becomes `_HELLO`
787 * (unprotected). When going back to TSDL, we need to protect `_HELLO`
788 * so that it becomes `__HELLO` to match the corresponding enumeration
791 * `src.ctf.fs` always uses the _same_ integer range sets for a selector
792 * FC mapping and a corresponding variant FC option. We can use that
793 * fact to find the original variant FC option names by matching variant
794 * FC options and enumeration FC mappings by range set.
797 int maybe_protect_variant_option_name(const bt_field_class
*ir_var_fc
,
798 const bt_field_class
*ir_tag_fc
, uint64_t opt_i
,
803 bt_field_class_type ir_var_fc_type
;
804 const void *opt_ranges
= NULL
;
805 const char *mapping_label
= NULL
;
806 const char *ir_opt_name
;
807 const bt_field_class_variant_option
*base_var_opt
;
808 bool force_protect
= false;
810 ir_var_fc_type
= bt_field_class_get_type(ir_var_fc
);
811 base_var_opt
= bt_field_class_variant_borrow_option_by_index_const(
813 BT_ASSERT(base_var_opt
);
814 ir_opt_name
= bt_field_class_variant_option_get_name(base_var_opt
);
815 BT_ASSERT(ir_opt_name
);
818 * Check if the variant FC option name is required to be
819 * protected (reserved TSDL keyword or starts with `_`). In that
820 * case, the name of the selector FC mapping we find must match
821 * exactly the protected name.
823 force_protect
= must_protect_identifier(ir_opt_name
);
825 g_string_assign(name_buf
, "_");
826 g_string_append(name_buf
, ir_opt_name
);
828 g_string_assign(name_buf
, ir_opt_name
);
831 /* Borrow option's ranges */
832 if (ir_var_fc_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
) {
833 /* No ranges: we're done */
835 } if (ir_var_fc_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
) {
836 const bt_field_class_variant_with_selector_field_integer_unsigned_option
*var_opt
=
837 bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const(
840 bt_field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const(
843 const bt_field_class_variant_with_selector_field_integer_signed_option
*var_opt
=
844 bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const(
847 bt_field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const(
851 /* Find corresponding mapping by range set in selector FC */
852 for (i
= 0; i
< bt_field_class_enumeration_get_mapping_count(ir_tag_fc
);
854 if (ir_var_fc_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
) {
855 const bt_field_class_enumeration_mapping
*mapping_base
;
856 const bt_field_class_enumeration_unsigned_mapping
*mapping
;
857 const bt_integer_range_set_unsigned
*mapping_ranges
;
859 mapping
= bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
861 mapping_ranges
= bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
864 if (bt_integer_range_set_unsigned_is_equal(opt_ranges
,
866 /* We have a winner */
868 bt_field_class_enumeration_unsigned_mapping_as_mapping_const(
871 bt_field_class_enumeration_mapping_get_label(
876 const bt_field_class_enumeration_mapping
*mapping_base
;
877 const bt_field_class_enumeration_signed_mapping
*mapping
;
878 const bt_integer_range_set_signed
*mapping_ranges
;
880 mapping
= bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
882 mapping_ranges
= bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
885 if (bt_integer_range_set_signed_is_equal(opt_ranges
,
887 /* We have a winner */
889 bt_field_class_enumeration_signed_mapping_as_mapping_const(
892 bt_field_class_enumeration_mapping_get_label(
899 if (!mapping_label
) {
900 /* Range set not found: invalid selector for CTF 1.8 */
906 * If the enumeration FC mapping name is not the same as the
907 * variant FC option name and we didn't protect already, try
908 * protecting the option name and check again.
910 if (strcmp(mapping_label
, name_buf
->str
) != 0) {
916 if (mapping_label
[0] == '\0') {
921 g_string_assign(name_buf
, "_");
922 g_string_append(name_buf
, ir_opt_name
);
924 if (strcmp(mapping_label
, name_buf
->str
) != 0) {
935 int translate_option_field_class(struct ctx
*ctx
)
937 struct fs_sink_ctf_field_class_option
*fc
=
938 fs_sink_ctf_field_class_option_create_empty(
939 cur_path_stack_top(ctx
)->ir_fc
,
940 cur_path_stack_top(ctx
)->index_in_parent
);
941 const bt_field_class
*content_ir_fc
=
942 bt_field_class_option_borrow_field_class_const(fc
->base
.ir_fc
);
948 * CTF 1.8 does not support the option field class type. To
949 * write something anyway, this component translates this type
950 * to a variant field class where the options are:
952 * * An empty structure field class.
953 * * The optional field class itself.
955 * The "tag" is always generated/before in that case (an 8-bit
956 * unsigned enumeration field class).
958 append_to_parent_field_class(ctx
, (void *) fc
);
959 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, false, content_ir_fc
,
962 BT_COMP_LOGE_STR("Cannot translate option field class content.");
966 ret
= translate_field_class(ctx
);
968 BT_COMP_LOGE_STR("Cannot translate option field class content.");
972 cur_path_stack_pop(ctx
);
973 update_parent_field_class_alignment(ctx
, fc
->base
.alignment
);
980 int translate_variant_field_class(struct ctx
*ctx
)
984 struct fs_sink_ctf_field_class_variant
*fc
=
985 fs_sink_ctf_field_class_variant_create_empty(
986 cur_path_stack_top(ctx
)->ir_fc
,
987 cur_path_stack_top(ctx
)->index_in_parent
);
988 bt_field_class_type ir_fc_type
;
989 const bt_field_path
*ir_selector_field_path
= NULL
;
990 struct fs_sink_ctf_field_class
*tgt_fc
= NULL
;
991 GString
*name_buf
= g_string_new(NULL
);
992 bt_value
*prot_opt_names
= bt_value_array_create();
997 BT_ASSERT(prot_opt_names
);
998 ir_fc_type
= bt_field_class_get_type(fc
->base
.ir_fc
);
999 opt_count
= bt_field_class_variant_get_option_count(fc
->base
.ir_fc
);
1001 if (bt_field_class_type_is(ir_fc_type
,
1002 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SELECTOR_FIELD
)) {
1003 ir_selector_field_path
= bt_field_class_variant_with_selector_field_borrow_selector_field_path_const(
1005 BT_ASSERT(ir_selector_field_path
);
1008 /* Resolve tag field class before appending to parent */
1009 resolve_field_class(ctx
, ir_selector_field_path
, fc
->tag_ref
,
1010 &fc
->tag_is_before
, &tgt_fc
);
1012 if (ir_selector_field_path
&& tgt_fc
) {
1013 uint64_t mapping_count
;
1014 uint64_t option_count
;
1016 /* CTF 1.8: selector FC must be an enumeration FC */
1017 bt_field_class_type type
= bt_field_class_get_type(
1020 if (!bt_field_class_type_is(type
,
1021 BT_FIELD_CLASS_TYPE_ENUMERATION
)) {
1022 fc
->tag_is_before
= true;
1027 * Call maybe_protect_variant_option_name() for each
1028 * option below. In that case we also want selector FC
1029 * to contain as many mappings as the variant FC has
1032 mapping_count
= bt_field_class_enumeration_get_mapping_count(
1034 option_count
= bt_field_class_variant_get_option_count(
1037 if (mapping_count
!= option_count
) {
1038 fc
->tag_is_before
= true;
1043 * No compatible selector field class for CTF 1.8:
1044 * create the appropriate selector field class.
1046 fc
->tag_is_before
= true;
1052 * First pass: detect any option name clash with option name
1053 * protection. In that case, we don't fail: just create the
1054 * selector field class before the variant field class.
1056 * After this, `prot_opt_names` contains the final option names,
1057 * potentially protected if needed. They can still be invalid
1058 * TSDL identifiers however; this will be checked by
1059 * cur_path_stack_push().
1061 for (i
= 0; i
< opt_count
; i
++) {
1062 if (!fc
->tag_is_before
) {
1063 BT_ASSERT(tgt_fc
->ir_fc
);
1064 ret
= maybe_protect_variant_option_name(fc
->base
.ir_fc
,
1065 tgt_fc
->ir_fc
, i
, name_buf
);
1067 fc
->tag_is_before
= true;
1071 ret
= bt_value_array_append_string_element(prot_opt_names
,
1078 for (i
= 0; i
< opt_count
; i
++) {
1080 const bt_value
*opt_name_a
=
1081 bt_value_array_borrow_element_by_index_const(
1084 for (j
= 0; j
< opt_count
; j
++) {
1085 const bt_value
*opt_name_b
;
1092 bt_value_array_borrow_element_by_index_const(
1094 if (bt_value_is_equal(opt_name_a
, opt_name_b
)) {
1096 * Variant FC option names are not
1097 * unique when protected.
1099 fc
->tag_is_before
= true;
1100 goto append_to_parent
;
1106 append_to_parent_field_class(ctx
, (void *) fc
);
1108 for (i
= 0; i
< opt_count
; i
++) {
1109 const bt_field_class_variant_option
*opt
;
1110 const bt_field_class
*opt_ir_fc
;
1111 const bt_value
*prot_opt_name_val
=
1112 bt_value_array_borrow_element_by_index_const(
1114 const char *prot_opt_name
= bt_value_string_get(
1117 BT_ASSERT(prot_opt_name
);
1118 opt
= bt_field_class_variant_borrow_option_by_index_const(
1120 opt_ir_fc
= bt_field_class_variant_option_borrow_field_class_const(
1124 * We don't ask cur_path_stack_push() to protect the
1125 * option name because it's already protected at this
1128 ret
= cur_path_stack_push(ctx
, i
, prot_opt_name
, false,
1129 opt_ir_fc
, (void *) fc
);
1131 BT_COMP_LOGE("Cannot translate variant field class option: "
1132 "name=\"%s\"", prot_opt_name
);
1136 ret
= translate_field_class(ctx
);
1138 BT_COMP_LOGE("Cannot translate variant field class option: "
1139 "name=\"%s\"", prot_opt_name
);
1143 cur_path_stack_pop(ctx
);
1148 g_string_free(name_buf
, TRUE
);
1151 bt_value_put_ref(prot_opt_names
);
1156 int translate_static_array_field_class(struct ctx
*ctx
)
1158 struct fs_sink_ctf_field_class_array
*fc
=
1159 fs_sink_ctf_field_class_array_create_empty(
1160 cur_path_stack_top(ctx
)->ir_fc
,
1161 cur_path_stack_top(ctx
)->index_in_parent
);
1162 const bt_field_class
*elem_ir_fc
=
1163 bt_field_class_array_borrow_element_field_class_const(
1164 fc
->base
.base
.ir_fc
);
1168 append_to_parent_field_class(ctx
, (void *) fc
);
1169 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, false, elem_ir_fc
,
1172 BT_COMP_LOGE_STR("Cannot translate static array field class element.");
1176 ret
= translate_field_class(ctx
);
1178 BT_COMP_LOGE_STR("Cannot translate static array field class element.");
1182 cur_path_stack_pop(ctx
);
1183 update_parent_field_class_alignment(ctx
, fc
->base
.base
.alignment
);
1190 int translate_dynamic_array_field_class(struct ctx
*ctx
)
1192 struct fs_sink_ctf_field_class_sequence
*fc
=
1193 fs_sink_ctf_field_class_sequence_create_empty(
1194 cur_path_stack_top(ctx
)->ir_fc
,
1195 cur_path_stack_top(ctx
)->index_in_parent
);
1196 const bt_field_class
*elem_ir_fc
=
1197 bt_field_class_array_borrow_element_field_class_const(
1198 fc
->base
.base
.ir_fc
);
1203 /* Resolve length field class before appending to parent */
1204 if (bt_field_class_get_type(cur_path_stack_top(ctx
)->ir_fc
) ==
1205 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
) {
1206 resolve_field_class(ctx
,
1207 bt_field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
1208 fc
->base
.base
.ir_fc
),
1209 fc
->length_ref
, &fc
->length_is_before
, NULL
);
1212 append_to_parent_field_class(ctx
, (void *) fc
);
1213 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, false, elem_ir_fc
,
1216 BT_COMP_LOGE_STR("Cannot translate dynamic array field class element.");
1220 ret
= translate_field_class(ctx
);
1222 BT_COMP_LOGE_STR("Cannot translate dynamic array field class element.");
1226 cur_path_stack_pop(ctx
);
1227 update_parent_field_class_alignment(ctx
, fc
->base
.base
.alignment
);
1234 int translate_bool_field_class(struct ctx
*ctx
)
1236 struct fs_sink_ctf_field_class_bool
*fc
=
1237 fs_sink_ctf_field_class_bool_create(
1238 cur_path_stack_top(ctx
)->ir_fc
,
1239 cur_path_stack_top(ctx
)->index_in_parent
);
1242 append_to_parent_field_class(ctx
, (void *) fc
);
1247 int translate_bit_array_field_class(struct ctx
*ctx
)
1249 struct fs_sink_ctf_field_class_bit_array
*fc
=
1250 fs_sink_ctf_field_class_bit_array_create(
1251 cur_path_stack_top(ctx
)->ir_fc
,
1252 cur_path_stack_top(ctx
)->index_in_parent
);
1255 append_to_parent_field_class(ctx
, (void *) fc
);
1260 int translate_integer_field_class(struct ctx
*ctx
)
1262 struct fs_sink_ctf_field_class_int
*fc
=
1263 fs_sink_ctf_field_class_int_create(
1264 cur_path_stack_top(ctx
)->ir_fc
,
1265 cur_path_stack_top(ctx
)->index_in_parent
);
1268 append_to_parent_field_class(ctx
, (void *) fc
);
1273 int translate_real_field_class(struct ctx
*ctx
)
1275 struct fs_sink_ctf_field_class_float
*fc
=
1276 fs_sink_ctf_field_class_float_create(
1277 cur_path_stack_top(ctx
)->ir_fc
,
1278 cur_path_stack_top(ctx
)->index_in_parent
);
1281 append_to_parent_field_class(ctx
, (void *) fc
);
1286 int translate_string_field_class(struct ctx
*ctx
)
1288 struct fs_sink_ctf_field_class_string
*fc
=
1289 fs_sink_ctf_field_class_string_create(
1290 cur_path_stack_top(ctx
)->ir_fc
,
1291 cur_path_stack_top(ctx
)->index_in_parent
);
1294 append_to_parent_field_class(ctx
, (void *) fc
);
1299 * Translates a field class, recursively.
1301 * The field class's IR field class, parent field class, and index
1302 * within its parent are in the context's current path's top element
1303 * (cur_path_stack_top()).
1306 int translate_field_class(struct ctx
*ctx
)
1309 bt_field_class_type ir_fc_type
=
1310 bt_field_class_get_type(cur_path_stack_top(ctx
)->ir_fc
);
1312 if (ir_fc_type
== BT_FIELD_CLASS_TYPE_BOOL
) {
1313 ret
= translate_bool_field_class(ctx
);
1314 } else if (ir_fc_type
== BT_FIELD_CLASS_TYPE_BIT_ARRAY
) {
1315 ret
= translate_bit_array_field_class(ctx
);
1316 } else if (bt_field_class_type_is(ir_fc_type
,
1317 BT_FIELD_CLASS_TYPE_INTEGER
)) {
1318 ret
= translate_integer_field_class(ctx
);
1319 } else if (bt_field_class_type_is(ir_fc_type
,
1320 BT_FIELD_CLASS_TYPE_REAL
)) {
1321 ret
= translate_real_field_class(ctx
);
1322 } else if (ir_fc_type
== BT_FIELD_CLASS_TYPE_STRING
) {
1323 ret
= translate_string_field_class(ctx
);
1324 } else if (ir_fc_type
== BT_FIELD_CLASS_TYPE_STRUCTURE
) {
1325 ret
= translate_structure_field_class(ctx
);
1326 } else if (ir_fc_type
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
) {
1327 ret
= translate_static_array_field_class(ctx
);
1328 } else if (bt_field_class_type_is(ir_fc_type
,
1329 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
)) {
1330 ret
= translate_dynamic_array_field_class(ctx
);
1331 } else if (bt_field_class_type_is(ir_fc_type
,
1332 BT_FIELD_CLASS_TYPE_OPTION
)) {
1333 ret
= translate_option_field_class(ctx
);
1334 } else if (bt_field_class_type_is(ir_fc_type
,
1335 BT_FIELD_CLASS_TYPE_VARIANT
)) {
1336 ret
= translate_variant_field_class(ctx
);
1345 int set_field_ref(struct fs_sink_ctf_field_class
*fc
, const char *fc_name
,
1346 struct fs_sink_ctf_field_class
*parent_fc
)
1349 GString
*field_ref
= NULL
;
1351 const char *tgt_type
;
1352 struct fs_sink_ctf_field_class_struct
*parent_struct_fc
=
1355 unsigned int suffix
= 0;
1357 if (!fc_name
|| !parent_fc
||
1358 parent_fc
->type
!= FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
1365 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
1368 * CTF 1.8 does not support the option field class type.
1369 * To write something anyway, this component translates
1370 * this type to a variant field class where the options
1373 * * An empty structure field class.
1374 * * The optional field class itself.
1376 * Because the option field class becomes a CTF variant
1377 * field class, we use the term "tag" too here.
1379 * The "tag" is always generated/before in that case (an
1380 * 8-bit unsigned enumeration field class).
1382 struct fs_sink_ctf_field_class_option
*opt_fc
= (void *) fc
;
1384 field_ref
= opt_fc
->tag_ref
;
1389 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1391 struct fs_sink_ctf_field_class_sequence
*seq_fc
= (void *) fc
;
1393 field_ref
= seq_fc
->length_ref
;
1394 is_before
= seq_fc
->length_is_before
;
1398 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
1400 struct fs_sink_ctf_field_class_variant
*var_fc
= (void *) fc
;
1402 field_ref
= var_fc
->tag_ref
;
1403 is_before
= var_fc
->tag_is_before
;
1411 BT_ASSERT(field_ref
);
1417 /* Initial field ref */
1418 g_string_printf(field_ref
, "__%s_%s", fc_name
, tgt_type
);
1421 * Make sure field ref does not clash with an existing field
1422 * class name within the same parent structure field class.
1425 bool name_ok
= true;
1427 for (i
= 0; i
< parent_struct_fc
->members
->len
; i
++) {
1428 struct fs_sink_ctf_named_field_class
*named_fc
=
1429 fs_sink_ctf_field_class_struct_borrow_member_by_index(
1430 parent_struct_fc
, i
);
1432 if (strcmp(field_ref
->str
, named_fc
->name
->str
) == 0) {
1440 /* No clash: we're done */
1444 /* Append suffix and try again */
1445 g_string_printf(field_ref
, "__%s_%s_%u", fc_name
, tgt_type
,
1455 * This function recursively sets field refs of sequence and variant
1456 * field classes when they are immediately before, avoiding name clashes
1457 * with existing field class names.
1459 * It can fail at this point if, for example, a sequence field class of
1460 * which to set the length's field ref has something else than a
1461 * structure field class as its parent: in this case, there's no
1462 * location to place the length field class immediately before the
1463 * sequence field class.
1466 int set_field_refs(struct fs_sink_ctf_field_class
* const fc
,
1467 const char *fc_name
, struct fs_sink_ctf_field_class
*parent_fc
)
1470 enum fs_sink_ctf_field_class_type fc_type
;
1476 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
1478 struct fs_sink_ctf_field_class_option
*opt_fc
= (void *) fc
;
1480 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
1485 ret
= set_field_refs(opt_fc
->content_fc
, NULL
, fc
);
1492 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
1493 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
1497 struct fs_sink_ctf_field_class_struct
*struct_fc
= NULL
;
1498 struct fs_sink_ctf_field_class_variant
*var_fc
= NULL
;
1499 struct fs_sink_ctf_named_field_class
*named_fc
;
1501 if (fc_type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
1502 struct_fc
= (void *) fc
;
1503 len
= struct_fc
->members
->len
;
1505 var_fc
= (void *) fc
;
1506 len
= var_fc
->options
->len
;
1507 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
1513 for (i
= 0; i
< len
; i
++) {
1514 if (fc_type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
1515 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
1518 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
1522 ret
= set_field_refs(named_fc
->fc
, named_fc
->name
->str
,
1531 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
1532 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1534 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
1537 if (fc_type
== FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
1538 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
1544 ret
= set_field_refs(array_base_fc
->elem_fc
, NULL
, fc
);
1560 * This function translates a root scope trace IR field class to
1561 * a CTF IR field class.
1563 * The resulting CTF IR field class is written to `*fc` so that it
1564 * exists as the parent object's (stream class or event class) true root
1565 * field class during the recursive translation for resolving purposes.
1566 * This is also why this function creates the empty structure field
1567 * class and then calls translate_structure_field_class_members() to
1571 int translate_scope_field_class(struct ctx
*ctx
, bt_field_path_scope scope
,
1572 struct fs_sink_ctf_field_class
**fc
,
1573 const bt_field_class
*ir_fc
)
1581 BT_ASSERT(bt_field_class_get_type(ir_fc
) ==
1582 BT_FIELD_CLASS_TYPE_STRUCTURE
);
1584 *fc
= (void *) fs_sink_ctf_field_class_struct_create_empty(
1585 ir_fc
, UINT64_C(-1));
1587 ctx
->cur_scope
= scope
;
1588 BT_ASSERT(ctx
->cur_path
->len
== 0);
1589 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, false, ir_fc
, NULL
);
1591 BT_COMP_LOGE("Cannot translate scope structure field class: "
1596 ret
= translate_structure_field_class_members(ctx
, (void *) *fc
, ir_fc
);
1598 BT_COMP_LOGE("Cannot translate scope structure field class: "
1603 cur_path_stack_pop(ctx
);
1605 /* Set field refs for preceding targets */
1606 ret
= set_field_refs(*fc
, NULL
, NULL
);
1613 void ctx_init(struct ctx
*ctx
, struct fs_sink_comp
*fs_sink
)
1615 memset(ctx
, 0, sizeof(struct ctx
));
1616 ctx
->cur_path
= g_array_new(FALSE
, TRUE
,
1617 sizeof(struct field_path_elem
));
1618 BT_ASSERT(ctx
->cur_path
);
1619 ctx
->log_level
= fs_sink
->log_level
;
1620 ctx
->self_comp
= fs_sink
->self_comp
;
1624 void ctx_fini(struct ctx
*ctx
)
1626 if (ctx
->cur_path
) {
1627 g_array_free(ctx
->cur_path
, TRUE
);
1628 ctx
->cur_path
= NULL
;
1633 int translate_event_class(struct fs_sink_comp
*fs_sink
,
1634 struct fs_sink_ctf_stream_class
*sc
,
1635 const bt_event_class
*ir_ec
,
1636 struct fs_sink_ctf_event_class
**out_ec
)
1640 struct fs_sink_ctf_event_class
*ec
;
1645 ctx_init(&ctx
, fs_sink
);
1646 ec
= fs_sink_ctf_event_class_create(sc
, ir_ec
);
1650 ret
= translate_scope_field_class(&ctx
, BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
,
1651 &ec
->spec_context_fc
,
1652 bt_event_class_borrow_specific_context_field_class_const(
1658 ret
= translate_scope_field_class(&ctx
, BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
,
1660 bt_event_class_borrow_payload_field_class_const(ir_ec
));
1672 int try_translate_event_class_trace_ir_to_ctf_ir(
1673 struct fs_sink_comp
*fs_sink
,
1674 struct fs_sink_ctf_stream_class
*sc
,
1675 const bt_event_class
*ir_ec
,
1676 struct fs_sink_ctf_event_class
**out_ec
)
1683 /* Check in hash table first */
1684 *out_ec
= g_hash_table_lookup(sc
->event_classes_from_ir
, ir_ec
);
1685 if (G_LIKELY(*out_ec
)) {
1689 ret
= translate_event_class(fs_sink
, sc
, ir_ec
, out_ec
);
1695 bool default_clock_class_name_exists(struct fs_sink_ctf_trace
*trace
,
1698 bool exists
= false;
1701 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1702 struct fs_sink_ctf_stream_class
*sc
=
1703 trace
->stream_classes
->pdata
[i
];
1705 if (sc
->default_clock_class_name
->len
== 0) {
1706 /* No default clock class */
1710 if (strcmp(sc
->default_clock_class_name
->str
, name
) == 0) {
1721 void make_unique_default_clock_class_name(struct fs_sink_ctf_stream_class
*sc
)
1723 unsigned int suffix
= 0;
1726 g_string_assign(sc
->default_clock_class_name
, "");
1727 sprintf(buf
, "default");
1729 while (default_clock_class_name_exists(sc
->trace
, buf
)) {
1730 sprintf(buf
, "default%u", suffix
);
1734 g_string_assign(sc
->default_clock_class_name
, buf
);
1738 int translate_stream_class(struct fs_sink_comp
*fs_sink
,
1739 struct fs_sink_ctf_trace
*trace
,
1740 const bt_stream_class
*ir_sc
,
1741 struct fs_sink_ctf_stream_class
**out_sc
)
1748 ctx_init(&ctx
, fs_sink
);
1749 *out_sc
= fs_sink_ctf_stream_class_create(trace
, ir_sc
);
1752 /* Set default clock class's protected name, if any */
1753 if ((*out_sc
)->default_clock_class
) {
1754 const char *name
= bt_clock_class_get_name(
1755 (*out_sc
)->default_clock_class
);
1758 /* Try original name, protected */
1759 g_string_assign((*out_sc
)->default_clock_class_name
,
1762 if (must_protect_identifier(name
)) {
1764 (*out_sc
)->default_clock_class_name
,
1768 g_string_assign((*out_sc
)->default_clock_class_name
,
1770 if (!ist_valid_identifier(
1771 (*out_sc
)->default_clock_class_name
->str
)) {
1772 /* Invalid: create a new name */
1773 make_unique_default_clock_class_name(*out_sc
);
1776 /* No name: create a name */
1777 make_unique_default_clock_class_name(*out_sc
);
1781 ctx
.cur_sc
= *out_sc
;
1782 ret
= translate_scope_field_class(&ctx
, BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
,
1783 &(*out_sc
)->packet_context_fc
,
1784 bt_stream_class_borrow_packet_context_field_class_const(ir_sc
));
1789 if ((*out_sc
)->packet_context_fc
) {
1791 * Make sure the structure field class's alignment is
1792 * enough: 8 is what we use for our own special members
1793 * in the packet context.
1795 fs_sink_ctf_field_class_struct_align_at_least(
1796 (void *) (*out_sc
)->packet_context_fc
, 8);
1799 ret
= translate_scope_field_class(&ctx
, BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
,
1800 &(*out_sc
)->event_common_context_fc
,
1801 bt_stream_class_borrow_event_common_context_field_class_const(
1810 fs_sink_ctf_stream_class_destroy(*out_sc
);
1819 int try_translate_stream_class_trace_ir_to_ctf_ir(
1820 struct fs_sink_comp
*fs_sink
,
1821 struct fs_sink_ctf_trace
*trace
,
1822 const bt_stream_class
*ir_sc
,
1823 struct fs_sink_ctf_stream_class
**out_sc
)
1831 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1832 *out_sc
= trace
->stream_classes
->pdata
[i
];
1834 if ((*out_sc
)->ir_sc
== ir_sc
) {
1839 ret
= translate_stream_class(fs_sink
, trace
, ir_sc
, out_sc
);
1846 struct fs_sink_ctf_trace
*translate_trace_trace_ir_to_ctf_ir(
1847 struct fs_sink_comp
*fs_sink
, const bt_trace
*ir_trace
)
1851 struct fs_sink_ctf_trace
*trace
= NULL
;
1853 /* Check that trace's environment is TSDL-compatible */
1854 count
= bt_trace_get_environment_entry_count(ir_trace
);
1855 for (i
= 0; i
< count
; i
++) {
1857 const bt_value
*val
;
1859 bt_trace_borrow_environment_entry_by_index_const(
1860 ir_trace
, i
, &name
, &val
);
1862 if (!ist_valid_identifier(name
)) {
1863 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, fs_sink
->log_level
,
1865 "Unsupported trace class's environment entry name: "
1866 "name=\"%s\"", name
);
1870 switch (bt_value_get_type(val
)) {
1871 case BT_VALUE_TYPE_SIGNED_INTEGER
:
1872 case BT_VALUE_TYPE_STRING
:
1875 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, fs_sink
->log_level
,
1877 "Unsupported trace class's environment entry value type: "
1879 bt_common_value_type_string(
1880 bt_value_get_type(val
)));
1885 trace
= fs_sink_ctf_trace_create(ir_trace
);