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_item_count(tgt_ir_field_path
)) {
205 const bt_field_path_item
*fp_item
=
206 bt_field_path_borrow_item_by_index_const(
207 tgt_ir_field_path
, i
);
208 struct fs_sink_ctf_named_field_class
*named_fc
= NULL
;
213 switch (tgt_fc
->type
) {
214 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
215 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
216 BT_FIELD_PATH_ITEM_TYPE_INDEX
);
217 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
219 bt_field_path_item_index_get_index(fp_item
));
221 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
222 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
223 BT_FIELD_PATH_ITEM_TYPE_INDEX
);
224 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
226 bt_field_path_item_index_get_index(fp_item
));
228 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
229 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
231 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
234 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
235 BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
);
236 tgt_fc
= array_base_fc
->elem_fc
;
244 tgt_fc
= named_fc
->fc
;
245 tgt_fc_name
= named_fc
->name
->str
;
251 BT_ASSERT(tgt_fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_INT
);
252 BT_ASSERT(tgt_fc_name
);
254 /* Find target field class having this name in current context */
255 for (si
= ctx
->cur_path
->len
- 1; si
>= 0; si
--) {
256 struct fs_sink_ctf_field_class
*fc
;
257 struct fs_sink_ctf_field_class_struct
*struct_fc
;
258 struct fs_sink_ctf_field_class_variant
*var_fc
;
259 struct fs_sink_ctf_named_field_class
*named_fc
;
262 field_path_elem
= cur_path_stack_at(ctx
, (uint64_t) si
);
263 fc
= field_path_elem
->parent_fc
;
265 /* Reached stack's bottom */
271 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
272 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
274 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
275 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
278 /* Not supported by TSDL 1.8 */
283 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
284 struct_fc
= (void *) fc
;
285 len
= struct_fc
->members
->len
;
287 var_fc
= (void *) fc
;
288 len
= var_fc
->options
->len
;
291 for (i
= 0; i
< len
; i
++) {
292 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
293 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
296 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
300 if (strcmp(named_fc
->name
->str
, tgt_fc_name
) == 0) {
301 if (named_fc
->fc
== tgt_fc
) {
302 g_string_assign(tgt_field_ref
,
306 * Using only the target field
307 * class's name, we're not
308 * reaching the target field
309 * class. This is not supported
325 * Creates an absolute field ref from IR field path `tgt_ir_field_path`.
327 * Returns a negative value if this resolving operation failed.
330 int create_absolute_field_ref(struct ctx
*ctx
,
331 const bt_field_path
*tgt_ir_field_path
, GString
*tgt_field_ref
)
334 struct fs_sink_ctf_field_class
*fc
= NULL
;
337 switch (bt_field_path_get_root_scope(tgt_ir_field_path
)) {
338 case BT_SCOPE_PACKET_CONTEXT
:
339 BT_ASSERT(ctx
->cur_sc
);
340 fc
= ctx
->cur_sc
->packet_context_fc
;
341 g_string_assign(tgt_field_ref
, "stream.packet.context");
343 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
344 BT_ASSERT(ctx
->cur_sc
);
345 fc
= ctx
->cur_sc
->event_common_context_fc
;
346 g_string_assign(tgt_field_ref
, "stream.event.context");
348 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
349 BT_ASSERT(ctx
->cur_ec
);
350 fc
= ctx
->cur_ec
->spec_context_fc
;
351 g_string_assign(tgt_field_ref
, "event.context");
353 case BT_SCOPE_EVENT_PAYLOAD
:
354 BT_ASSERT(ctx
->cur_ec
);
355 fc
= ctx
->cur_ec
->payload_fc
;
356 g_string_assign(tgt_field_ref
, "event.fields");
364 for (i
= 0; i
< bt_field_path_get_item_count(tgt_ir_field_path
); i
++) {
365 const bt_field_path_item
*fp_item
=
366 bt_field_path_borrow_item_by_index_const(
367 tgt_ir_field_path
, i
);
368 struct fs_sink_ctf_named_field_class
*named_fc
= NULL
;
370 if (bt_field_path_item_get_type(fp_item
) !=
371 BT_FIELD_PATH_ITEM_TYPE_INDEX
) {
372 /* Not supported by TSDL 1.8 */
378 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
379 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
380 BT_FIELD_PATH_ITEM_TYPE_INDEX
);
381 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
383 bt_field_path_item_index_get_index(fp_item
));
385 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
386 BT_ASSERT(bt_field_path_item_get_type(fp_item
) ==
387 BT_FIELD_PATH_ITEM_TYPE_INDEX
);
388 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
390 bt_field_path_item_index_get_index(fp_item
));
397 g_string_append_c(tgt_field_ref
, '.');
398 g_string_append(tgt_field_ref
, named_fc
->name
->str
);
407 * Resolves a target field class located at `tgt_ir_field_path`, writing
408 * the resolved field ref to `tgt_field_ref` and setting
409 * `*create_before` according to whether or not the target field must be
410 * created immediately before (in which case `tgt_field_ref` is
414 void resolve_field_class(struct ctx
*ctx
,
415 const bt_field_path
*tgt_ir_field_path
,
416 GString
*tgt_field_ref
, bool *create_before
)
421 *create_before
= false;
423 if (!tgt_ir_field_path
) {
424 *create_before
= true;
428 tgt_scope
= bt_field_path_get_root_scope(tgt_ir_field_path
);
430 if (tgt_scope
== ctx
->cur_scope
) {
432 * Try, in this order:
434 * 1. Use a relative path, using only the target field
435 * class's name. This is what is the most commonly
436 * supported by popular CTF reading tools.
438 * 2. Use an absolute path. This could fail if there's
439 * an array field class from the current root's field
440 * class to the target field class.
442 * 3. Create the target field class before the
443 * requesting field class (fallback).
445 ret
= create_relative_field_ref(ctx
, tgt_ir_field_path
,
448 ret
= create_absolute_field_ref(ctx
, tgt_ir_field_path
,
451 *create_before
= true;
457 ret
= create_absolute_field_ref(ctx
, tgt_ir_field_path
,
460 /* It must always work in previous scopes */
469 int translate_field_class(struct ctx
*ctx
);
472 void append_to_parent_field_class(struct ctx
*ctx
,
473 struct fs_sink_ctf_field_class
*fc
)
475 struct fs_sink_ctf_field_class
*parent_fc
=
476 cur_path_stack_top(ctx
)->parent_fc
;
478 switch (parent_fc
->type
) {
479 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
480 fs_sink_ctf_field_class_struct_append_member((void *) parent_fc
,
481 cur_path_stack_top(ctx
)->name
->str
, fc
);
483 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
484 fs_sink_ctf_field_class_variant_append_option((void *) parent_fc
,
485 cur_path_stack_top(ctx
)->name
->str
, fc
);
487 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
488 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
490 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
493 BT_ASSERT(!array_base_fc
->elem_fc
);
494 array_base_fc
->elem_fc
= fc
;
495 array_base_fc
->base
.alignment
= fc
->alignment
;
504 void update_parent_field_class_alignment(struct ctx
*ctx
,
505 unsigned int alignment
)
507 struct fs_sink_ctf_field_class
*parent_fc
=
508 cur_path_stack_top(ctx
)->parent_fc
;
510 switch (parent_fc
->type
) {
511 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
512 fs_sink_ctf_field_class_struct_align_at_least(
513 (void *) parent_fc
, alignment
);
515 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
516 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
518 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
521 array_base_fc
->base
.alignment
= alignment
;
530 int translate_structure_field_class_members(struct ctx
*ctx
,
531 struct fs_sink_ctf_field_class_struct
*struct_fc
,
532 const bt_field_class
*ir_fc
)
537 for (i
= 0; i
< bt_field_class_structure_get_member_count(ir_fc
); i
++) {
538 const bt_field_class_structure_member
*member
;
540 const bt_field_class
*memb_ir_fc
;
543 bt_field_class_structure_borrow_member_by_index_const(
545 name
= bt_field_class_structure_member_get_name(member
);
546 memb_ir_fc
= bt_field_class_structure_member_borrow_field_class_const(
548 ret
= cur_path_stack_push(ctx
, i
, name
, memb_ir_fc
,
551 BT_LOGE("Cannot translate structure field class member: "
552 "name=\"%s\"", name
);
556 ret
= translate_field_class(ctx
);
558 BT_LOGE("Cannot translate structure field class member: "
559 "name=\"%s\"", name
);
563 cur_path_stack_pop(ctx
);
571 int translate_structure_field_class(struct ctx
*ctx
)
574 struct fs_sink_ctf_field_class_struct
*fc
=
575 fs_sink_ctf_field_class_struct_create_empty(
576 cur_path_stack_top(ctx
)->ir_fc
,
577 cur_path_stack_top(ctx
)->index_in_parent
);
580 append_to_parent_field_class(ctx
, (void *) fc
);
581 ret
= translate_structure_field_class_members(ctx
, fc
, fc
->base
.ir_fc
);
586 update_parent_field_class_alignment(ctx
, fc
->base
.alignment
);
593 int translate_variant_field_class(struct ctx
*ctx
)
597 struct fs_sink_ctf_field_class_variant
*fc
=
598 fs_sink_ctf_field_class_variant_create_empty(
599 cur_path_stack_top(ctx
)->ir_fc
,
600 cur_path_stack_top(ctx
)->index_in_parent
);
604 /* Resolve tag field class before appending to parent */
605 resolve_field_class(ctx
,
606 bt_field_class_variant_borrow_selector_field_path_const(
607 fc
->base
.ir_fc
), fc
->tag_ref
, &fc
->tag_is_before
);
609 append_to_parent_field_class(ctx
, (void *) fc
);
611 for (i
= 0; i
< bt_field_class_variant_get_option_count(fc
->base
.ir_fc
);
613 const bt_field_class_variant_option
*opt
;
615 const bt_field_class
*opt_ir_fc
;
617 opt
= bt_field_class_variant_borrow_option_by_index_const(
619 name
= bt_field_class_variant_option_get_name(opt
);
620 opt_ir_fc
= bt_field_class_variant_option_borrow_field_class_const(
622 ret
= cur_path_stack_push(ctx
, i
, name
, opt_ir_fc
, (void *) fc
);
624 BT_LOGE("Cannot translate variant field class option: "
625 "name=\"%s\"", name
);
629 ret
= translate_field_class(ctx
);
631 BT_LOGE("Cannot translate variant field class option: "
632 "name=\"%s\"", name
);
636 cur_path_stack_pop(ctx
);
644 int translate_static_array_field_class(struct ctx
*ctx
)
646 struct fs_sink_ctf_field_class_array
*fc
=
647 fs_sink_ctf_field_class_array_create_empty(
648 cur_path_stack_top(ctx
)->ir_fc
,
649 cur_path_stack_top(ctx
)->index_in_parent
);
650 const bt_field_class
*elem_ir_fc
=
651 bt_field_class_array_borrow_element_field_class_const(
652 fc
->base
.base
.ir_fc
);
656 append_to_parent_field_class(ctx
, (void *) fc
);
657 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, elem_ir_fc
,
660 BT_LOGE_STR("Cannot translate static array field class element.");
664 ret
= translate_field_class(ctx
);
666 BT_LOGE_STR("Cannot translate static array field class element.");
670 cur_path_stack_pop(ctx
);
671 update_parent_field_class_alignment(ctx
, fc
->base
.base
.alignment
);
678 int translate_dynamic_array_field_class(struct ctx
*ctx
)
680 struct fs_sink_ctf_field_class_sequence
*fc
=
681 fs_sink_ctf_field_class_sequence_create_empty(
682 cur_path_stack_top(ctx
)->ir_fc
,
683 cur_path_stack_top(ctx
)->index_in_parent
);
684 const bt_field_class
*elem_ir_fc
=
685 bt_field_class_array_borrow_element_field_class_const(
686 fc
->base
.base
.ir_fc
);
691 /* Resolve length field class before appending to parent */
692 resolve_field_class(ctx
,
693 bt_field_class_dynamic_array_borrow_length_field_path_const(
694 fc
->base
.base
.ir_fc
),
695 fc
->length_ref
, &fc
->length_is_before
);
697 append_to_parent_field_class(ctx
, (void *) fc
);
698 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, elem_ir_fc
,
701 BT_LOGE_STR("Cannot translate dynamic array field class element.");
705 ret
= translate_field_class(ctx
);
707 BT_LOGE_STR("Cannot translate dynamic array field class element.");
711 cur_path_stack_pop(ctx
);
712 update_parent_field_class_alignment(ctx
, fc
->base
.base
.alignment
);
719 int translate_integer_field_class(struct ctx
*ctx
)
721 struct fs_sink_ctf_field_class_int
*fc
=
722 fs_sink_ctf_field_class_int_create(
723 cur_path_stack_top(ctx
)->ir_fc
,
724 cur_path_stack_top(ctx
)->index_in_parent
);
727 append_to_parent_field_class(ctx
, (void *) fc
);
732 int translate_real_field_class(struct ctx
*ctx
)
734 struct fs_sink_ctf_field_class_float
*fc
=
735 fs_sink_ctf_field_class_float_create(
736 cur_path_stack_top(ctx
)->ir_fc
,
737 cur_path_stack_top(ctx
)->index_in_parent
);
740 append_to_parent_field_class(ctx
, (void *) fc
);
745 int translate_string_field_class(struct ctx
*ctx
)
747 struct fs_sink_ctf_field_class_string
*fc
=
748 fs_sink_ctf_field_class_string_create(
749 cur_path_stack_top(ctx
)->ir_fc
,
750 cur_path_stack_top(ctx
)->index_in_parent
);
753 append_to_parent_field_class(ctx
, (void *) fc
);
758 * Translates a field class, recursively.
760 * The field class's IR field class, parent field class, and index
761 * within its parent are in the context's current path's top element
762 * (cur_path_stack_top()).
765 int translate_field_class(struct ctx
*ctx
)
769 switch (bt_field_class_get_type(cur_path_stack_top(ctx
)->ir_fc
)) {
770 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
771 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
772 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
773 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
774 ret
= translate_integer_field_class(ctx
);
776 case BT_FIELD_CLASS_TYPE_REAL
:
777 ret
= translate_real_field_class(ctx
);
779 case BT_FIELD_CLASS_TYPE_STRING
:
780 ret
= translate_string_field_class(ctx
);
782 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
783 ret
= translate_structure_field_class(ctx
);
785 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
786 ret
= translate_static_array_field_class(ctx
);
788 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
789 ret
= translate_dynamic_array_field_class(ctx
);
791 case BT_FIELD_CLASS_TYPE_VARIANT
:
792 ret
= translate_variant_field_class(ctx
);
802 int set_field_ref(struct fs_sink_ctf_field_class
*fc
, const char *fc_name
,
803 struct fs_sink_ctf_field_class
*parent_fc
)
806 GString
*field_ref
= NULL
;
808 const char *tgt_type
;
809 struct fs_sink_ctf_field_class_struct
*parent_struct_fc
=
812 unsigned int suffix
= 0;
814 if (!fc_name
|| !parent_fc
||
815 parent_fc
->type
!= FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
822 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
824 struct fs_sink_ctf_field_class_sequence
*seq_fc
= (void *) fc
;
826 field_ref
= seq_fc
->length_ref
;
827 is_before
= seq_fc
->length_is_before
;
831 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
833 struct fs_sink_ctf_field_class_variant
*var_fc
= (void *) fc
;
835 field_ref
= var_fc
->tag_ref
;
836 is_before
= var_fc
->tag_is_before
;
844 BT_ASSERT(field_ref
);
850 /* Initial field ref */
851 g_string_printf(field_ref
, "__%s_%s", fc_name
, tgt_type
);
854 * Make sure field ref does not clash with an existing field
855 * class name within the same parent structure field class.
860 for (i
= 0; i
< parent_struct_fc
->members
->len
; i
++) {
861 struct fs_sink_ctf_named_field_class
*named_fc
=
862 fs_sink_ctf_field_class_struct_borrow_member_by_index(
863 parent_struct_fc
, i
);
865 if (strcmp(field_ref
->str
, named_fc
->name
->str
) == 0) {
873 /* No clash: we're done */
877 /* Append suffix and try again */
878 g_string_printf(field_ref
, "__%s_%s_%u", fc_name
, tgt_type
,
888 * This function recursively sets field refs of sequence and variant
889 * field classes when they are immediately before, avoiding name clashes
890 * with existing field class names.
892 * It can fail at this point if, for example, a sequence field class of
893 * which to set the length's field ref has something else than a
894 * structure field class as its parent: in this case, there's no
895 * location to place the length field class immediately before the
896 * sequence field class.
899 int set_field_refs(struct fs_sink_ctf_field_class
*fc
, const char *fc_name
,
900 struct fs_sink_ctf_field_class
*parent_fc
)
906 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
907 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
911 struct fs_sink_ctf_field_class_struct
*struct_fc
;
912 struct fs_sink_ctf_field_class_variant
*var_fc
;
913 struct fs_sink_ctf_named_field_class
*named_fc
;
915 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
916 struct_fc
= (void *) fc
;
917 len
= struct_fc
->members
->len
;
919 var_fc
= (void *) fc
;
920 len
= var_fc
->options
->len
;
921 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
927 for (i
= 0; i
< len
; i
++) {
928 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
) {
929 named_fc
= fs_sink_ctf_field_class_struct_borrow_member_by_index(
932 named_fc
= fs_sink_ctf_field_class_variant_borrow_option_by_index(
936 ret
= set_field_refs(named_fc
->fc
, named_fc
->name
->str
,
945 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
946 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
948 struct fs_sink_ctf_field_class_array_base
*array_base_fc
=
951 if (fc
->type
== FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
952 ret
= set_field_ref(fc
, fc_name
, parent_fc
);
958 ret
= set_field_refs(array_base_fc
->elem_fc
, NULL
, fc
);
974 * This function translates a root scope trace IR field class to
975 * a CTF IR field class.
977 * The resulting CTF IR field class is written to `*fc` so that it
978 * exists as the parent object's (stream class or event class) true root
979 * field class during the recursive translation for resolving purposes.
980 * This is also why this function creates the empty structure field
981 * class and then calls translate_structure_field_class_members() to
985 int translate_scope_field_class(struct ctx
*ctx
, bt_scope scope
,
986 struct fs_sink_ctf_field_class
**fc
,
987 const bt_field_class
*ir_fc
)
995 BT_ASSERT(bt_field_class_get_type(ir_fc
) ==
996 BT_FIELD_CLASS_TYPE_STRUCTURE
);
998 *fc
= (void *) fs_sink_ctf_field_class_struct_create_empty(
999 ir_fc
, UINT64_C(-1));
1001 ctx
->cur_scope
= scope
;
1002 BT_ASSERT(ctx
->cur_path
->len
== 0);
1003 ret
= cur_path_stack_push(ctx
, UINT64_C(-1), NULL
, ir_fc
, NULL
);
1005 BT_LOGE("Cannot translate scope structure field class: "
1010 ret
= translate_structure_field_class_members(ctx
, (void *) *fc
, ir_fc
);
1012 BT_LOGE("Cannot translate scope structure field class: "
1017 cur_path_stack_pop(ctx
);
1019 /* Set field refs for preceding targets */
1020 ret
= set_field_refs(*fc
, NULL
, NULL
);
1027 void ctx_init(struct ctx
*ctx
)
1029 memset(ctx
, 0, sizeof(struct ctx
));
1030 ctx
->cur_path
= g_array_new(FALSE
, TRUE
,
1031 sizeof(struct field_path_elem
));
1032 BT_ASSERT(ctx
->cur_path
);
1036 void ctx_fini(struct ctx
*ctx
)
1038 if (ctx
->cur_path
) {
1039 g_array_free(ctx
->cur_path
, TRUE
);
1040 ctx
->cur_path
= NULL
;
1045 int translate_event_class(struct fs_sink_ctf_stream_class
*sc
,
1046 const bt_event_class
*ir_ec
,
1047 struct fs_sink_ctf_event_class
**out_ec
)
1051 struct fs_sink_ctf_event_class
*ec
;
1057 ec
= fs_sink_ctf_event_class_create(sc
, ir_ec
);
1061 ret
= translate_scope_field_class(&ctx
, BT_SCOPE_EVENT_SPECIFIC_CONTEXT
,
1062 &ec
->spec_context_fc
,
1063 bt_event_class_borrow_specific_context_field_class_const(
1069 ret
= translate_scope_field_class(&ctx
, BT_SCOPE_EVENT_PAYLOAD
,
1071 bt_event_class_borrow_payload_field_class_const(ir_ec
));
1083 int try_translate_event_class_trace_ir_to_ctf_ir(
1084 struct fs_sink_ctf_stream_class
*sc
,
1085 const bt_event_class
*ir_ec
,
1086 struct fs_sink_ctf_event_class
**out_ec
)
1093 /* Check in hash table first */
1094 *out_ec
= g_hash_table_lookup(sc
->event_classes_from_ir
, ir_ec
);
1095 if (likely(*out_ec
)) {
1099 ret
= translate_event_class(sc
, ir_ec
, out_ec
);
1105 bool default_clock_class_name_exists(struct fs_sink_ctf_trace_class
*tc
,
1108 bool exists
= false;
1111 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1112 struct fs_sink_ctf_stream_class
*sc
=
1113 tc
->stream_classes
->pdata
[i
];
1115 if (sc
->default_clock_class_name
->len
== 0) {
1116 /* No default clock class */
1120 if (strcmp(sc
->default_clock_class_name
->str
, name
) == 0) {
1131 void make_unique_default_clock_class_name(struct fs_sink_ctf_stream_class
*sc
)
1133 unsigned int suffix
= 0;
1136 g_string_assign(sc
->default_clock_class_name
, "");
1137 sprintf(buf
, "default");
1139 while (default_clock_class_name_exists(sc
->tc
, buf
)) {
1140 sprintf(buf
, "default%u", suffix
);
1144 g_string_assign(sc
->default_clock_class_name
, buf
);
1148 int translate_stream_class(struct fs_sink_ctf_trace_class
*tc
,
1149 const bt_stream_class
*ir_sc
,
1150 struct fs_sink_ctf_stream_class
**out_sc
)
1158 *out_sc
= fs_sink_ctf_stream_class_create(tc
, ir_sc
);
1161 /* Set default clock class's protected name, if any */
1162 if ((*out_sc
)->default_clock_class
) {
1163 const char *name
= bt_clock_class_get_name(
1164 (*out_sc
)->default_clock_class
);
1166 if (!bt_stream_class_default_clock_is_always_known(ir_sc
)) {
1167 BT_LOGE("Unsupported stream clock which can have an unknown value: "
1169 bt_stream_class_get_name(ir_sc
));
1174 /* Try original name, protected */
1175 g_string_assign((*out_sc
)->default_clock_class_name
,
1177 ret
= fs_sink_ctf_protect_name(
1178 (*out_sc
)->default_clock_class_name
);
1180 /* Invalid: create a new name */
1181 make_unique_default_clock_class_name(*out_sc
);
1185 /* No name: create a name */
1186 make_unique_default_clock_class_name(*out_sc
);
1190 ctx
.cur_sc
= *out_sc
;
1191 ret
= translate_scope_field_class(&ctx
, BT_SCOPE_PACKET_CONTEXT
,
1192 &(*out_sc
)->packet_context_fc
,
1193 bt_stream_class_borrow_packet_context_field_class_const(ir_sc
));
1198 if ((*out_sc
)->packet_context_fc
) {
1200 * Make sure the structure field class's alignment is
1201 * enough: 8 is what we use for our own special members
1202 * in the packet context.
1204 fs_sink_ctf_field_class_struct_align_at_least(
1205 (void *) (*out_sc
)->packet_context_fc
, 8);
1208 ret
= translate_scope_field_class(&ctx
, BT_SCOPE_EVENT_COMMON_CONTEXT
,
1209 &(*out_sc
)->event_common_context_fc
,
1210 bt_stream_class_borrow_event_common_context_field_class_const(
1219 fs_sink_ctf_stream_class_destroy(*out_sc
);
1228 int try_translate_stream_class_trace_ir_to_ctf_ir(
1229 struct fs_sink_ctf_trace_class
*tc
,
1230 const bt_stream_class
*ir_sc
,
1231 struct fs_sink_ctf_stream_class
**out_sc
)
1239 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1240 *out_sc
= tc
->stream_classes
->pdata
[i
];
1242 if ((*out_sc
)->ir_sc
== ir_sc
) {
1247 ret
= translate_stream_class(tc
, ir_sc
, out_sc
);
1254 struct fs_sink_ctf_trace_class
*translate_trace_class_trace_ir_to_ctf_ir(
1255 const bt_trace_class
*ir_tc
)
1259 struct fs_sink_ctf_trace_class
*tc
= NULL
;
1261 /* Check that trace class's environment is TSDL-compatible */
1262 count
= bt_trace_class_get_environment_entry_count(ir_tc
);
1263 for (i
= 0; i
< count
; i
++) {
1265 const bt_value
*val
;
1267 bt_trace_class_borrow_environment_entry_by_index_const(
1268 ir_tc
, i
, &name
, &val
);
1270 if (!fs_sink_ctf_ist_valid_identifier(name
)) {
1271 BT_LOGE("Unsupported trace class's environment entry name: "
1272 "name=\"%s\"", name
);
1276 switch (bt_value_get_type(val
)) {
1277 case BT_VALUE_TYPE_INTEGER
:
1278 case BT_VALUE_TYPE_STRING
:
1281 BT_LOGE("Unsupported trace class's environment entry value type: "
1283 bt_common_value_type_string(
1284 bt_value_get_type(val
)));
1289 tc
= fs_sink_ctf_trace_class_create(ir_tc
);