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 struct field_path_elem
*get_field_path_elem(
159 GArray
*full_field_path
, uint64_t i
)
161 return &g_array_index(full_field_path
, struct field_path_elem
,
167 * Creates a relative field ref (a single name) from IR field path
168 * `tgt_ir_field_path`.
170 * This function tries to locate the target field class recursively from
171 * the top to the bottom of the context's current path using only the
172 * target field class's own name. This is because many CTF reading tools
173 * do not support a relative field ref with more than one element, for
174 * example `prev_struct.len`.
176 * Returns a negative value if this resolving operation failed.
179 int create_relative_field_ref(struct ctx
*ctx
,
180 const bt_field_path
*tgt_ir_field_path
, GString
*tgt_field_ref
)
183 struct fs_sink_ctf_field_class
*tgt_fc
= NULL
;
186 const char *tgt_fc_name
;
187 struct field_path_elem
*field_path_elem
;
189 /* Get target field class's name */
190 switch (bt_field_path_get_root_scope(tgt_ir_field_path
)) {
191 case BT_SCOPE_PACKET_CONTEXT
:
192 BT_ASSERT(ctx
->cur_sc
);
193 tgt_fc
= ctx
->cur_sc
->packet_context_fc
;
195 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
196 BT_ASSERT(ctx
->cur_sc
);
197 tgt_fc
= ctx
->cur_sc
->event_common_context_fc
;
199 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
200 BT_ASSERT(ctx
->cur_ec
);
201 tgt_fc
= ctx
->cur_ec
->spec_context_fc
;
203 case BT_SCOPE_EVENT_PAYLOAD
:
204 BT_ASSERT(ctx
->cur_ec
);
205 tgt_fc
= ctx
->cur_ec
->payload_fc
;
213 while (i
< bt_field_path_get_index_count(tgt_ir_field_path
)) {
214 uint64_t index
= bt_field_path_get_index_by_index(
215 tgt_ir_field_path
, i
);
216 struct fs_sink_ctf_named_field_class
*named_fc
= NULL
;
220 switch (tgt_fc
->type
) {
221 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
222 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
223 (void *) tgt_fc
, index
);
225 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
226 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
227 (void *) tgt_fc
, index
);
229 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
230 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
232 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
235 tgt_fc
= array_base_fc
->elem_fc
;
243 tgt_fc
= named_fc
->fc
;
244 tgt_fc_name
= named_fc
->name
->str
;
250 BT_ASSERT(tgt_fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_INT
);
251 BT_ASSERT(tgt_fc_name
);
253 /* Find target field class having this name in current context */
254 for (si
= ctx
->cur_path
->len
- 1; si
>= 0; si
--) {
255 struct fs_sink_ctf_field_class
*fc
;
256 struct fs_sink_ctf_field_class_struct
*struct_fc
;
257 struct fs_sink_ctf_field_class_variant
*var_fc
;
258 struct fs_sink_ctf_named_field_class
*named_fc
;
261 field_path_elem
= cur_path_stack_at(ctx
, (uint64_t) si
);
262 fc
= field_path_elem
->parent_fc
;
264 /* Reached stack's bottom */
270 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
271 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
273 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
274 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
277 /* Not supported by TSDL 1.8 */
282 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
283 struct_fc
= (void *) fc
;
284 len
= struct_fc
->members
->len
;
286 var_fc
= (void *) fc
;
287 len
= var_fc
->options
->len
;
290 for (i
= 0; i
< len
; i
++) {
291 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
292 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
295 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
299 if (strcmp(named_fc
->name
->str
, tgt_fc_name
) == 0) {
300 if (named_fc
->fc
== tgt_fc
) {
301 g_string_assign(tgt_field_ref
,
305 * Using only the target field
306 * class's name, we're not
307 * reaching the target field
308 * class. This is not supported
324 * Creates an absolute field ref from IR field path `tgt_ir_field_path`.
326 * Returns a negative value if this resolving operation failed.
329 int create_absolute_field_ref(struct ctx
*ctx
,
330 const bt_field_path
*tgt_ir_field_path
, GString
*tgt_field_ref
)
333 struct fs_sink_ctf_field_class
*fc
= NULL
;
336 switch (bt_field_path_get_root_scope(tgt_ir_field_path
)) {
337 case BT_SCOPE_PACKET_CONTEXT
:
338 BT_ASSERT(ctx
->cur_sc
);
339 fc
= ctx
->cur_sc
->packet_context_fc
;
340 g_string_assign(tgt_field_ref
, "stream.packet.context");
342 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
343 BT_ASSERT(ctx
->cur_sc
);
344 fc
= ctx
->cur_sc
->event_common_context_fc
;
345 g_string_assign(tgt_field_ref
, "stream.event.context");
347 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
348 BT_ASSERT(ctx
->cur_ec
);
349 fc
= ctx
->cur_ec
->spec_context_fc
;
350 g_string_assign(tgt_field_ref
, "event.context");
352 case BT_SCOPE_EVENT_PAYLOAD
:
353 BT_ASSERT(ctx
->cur_ec
);
354 fc
= ctx
->cur_ec
->payload_fc
;
355 g_string_assign(tgt_field_ref
, "event.fields");
363 for (i
= 0; i
< bt_field_path_get_index_count(tgt_ir_field_path
); i
++) {
364 uint64_t index
= bt_field_path_get_index_by_index(
365 tgt_ir_field_path
, i
);
366 struct fs_sink_ctf_named_field_class
*named_fc
= NULL
;
369 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
370 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
373 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
374 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
377 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
378 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
379 /* Not supported by TSDL 1.8 */
387 g_string_append_c(tgt_field_ref
, '.');
388 g_string_append(tgt_field_ref
, named_fc
->name
->str
);
397 * Resolves a target field class located at `tgt_ir_field_path`, writing
398 * the resolved field ref to `tgt_field_ref` and setting
399 * `*create_before` according to whether or not the target field must be
400 * created immediately before (in which case `tgt_field_ref` is
404 void resolve_field_class(struct ctx
*ctx
,
405 const bt_field_path
*tgt_ir_field_path
,
406 GString
*tgt_field_ref
, bool *create_before
)
411 *create_before
= false;
413 if (!tgt_ir_field_path
) {
414 *create_before
= true;
418 tgt_scope
= bt_field_path_get_root_scope(tgt_ir_field_path
);
420 if (tgt_scope
== ctx
->cur_scope
) {
422 * Try, in this order:
424 * 1. Use a relative path, using only the target field
425 * class's name. This is what is the most commonly
426 * supported by popular CTF reading tools.
428 * 2. Use an absolute path. This could fail if there's
429 * an array field class from the current root's field
430 * class to the target field class.
432 * 3. Create the target field class before the
433 * requesting field class (fallback).
435 ret
= create_relative_field_ref(ctx
, tgt_ir_field_path
,
438 ret
= create_absolute_field_ref(ctx
, tgt_ir_field_path
,
441 *create_before
= true;
447 ret
= create_absolute_field_ref(ctx
, tgt_ir_field_path
,
450 /* It must always work in previous scopes */
459 int translate_field_class(struct ctx
*ctx
);
462 void append_to_parent_field_class(struct ctx
*ctx
,
463 struct fs_sink_ctf_field_class
*fc
)
465 struct fs_sink_ctf_field_class
*parent_fc
=
466 cur_path_stack_top(ctx
)->parent_fc
;
468 switch (parent_fc
->type
) {
469 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
470 fs_sink_ctf_field_class_struct_append_member((void *) parent_fc
,
471 cur_path_stack_top(ctx
)->name
->str
, fc
);
473 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
474 fs_sink_ctf_field_class_variant_append_option((void *) parent_fc
,
475 cur_path_stack_top(ctx
)->name
->str
, fc
);
477 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
478 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
480 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
483 BT_ASSERT(!array_base_fc
->elem_fc
);
484 array_base_fc
->elem_fc
= fc
;
485 array_base_fc
->base
.alignment
= fc
->alignment
;
494 void update_parent_field_class_alignment(struct ctx
*ctx
,
495 unsigned int alignment
)
497 struct fs_sink_ctf_field_class
*parent_fc
=
498 cur_path_stack_top(ctx
)->parent_fc
;
500 switch (parent_fc
->type
) {
501 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
502 fs_sink_ctf_field_class_struct_align_at_least(
503 (void *) parent_fc
, alignment
);
505 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
506 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
508 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
511 array_base_fc
->base
.alignment
= alignment
;
520 int translate_structure_field_class_members(struct ctx
*ctx
,
521 struct fs_sink_ctf_field_class_struct
*struct_fc
,
522 const bt_field_class
*ir_fc
)
527 for (i
= 0; i
< bt_field_class_structure_get_member_count(ir_fc
); i
++) {
528 const bt_field_class_structure_member
*member
;
530 const bt_field_class
*memb_ir_fc
;
533 bt_field_class_structure_borrow_member_by_index_const(
535 name
= bt_field_class_structure_member_get_name(member
);
536 memb_ir_fc
= bt_field_class_structure_member_borrow_field_class_const(
538 ret
= cur_path_stack_push(ctx
, i
, name
, memb_ir_fc
,
541 BT_LOGE("Cannot translate structure field class member: "
542 "name=\"%s\"", name
);
546 ret
= translate_field_class(ctx
);
548 BT_LOGE("Cannot translate structure field class member: "
549 "name=\"%s\"", name
);
553 cur_path_stack_pop(ctx
);
561 int translate_structure_field_class(struct ctx
*ctx
)
564 struct fs_sink_ctf_field_class_struct
*fc
=
565 fs_sink_ctf_field_class_struct_create_empty(
566 cur_path_stack_top(ctx
)->ir_fc
,
567 cur_path_stack_top(ctx
)->index_in_parent
);
570 append_to_parent_field_class(ctx
, (void *) fc
);
571 ret
= translate_structure_field_class_members(ctx
, fc
, fc
->base
.ir_fc
);
576 update_parent_field_class_alignment(ctx
, fc
->base
.alignment
);
583 int translate_variant_field_class(struct ctx
*ctx
)
587 struct fs_sink_ctf_field_class_variant
*fc
=
588 fs_sink_ctf_field_class_variant_create_empty(
589 cur_path_stack_top(ctx
)->ir_fc
,
590 cur_path_stack_top(ctx
)->index_in_parent
);
594 /* Resolve tag field class before appending to parent */
595 resolve_field_class(ctx
,
596 bt_field_class_variant_borrow_selector_field_path_const(
597 fc
->base
.ir_fc
), fc
->tag_ref
, &fc
->tag_is_before
);
599 append_to_parent_field_class(ctx
, (void *) fc
);
601 for (i
= 0; i
< bt_field_class_variant_get_option_count(fc
->base
.ir_fc
);
603 const bt_field_class_variant_option
*opt
;
605 const bt_field_class
*opt_ir_fc
;
607 opt
= bt_field_class_variant_borrow_option_by_index_const(
609 name
= bt_field_class_variant_option_get_name(opt
);
610 opt_ir_fc
= bt_field_class_variant_option_borrow_field_class_const(
612 ret
= cur_path_stack_push(ctx
, i
, name
, opt_ir_fc
, (void *) fc
);
614 BT_LOGE("Cannot translate variant field class option: "
615 "name=\"%s\"", name
);
619 ret
= translate_field_class(ctx
);
621 BT_LOGE("Cannot translate variant field class option: "
622 "name=\"%s\"", name
);
626 cur_path_stack_pop(ctx
);
634 int translate_static_array_field_class(struct ctx
*ctx
)
636 struct fs_sink_ctf_field_class_array
*fc
=
637 fs_sink_ctf_field_class_array_create_empty(
638 cur_path_stack_top(ctx
)->ir_fc
,
639 cur_path_stack_top(ctx
)->index_in_parent
);
640 const bt_field_class
*elem_ir_fc
=
641 bt_field_class_array_borrow_element_field_class_const(
642 fc
->base
.base
.ir_fc
);
646 append_to_parent_field_class(ctx
, (void *) fc
);
647 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, elem_ir_fc
,
650 BT_LOGE_STR("Cannot translate static array field class element.");
654 ret
= translate_field_class(ctx
);
656 BT_LOGE_STR("Cannot translate static array field class element.");
660 cur_path_stack_pop(ctx
);
661 update_parent_field_class_alignment(ctx
, fc
->base
.base
.alignment
);
668 int translate_dynamic_array_field_class(struct ctx
*ctx
)
670 struct fs_sink_ctf_field_class_sequence
*fc
=
671 fs_sink_ctf_field_class_sequence_create_empty(
672 cur_path_stack_top(ctx
)->ir_fc
,
673 cur_path_stack_top(ctx
)->index_in_parent
);
674 const bt_field_class
*elem_ir_fc
=
675 bt_field_class_array_borrow_element_field_class_const(
676 fc
->base
.base
.ir_fc
);
681 /* Resolve length field class before appending to parent */
682 resolve_field_class(ctx
,
683 bt_field_class_dynamic_array_borrow_length_field_path_const(
684 fc
->base
.base
.ir_fc
),
685 fc
->length_ref
, &fc
->length_is_before
);
687 append_to_parent_field_class(ctx
, (void *) fc
);
688 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, elem_ir_fc
,
691 BT_LOGE_STR("Cannot translate dynamic array field class element.");
695 ret
= translate_field_class(ctx
);
697 BT_LOGE_STR("Cannot translate dynamic array field class element.");
701 cur_path_stack_pop(ctx
);
702 update_parent_field_class_alignment(ctx
, fc
->base
.base
.alignment
);
709 int translate_integer_field_class(struct ctx
*ctx
)
711 struct fs_sink_ctf_field_class_int
*fc
=
712 fs_sink_ctf_field_class_int_create(
713 cur_path_stack_top(ctx
)->ir_fc
,
714 cur_path_stack_top(ctx
)->index_in_parent
);
717 append_to_parent_field_class(ctx
, (void *) fc
);
722 int translate_real_field_class(struct ctx
*ctx
)
724 struct fs_sink_ctf_field_class_float
*fc
=
725 fs_sink_ctf_field_class_float_create(
726 cur_path_stack_top(ctx
)->ir_fc
,
727 cur_path_stack_top(ctx
)->index_in_parent
);
730 append_to_parent_field_class(ctx
, (void *) fc
);
735 int translate_string_field_class(struct ctx
*ctx
)
737 struct fs_sink_ctf_field_class_string
*fc
=
738 fs_sink_ctf_field_class_string_create(
739 cur_path_stack_top(ctx
)->ir_fc
,
740 cur_path_stack_top(ctx
)->index_in_parent
);
743 append_to_parent_field_class(ctx
, (void *) fc
);
748 * Translates a field class, recursively.
750 * The field class's IR field class, parent field class, and index
751 * within its parent are in the context's current path's top element
752 * (cur_path_stack_top()).
755 int translate_field_class(struct ctx
*ctx
)
759 switch (bt_field_class_get_type(cur_path_stack_top(ctx
)->ir_fc
)) {
760 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
761 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
762 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
763 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
764 ret
= translate_integer_field_class(ctx
);
766 case BT_FIELD_CLASS_TYPE_REAL
:
767 ret
= translate_real_field_class(ctx
);
769 case BT_FIELD_CLASS_TYPE_STRING
:
770 ret
= translate_string_field_class(ctx
);
772 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
773 ret
= translate_structure_field_class(ctx
);
775 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
776 ret
= translate_static_array_field_class(ctx
);
778 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
779 ret
= translate_dynamic_array_field_class(ctx
);
781 case BT_FIELD_CLASS_TYPE_VARIANT
:
782 ret
= translate_variant_field_class(ctx
);
792 int set_field_ref(struct fs_sink_ctf_field_class
*fc
, const char *fc_name
,
793 struct fs_sink_ctf_field_class
*parent_fc
)
796 GString
*field_ref
= NULL
;
798 const char *tgt_type
;
799 struct fs_sink_ctf_field_class_struct
*parent_struct_fc
=
802 unsigned int suffix
= 0;
804 if (!fc_name
|| !parent_fc
||
805 parent_fc
->type
!= FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
812 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
814 struct fs_sink_ctf_field_class_sequence
*seq_fc
= (void *) fc
;
816 field_ref
= seq_fc
->length_ref
;
817 is_before
= seq_fc
->length_is_before
;
821 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
823 struct fs_sink_ctf_field_class_variant
*var_fc
= (void *) fc
;
825 field_ref
= var_fc
->tag_ref
;
826 is_before
= var_fc
->tag_is_before
;
834 BT_ASSERT(field_ref
);
840 /* Initial field ref */
841 g_string_printf(field_ref
, "__%s_%s", fc_name
, tgt_type
);
844 * Make sure field ref does not clash with an existing field
845 * class name within the same parent structure field class.
850 for (i
= 0; i
< parent_struct_fc
->members
->len
; i
++) {
851 struct fs_sink_ctf_named_field_class
*named_fc
=
852 fs_sink_ctf_field_class_struct_borrow_member_by_index(
853 parent_struct_fc
, i
);
855 if (strcmp(field_ref
->str
, named_fc
->name
->str
) == 0) {
863 /* No clash: we're done */
867 /* Append suffix and try again */
868 g_string_printf(field_ref
, "__%s_%s_%u", fc_name
, tgt_type
,
878 * This function recursively sets field refs of sequence and variant
879 * field classes when they are immediately before, avoiding name clashes
880 * with existing field class names.
882 * It can fail at this point if, for example, a sequence field class of
883 * which to set the length's field ref has something else than a
884 * structure field class as its parent: in this case, there's no
885 * location to place the length field class immediately before the
886 * sequence field class.
889 int set_field_refs(struct fs_sink_ctf_field_class
*fc
, const char *fc_name
,
890 struct fs_sink_ctf_field_class
*parent_fc
)
896 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
897 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
901 struct fs_sink_ctf_field_class_struct
*struct_fc
;
902 struct fs_sink_ctf_field_class_variant
*var_fc
;
903 struct fs_sink_ctf_named_field_class
*named_fc
;
905 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
906 struct_fc
= (void *) fc
;
907 len
= struct_fc
->members
->len
;
909 var_fc
= (void *) fc
;
910 len
= var_fc
->options
->len
;
911 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
917 for (i
= 0; i
< len
; i
++) {
918 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
919 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
922 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
926 ret
= set_field_refs(named_fc
->fc
, named_fc
->name
->str
,
935 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
936 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
938 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
941 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
942 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
948 ret
= set_field_refs(array_base_fc
->elem_fc
, NULL
, fc
);
964 * This function translates a root scope trace IR field class to
965 * a CTF IR field class.
967 * The resulting CTF IR field class is written to `*fc` so that it
968 * exists as the parent object's (stream class or event class) true root
969 * field class during the recursive translation for resolving purposes.
970 * This is also why this function creates the empty structure field
971 * class and then calls translate_structure_field_class_members() to
975 int translate_scope_field_class(struct ctx
*ctx
, bt_scope scope
,
976 struct fs_sink_ctf_field_class
**fc
,
977 const bt_field_class
*ir_fc
)
985 BT_ASSERT(bt_field_class_get_type(ir_fc
) ==
986 BT_FIELD_CLASS_TYPE_STRUCTURE
);
988 *fc
= (void *) fs_sink_ctf_field_class_struct_create_empty(
989 ir_fc
, UINT64_C(-1));
991 ctx
->cur_scope
= scope
;
992 BT_ASSERT(ctx
->cur_path
->len
== 0);
993 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, ir_fc
, NULL
);
995 BT_LOGE("Cannot translate scope structure field class: "
1000 ret
= translate_structure_field_class_members(ctx
, (void *) *fc
, ir_fc
);
1002 BT_LOGE("Cannot translate scope structure field class: "
1007 cur_path_stack_pop(ctx
);
1009 /* Set field refs for preceding targets */
1010 ret
= set_field_refs(*fc
, NULL
, NULL
);
1017 void ctx_init(struct ctx
*ctx
)
1019 memset(ctx
, 0, sizeof(struct ctx
));
1020 ctx
->cur_path
= g_array_new(FALSE
, TRUE
,
1021 sizeof(struct field_path_elem
));
1022 BT_ASSERT(ctx
->cur_path
);
1026 void ctx_fini(struct ctx
*ctx
)
1028 if (ctx
->cur_path
) {
1029 g_array_free(ctx
->cur_path
, TRUE
);
1030 ctx
->cur_path
= NULL
;
1035 int translate_event_class(struct fs_sink_ctf_stream_class
*sc
,
1036 const bt_event_class
*ir_ec
,
1037 struct fs_sink_ctf_event_class
**out_ec
)
1041 struct fs_sink_ctf_event_class
*ec
;
1047 ec
= fs_sink_ctf_event_class_create(sc
, ir_ec
);
1051 ret
= translate_scope_field_class(&ctx
, BT_SCOPE_EVENT_SPECIFIC_CONTEXT
,
1052 &ec
->spec_context_fc
,
1053 bt_event_class_borrow_specific_context_field_class_const(
1059 ret
= translate_scope_field_class(&ctx
, BT_SCOPE_EVENT_PAYLOAD
,
1061 bt_event_class_borrow_payload_field_class_const(ir_ec
));
1073 int try_translate_event_class_trace_ir_to_ctf_ir(
1074 struct fs_sink_ctf_stream_class
*sc
,
1075 const bt_event_class
*ir_ec
,
1076 struct fs_sink_ctf_event_class
**out_ec
)
1083 /* Check in hash table first */
1084 *out_ec
= g_hash_table_lookup(sc
->event_classes_from_ir
, ir_ec
);
1085 if (likely(*out_ec
)) {
1089 ret
= translate_event_class(sc
, ir_ec
, out_ec
);
1095 bool default_clock_class_name_exists(struct fs_sink_ctf_trace_class
*tc
,
1098 bool exists
= false;
1101 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1102 struct fs_sink_ctf_stream_class
*sc
=
1103 tc
->stream_classes
->pdata
[i
];
1105 if (sc
->default_clock_class_name
->len
== 0) {
1106 /* No default clock class */
1110 if (strcmp(sc
->default_clock_class_name
->str
, name
) == 0) {
1121 void make_unique_default_clock_class_name(struct fs_sink_ctf_stream_class
*sc
)
1123 unsigned int suffix
= 0;
1126 g_string_assign(sc
->default_clock_class_name
, "");
1127 sprintf(buf
, "default");
1129 while (default_clock_class_name_exists(sc
->tc
, buf
)) {
1130 sprintf(buf
, "default%u", suffix
);
1134 g_string_assign(sc
->default_clock_class_name
, buf
);
1138 int translate_stream_class(struct fs_sink_ctf_trace_class
*tc
,
1139 const bt_stream_class
*ir_sc
,
1140 struct fs_sink_ctf_stream_class
**out_sc
)
1148 *out_sc
= fs_sink_ctf_stream_class_create(tc
, ir_sc
);
1151 /* Set default clock class's protected name, if any */
1152 if ((*out_sc
)->default_clock_class
) {
1153 const char *name
= bt_clock_class_get_name(
1154 (*out_sc
)->default_clock_class
);
1156 if (!bt_stream_class_default_clock_is_always_known(ir_sc
)) {
1157 BT_LOGE("Unsupported stream clock which can have an unknown value: "
1159 bt_stream_class_get_name(ir_sc
));
1164 /* Try original name, protected */
1165 g_string_assign((*out_sc
)->default_clock_class_name
,
1167 ret
= fs_sink_ctf_protect_name(
1168 (*out_sc
)->default_clock_class_name
);
1170 /* Invalid: create a new name */
1171 make_unique_default_clock_class_name(*out_sc
);
1175 /* No name: create a name */
1176 make_unique_default_clock_class_name(*out_sc
);
1180 ctx
.cur_sc
= *out_sc
;
1181 ret
= translate_scope_field_class(&ctx
, BT_SCOPE_PACKET_CONTEXT
,
1182 &(*out_sc
)->packet_context_fc
,
1183 bt_stream_class_borrow_packet_context_field_class_const(ir_sc
));
1188 if ((*out_sc
)->packet_context_fc
) {
1190 * Make sure the structure field class's alignment is
1191 * enough: 8 is what we use for our own special members
1192 * in the packet context.
1194 fs_sink_ctf_field_class_struct_align_at_least(
1195 (void *) (*out_sc
)->packet_context_fc
, 8);
1198 ret
= translate_scope_field_class(&ctx
, BT_SCOPE_EVENT_COMMON_CONTEXT
,
1199 &(*out_sc
)->event_common_context_fc
,
1200 bt_stream_class_borrow_event_common_context_field_class_const(
1209 fs_sink_ctf_stream_class_destroy(*out_sc
);
1218 int try_translate_stream_class_trace_ir_to_ctf_ir(
1219 struct fs_sink_ctf_trace_class
*tc
,
1220 const bt_stream_class
*ir_sc
,
1221 struct fs_sink_ctf_stream_class
**out_sc
)
1229 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1230 *out_sc
= tc
->stream_classes
->pdata
[i
];
1232 if ((*out_sc
)->ir_sc
== ir_sc
) {
1237 ret
= translate_stream_class(tc
, ir_sc
, out_sc
);
1244 struct fs_sink_ctf_trace_class
*translate_trace_class_trace_ir_to_ctf_ir(
1245 const bt_trace_class
*ir_tc
)
1249 struct fs_sink_ctf_trace_class
*tc
= NULL
;
1251 /* Check that trace class's environment is TSDL-compatible */
1252 count
= bt_trace_class_get_environment_entry_count(ir_tc
);
1253 for (i
= 0; i
< count
; i
++) {
1255 const bt_value
*val
;
1257 bt_trace_class_borrow_environment_entry_by_index_const(
1258 ir_tc
, i
, &name
, &val
);
1260 if (!fs_sink_ctf_ist_valid_identifier(name
)) {
1261 BT_LOGE("Unsupported trace class's environment entry name: "
1262 "name=\"%s\"", name
);
1266 switch (bt_value_get_type(val
)) {
1267 case BT_VALUE_TYPE_INTEGER
:
1268 case BT_VALUE_TYPE_STRING
:
1271 BT_LOGE("Unsupported trace class's environment entry value type: "
1273 bt_common_value_type_string(
1274 bt_value_get_type(val
)));
1279 tc
= fs_sink_ctf_trace_class_create(ir_tc
);