2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
7 #define BT_COMP_LOG_SELF_COMP (ctx->self_comp)
8 #define BT_LOG_OUTPUT_LEVEL (ctx->log_level)
9 #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/TRANSLATE-TRACE-IR-TO-CTF-IR"
10 #include "logging/comp-logging.h"
12 #include "translate-trace-ir-to-ctf-ir.hpp"
14 #include <babeltrace2/babeltrace.h>
15 #include "common/macros.h"
16 #include "common/common.h"
17 #include "common/assert.h"
23 #include "fs-sink.hpp"
24 #include "fs-sink-ctf-meta.hpp"
26 struct field_path_elem
{
27 uint64_t index_in_parent
;
31 const bt_field_class
*ir_fc
;
34 struct fs_sink_ctf_field_class
*parent_fc
;
38 bt_logging_level log_level
;
39 bt_self_component
*self_comp
;
42 struct fs_sink_ctf_stream_class
*cur_sc
;
45 struct fs_sink_ctf_event_class
*cur_ec
;
47 bt_field_path_scope cur_scope
;
50 * Array of `struct field_path_elem` */
55 struct field_path_elem
*cur_path_stack_at(struct ctx
*ctx
, uint64_t i
)
57 BT_ASSERT(i
< ctx
->cur_path
->len
);
58 return &g_array_index(ctx
->cur_path
, struct field_path_elem
, i
);
62 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);
69 bool is_reserved_member_name(const char *name
, const char *reserved_name
)
71 bool is_reserved
= false;
73 if (strcmp(name
, reserved_name
) == 0) {
78 if (name
[0] == '_' && strcmp(&name
[1], reserved_name
) == 0) {
87 static const char *reserved_tsdl_keywords
[] = {
119 bool ist_valid_identifier(const char *name
)
123 bool ist_valid
= true;
125 /* Make sure the name is not a reserved keyword */
126 for (i
= 0; i
< sizeof(reserved_tsdl_keywords
) / sizeof(*reserved_tsdl_keywords
);
128 if (strcmp(name
, reserved_tsdl_keywords
[i
]) == 0) {
134 /* Make sure the name is not an empty string */
135 if (strlen(name
) == 0) {
140 /* Make sure the name starts with a letter or `_` */
141 if (!isalpha((unsigned char) name
[0]) && name
[0] != '_') {
146 /* Make sure the name only contains letters, digits, and `_` */
147 for (at
= name
; *at
!= '\0'; at
++) {
148 if (!isalnum((unsigned char) *at
) && *at
!= '_') {
159 bool must_protect_identifier(const char *name
)
162 bool must_protect
= false;
164 /* Protect a reserved keyword */
165 for (i
= 0; i
< sizeof(reserved_tsdl_keywords
) / sizeof(*reserved_tsdl_keywords
);
167 if (strcmp(name
, reserved_tsdl_keywords
[i
]) == 0) {
173 /* Protect an identifier which already starts with `_` */
174 if (name
[0] == '_') {
184 int cur_path_stack_push(struct ctx
*ctx
,
185 uint64_t index_in_parent
, const char *name
,
186 bool force_protect_name
, const bt_field_class
*ir_fc
,
187 struct fs_sink_ctf_field_class
*parent_fc
)
190 struct field_path_elem
*field_path_elem
;
192 g_array_set_size(ctx
->cur_path
, ctx
->cur_path
->len
+ 1);
193 field_path_elem
= cur_path_stack_top(ctx
);
194 field_path_elem
->index_in_parent
= index_in_parent
;
195 field_path_elem
->name
= g_string_new(NULL
);
198 if (force_protect_name
) {
199 g_string_assign(field_path_elem
->name
, "_");
202 g_string_append(field_path_elem
->name
, name
);
204 if (ctx
->cur_scope
== BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
) {
205 if (is_reserved_member_name(name
, "packet_size") ||
206 is_reserved_member_name(name
, "content_size") ||
207 is_reserved_member_name(name
, "timestamp_begin") ||
208 is_reserved_member_name(name
, "timestamp_end") ||
209 is_reserved_member_name(name
, "events_discarded") ||
210 is_reserved_member_name(name
, "packet_seq_num")) {
211 BT_COMP_LOGE("Unsupported reserved TSDL structure field class member "
212 "or variant field class option name: name=\"%s\"",
219 if (!ist_valid_identifier(field_path_elem
->name
->str
)) {
221 BT_COMP_LOGE("Unsupported non-TSDL structure field class member "
222 "or variant field class option name: name=\"%s\"",
223 field_path_elem
->name
->str
);
228 field_path_elem
->ir_fc
= ir_fc
;
229 field_path_elem
->parent_fc
= parent_fc
;
236 void cur_path_stack_pop(struct ctx
*ctx
)
238 struct field_path_elem
*field_path_elem
;
240 BT_ASSERT(ctx
->cur_path
->len
> 0);
241 field_path_elem
= cur_path_stack_top(ctx
);
243 if (field_path_elem
->name
) {
244 g_string_free(field_path_elem
->name
, TRUE
);
245 field_path_elem
->name
= NULL
;
248 g_array_set_size(ctx
->cur_path
, ctx
->cur_path
->len
- 1);
252 * Creates a relative field ref (a single name) from IR field path
253 * `tgt_ir_field_path`.
255 * This function tries to locate the target field class recursively from
256 * the top to the bottom of the context's current path using only the
257 * target field class's own name. This is because many CTF reading tools
258 * do not support a relative field ref with more than one element, for
259 * example `prev_struct.len`.
261 * Returns a negative value if this resolving operation failed.
264 int create_relative_field_ref(struct ctx
*ctx
,
265 const bt_field_path
*tgt_ir_field_path
, GString
*tgt_field_ref
,
266 struct fs_sink_ctf_field_class
**user_tgt_fc
)
269 struct fs_sink_ctf_field_class
*tgt_fc
= NULL
;
272 const char *tgt_fc_name
= NULL
;
273 struct field_path_elem
*field_path_elem
;
275 /* Get target field class's name */
276 switch (bt_field_path_get_root_scope(tgt_ir_field_path
)) {
277 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
:
278 BT_ASSERT(ctx
->cur_sc
);
279 tgt_fc
= ctx
->cur_sc
->packet_context_fc
;
281 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
:
282 BT_ASSERT(ctx
->cur_sc
);
283 tgt_fc
= ctx
->cur_sc
->event_common_context_fc
;
285 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
:
286 BT_ASSERT(ctx
->cur_ec
);
287 tgt_fc
= ctx
->cur_ec
->spec_context_fc
;
289 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
:
290 BT_ASSERT(ctx
->cur_ec
);
291 tgt_fc
= ctx
->cur_ec
->payload_fc
;
299 while (i
< bt_field_path_get_item_count(tgt_ir_field_path
)) {
300 const bt_field_path_item
*fp_item
=
301 bt_field_path_borrow_item_by_index_const(
302 tgt_ir_field_path
, i
);
303 struct fs_sink_ctf_named_field_class
*named_fc
= NULL
;
308 if (bt_field_path_item_get_type(fp_item
) ==
309 BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT
) {
310 /* Not supported by CTF 1.8 */
315 switch (tgt_fc
->type
) {
316 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
317 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
318 BT_FIELD_PATH_ITEM_TYPE_INDEX
);
319 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
320 fs_sink_ctf_field_class_as_struct(tgt_fc
),
321 bt_field_path_item_index_get_index(fp_item
));
323 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
324 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
325 BT_FIELD_PATH_ITEM_TYPE_INDEX
);
326 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
327 fs_sink_ctf_field_class_as_variant(tgt_fc
),
328 bt_field_path_item_index_get_index(fp_item
));
330 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
331 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
333 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
334 fs_sink_ctf_field_class_as_array_base(tgt_fc
);
336 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
337 BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
);
338 tgt_fc
= array_base_fc
->elem_fc
;
346 tgt_fc
= named_fc
->fc
;
347 tgt_fc_name
= named_fc
->name
->str
;
353 BT_ASSERT(tgt_fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_INT
);
354 BT_ASSERT(tgt_fc_name
);
356 /* Find target field class having this name in current context */
357 for (si
= ctx
->cur_path
->len
- 1; si
>= 0; si
--) {
358 struct fs_sink_ctf_field_class
*fc
;
359 struct fs_sink_ctf_field_class_struct
*struct_fc
= NULL
;
360 struct fs_sink_ctf_field_class_variant
*var_fc
= NULL
;
361 struct fs_sink_ctf_named_field_class
*named_fc
;
364 field_path_elem
= cur_path_stack_at(ctx
, (uint64_t) si
);
365 fc
= field_path_elem
->parent_fc
;
367 /* Reached stack's bottom */
373 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
374 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
376 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
377 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
380 /* Not supported by TSDL 1.8 */
385 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
386 struct_fc
= fs_sink_ctf_field_class_as_struct(fc
);
387 len
= struct_fc
->members
->len
;
389 var_fc
= fs_sink_ctf_field_class_as_variant(fc
);
390 len
= var_fc
->options
->len
;
393 for (i
= 0; i
< len
; i
++) {
394 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
395 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
398 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
402 if (strcmp(named_fc
->name
->str
, tgt_fc_name
) == 0) {
403 if (named_fc
->fc
== tgt_fc
) {
404 g_string_assign(tgt_field_ref
,
408 *user_tgt_fc
= tgt_fc
;
412 * Using only the target field
413 * class's name, we're not
414 * reaching the target field
415 * class. This is not supported
431 * Creates an absolute field ref from IR field path `tgt_ir_field_path`.
433 * Returns a negative value if this resolving operation failed.
436 int create_absolute_field_ref(struct ctx
*ctx
,
437 const bt_field_path
*tgt_ir_field_path
, GString
*tgt_field_ref
,
438 struct fs_sink_ctf_field_class
**user_tgt_fc
)
441 struct fs_sink_ctf_field_class
*fc
= NULL
;
444 switch (bt_field_path_get_root_scope(tgt_ir_field_path
)) {
445 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
:
446 BT_ASSERT(ctx
->cur_sc
);
447 fc
= ctx
->cur_sc
->packet_context_fc
;
448 g_string_assign(tgt_field_ref
, "stream.packet.context");
450 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
:
451 BT_ASSERT(ctx
->cur_sc
);
452 fc
= ctx
->cur_sc
->event_common_context_fc
;
453 g_string_assign(tgt_field_ref
, "stream.event.context");
455 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
:
456 BT_ASSERT(ctx
->cur_ec
);
457 fc
= ctx
->cur_ec
->spec_context_fc
;
458 g_string_assign(tgt_field_ref
, "event.context");
460 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
:
461 BT_ASSERT(ctx
->cur_ec
);
462 fc
= ctx
->cur_ec
->payload_fc
;
463 g_string_assign(tgt_field_ref
, "event.fields");
471 for (i
= 0; i
< bt_field_path_get_item_count(tgt_ir_field_path
); i
++) {
472 const bt_field_path_item
*fp_item
=
473 bt_field_path_borrow_item_by_index_const(
474 tgt_ir_field_path
, i
);
475 struct fs_sink_ctf_named_field_class
*named_fc
= NULL
;
477 if (bt_field_path_item_get_type(fp_item
) !=
478 BT_FIELD_PATH_ITEM_TYPE_INDEX
) {
479 /* Not supported by TSDL 1.8 */
485 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
486 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
487 BT_FIELD_PATH_ITEM_TYPE_INDEX
);
488 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
489 fs_sink_ctf_field_class_as_struct(fc
),
490 bt_field_path_item_index_get_index(fp_item
));
492 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
493 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
494 BT_FIELD_PATH_ITEM_TYPE_INDEX
);
495 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
496 fs_sink_ctf_field_class_as_variant(fc
),
497 bt_field_path_item_index_get_index(fp_item
));
504 g_string_append_c(tgt_field_ref
, '.');
505 g_string_append(tgt_field_ref
, named_fc
->name
->str
);
518 * Resolves a target field class located at `tgt_ir_field_path`, writing
519 * the resolved field ref to `tgt_field_ref` and setting
520 * `*create_before` according to whether or not the target field must be
521 * created immediately before (in which case `tgt_field_ref` is
525 void resolve_field_class(struct ctx
*ctx
,
526 const bt_field_path
*tgt_ir_field_path
,
527 GString
*tgt_field_ref
, bool *create_before
,
528 struct fs_sink_ctf_field_class
**user_tgt_fc
)
531 bt_field_path_scope tgt_scope
;
533 *create_before
= false;
535 if (!tgt_ir_field_path
) {
536 *create_before
= true;
540 tgt_scope
= bt_field_path_get_root_scope(tgt_ir_field_path
);
542 if (tgt_scope
== ctx
->cur_scope
) {
544 * Try, in this order:
546 * 1. Use a relative path, using only the target field
547 * class's name. This is what is the most commonly
548 * supported by popular CTF reading tools.
550 * 2. Use an absolute path. This could fail if there's
551 * an array field class from the current root's field
552 * class to the target field class.
554 * 3. Create the target field class before the
555 * requesting field class (fallback).
557 ret
= create_relative_field_ref(ctx
, tgt_ir_field_path
,
558 tgt_field_ref
, user_tgt_fc
);
560 ret
= create_absolute_field_ref(ctx
, tgt_ir_field_path
,
561 tgt_field_ref
, user_tgt_fc
);
563 *create_before
= true;
568 ret
= create_absolute_field_ref(ctx
, tgt_ir_field_path
,
569 tgt_field_ref
, user_tgt_fc
);
571 /* It must always work in previous scopes */
580 int translate_field_class(struct ctx
*ctx
);
583 void append_to_parent_field_class(struct ctx
*ctx
,
584 struct fs_sink_ctf_field_class
*fc
)
586 struct fs_sink_ctf_field_class
*parent_fc
=
587 cur_path_stack_top(ctx
)->parent_fc
;
589 switch (parent_fc
->type
) {
590 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
591 fs_sink_ctf_field_class_struct_append_member(
592 fs_sink_ctf_field_class_as_struct(parent_fc
),
593 cur_path_stack_top(ctx
)->name
->str
, fc
);
595 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
597 struct fs_sink_ctf_field_class_option
*opt_fc
=
598 fs_sink_ctf_field_class_as_option(parent_fc
);
600 BT_ASSERT(!opt_fc
->content_fc
);
601 opt_fc
->content_fc
= fc
;
602 opt_fc
->base
.alignment
= fc
->alignment
;
605 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
606 fs_sink_ctf_field_class_variant_append_option(
607 fs_sink_ctf_field_class_as_variant(parent_fc
),
608 cur_path_stack_top(ctx
)->name
->str
, fc
);
610 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
611 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
613 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
614 fs_sink_ctf_field_class_as_array_base(parent_fc
);
616 BT_ASSERT(!array_base_fc
->elem_fc
);
617 array_base_fc
->elem_fc
= fc
;
618 array_base_fc
->base
.alignment
= fc
->alignment
;
627 void update_parent_field_class_alignment(struct ctx
*ctx
,
628 unsigned int alignment
)
630 struct fs_sink_ctf_field_class
*parent_fc
=
631 cur_path_stack_top(ctx
)->parent_fc
;
633 switch (parent_fc
->type
) {
634 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
635 fs_sink_ctf_field_class_struct_align_at_least(
636 fs_sink_ctf_field_class_as_struct(parent_fc
),
639 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
640 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
642 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
643 fs_sink_ctf_field_class_as_array_base(parent_fc
);
645 array_base_fc
->base
.alignment
= alignment
;
654 int translate_structure_field_class_members(struct ctx
*ctx
,
655 struct fs_sink_ctf_field_class_struct
*struct_fc
,
656 const bt_field_class
*ir_fc
)
661 for (i
= 0; i
< bt_field_class_structure_get_member_count(ir_fc
); i
++) {
662 const bt_field_class_structure_member
*member
;
664 const bt_field_class
*memb_ir_fc
;
667 bt_field_class_structure_borrow_member_by_index_const(
669 name
= bt_field_class_structure_member_get_name(member
);
670 memb_ir_fc
= bt_field_class_structure_member_borrow_field_class_const(
672 ret
= cur_path_stack_push(ctx
, i
, name
, true, memb_ir_fc
,
675 BT_COMP_LOGE("Cannot translate structure field class member: "
676 "name=\"%s\"", name
);
680 ret
= translate_field_class(ctx
);
682 BT_COMP_LOGE("Cannot translate structure field class member: "
683 "name=\"%s\"", name
);
687 cur_path_stack_pop(ctx
);
695 int translate_structure_field_class(struct ctx
*ctx
)
698 struct fs_sink_ctf_field_class_struct
*fc
=
699 fs_sink_ctf_field_class_struct_create_empty(
700 cur_path_stack_top(ctx
)->ir_fc
,
701 cur_path_stack_top(ctx
)->index_in_parent
);
704 append_to_parent_field_class(ctx
, &fc
->base
);
705 ret
= translate_structure_field_class_members(ctx
, fc
, fc
->base
.ir_fc
);
710 update_parent_field_class_alignment(ctx
, fc
->base
.alignment
);
717 * This function protects a given variant FC option name (with the `_`
718 * prefix) if required. On success, `name_buf->str` contains the variant
719 * FC option name to use (original option name or protected if
722 * One of the goals of `sink.ctf.fs` is to write a CTF trace which is as
723 * close as possible to an original CTF trace as decoded by
726 * This scenario is valid in CTF 1.8:
738 * Once in trace IR, the enumeration FC mapping names and variant FC
739 * option names are kept as is. For this reason, we don't want to
740 * protect the variant FC option names here (by prepending `_`): this
741 * would make the variant FC option name and the enumeration FC mapping
744 * This scenario is also valid in CTF 1.8:
756 * Once in trace IR, the enumeration FC mapping names are kept as is,
757 * but the `_HELLO` variant FC option name becomes `HELLO` (unprotected
758 * for presentation, as recommended by CTF 1.8). When going back to
759 * TSDL, we need to protect `HELLO` so that it becomes `_HELLO` to match
760 * the corresponding enumeration FC mapping name.
762 * This scenario is also valid in CTF 1.8:
774 * Once in trace IR, the enumeration FC mapping names are kept as is,
775 * but the `__HELLO` variant FC option name becomes `_HELLO`
776 * (unprotected). When going back to TSDL, we need to protect `_HELLO`
777 * so that it becomes `__HELLO` to match the corresponding enumeration
780 * `src.ctf.fs` always uses the _same_ integer range sets for a selector
781 * FC mapping and a corresponding variant FC option. We can use that
782 * fact to find the original variant FC option names by matching variant
783 * FC options and enumeration FC mappings by range set.
786 int maybe_protect_variant_option_name(const bt_field_class
*ir_var_fc
,
787 const bt_field_class
*ir_tag_fc
, uint64_t opt_i
,
792 bt_field_class_type ir_var_fc_type
;
793 const bt_integer_range_set_unsigned
*opt_ranges_unsigned
= NULL
;
794 const bt_integer_range_set_signed
*opt_ranges_signed
= NULL
;
795 const char *mapping_label
= NULL
;
796 const char *ir_opt_name
;
797 const bt_field_class_variant_option
*base_var_opt
;
798 bool force_protect
= false;
800 ir_var_fc_type
= bt_field_class_get_type(ir_var_fc
);
801 base_var_opt
= bt_field_class_variant_borrow_option_by_index_const(
803 BT_ASSERT(base_var_opt
);
804 ir_opt_name
= bt_field_class_variant_option_get_name(base_var_opt
);
805 BT_ASSERT(ir_opt_name
);
808 * Check if the variant FC option name is required to be
809 * protected (reserved TSDL keyword or starts with `_`). In that
810 * case, the name of the selector FC mapping we find must match
811 * exactly the protected name.
813 force_protect
= must_protect_identifier(ir_opt_name
);
815 g_string_assign(name_buf
, "_");
816 g_string_append(name_buf
, ir_opt_name
);
818 g_string_assign(name_buf
, ir_opt_name
);
821 /* Borrow option's ranges */
822 if (ir_var_fc_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
) {
823 /* No ranges: we're done */
825 } if (ir_var_fc_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
) {
826 const bt_field_class_variant_with_selector_field_integer_unsigned_option
*var_opt
=
827 bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const(
829 opt_ranges_unsigned
=
830 bt_field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const(
833 const bt_field_class_variant_with_selector_field_integer_signed_option
*var_opt
=
834 bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const(
837 bt_field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const(
841 /* Find corresponding mapping by range set in selector FC */
842 for (i
= 0; i
< bt_field_class_enumeration_get_mapping_count(ir_tag_fc
);
844 if (ir_var_fc_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
) {
845 const bt_field_class_enumeration_mapping
*mapping_base
;
846 const bt_field_class_enumeration_unsigned_mapping
*mapping
;
847 const bt_integer_range_set_unsigned
*mapping_ranges
;
849 mapping
= bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
851 mapping_ranges
= bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
854 if (bt_integer_range_set_unsigned_is_equal(opt_ranges_unsigned
,
856 /* We have a winner */
858 bt_field_class_enumeration_unsigned_mapping_as_mapping_const(
861 bt_field_class_enumeration_mapping_get_label(
866 const bt_field_class_enumeration_mapping
*mapping_base
;
867 const bt_field_class_enumeration_signed_mapping
*mapping
;
868 const bt_integer_range_set_signed
*mapping_ranges
;
870 mapping
= bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
872 mapping_ranges
= bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
875 if (bt_integer_range_set_signed_is_equal(opt_ranges_signed
,
877 /* We have a winner */
879 bt_field_class_enumeration_signed_mapping_as_mapping_const(
882 bt_field_class_enumeration_mapping_get_label(
889 if (!mapping_label
) {
890 /* Range set not found: invalid selector for CTF 1.8 */
896 * If the enumeration FC mapping name is not the same as the
897 * variant FC option name and we didn't protect already, try
898 * protecting the option name and check again.
900 if (strcmp(mapping_label
, name_buf
->str
) != 0) {
906 if (mapping_label
[0] == '\0') {
911 g_string_assign(name_buf
, "_");
912 g_string_append(name_buf
, ir_opt_name
);
914 if (strcmp(mapping_label
, name_buf
->str
) != 0) {
925 int translate_option_field_class(struct ctx
*ctx
)
927 struct fs_sink_ctf_field_class_option
*fc
=
928 fs_sink_ctf_field_class_option_create_empty(
929 cur_path_stack_top(ctx
)->ir_fc
,
930 cur_path_stack_top(ctx
)->index_in_parent
);
931 const bt_field_class
*content_ir_fc
=
932 bt_field_class_option_borrow_field_class_const(fc
->base
.ir_fc
);
938 * CTF 1.8 does not support the option field class type. To
939 * write something anyway, this component translates this type
940 * to a variant field class where the options are:
942 * * An empty structure field class.
943 * * The optional field class itself.
945 * The "tag" is always generated/before in that case (an 8-bit
946 * unsigned enumeration field class).
948 append_to_parent_field_class(ctx
, &fc
->base
);
949 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, false, content_ir_fc
,
952 BT_COMP_LOGE_STR("Cannot translate option field class content.");
956 ret
= translate_field_class(ctx
);
958 BT_COMP_LOGE_STR("Cannot translate option field class content.");
962 cur_path_stack_pop(ctx
);
963 update_parent_field_class_alignment(ctx
, fc
->base
.alignment
);
970 int translate_variant_field_class(struct ctx
*ctx
)
974 struct fs_sink_ctf_field_class_variant
*fc
=
975 fs_sink_ctf_field_class_variant_create_empty(
976 cur_path_stack_top(ctx
)->ir_fc
,
977 cur_path_stack_top(ctx
)->index_in_parent
);
978 bt_field_class_type ir_fc_type
;
979 const bt_field_path
*ir_selector_field_path
= NULL
;
980 struct fs_sink_ctf_field_class
*tgt_fc
= NULL
;
981 GString
*name_buf
= g_string_new(NULL
);
982 bt_value
*prot_opt_names
= bt_value_array_create();
987 BT_ASSERT(prot_opt_names
);
988 ir_fc_type
= bt_field_class_get_type(fc
->base
.ir_fc
);
989 opt_count
= bt_field_class_variant_get_option_count(fc
->base
.ir_fc
);
991 if (bt_field_class_type_is(ir_fc_type
,
992 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SELECTOR_FIELD
)) {
993 ir_selector_field_path
= bt_field_class_variant_with_selector_field_borrow_selector_field_path_const(
995 BT_ASSERT(ir_selector_field_path
);
998 /* Resolve tag field class before appending to parent */
999 resolve_field_class(ctx
, ir_selector_field_path
, fc
->tag_ref
,
1000 &fc
->tag_is_before
, &tgt_fc
);
1002 if (ir_selector_field_path
&& tgt_fc
) {
1003 uint64_t mapping_count
;
1004 uint64_t option_count
;
1006 /* CTF 1.8: selector FC must be an enumeration FC */
1007 bt_field_class_type type
= bt_field_class_get_type(
1010 if (!bt_field_class_type_is(type
,
1011 BT_FIELD_CLASS_TYPE_ENUMERATION
)) {
1012 fc
->tag_is_before
= true;
1017 * Call maybe_protect_variant_option_name() for each
1018 * option below. In that case we also want selector FC
1019 * to contain as many mappings as the variant FC has
1022 mapping_count
= bt_field_class_enumeration_get_mapping_count(
1024 option_count
= bt_field_class_variant_get_option_count(
1027 if (mapping_count
!= option_count
) {
1028 fc
->tag_is_before
= true;
1033 * No compatible selector field class for CTF 1.8:
1034 * create the appropriate selector field class.
1036 fc
->tag_is_before
= true;
1042 * First pass: detect any option name clash with option name
1043 * protection. In that case, we don't fail: just create the
1044 * selector field class before the variant field class.
1046 * After this, `prot_opt_names` contains the final option names,
1047 * potentially protected if needed. They can still be invalid
1048 * TSDL identifiers however; this will be checked by
1049 * cur_path_stack_push().
1051 for (i
= 0; i
< opt_count
; i
++) {
1052 if (!fc
->tag_is_before
) {
1053 BT_ASSERT(tgt_fc
->ir_fc
);
1054 ret
= maybe_protect_variant_option_name(fc
->base
.ir_fc
,
1055 tgt_fc
->ir_fc
, i
, name_buf
);
1057 fc
->tag_is_before
= true;
1061 ret
= bt_value_array_append_string_element(prot_opt_names
,
1068 for (i
= 0; i
< opt_count
; i
++) {
1070 const bt_value
*opt_name_a
=
1071 bt_value_array_borrow_element_by_index_const(
1074 for (j
= 0; j
< opt_count
; j
++) {
1075 const bt_value
*opt_name_b
;
1082 bt_value_array_borrow_element_by_index_const(
1084 if (bt_value_is_equal(opt_name_a
, opt_name_b
)) {
1086 * Variant FC option names are not
1087 * unique when protected.
1089 fc
->tag_is_before
= true;
1090 goto append_to_parent
;
1096 append_to_parent_field_class(ctx
, &fc
->base
);
1098 for (i
= 0; i
< opt_count
; i
++) {
1099 const bt_field_class_variant_option
*opt
;
1100 const bt_field_class
*opt_ir_fc
;
1101 const bt_value
*prot_opt_name_val
=
1102 bt_value_array_borrow_element_by_index_const(
1104 const char *prot_opt_name
= bt_value_string_get(
1107 BT_ASSERT(prot_opt_name
);
1108 opt
= bt_field_class_variant_borrow_option_by_index_const(
1110 opt_ir_fc
= bt_field_class_variant_option_borrow_field_class_const(
1114 * We don't ask cur_path_stack_push() to protect the
1115 * option name because it's already protected at this
1118 ret
= cur_path_stack_push(ctx
, i
, prot_opt_name
, false,
1119 opt_ir_fc
, &fc
->base
);
1121 BT_COMP_LOGE("Cannot translate variant field class option: "
1122 "name=\"%s\"", prot_opt_name
);
1126 ret
= translate_field_class(ctx
);
1128 BT_COMP_LOGE("Cannot translate variant field class option: "
1129 "name=\"%s\"", prot_opt_name
);
1133 cur_path_stack_pop(ctx
);
1138 g_string_free(name_buf
, TRUE
);
1141 bt_value_put_ref(prot_opt_names
);
1146 int translate_static_array_field_class(struct ctx
*ctx
)
1148 struct fs_sink_ctf_field_class_array
*fc
=
1149 fs_sink_ctf_field_class_array_create_empty(
1150 cur_path_stack_top(ctx
)->ir_fc
,
1151 cur_path_stack_top(ctx
)->index_in_parent
);
1152 const bt_field_class
*elem_ir_fc
=
1153 bt_field_class_array_borrow_element_field_class_const(
1154 fc
->base
.base
.ir_fc
);
1158 append_to_parent_field_class(ctx
, &fc
->base
.base
);
1159 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, false, elem_ir_fc
,
1162 BT_COMP_LOGE_STR("Cannot translate static array field class element.");
1166 ret
= translate_field_class(ctx
);
1168 BT_COMP_LOGE_STR("Cannot translate static array field class element.");
1172 cur_path_stack_pop(ctx
);
1173 update_parent_field_class_alignment(ctx
, fc
->base
.base
.alignment
);
1180 int translate_dynamic_array_field_class(struct ctx
*ctx
)
1182 struct fs_sink_ctf_field_class_sequence
*fc
=
1183 fs_sink_ctf_field_class_sequence_create_empty(
1184 cur_path_stack_top(ctx
)->ir_fc
,
1185 cur_path_stack_top(ctx
)->index_in_parent
);
1186 const bt_field_class
*elem_ir_fc
=
1187 bt_field_class_array_borrow_element_field_class_const(
1188 fc
->base
.base
.ir_fc
);
1193 /* Resolve length field class before appending to parent */
1194 if (bt_field_class_get_type(cur_path_stack_top(ctx
)->ir_fc
) ==
1195 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
) {
1196 resolve_field_class(ctx
,
1197 bt_field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
1198 fc
->base
.base
.ir_fc
),
1199 fc
->length_ref
, &fc
->length_is_before
, NULL
);
1202 append_to_parent_field_class(ctx
, &fc
->base
.base
);
1203 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, false, elem_ir_fc
,
1206 BT_COMP_LOGE_STR("Cannot translate dynamic array field class element.");
1210 ret
= translate_field_class(ctx
);
1212 BT_COMP_LOGE_STR("Cannot translate dynamic array field class element.");
1216 cur_path_stack_pop(ctx
);
1217 update_parent_field_class_alignment(ctx
, fc
->base
.base
.alignment
);
1224 int translate_bool_field_class(struct ctx
*ctx
)
1226 struct fs_sink_ctf_field_class_bool
*fc
=
1227 fs_sink_ctf_field_class_bool_create(
1228 cur_path_stack_top(ctx
)->ir_fc
,
1229 cur_path_stack_top(ctx
)->index_in_parent
);
1232 append_to_parent_field_class(ctx
, &fc
->base
.base
);
1237 int translate_bit_array_field_class(struct ctx
*ctx
)
1239 struct fs_sink_ctf_field_class_bit_array
*fc
=
1240 fs_sink_ctf_field_class_bit_array_create(
1241 cur_path_stack_top(ctx
)->ir_fc
,
1242 cur_path_stack_top(ctx
)->index_in_parent
);
1245 append_to_parent_field_class(ctx
, &fc
->base
);
1250 int translate_integer_field_class(struct ctx
*ctx
)
1252 struct fs_sink_ctf_field_class_int
*fc
=
1253 fs_sink_ctf_field_class_int_create(
1254 cur_path_stack_top(ctx
)->ir_fc
,
1255 cur_path_stack_top(ctx
)->index_in_parent
);
1258 append_to_parent_field_class(ctx
, &fc
->base
.base
);
1263 int translate_real_field_class(struct ctx
*ctx
)
1265 struct fs_sink_ctf_field_class_float
*fc
=
1266 fs_sink_ctf_field_class_float_create(
1267 cur_path_stack_top(ctx
)->ir_fc
,
1268 cur_path_stack_top(ctx
)->index_in_parent
);
1271 append_to_parent_field_class(ctx
, &fc
->base
.base
);
1276 int translate_string_field_class(struct ctx
*ctx
)
1278 struct fs_sink_ctf_field_class_string
*fc
=
1279 fs_sink_ctf_field_class_string_create(
1280 cur_path_stack_top(ctx
)->ir_fc
,
1281 cur_path_stack_top(ctx
)->index_in_parent
);
1284 append_to_parent_field_class(ctx
, &fc
->base
);
1289 * Translates a field class, recursively.
1291 * The field class's IR field class, parent field class, and index
1292 * within its parent are in the context's current path's top element
1293 * (cur_path_stack_top()).
1296 int translate_field_class(struct ctx
*ctx
)
1299 bt_field_class_type ir_fc_type
=
1300 bt_field_class_get_type(cur_path_stack_top(ctx
)->ir_fc
);
1302 if (ir_fc_type
== BT_FIELD_CLASS_TYPE_BOOL
) {
1303 ret
= translate_bool_field_class(ctx
);
1304 } else if (ir_fc_type
== BT_FIELD_CLASS_TYPE_BIT_ARRAY
) {
1305 ret
= translate_bit_array_field_class(ctx
);
1306 } else if (bt_field_class_type_is(ir_fc_type
,
1307 BT_FIELD_CLASS_TYPE_INTEGER
)) {
1308 ret
= translate_integer_field_class(ctx
);
1309 } else if (bt_field_class_type_is(ir_fc_type
,
1310 BT_FIELD_CLASS_TYPE_REAL
)) {
1311 ret
= translate_real_field_class(ctx
);
1312 } else if (ir_fc_type
== BT_FIELD_CLASS_TYPE_STRING
) {
1313 ret
= translate_string_field_class(ctx
);
1314 } else if (ir_fc_type
== BT_FIELD_CLASS_TYPE_STRUCTURE
) {
1315 ret
= translate_structure_field_class(ctx
);
1316 } else if (ir_fc_type
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
) {
1317 ret
= translate_static_array_field_class(ctx
);
1318 } else if (bt_field_class_type_is(ir_fc_type
,
1319 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
)) {
1320 ret
= translate_dynamic_array_field_class(ctx
);
1321 } else if (bt_field_class_type_is(ir_fc_type
,
1322 BT_FIELD_CLASS_TYPE_OPTION
)) {
1323 ret
= translate_option_field_class(ctx
);
1324 } else if (bt_field_class_type_is(ir_fc_type
,
1325 BT_FIELD_CLASS_TYPE_VARIANT
)) {
1326 ret
= translate_variant_field_class(ctx
);
1335 int set_field_ref(struct fs_sink_ctf_field_class
*fc
, const char *fc_name
,
1336 struct fs_sink_ctf_field_class
*parent_fc
)
1339 GString
*field_ref
= NULL
;
1341 const char *tgt_type
;
1342 struct fs_sink_ctf_field_class_struct
*parent_struct_fc
=
1343 fs_sink_ctf_field_class_as_struct(parent_fc
);
1345 unsigned int suffix
= 0;
1347 if (!fc_name
|| !parent_fc
||
1348 parent_fc
->type
!= FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
1355 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
1358 * CTF 1.8 does not support the option field class type.
1359 * To write something anyway, this component translates
1360 * this type to a variant field class where the options
1363 * * An empty structure field class.
1364 * * The optional field class itself.
1366 * Because the option field class becomes a CTF variant
1367 * field class, we use the term "tag" too here.
1369 * The "tag" is always generated/before in that case (an
1370 * 8-bit unsigned enumeration field class).
1372 struct fs_sink_ctf_field_class_option
*opt_fc
=
1373 fs_sink_ctf_field_class_as_option(fc
);
1375 field_ref
= opt_fc
->tag_ref
;
1380 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1382 struct fs_sink_ctf_field_class_sequence
*seq_fc
=
1383 fs_sink_ctf_field_class_as_sequence(fc
);
1385 field_ref
= seq_fc
->length_ref
;
1386 is_before
= seq_fc
->length_is_before
;
1390 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
1392 struct fs_sink_ctf_field_class_variant
*var_fc
=
1393 fs_sink_ctf_field_class_as_variant(fc
);
1395 field_ref
= var_fc
->tag_ref
;
1396 is_before
= var_fc
->tag_is_before
;
1404 BT_ASSERT(field_ref
);
1410 /* Initial field ref */
1411 g_string_printf(field_ref
, "__%s_%s", fc_name
, tgt_type
);
1414 * Make sure field ref does not clash with an existing field
1415 * class name within the same parent structure field class.
1418 bool name_ok
= true;
1420 for (i
= 0; i
< parent_struct_fc
->members
->len
; i
++) {
1421 struct fs_sink_ctf_named_field_class
*named_fc
=
1422 fs_sink_ctf_field_class_struct_borrow_member_by_index(
1423 parent_struct_fc
, i
);
1425 if (strcmp(field_ref
->str
, named_fc
->name
->str
) == 0) {
1433 /* No clash: we're done */
1437 /* Append suffix and try again */
1438 g_string_printf(field_ref
, "__%s_%s_%u", fc_name
, tgt_type
,
1448 * This function recursively sets field refs of sequence and variant
1449 * field classes when they are immediately before, avoiding name clashes
1450 * with existing field class names.
1452 * It can fail at this point if, for example, a sequence field class of
1453 * which to set the length's field ref has something else than a
1454 * structure field class as its parent: in this case, there's no
1455 * location to place the length field class immediately before the
1456 * sequence field class.
1459 int set_field_refs(struct fs_sink_ctf_field_class
* const fc
,
1460 const char *fc_name
, struct fs_sink_ctf_field_class
*parent_fc
)
1463 enum fs_sink_ctf_field_class_type fc_type
;
1469 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
1471 struct fs_sink_ctf_field_class_option
*opt_fc
=
1472 fs_sink_ctf_field_class_as_option(fc
);
1474 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
1479 ret
= set_field_refs(opt_fc
->content_fc
, NULL
, fc
);
1486 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
1487 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
1491 struct fs_sink_ctf_field_class_struct
*struct_fc
= NULL
;
1492 struct fs_sink_ctf_field_class_variant
*var_fc
= NULL
;
1493 struct fs_sink_ctf_named_field_class
*named_fc
;
1495 if (fc_type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
1496 struct_fc
= fs_sink_ctf_field_class_as_struct(fc
);
1497 len
= struct_fc
->members
->len
;
1499 var_fc
= fs_sink_ctf_field_class_as_variant(fc
);
1500 len
= var_fc
->options
->len
;
1501 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
1507 for (i
= 0; i
< len
; i
++) {
1508 if (fc_type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
1509 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
1512 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
1516 ret
= set_field_refs(named_fc
->fc
, named_fc
->name
->str
,
1525 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
1526 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1528 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
1529 fs_sink_ctf_field_class_as_array_base(fc
);
1531 if (fc_type
== FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
1532 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
1538 ret
= set_field_refs(array_base_fc
->elem_fc
, NULL
, fc
);
1554 * This function translates a root scope trace IR field class to
1555 * a CTF IR field class.
1557 * The resulting CTF IR field class is written to `*fc` so that it
1558 * exists as the parent object's (stream class or event class) true root
1559 * field class during the recursive translation for resolving purposes.
1560 * This is also why this function creates the empty structure field
1561 * class and then calls translate_structure_field_class_members() to
1565 int translate_scope_field_class(struct ctx
*ctx
, bt_field_path_scope scope
,
1566 struct fs_sink_ctf_field_class
**fc
,
1567 const bt_field_class
*ir_fc
)
1575 BT_ASSERT(bt_field_class_get_type(ir_fc
) ==
1576 BT_FIELD_CLASS_TYPE_STRUCTURE
);
1578 *fc
= &fs_sink_ctf_field_class_struct_create_empty(
1579 ir_fc
, UINT64_C(-1))->base
;
1581 ctx
->cur_scope
= scope
;
1582 BT_ASSERT(ctx
->cur_path
->len
== 0);
1583 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, false, ir_fc
, NULL
);
1585 BT_COMP_LOGE("Cannot translate scope structure field class: "
1590 ret
= translate_structure_field_class_members(ctx
,
1591 fs_sink_ctf_field_class_as_struct(*fc
), ir_fc
);
1593 BT_COMP_LOGE("Cannot translate scope structure field class: "
1598 cur_path_stack_pop(ctx
);
1600 /* Set field refs for preceding targets */
1601 ret
= set_field_refs(*fc
, NULL
, NULL
);
1608 void ctx_init(struct ctx
*ctx
, struct fs_sink_comp
*fs_sink
)
1610 memset(ctx
, 0, sizeof(struct ctx
));
1611 ctx
->cur_path
= g_array_new(FALSE
, TRUE
,
1612 sizeof(struct field_path_elem
));
1613 BT_ASSERT(ctx
->cur_path
);
1614 ctx
->log_level
= fs_sink
->log_level
;
1615 ctx
->self_comp
= fs_sink
->self_comp
;
1619 void ctx_fini(struct ctx
*ctx
)
1621 if (ctx
->cur_path
) {
1622 g_array_free(ctx
->cur_path
, TRUE
);
1623 ctx
->cur_path
= NULL
;
1628 int translate_event_class(struct fs_sink_comp
*fs_sink
,
1629 struct fs_sink_ctf_stream_class
*sc
,
1630 const bt_event_class
*ir_ec
,
1631 struct fs_sink_ctf_event_class
**out_ec
)
1635 struct fs_sink_ctf_event_class
*ec
;
1640 ctx_init(&ctx
, fs_sink
);
1641 ec
= fs_sink_ctf_event_class_create(sc
, ir_ec
);
1645 ret
= translate_scope_field_class(&ctx
, BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
,
1646 &ec
->spec_context_fc
,
1647 bt_event_class_borrow_specific_context_field_class_const(
1653 ret
= translate_scope_field_class(&ctx
, BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
,
1655 bt_event_class_borrow_payload_field_class_const(ir_ec
));
1667 int try_translate_event_class_trace_ir_to_ctf_ir(
1668 struct fs_sink_comp
*fs_sink
,
1669 struct fs_sink_ctf_stream_class
*sc
,
1670 const bt_event_class
*ir_ec
,
1671 struct fs_sink_ctf_event_class
**out_ec
)
1678 /* Check in hash table first */
1679 *out_ec
= (fs_sink_ctf_event_class
*) g_hash_table_lookup(
1680 sc
->event_classes_from_ir
, ir_ec
);
1681 if (G_LIKELY(*out_ec
)) {
1685 ret
= translate_event_class(fs_sink
, sc
, ir_ec
, out_ec
);
1692 bool default_clock_class_name_exists(struct fs_sink_ctf_trace
*trace
,
1695 bool exists
= false;
1698 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1699 struct fs_sink_ctf_stream_class
*sc
=
1700 (fs_sink_ctf_stream_class
*) trace
->stream_classes
->pdata
[i
];
1702 if (sc
->default_clock_class_name
->len
== 0) {
1703 /* No default clock class */
1707 if (strcmp(sc
->default_clock_class_name
->str
, name
) == 0) {
1718 void make_unique_default_clock_class_name(struct fs_sink_ctf_stream_class
*sc
)
1720 unsigned int suffix
= 0;
1723 g_string_assign(sc
->default_clock_class_name
, "");
1724 sprintf(buf
, "default");
1726 while (default_clock_class_name_exists(sc
->trace
, buf
)) {
1727 sprintf(buf
, "default%u", suffix
);
1731 g_string_assign(sc
->default_clock_class_name
, buf
);
1735 int translate_stream_class(struct fs_sink_comp
*fs_sink
,
1736 struct fs_sink_ctf_trace
*trace
,
1737 const bt_stream_class
*ir_sc
,
1738 struct fs_sink_ctf_stream_class
**out_sc
)
1745 ctx_init(&ctx
, fs_sink
);
1746 *out_sc
= fs_sink_ctf_stream_class_create(trace
, ir_sc
);
1749 /* Set default clock class's protected name, if any */
1750 if ((*out_sc
)->default_clock_class
) {
1751 const char *name
= bt_clock_class_get_name(
1752 (*out_sc
)->default_clock_class
);
1755 /* Try original name, protected */
1756 g_string_assign((*out_sc
)->default_clock_class_name
,
1759 if (must_protect_identifier(name
)) {
1761 (*out_sc
)->default_clock_class_name
,
1765 g_string_assign((*out_sc
)->default_clock_class_name
,
1767 if (!ist_valid_identifier(
1768 (*out_sc
)->default_clock_class_name
->str
)) {
1769 /* Invalid: create a new name */
1770 make_unique_default_clock_class_name(*out_sc
);
1773 /* No name: create a name */
1774 make_unique_default_clock_class_name(*out_sc
);
1778 ctx
.cur_sc
= *out_sc
;
1779 ret
= translate_scope_field_class(&ctx
, BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
,
1780 &(*out_sc
)->packet_context_fc
,
1781 bt_stream_class_borrow_packet_context_field_class_const(ir_sc
));
1786 if ((*out_sc
)->packet_context_fc
) {
1788 * Make sure the structure field class's alignment is
1789 * enough: 8 is what we use for our own special members
1790 * in the packet context.
1792 fs_sink_ctf_field_class_struct_align_at_least(
1793 fs_sink_ctf_field_class_as_struct((*out_sc
)->packet_context_fc
), 8);
1796 ret
= translate_scope_field_class(&ctx
, BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
,
1797 &(*out_sc
)->event_common_context_fc
,
1798 bt_stream_class_borrow_event_common_context_field_class_const(
1807 fs_sink_ctf_stream_class_destroy(*out_sc
);
1816 int try_translate_stream_class_trace_ir_to_ctf_ir(
1817 struct fs_sink_comp
*fs_sink
,
1818 struct fs_sink_ctf_trace
*trace
,
1819 const bt_stream_class
*ir_sc
,
1820 struct fs_sink_ctf_stream_class
**out_sc
)
1828 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
1829 *out_sc
= (fs_sink_ctf_stream_class
*) trace
->stream_classes
->pdata
[i
];
1831 if ((*out_sc
)->ir_sc
== ir_sc
) {
1836 ret
= translate_stream_class(fs_sink
, trace
, ir_sc
, out_sc
);
1843 struct fs_sink_ctf_trace
*translate_trace_trace_ir_to_ctf_ir(
1844 struct fs_sink_comp
*fs_sink
, const bt_trace
*ir_trace
)
1848 struct fs_sink_ctf_trace
*trace
= NULL
;
1850 /* Check that trace's environment is TSDL-compatible */
1851 count
= bt_trace_get_environment_entry_count(ir_trace
);
1852 for (i
= 0; i
< count
; i
++) {
1854 const bt_value
*val
;
1856 bt_trace_borrow_environment_entry_by_index_const(
1857 ir_trace
, i
, &name
, &val
);
1859 if (!ist_valid_identifier(name
)) {
1860 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, fs_sink
->log_level
,
1862 "Unsupported trace class's environment entry name: "
1863 "name=\"%s\"", name
);
1867 switch (bt_value_get_type(val
)) {
1868 case BT_VALUE_TYPE_SIGNED_INTEGER
:
1869 case BT_VALUE_TYPE_STRING
:
1872 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, fs_sink
->log_level
,
1874 "Unsupported trace class's environment entry value type: "
1876 bt_common_value_type_string(
1877 bt_value_get_type(val
)));
1882 trace
= fs_sink_ctf_trace_create(ir_trace
);