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_LOG_TAG "PLUGIN-CTF-FS-SINK-TRANSLATE-TRACE-IR-TO-CTF-IR"
26 #include <babeltrace/babeltrace.h>
27 #include <babeltrace/babeltrace-internal.h>
28 #include <babeltrace/common-internal.h>
29 #include <babeltrace/assert-internal.h>
35 #include "fs-sink-ctf-meta.h"
37 struct field_path_elem
{
38 uint64_t index_in_parent
;
42 const bt_field_class
*ir_fc
;
45 struct fs_sink_ctf_field_class
*parent_fc
;
50 struct fs_sink_ctf_stream_class
*cur_sc
;
53 struct fs_sink_ctf_event_class
*cur_ec
;
58 * Array of `struct field_path_elem` */
63 struct field_path_elem
*cur_path_stack_at(struct ctx
*ctx
, uint64_t i
)
65 BT_ASSERT(i
< ctx
->cur_path
->len
);
66 return &g_array_index(ctx
->cur_path
, struct field_path_elem
, i
);
70 struct field_path_elem
*cur_path_stack_top(struct ctx
*ctx
)
72 BT_ASSERT(ctx
->cur_path
->len
> 0);
73 return cur_path_stack_at(ctx
, ctx
->cur_path
->len
- 1);
77 bool is_reserved_member_name(const char *name
, const char *reserved_name
)
79 bool is_reserved
= false;
81 if (strcmp(name
, reserved_name
) == 0) {
86 if (name
[0] == '_' && strcmp(&name
[1], reserved_name
) == 0) {
96 int cur_path_stack_push(struct ctx
*ctx
,
97 uint64_t index_in_parent
, const char *ir_name
,
98 const bt_field_class
*ir_fc
,
99 struct fs_sink_ctf_field_class
*parent_fc
)
102 struct field_path_elem
*field_path_elem
;
104 g_array_set_size(ctx
->cur_path
, ctx
->cur_path
->len
+ 1);
105 field_path_elem
= cur_path_stack_top(ctx
);
106 field_path_elem
->index_in_parent
= index_in_parent
;
107 field_path_elem
->name
= g_string_new(ir_name
);
110 if (ctx
->cur_scope
== BT_SCOPE_PACKET_CONTEXT
) {
111 if (is_reserved_member_name(ir_name
, "packet_size") ||
112 is_reserved_member_name(ir_name
, "content_size") ||
113 is_reserved_member_name(ir_name
, "timestamp_begin") ||
114 is_reserved_member_name(ir_name
, "timestamp_end") ||
115 is_reserved_member_name(ir_name
, "events_discarded") ||
116 is_reserved_member_name(ir_name
, "packet_seq_num")) {
117 BT_LOGE("Unsupported reserved TSDL structure field class member "
118 "or variant field class option name: name=\"%s\"",
125 ret
= fs_sink_ctf_protect_name(field_path_elem
->name
);
127 BT_LOGE("Unsupported non-TSDL structure field class member "
128 "or variant field class option name: name=\"%s\"",
134 field_path_elem
->ir_fc
= ir_fc
;
135 field_path_elem
->parent_fc
= parent_fc
;
142 void cur_path_stack_pop(struct ctx
*ctx
)
144 struct field_path_elem
*field_path_elem
;
146 BT_ASSERT(ctx
->cur_path
->len
> 0);
147 field_path_elem
= cur_path_stack_top(ctx
);
149 if (field_path_elem
->name
) {
150 g_string_free(field_path_elem
->name
, TRUE
);
151 field_path_elem
->name
= NULL
;
154 g_array_set_size(ctx
->cur_path
, ctx
->cur_path
->len
- 1);
158 * Creates a relative field ref (a single name) from IR field path
159 * `tgt_ir_field_path`.
161 * This function tries to locate the target field class recursively from
162 * the top to the bottom of the context's current path using only the
163 * target field class's own name. This is because many CTF reading tools
164 * do not support a relative field ref with more than one element, for
165 * example `prev_struct.len`.
167 * Returns a negative value if this resolving operation failed.
170 int create_relative_field_ref(struct ctx
*ctx
,
171 const bt_field_path
*tgt_ir_field_path
, GString
*tgt_field_ref
)
174 struct fs_sink_ctf_field_class
*tgt_fc
= NULL
;
177 const char *tgt_fc_name
;
178 struct field_path_elem
*field_path_elem
;
180 /* Get target field class's name */
181 switch (bt_field_path_get_root_scope(tgt_ir_field_path
)) {
182 case BT_SCOPE_PACKET_CONTEXT
:
183 BT_ASSERT(ctx
->cur_sc
);
184 tgt_fc
= ctx
->cur_sc
->packet_context_fc
;
186 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
187 BT_ASSERT(ctx
->cur_sc
);
188 tgt_fc
= ctx
->cur_sc
->event_common_context_fc
;
190 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
191 BT_ASSERT(ctx
->cur_ec
);
192 tgt_fc
= ctx
->cur_ec
->spec_context_fc
;
194 case BT_SCOPE_EVENT_PAYLOAD
:
195 BT_ASSERT(ctx
->cur_ec
);
196 tgt_fc
= ctx
->cur_ec
->payload_fc
;
204 while (i
< bt_field_path_get_index_count(tgt_ir_field_path
)) {
205 uint64_t index
= bt_field_path_get_index_by_index(
206 tgt_ir_field_path
, i
);
207 struct fs_sink_ctf_named_field_class
*named_fc
= NULL
;
211 switch (tgt_fc
->type
) {
212 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
213 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
214 (void *) tgt_fc
, index
);
216 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
217 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
218 (void *) tgt_fc
, index
);
220 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
221 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
223 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
226 tgt_fc
= array_base_fc
->elem_fc
;
234 tgt_fc
= named_fc
->fc
;
235 tgt_fc_name
= named_fc
->name
->str
;
241 BT_ASSERT(tgt_fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_INT
);
242 BT_ASSERT(tgt_fc_name
);
244 /* Find target field class having this name in current context */
245 for (si
= ctx
->cur_path
->len
- 1; si
>= 0; si
--) {
246 struct fs_sink_ctf_field_class
*fc
;
247 struct fs_sink_ctf_field_class_struct
*struct_fc
;
248 struct fs_sink_ctf_field_class_variant
*var_fc
;
249 struct fs_sink_ctf_named_field_class
*named_fc
;
252 field_path_elem
= cur_path_stack_at(ctx
, (uint64_t) si
);
253 fc
= field_path_elem
->parent_fc
;
255 /* Reached stack's bottom */
261 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
262 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
264 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
265 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
268 /* Not supported by TSDL 1.8 */
273 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
274 struct_fc
= (void *) fc
;
275 len
= struct_fc
->members
->len
;
277 var_fc
= (void *) fc
;
278 len
= var_fc
->options
->len
;
281 for (i
= 0; i
< len
; i
++) {
282 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
283 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
286 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
290 if (strcmp(named_fc
->name
->str
, tgt_fc_name
) == 0) {
291 if (named_fc
->fc
== tgt_fc
) {
292 g_string_assign(tgt_field_ref
,
296 * Using only the target field
297 * class's name, we're not
298 * reaching the target field
299 * class. This is not supported
315 * Creates an absolute field ref from IR field path `tgt_ir_field_path`.
317 * Returns a negative value if this resolving operation failed.
320 int create_absolute_field_ref(struct ctx
*ctx
,
321 const bt_field_path
*tgt_ir_field_path
, GString
*tgt_field_ref
)
324 struct fs_sink_ctf_field_class
*fc
= NULL
;
327 switch (bt_field_path_get_root_scope(tgt_ir_field_path
)) {
328 case BT_SCOPE_PACKET_CONTEXT
:
329 BT_ASSERT(ctx
->cur_sc
);
330 fc
= ctx
->cur_sc
->packet_context_fc
;
331 g_string_assign(tgt_field_ref
, "stream.packet.context");
333 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
334 BT_ASSERT(ctx
->cur_sc
);
335 fc
= ctx
->cur_sc
->event_common_context_fc
;
336 g_string_assign(tgt_field_ref
, "stream.event.context");
338 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
339 BT_ASSERT(ctx
->cur_ec
);
340 fc
= ctx
->cur_ec
->spec_context_fc
;
341 g_string_assign(tgt_field_ref
, "event.context");
343 case BT_SCOPE_EVENT_PAYLOAD
:
344 BT_ASSERT(ctx
->cur_ec
);
345 fc
= ctx
->cur_ec
->payload_fc
;
346 g_string_assign(tgt_field_ref
, "event.fields");
354 for (i
= 0; i
< bt_field_path_get_index_count(tgt_ir_field_path
); i
++) {
355 uint64_t index
= bt_field_path_get_index_by_index(
356 tgt_ir_field_path
, i
);
357 struct fs_sink_ctf_named_field_class
*named_fc
= NULL
;
360 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
361 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
364 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
365 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
368 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
369 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
370 /* Not supported by TSDL 1.8 */
378 g_string_append_c(tgt_field_ref
, '.');
379 g_string_append(tgt_field_ref
, named_fc
->name
->str
);
388 * Resolves a target field class located at `tgt_ir_field_path`, writing
389 * the resolved field ref to `tgt_field_ref` and setting
390 * `*create_before` according to whether or not the target field must be
391 * created immediately before (in which case `tgt_field_ref` is
395 void resolve_field_class(struct ctx
*ctx
,
396 const bt_field_path
*tgt_ir_field_path
,
397 GString
*tgt_field_ref
, bool *create_before
)
402 *create_before
= false;
404 if (!tgt_ir_field_path
) {
405 *create_before
= true;
409 tgt_scope
= bt_field_path_get_root_scope(tgt_ir_field_path
);
411 if (tgt_scope
== ctx
->cur_scope
) {
413 * Try, in this order:
415 * 1. Use a relative path, using only the target field
416 * class's name. This is what is the most commonly
417 * supported by popular CTF reading tools.
419 * 2. Use an absolute path. This could fail if there's
420 * an array field class from the current root's field
421 * class to the target field class.
423 * 3. Create the target field class before the
424 * requesting field class (fallback).
426 ret
= create_relative_field_ref(ctx
, tgt_ir_field_path
,
429 ret
= create_absolute_field_ref(ctx
, tgt_ir_field_path
,
432 *create_before
= true;
438 ret
= create_absolute_field_ref(ctx
, tgt_ir_field_path
,
441 /* It must always work in previous scopes */
450 int translate_field_class(struct ctx
*ctx
);
453 void append_to_parent_field_class(struct ctx
*ctx
,
454 struct fs_sink_ctf_field_class
*fc
)
456 struct fs_sink_ctf_field_class
*parent_fc
=
457 cur_path_stack_top(ctx
)->parent_fc
;
459 switch (parent_fc
->type
) {
460 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
461 fs_sink_ctf_field_class_struct_append_member((void *) parent_fc
,
462 cur_path_stack_top(ctx
)->name
->str
, fc
);
464 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
465 fs_sink_ctf_field_class_variant_append_option((void *) parent_fc
,
466 cur_path_stack_top(ctx
)->name
->str
, fc
);
468 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
469 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
471 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
474 BT_ASSERT(!array_base_fc
->elem_fc
);
475 array_base_fc
->elem_fc
= fc
;
476 array_base_fc
->base
.alignment
= fc
->alignment
;
485 void update_parent_field_class_alignment(struct ctx
*ctx
,
486 unsigned int alignment
)
488 struct fs_sink_ctf_field_class
*parent_fc
=
489 cur_path_stack_top(ctx
)->parent_fc
;
491 switch (parent_fc
->type
) {
492 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
493 fs_sink_ctf_field_class_struct_align_at_least(
494 (void *) parent_fc
, alignment
);
496 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
497 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
499 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
502 array_base_fc
->base
.alignment
= alignment
;
511 int translate_structure_field_class_members(struct ctx
*ctx
,
512 struct fs_sink_ctf_field_class_struct
*struct_fc
,
513 const bt_field_class
*ir_fc
)
518 for (i
= 0; i
< bt_field_class_structure_get_member_count(ir_fc
); i
++) {
519 const bt_field_class_structure_member
*member
;
521 const bt_field_class
*memb_ir_fc
;
524 bt_field_class_structure_borrow_member_by_index_const(
526 name
= bt_field_class_structure_member_get_name(member
);
527 memb_ir_fc
= bt_field_class_structure_member_borrow_field_class_const(
529 ret
= cur_path_stack_push(ctx
, i
, name
, memb_ir_fc
,
532 BT_LOGE("Cannot translate structure field class member: "
533 "name=\"%s\"", name
);
537 ret
= translate_field_class(ctx
);
539 BT_LOGE("Cannot translate structure field class member: "
540 "name=\"%s\"", name
);
544 cur_path_stack_pop(ctx
);
552 int translate_structure_field_class(struct ctx
*ctx
)
555 struct fs_sink_ctf_field_class_struct
*fc
=
556 fs_sink_ctf_field_class_struct_create_empty(
557 cur_path_stack_top(ctx
)->ir_fc
,
558 cur_path_stack_top(ctx
)->index_in_parent
);
561 append_to_parent_field_class(ctx
, (void *) fc
);
562 ret
= translate_structure_field_class_members(ctx
, fc
, fc
->base
.ir_fc
);
567 update_parent_field_class_alignment(ctx
, fc
->base
.alignment
);
574 int translate_variant_field_class(struct ctx
*ctx
)
578 struct fs_sink_ctf_field_class_variant
*fc
=
579 fs_sink_ctf_field_class_variant_create_empty(
580 cur_path_stack_top(ctx
)->ir_fc
,
581 cur_path_stack_top(ctx
)->index_in_parent
);
585 /* Resolve tag field class before appending to parent */
586 resolve_field_class(ctx
,
587 bt_field_class_variant_borrow_selector_field_path_const(
588 fc
->base
.ir_fc
), fc
->tag_ref
, &fc
->tag_is_before
);
590 append_to_parent_field_class(ctx
, (void *) fc
);
592 for (i
= 0; i
< bt_field_class_variant_get_option_count(fc
->base
.ir_fc
);
594 const bt_field_class_variant_option
*opt
;
596 const bt_field_class
*opt_ir_fc
;
598 opt
= bt_field_class_variant_borrow_option_by_index_const(
600 name
= bt_field_class_variant_option_get_name(opt
);
601 opt_ir_fc
= bt_field_class_variant_option_borrow_field_class_const(
603 ret
= cur_path_stack_push(ctx
, i
, name
, opt_ir_fc
, (void *) fc
);
605 BT_LOGE("Cannot translate variant field class option: "
606 "name=\"%s\"", name
);
610 ret
= translate_field_class(ctx
);
612 BT_LOGE("Cannot translate variant field class option: "
613 "name=\"%s\"", name
);
617 cur_path_stack_pop(ctx
);
625 int translate_static_array_field_class(struct ctx
*ctx
)
627 struct fs_sink_ctf_field_class_array
*fc
=
628 fs_sink_ctf_field_class_array_create_empty(
629 cur_path_stack_top(ctx
)->ir_fc
,
630 cur_path_stack_top(ctx
)->index_in_parent
);
631 const bt_field_class
*elem_ir_fc
=
632 bt_field_class_array_borrow_element_field_class_const(
633 fc
->base
.base
.ir_fc
);
637 append_to_parent_field_class(ctx
, (void *) fc
);
638 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, elem_ir_fc
,
641 BT_LOGE_STR("Cannot translate static array field class element.");
645 ret
= translate_field_class(ctx
);
647 BT_LOGE_STR("Cannot translate static array field class element.");
651 cur_path_stack_pop(ctx
);
652 update_parent_field_class_alignment(ctx
, fc
->base
.base
.alignment
);
659 int translate_dynamic_array_field_class(struct ctx
*ctx
)
661 struct fs_sink_ctf_field_class_sequence
*fc
=
662 fs_sink_ctf_field_class_sequence_create_empty(
663 cur_path_stack_top(ctx
)->ir_fc
,
664 cur_path_stack_top(ctx
)->index_in_parent
);
665 const bt_field_class
*elem_ir_fc
=
666 bt_field_class_array_borrow_element_field_class_const(
667 fc
->base
.base
.ir_fc
);
672 /* Resolve length field class before appending to parent */
673 resolve_field_class(ctx
,
674 bt_field_class_dynamic_array_borrow_length_field_path_const(
675 fc
->base
.base
.ir_fc
),
676 fc
->length_ref
, &fc
->length_is_before
);
678 append_to_parent_field_class(ctx
, (void *) fc
);
679 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, elem_ir_fc
,
682 BT_LOGE_STR("Cannot translate dynamic array field class element.");
686 ret
= translate_field_class(ctx
);
688 BT_LOGE_STR("Cannot translate dynamic array field class element.");
692 cur_path_stack_pop(ctx
);
693 update_parent_field_class_alignment(ctx
, fc
->base
.base
.alignment
);
700 int translate_integer_field_class(struct ctx
*ctx
)
702 struct fs_sink_ctf_field_class_int
*fc
=
703 fs_sink_ctf_field_class_int_create(
704 cur_path_stack_top(ctx
)->ir_fc
,
705 cur_path_stack_top(ctx
)->index_in_parent
);
708 append_to_parent_field_class(ctx
, (void *) fc
);
713 int translate_real_field_class(struct ctx
*ctx
)
715 struct fs_sink_ctf_field_class_float
*fc
=
716 fs_sink_ctf_field_class_float_create(
717 cur_path_stack_top(ctx
)->ir_fc
,
718 cur_path_stack_top(ctx
)->index_in_parent
);
721 append_to_parent_field_class(ctx
, (void *) fc
);
726 int translate_string_field_class(struct ctx
*ctx
)
728 struct fs_sink_ctf_field_class_string
*fc
=
729 fs_sink_ctf_field_class_string_create(
730 cur_path_stack_top(ctx
)->ir_fc
,
731 cur_path_stack_top(ctx
)->index_in_parent
);
734 append_to_parent_field_class(ctx
, (void *) fc
);
739 * Translates a field class, recursively.
741 * The field class's IR field class, parent field class, and index
742 * within its parent are in the context's current path's top element
743 * (cur_path_stack_top()).
746 int translate_field_class(struct ctx
*ctx
)
750 switch (bt_field_class_get_type(cur_path_stack_top(ctx
)->ir_fc
)) {
751 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
752 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
753 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
754 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
755 ret
= translate_integer_field_class(ctx
);
757 case BT_FIELD_CLASS_TYPE_REAL
:
758 ret
= translate_real_field_class(ctx
);
760 case BT_FIELD_CLASS_TYPE_STRING
:
761 ret
= translate_string_field_class(ctx
);
763 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
764 ret
= translate_structure_field_class(ctx
);
766 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
767 ret
= translate_static_array_field_class(ctx
);
769 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
770 ret
= translate_dynamic_array_field_class(ctx
);
772 case BT_FIELD_CLASS_TYPE_VARIANT
:
773 ret
= translate_variant_field_class(ctx
);
783 int set_field_ref(struct fs_sink_ctf_field_class
*fc
, const char *fc_name
,
784 struct fs_sink_ctf_field_class
*parent_fc
)
787 GString
*field_ref
= NULL
;
789 const char *tgt_type
;
790 struct fs_sink_ctf_field_class_struct
*parent_struct_fc
=
793 unsigned int suffix
= 0;
795 if (!fc_name
|| !parent_fc
||
796 parent_fc
->type
!= FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
803 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
805 struct fs_sink_ctf_field_class_sequence
*seq_fc
= (void *) fc
;
807 field_ref
= seq_fc
->length_ref
;
808 is_before
= seq_fc
->length_is_before
;
812 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
814 struct fs_sink_ctf_field_class_variant
*var_fc
= (void *) fc
;
816 field_ref
= var_fc
->tag_ref
;
817 is_before
= var_fc
->tag_is_before
;
825 BT_ASSERT(field_ref
);
831 /* Initial field ref */
832 g_string_printf(field_ref
, "__%s_%s", fc_name
, tgt_type
);
835 * Make sure field ref does not clash with an existing field
836 * class name within the same parent structure field class.
841 for (i
= 0; i
< parent_struct_fc
->members
->len
; i
++) {
842 struct fs_sink_ctf_named_field_class
*named_fc
=
843 fs_sink_ctf_field_class_struct_borrow_member_by_index(
844 parent_struct_fc
, i
);
846 if (strcmp(field_ref
->str
, named_fc
->name
->str
) == 0) {
854 /* No clash: we're done */
858 /* Append suffix and try again */
859 g_string_printf(field_ref
, "__%s_%s_%u", fc_name
, tgt_type
,
869 * This function recursively sets field refs of sequence and variant
870 * field classes when they are immediately before, avoiding name clashes
871 * with existing field class names.
873 * It can fail at this point if, for example, a sequence field class of
874 * which to set the length's field ref has something else than a
875 * structure field class as its parent: in this case, there's no
876 * location to place the length field class immediately before the
877 * sequence field class.
880 int set_field_refs(struct fs_sink_ctf_field_class
*fc
, const char *fc_name
,
881 struct fs_sink_ctf_field_class
*parent_fc
)
887 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
888 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
892 struct fs_sink_ctf_field_class_struct
*struct_fc
;
893 struct fs_sink_ctf_field_class_variant
*var_fc
;
894 struct fs_sink_ctf_named_field_class
*named_fc
;
896 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
897 struct_fc
= (void *) fc
;
898 len
= struct_fc
->members
->len
;
900 var_fc
= (void *) fc
;
901 len
= var_fc
->options
->len
;
902 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
908 for (i
= 0; i
< len
; i
++) {
909 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
910 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
913 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
917 ret
= set_field_refs(named_fc
->fc
, named_fc
->name
->str
,
926 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
927 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
929 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
932 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
933 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
939 ret
= set_field_refs(array_base_fc
->elem_fc
, NULL
, fc
);
955 * This function translates a root scope trace IR field class to
956 * a CTF IR field class.
958 * The resulting CTF IR field class is written to `*fc` so that it
959 * exists as the parent object's (stream class or event class) true root
960 * field class during the recursive translation for resolving purposes.
961 * This is also why this function creates the empty structure field
962 * class and then calls translate_structure_field_class_members() to
966 int translate_scope_field_class(struct ctx
*ctx
, bt_scope scope
,
967 struct fs_sink_ctf_field_class
**fc
,
968 const bt_field_class
*ir_fc
)
976 BT_ASSERT(bt_field_class_get_type(ir_fc
) ==
977 BT_FIELD_CLASS_TYPE_STRUCTURE
);
979 *fc
= (void *) fs_sink_ctf_field_class_struct_create_empty(
980 ir_fc
, UINT64_C(-1));
982 ctx
->cur_scope
= scope
;
983 BT_ASSERT(ctx
->cur_path
->len
== 0);
984 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, ir_fc
, NULL
);
986 BT_LOGE("Cannot translate scope structure field class: "
991 ret
= translate_structure_field_class_members(ctx
, (void *) *fc
, ir_fc
);
993 BT_LOGE("Cannot translate scope structure field class: "
998 cur_path_stack_pop(ctx
);
1000 /* Set field refs for preceding targets */
1001 ret
= set_field_refs(*fc
, NULL
, NULL
);
1008 void ctx_init(struct ctx
*ctx
)
1010 memset(ctx
, 0, sizeof(struct ctx
));
1011 ctx
->cur_path
= g_array_new(FALSE
, TRUE
,
1012 sizeof(struct field_path_elem
));
1013 BT_ASSERT(ctx
->cur_path
);
1017 void ctx_fini(struct ctx
*ctx
)
1019 if (ctx
->cur_path
) {
1020 g_array_free(ctx
->cur_path
, TRUE
);
1021 ctx
->cur_path
= NULL
;
1026 int translate_event_class(struct fs_sink_ctf_stream_class
*sc
,
1027 const bt_event_class
*ir_ec
,
1028 struct fs_sink_ctf_event_class
**out_ec
)
1032 struct fs_sink_ctf_event_class
*ec
;
1038 ec
= fs_sink_ctf_event_class_create(sc
, ir_ec
);
1042 ret
= translate_scope_field_class(&ctx
, BT_SCOPE_EVENT_SPECIFIC_CONTEXT
,
1043 &ec
->spec_context_fc
,
1044 bt_event_class_borrow_specific_context_field_class_const(
1050 ret
= translate_scope_field_class(&ctx
, BT_SCOPE_EVENT_PAYLOAD
,
1052 bt_event_class_borrow_payload_field_class_const(ir_ec
));
1064 int try_translate_event_class_trace_ir_to_ctf_ir(
1065 struct fs_sink_ctf_stream_class
*sc
,
1066 const bt_event_class
*ir_ec
,
1067 struct fs_sink_ctf_event_class
**out_ec
)
1074 /* Check in hash table first */
1075 *out_ec
= g_hash_table_lookup(sc
->event_classes_from_ir
, ir_ec
);
1076 if (likely(*out_ec
)) {
1080 ret
= translate_event_class(sc
, ir_ec
, out_ec
);
1086 bool default_clock_class_name_exists(struct fs_sink_ctf_trace_class
*tc
,
1089 bool exists
= false;
1092 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1093 struct fs_sink_ctf_stream_class
*sc
=
1094 tc
->stream_classes
->pdata
[i
];
1096 if (sc
->default_clock_class_name
->len
== 0) {
1097 /* No default clock class */
1101 if (strcmp(sc
->default_clock_class_name
->str
, name
) == 0) {
1112 void make_unique_default_clock_class_name(struct fs_sink_ctf_stream_class
*sc
)
1114 unsigned int suffix
= 0;
1117 g_string_assign(sc
->default_clock_class_name
, "");
1118 sprintf(buf
, "default");
1120 while (default_clock_class_name_exists(sc
->tc
, buf
)) {
1121 sprintf(buf
, "default%u", suffix
);
1125 g_string_assign(sc
->default_clock_class_name
, buf
);
1129 int translate_stream_class(struct fs_sink_ctf_trace_class
*tc
,
1130 const bt_stream_class
*ir_sc
,
1131 struct fs_sink_ctf_stream_class
**out_sc
)
1139 *out_sc
= fs_sink_ctf_stream_class_create(tc
, ir_sc
);
1142 /* Set default clock class's protected name, if any */
1143 if ((*out_sc
)->default_clock_class
) {
1144 const char *name
= bt_clock_class_get_name(
1145 (*out_sc
)->default_clock_class
);
1147 if (!bt_stream_class_default_clock_is_always_known(ir_sc
)) {
1148 BT_LOGE("Unsupported stream clock which can have an unknown value: "
1150 bt_stream_class_get_name(ir_sc
));
1155 /* Try original name, protected */
1156 g_string_assign((*out_sc
)->default_clock_class_name
,
1158 ret
= fs_sink_ctf_protect_name(
1159 (*out_sc
)->default_clock_class_name
);
1161 /* Invalid: create a new name */
1162 make_unique_default_clock_class_name(*out_sc
);
1166 /* No name: create a name */
1167 make_unique_default_clock_class_name(*out_sc
);
1171 ctx
.cur_sc
= *out_sc
;
1172 ret
= translate_scope_field_class(&ctx
, BT_SCOPE_PACKET_CONTEXT
,
1173 &(*out_sc
)->packet_context_fc
,
1174 bt_stream_class_borrow_packet_context_field_class_const(ir_sc
));
1179 if ((*out_sc
)->packet_context_fc
) {
1181 * Make sure the structure field class's alignment is
1182 * enough: 8 is what we use for our own special members
1183 * in the packet context.
1185 fs_sink_ctf_field_class_struct_align_at_least(
1186 (void *) (*out_sc
)->packet_context_fc
, 8);
1189 ret
= translate_scope_field_class(&ctx
, BT_SCOPE_EVENT_COMMON_CONTEXT
,
1190 &(*out_sc
)->event_common_context_fc
,
1191 bt_stream_class_borrow_event_common_context_field_class_const(
1200 fs_sink_ctf_stream_class_destroy(*out_sc
);
1209 int try_translate_stream_class_trace_ir_to_ctf_ir(
1210 struct fs_sink_ctf_trace_class
*tc
,
1211 const bt_stream_class
*ir_sc
,
1212 struct fs_sink_ctf_stream_class
**out_sc
)
1220 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1221 *out_sc
= tc
->stream_classes
->pdata
[i
];
1223 if ((*out_sc
)->ir_sc
== ir_sc
) {
1228 ret
= translate_stream_class(tc
, ir_sc
, out_sc
);
1235 struct fs_sink_ctf_trace_class
*translate_trace_class_trace_ir_to_ctf_ir(
1236 const bt_trace_class
*ir_tc
)
1240 struct fs_sink_ctf_trace_class
*tc
= NULL
;
1242 /* Check that trace class's environment is TSDL-compatible */
1243 count
= bt_trace_class_get_environment_entry_count(ir_tc
);
1244 for (i
= 0; i
< count
; i
++) {
1246 const bt_value
*val
;
1248 bt_trace_class_borrow_environment_entry_by_index_const(
1249 ir_tc
, i
, &name
, &val
);
1251 if (!fs_sink_ctf_ist_valid_identifier(name
)) {
1252 BT_LOGE("Unsupported trace class's environment entry name: "
1253 "name=\"%s\"", name
);
1257 switch (bt_value_get_type(val
)) {
1258 case BT_VALUE_TYPE_INTEGER
:
1259 case BT_VALUE_TYPE_STRING
:
1262 BT_LOGE("Unsupported trace class's environment entry value type: "
1264 bt_common_value_type_string(
1265 bt_value_get_type(val
)));
1270 tc
= fs_sink_ctf_trace_class_create(ir_tc
);