2 * SPDX-License-Identifier: MIT
4 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
7 * Babeltrace - CTF writer: Type resolving internal
10 #define BT_LOG_TAG "CTF-WRITER/RESOLVE"
18 #include <babeltrace2-ctf-writer/field-types.h>
19 #include <babeltrace2-ctf-writer/object.h>
20 #include <babeltrace2-ctf-writer/stream-class.h>
21 #include <babeltrace2/types.h>
23 #include "common/macros.h"
24 #include "common/assert.h"
26 #include "field-path.h"
31 typedef GPtrArray type_stack
;
36 * `type` contains a compound field type (structure, variant, array,
37 * or sequence) and `index` indicates the index of the field type in
38 * the upper frame (-1 for array and sequence field types).
40 * `type` is owned by the stack frame.
42 struct type_stack_frame
{
43 struct bt_ctf_field_type_common
*type
;
48 * The current context of the resolving engine.
50 * `scopes` contain the 6 CTF scope field types (see CTF, sect. 7.3.2)
51 * in the following order:
56 * * Stream event context
60 struct resolve_context
{
61 struct bt_ctf_private_value
*environment
;
62 struct bt_ctf_field_type_common
*scopes
[6];
64 /* Root scope being visited */
65 enum bt_ctf_scope root_scope
;
66 type_stack
*type_stack
;
67 struct bt_ctf_field_type_common
*cur_field_type
;
70 /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
71 static const char * const absolute_path_prefixes
[] = {
72 [BT_CTF_SCOPE_ENV
] = "env.",
73 [BT_CTF_SCOPE_TRACE_PACKET_HEADER
] = "trace.packet.header.",
74 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
] = "stream.packet.context.",
75 [BT_CTF_SCOPE_STREAM_EVENT_HEADER
] = "stream.event.header.",
76 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
] = "stream.event.context.",
77 [BT_CTF_SCOPE_EVENT_CONTEXT
] = "event.context.",
78 [BT_CTF_SCOPE_EVENT_FIELDS
] = "event.fields.",
81 /* Number of path tokens used for the absolute prefixes */
82 static const int absolute_path_prefix_ptoken_counts
[] = {
83 [BT_CTF_SCOPE_ENV
] = 1,
84 [BT_CTF_SCOPE_TRACE_PACKET_HEADER
] = 3,
85 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
] = 3,
86 [BT_CTF_SCOPE_STREAM_EVENT_HEADER
] = 3,
87 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
] = 3,
88 [BT_CTF_SCOPE_EVENT_CONTEXT
] = 2,
89 [BT_CTF_SCOPE_EVENT_FIELDS
] = 2,
93 * Destroys a type stack frame.
96 void type_stack_destroy_notify(gpointer data
)
98 struct type_stack_frame
*frame
= data
;
100 BT_CTF_OBJECT_PUT_REF_AND_RESET(frame
->type
);
105 * Creates a type stack.
107 * Return value is owned by the caller.
110 type_stack
*type_stack_create(void)
112 return g_ptr_array_new_with_free_func(type_stack_destroy_notify
);
116 * Destroys a type stack.
119 void type_stack_destroy(type_stack
*stack
)
121 g_ptr_array_free(stack
, TRUE
);
125 * Pushes a field type onto a type stack.
127 * `type` is owned by the caller (stack frame gets a new reference).
130 int type_stack_push(type_stack
*stack
, struct bt_ctf_field_type_common
*type
)
133 struct type_stack_frame
*frame
= NULL
;
135 if (!stack
|| !type
) {
136 BT_LOGW("Invalid parameter: stack or type is NULL.");
141 frame
= g_new0(struct type_stack_frame
, 1);
143 BT_LOGE_STR("Failed to allocate one field type stack frame.");
148 BT_LOGT("Pushing field type on context's stack: "
149 "ft-addr=%p, stack-size-before=%u", type
, stack
->len
);
150 frame
->type
= bt_ctf_object_get_ref(type
);
151 g_ptr_array_add(stack
, frame
);
158 * Checks whether or not `stack` is empty.
161 bt_ctf_bool
type_stack_empty(type_stack
*stack
)
163 return stack
->len
== 0;
167 * Returns the number of frames in `stack`.
170 size_t type_stack_size(type_stack
*stack
)
176 * Returns the top frame of `stack`.
178 * Return value is owned by `stack`.
181 struct type_stack_frame
*type_stack_peek(type_stack
*stack
)
184 BT_ASSERT(!type_stack_empty(stack
));
186 return g_ptr_array_index(stack
, stack
->len
- 1);
190 * Returns the frame at index `index` in `stack`.
192 * Return value is owned by `stack`.
195 struct type_stack_frame
*type_stack_at(type_stack
*stack
, size_t index
)
198 BT_ASSERT(index
< stack
->len
);
200 return g_ptr_array_index(stack
, index
);
204 * Removes the top frame of `stack`.
207 void type_stack_pop(type_stack
*stack
)
209 if (!type_stack_empty(stack
)) {
211 * This will call the frame's destructor and free it, as
212 * well as put its contained field type.
214 BT_LOGT("Popping context's stack: stack-size-before=%u",
216 g_ptr_array_set_size(stack
, stack
->len
- 1);
221 * Returns the scope field type of `scope` in the context `ctx`.
223 * Return value is owned by `ctx` on success.
226 struct bt_ctf_field_type_common
*get_type_from_ctx(struct resolve_context
*ctx
,
227 enum bt_ctf_scope scope
)
229 BT_ASSERT_DBG(scope
>= BT_CTF_SCOPE_TRACE_PACKET_HEADER
&&
230 scope
<= BT_CTF_SCOPE_EVENT_FIELDS
);
232 return ctx
->scopes
[scope
- BT_CTF_SCOPE_TRACE_PACKET_HEADER
];
236 * Returns the CTF scope from a path string. May return
237 * CTF_NODE_UNKNOWN if the path is found to be relative.
240 enum bt_ctf_scope
get_root_scope_from_absolute_pathstr(const char *pathstr
)
242 enum bt_ctf_scope scope
;
243 enum bt_ctf_scope ret
= BT_CTF_SCOPE_UNKNOWN
;
244 const size_t prefixes_count
= sizeof(absolute_path_prefixes
) /
245 sizeof(*absolute_path_prefixes
);
247 for (scope
= BT_CTF_SCOPE_ENV
; scope
< BT_CTF_SCOPE_ENV
+
248 prefixes_count
; scope
++) {
250 * Chech if path string starts with a known absolute
253 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
255 if (strncmp(pathstr
, absolute_path_prefixes
[scope
],
256 strlen(absolute_path_prefixes
[scope
]))) {
257 /* Prefix does not match: try the next one */
258 BT_LOGT("Prefix does not match: trying the next one: "
259 "path=\"%s\", path-prefix=\"%s\", scope=%s",
260 pathstr
, absolute_path_prefixes
[scope
],
261 bt_ctf_scope_string(scope
));
267 BT_LOGT("Found root scope from absolute path: "
268 "path=\"%s\", scope=%s", pathstr
,
269 bt_ctf_scope_string(scope
));
278 * Destroys a path token.
281 void ptokens_destroy_func(gpointer ptoken
,
282 gpointer data
__attribute__((unused
)))
284 g_string_free(ptoken
, TRUE
);
288 * Destroys a path token list.
291 void ptokens_destroy(GList
*ptokens
)
297 g_list_foreach(ptokens
, ptokens_destroy_func
, NULL
);
298 g_list_free(ptokens
);
302 * Returns the string contained in a path token.
305 const char *ptoken_get_string(GList
*ptoken
)
307 GString
*tokenstr
= (GString
*) ptoken
->data
;
309 return tokenstr
->str
;
313 * Converts a path string to a path token list, that is, splits the
314 * individual words of a path string into a list of individual
317 * Return value is owned by the caller on success.
320 GList
*pathstr_to_ptokens(const char *pathstr
)
322 const char *at
= pathstr
;
323 const char *last
= at
;
324 GList
*ptokens
= NULL
;
327 if (*at
== '.' || *at
== '\0') {
331 /* Error: empty token */
332 BT_LOGW("Empty path token: path=\"%s\", pos=%u",
333 pathstr
, (int) (at
- pathstr
));
337 tokenstr
= g_string_new(NULL
);
338 g_string_append_len(tokenstr
, last
, at
- last
);
339 ptokens
= g_list_append(ptokens
, tokenstr
);
353 ptokens_destroy(ptokens
);
358 * Converts a path token list to a field path object. The path token
359 * list is relative from `type`. The index of the source looking for
360 * its target within `type` is indicated by `src_index`. This can be
361 * `INT_MAX` if the source is contained in `type`.
363 * `ptokens` is owned by the caller. `field_path` is an output parameter
364 * owned by the caller that must be filled here. `type` is owned by the
368 int ptokens_to_field_path(GList
*ptokens
, struct bt_ctf_field_path
*field_path
,
369 struct bt_ctf_field_type_common
*type
, int src_index
)
372 GList
*cur_ptoken
= ptokens
;
373 bt_ctf_bool first_level_done
= BT_CTF_FALSE
;
375 /* Get our own reference */
376 bt_ctf_object_get_ref(type
);
381 struct bt_ctf_field_type_common
*child_type
;
382 const char *field_name
= ptoken_get_string(cur_ptoken
);
383 enum bt_ctf_field_type_id type_id
=
384 bt_ctf_field_type_common_get_type_id(type
);
386 BT_LOGT("Current path token: token=\"%s\"", field_name
);
388 /* Find to which index corresponds the current path token */
389 if (type_id
== BT_CTF_FIELD_TYPE_ID_ARRAY
||
390 type_id
== BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
393 child_index
= bt_ctf_field_type_common_get_field_index(type
,
395 if (child_index
< 0) {
397 * Error: field name does not exist or
398 * wrong current type.
400 BT_LOGW("Cannot get index of field type: "
401 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
402 field_name
, src_index
, child_index
, first_level_done
);
405 } else if (child_index
> src_index
&&
407 BT_LOGW("Child field type is located after source field type: "
408 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
409 field_name
, src_index
, child_index
, first_level_done
);
414 /* Next path token */
415 cur_ptoken
= g_list_next(cur_ptoken
);
416 first_level_done
= BT_CTF_TRUE
;
419 /* Create new field path entry */
420 g_array_append_val(field_path
->indexes
, child_index
);
422 /* Get child field type */
423 child_type
= bt_ctf_field_type_common_borrow_field_at_index(type
,
426 BT_LOGW("Cannot get child field type: "
427 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
428 field_name
, src_index
, child_index
, first_level_done
);
433 /* Move child type to current type */
434 bt_ctf_object_get_ref(child_type
);
435 BT_CTF_OBJECT_MOVE_REF(type
, child_type
);
439 bt_ctf_object_put_ref(type
);
444 * Converts a known absolute path token list to a field path object
445 * within the resolving context `ctx`.
447 * `ptokens` is owned by the caller. `field_path` is an output parameter
448 * owned by the caller that must be filled here.
451 int absolute_ptokens_to_field_path(GList
*ptokens
,
452 struct bt_ctf_field_path
*field_path
,
453 struct resolve_context
*ctx
)
457 struct bt_ctf_field_type_common
*type
;
459 /* Skip absolute path tokens */
460 cur_ptoken
= g_list_nth(ptokens
,
461 absolute_path_prefix_ptoken_counts
[field_path
->root
]);
463 /* Start with root type */
464 type
= get_type_from_ctx(ctx
, field_path
->root
);
466 /* Error: root type is not available */
467 BT_LOGW("Root field type is not available: "
469 bt_ctf_scope_string(field_path
->root
));
475 ret
= ptokens_to_field_path(cur_ptoken
, field_path
, type
, INT_MAX
);
482 * Converts a known relative path token list to a field path object
483 * within the resolving context `ctx`.
485 * `ptokens` is owned by the caller. `field_path` is an output parameter
486 * owned by the caller that must be filled here.
489 int relative_ptokens_to_field_path(GList
*ptokens
,
490 struct bt_ctf_field_path
*field_path
,
491 struct resolve_context
*ctx
)
494 int parent_pos_in_stack
;
495 struct bt_ctf_field_path
*tail_field_path
= bt_ctf_field_path_create();
497 if (!tail_field_path
) {
498 BT_LOGE_STR("Cannot create empty field path.");
503 parent_pos_in_stack
= type_stack_size(ctx
->type_stack
) - 1;
505 while (parent_pos_in_stack
>= 0) {
506 struct bt_ctf_field_type_common
*parent_type
=
507 type_stack_at(ctx
->type_stack
,
508 parent_pos_in_stack
)->type
;
509 int cur_index
= type_stack_at(ctx
->type_stack
,
510 parent_pos_in_stack
)->index
;
512 BT_LOGT("Locating target field type from current parent field type: "
513 "parent-pos=%d, parent-ft-addr=%p, cur-index=%d",
514 parent_pos_in_stack
, parent_type
, cur_index
);
516 /* Locate target from current parent type */
517 ret
= ptokens_to_field_path(ptokens
, tail_field_path
,
518 parent_type
, cur_index
);
520 /* Not found... yet */
521 BT_LOGT_STR("Not found at this point.");
522 bt_ctf_field_path_clear(tail_field_path
);
524 /* Found: stitch tail field path to head field path */
526 int tail_field_path_len
=
527 tail_field_path
->indexes
->len
;
529 while (BT_CTF_TRUE
) {
530 struct bt_ctf_field_type_common
*cur_type
=
531 type_stack_at(ctx
->type_stack
, i
)->type
;
532 int index
= type_stack_at(
533 ctx
->type_stack
, i
)->index
;
535 if (cur_type
== parent_type
) {
539 g_array_append_val(field_path
->indexes
,
544 for (i
= 0; i
< tail_field_path_len
; i
++) {
545 int index
= bt_g_array_index(
546 tail_field_path
->indexes
,
549 g_array_append_val(field_path
->indexes
,
555 parent_pos_in_stack
--;
558 if (parent_pos_in_stack
< 0) {
559 /* Not found: look in previous scopes */
562 while (field_path
->root
>= BT_CTF_SCOPE_TRACE_PACKET_HEADER
) {
563 struct bt_ctf_field_type_common
*root_type
;
564 bt_ctf_field_path_clear(field_path
);
566 BT_LOGT("Looking into potential root scope: scope=%s",
567 bt_ctf_scope_string(field_path
->root
));
568 root_type
= get_type_from_ctx(ctx
, field_path
->root
);
574 /* Locate target in previous scope */
575 ret
= ptokens_to_field_path(ptokens
, field_path
,
579 BT_LOGT_STR("Not found in this scope.");
585 BT_LOGT_STR("Found in this scope.");
591 BT_CTF_OBJECT_PUT_REF_AND_RESET(tail_field_path
);
596 * Converts a path string to a field path object within the resolving
599 * Return value is owned by the caller on success.
602 struct bt_ctf_field_path
*pathstr_to_field_path(const char *pathstr
,
603 struct resolve_context
*ctx
)
606 enum bt_ctf_scope root_scope
;
607 GList
*ptokens
= NULL
;
608 struct bt_ctf_field_path
*field_path
= NULL
;
610 /* Create field path */
611 field_path
= bt_ctf_field_path_create();
613 BT_LOGE_STR("Cannot create empty field path.");
618 /* Convert path string to path tokens */
619 ptokens
= pathstr_to_ptokens(pathstr
);
621 BT_LOGW("Cannot convert path string to path tokens: "
622 "path=\"%s\"", pathstr
);
627 /* Absolute or relative path? */
628 root_scope
= get_root_scope_from_absolute_pathstr(pathstr
);
630 if (root_scope
== BT_CTF_SCOPE_UNKNOWN
) {
631 /* Relative path: start with current root scope */
632 field_path
->root
= ctx
->root_scope
;
633 BT_LOGT("Detected relative path: starting with current root scope: "
634 "scope=%s", bt_ctf_scope_string(field_path
->root
));
635 ret
= relative_ptokens_to_field_path(ptokens
, field_path
, ctx
);
637 BT_LOGW("Cannot get relative field path of path string: "
638 "path=\"%s\", start-scope=%s, end-scope=%s",
639 pathstr
, bt_ctf_scope_string(ctx
->root_scope
),
640 bt_ctf_scope_string(field_path
->root
));
643 } else if (root_scope
== BT_CTF_SCOPE_ENV
) {
644 BT_LOGW("Sequence field types referring the trace environment are not supported as of this version: "
645 "path=\"%s\"", pathstr
);
649 /* Absolute path: use found root scope */
650 field_path
->root
= root_scope
;
651 BT_LOGT("Detected absolute path: using root scope: "
652 "scope=%s", bt_ctf_scope_string(field_path
->root
));
653 ret
= absolute_ptokens_to_field_path(ptokens
, field_path
, ctx
);
655 BT_LOGW("Cannot get absolute field path of path string: "
656 "path=\"%s\", root-scope=%s",
657 pathstr
, bt_ctf_scope_string(root_scope
));
663 GString
*field_path_pretty
=
664 bt_ctf_field_path_string(field_path
);
665 const char *field_path_pretty_str
=
666 field_path_pretty
? field_path_pretty
->str
: "(null)";
668 BT_LOGT("Found field path: path=\"%s\", field-path=\"%s\"",
669 pathstr
, field_path_pretty_str
);
671 if (field_path_pretty
) {
672 g_string_free(field_path_pretty
, TRUE
);
678 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_path
);
681 ptokens_destroy(ptokens
);
686 * Retrieves a field type by following the field path `field_path` in
687 * the resolving context `ctx`.
689 * Return value is owned by the caller on success.
692 struct bt_ctf_field_type_common
*field_path_to_field_type(
693 struct bt_ctf_field_path
*field_path
,
694 struct resolve_context
*ctx
)
697 struct bt_ctf_field_type_common
*type
;
699 /* Start with root type */
700 type
= get_type_from_ctx(ctx
, field_path
->root
);
701 bt_ctf_object_get_ref(type
);
703 /* Error: root type is not available */
704 BT_LOGW("Root field type is not available: root-scope=%s",
705 bt_ctf_scope_string(field_path
->root
));
710 for (i
= 0; i
< field_path
->indexes
->len
; i
++) {
711 struct bt_ctf_field_type_common
*child_type
;
713 bt_g_array_index(field_path
->indexes
, int, i
);
715 /* Get child field type */
716 child_type
= bt_ctf_field_type_common_borrow_field_at_index(type
,
719 BT_LOGW("Cannot get field type: "
720 "parent-ft-addr=%p, index=%d", type
, i
);
724 /* Move child type to current type */
725 bt_ctf_object_get_ref(child_type
);
726 BT_CTF_OBJECT_MOVE_REF(type
, child_type
);
732 BT_CTF_OBJECT_PUT_REF_AND_RESET(type
);
737 * Returns the equivalent field path object of the context type stack.
739 * Return value is owned by the caller on success.
742 struct bt_ctf_field_path
*get_ctx_stack_field_path(struct resolve_context
*ctx
)
745 struct bt_ctf_field_path
*field_path
;
747 /* Create field path */
748 field_path
= bt_ctf_field_path_create();
750 BT_LOGE_STR("Cannot create empty field path.");
754 field_path
->root
= ctx
->root_scope
;
756 for (i
= 0; i
< type_stack_size(ctx
->type_stack
); i
++) {
757 struct type_stack_frame
*frame
;
759 frame
= type_stack_at(ctx
->type_stack
, i
);
760 g_array_append_val(field_path
->indexes
, frame
->index
);
766 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_path
);
771 * Returns the lowest common ancestor of two field path objects
772 * having the same root scope.
774 * `field_path1` and `field_path2` are owned by the caller.
777 int get_field_paths_lca_index(struct bt_ctf_field_path
*field_path1
,
778 struct bt_ctf_field_path
*field_path2
)
781 int field_path1_len
, field_path2_len
;
783 if (BT_LOG_ON_TRACE
) {
784 GString
*field_path1_pretty
=
785 bt_ctf_field_path_string(field_path1
);
786 GString
*field_path2_pretty
=
787 bt_ctf_field_path_string(field_path2
);
788 const char *field_path1_pretty_str
=
789 field_path1_pretty
? field_path1_pretty
->str
: "(null)";
790 const char *field_path2_pretty_str
=
791 field_path2_pretty
? field_path2_pretty
->str
: "(null)";
793 BT_LOGT("Finding lowest common ancestor (LCA) between two field paths: "
794 "field-path-1=\"%s\", field-path-2=\"%s\"",
795 field_path1_pretty_str
, field_path2_pretty_str
);
797 if (field_path1_pretty
) {
798 g_string_free(field_path1_pretty
, TRUE
);
801 if (field_path2_pretty
) {
802 g_string_free(field_path2_pretty
, TRUE
);
807 * Start from both roots and find the first mismatch.
809 BT_ASSERT_DBG(field_path1
->root
== field_path2
->root
);
810 field_path1_len
= field_path1
->indexes
->len
;
811 field_path2_len
= field_path2
->indexes
->len
;
813 while (BT_CTF_TRUE
) {
814 int target_index
, ctx_index
;
816 if (lca_index
== field_path2_len
||
817 lca_index
== field_path1_len
) {
819 * This means that both field paths never split.
820 * This is invalid because the target cannot be
821 * an ancestor of the source.
823 BT_LOGW("Source field type is an ancestor of target field type or vice versa: "
824 "lca-index=%d, field-path-1-len=%d, "
825 "field-path-2-len=%d",
826 lca_index
, field_path1_len
, field_path2_len
);
831 target_index
= bt_g_array_index(field_path1
->indexes
, int,
833 ctx_index
= bt_g_array_index(field_path2
->indexes
, int,
836 if (target_index
!= ctx_index
) {
837 /* LCA index is the previous */
844 BT_LOGT("Found LCA: lca-index=%d", lca_index
);
849 * Validates a target field path.
851 * `target_field_path` and `target_type` are owned by the caller.
854 int validate_target_field_path(struct bt_ctf_field_path
*target_field_path
,
855 struct bt_ctf_field_type_common
*target_type
,
856 struct resolve_context
*ctx
)
859 struct bt_ctf_field_path
*ctx_field_path
;
860 int target_field_path_len
= target_field_path
->indexes
->len
;
862 enum bt_ctf_field_type_id ctx_cur_field_type_id
;
863 enum bt_ctf_field_type_id target_type_id
;
865 /* Get context field path */
866 ctx_field_path
= get_ctx_stack_field_path(ctx
);
867 if (!ctx_field_path
) {
868 BT_LOGW_STR("Cannot get field path from context's stack.");
874 * Make sure the target is not a root.
876 if (target_field_path_len
== 0) {
877 BT_LOGW_STR("Target field path's length is 0 (targeting the root).");
883 * Make sure the root of the target field path is not located
884 * after the context field path's root.
886 if (target_field_path
->root
> ctx_field_path
->root
) {
887 BT_LOGW("Target field type is located after source field type: "
888 "target-root=%s, source-root=%s",
889 bt_ctf_scope_string(target_field_path
->root
),
890 bt_ctf_scope_string(ctx_field_path
->root
));
895 if (target_field_path
->root
== ctx_field_path
->root
) {
896 int target_index
, ctx_index
;
899 * Find the index of the lowest common ancestor of both field
902 lca_index
= get_field_paths_lca_index(target_field_path
,
905 BT_LOGW_STR("Cannot get least common ancestor.");
911 * Make sure the target field path is located before the
912 * context field path.
914 target_index
= bt_g_array_index(target_field_path
->indexes
,
916 ctx_index
= bt_g_array_index(ctx_field_path
->indexes
,
919 if (target_index
>= ctx_index
) {
920 BT_LOGW("Target field type's index is greater than or equal to source field type's index in LCA: "
921 "lca-index=%d, target-index=%d, source-index=%d",
922 lca_index
, target_index
, ctx_index
);
929 * Make sure the target type has the right type and properties.
931 ctx_cur_field_type_id
= bt_ctf_field_type_common_get_type_id(
932 ctx
->cur_field_type
);
933 target_type_id
= bt_ctf_field_type_common_get_type_id(target_type
);
935 switch (ctx_cur_field_type_id
) {
936 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
937 if (target_type_id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) {
938 BT_LOGW("Variant field type's tag field type is not an enumeration field type: "
939 "tag-ft-addr=%p, tag-ft-id=%s",
941 bt_ctf_field_type_id_string(target_type_id
));
946 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
947 if (target_type_id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
||
948 bt_ctf_field_type_common_integer_is_signed(target_type
)) {
949 BT_LOGW("Sequence field type's length field type is not an unsigned integer field type: "
950 "length-ft-addr=%p, length-ft-id=%s",
952 bt_ctf_field_type_id_string(target_type_id
));
962 BT_CTF_OBJECT_PUT_REF_AND_RESET(ctx_field_path
);
967 * Resolves a variant or sequence field type `type`.
969 * `type` is owned by the caller.
972 int resolve_sequence_or_variant_type(struct bt_ctf_field_type_common
*type
,
973 struct resolve_context
*ctx
)
977 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_common_get_type_id(type
);
978 struct bt_ctf_field_path
*target_field_path
= NULL
;
979 struct bt_ctf_field_type_common
*target_type
= NULL
;
980 GString
*target_field_path_pretty
= NULL
;
981 const char *target_field_path_pretty_str
;
984 /* Get path string */
986 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
988 bt_ctf_field_type_common_sequence_get_length_field_name(type
);
990 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
992 bt_ctf_field_type_common_variant_get_tag_name(type
);
999 BT_LOGW_STR("Cannot get path string.");
1004 /* Get target field path out of path string */
1005 target_field_path
= pathstr_to_field_path(pathstr
, ctx
);
1006 if (!target_field_path
) {
1007 BT_LOGW("Cannot get target field path for path string: "
1008 "path=\"%s\"", pathstr
);
1013 target_field_path_pretty
= bt_ctf_field_path_string(target_field_path
);
1014 target_field_path_pretty_str
=
1015 target_field_path_pretty
? target_field_path_pretty
->str
: NULL
;
1017 /* Get target field type */
1018 target_type
= field_path_to_field_type(target_field_path
, ctx
);
1020 BT_LOGW("Cannot get target field type for path string: "
1021 "path=\"%s\", target-field-path=\"%s\"",
1022 pathstr
, target_field_path_pretty_str
);
1027 ret
= validate_target_field_path(target_field_path
, target_type
, ctx
);
1029 BT_LOGW("Invalid target field path for path string: "
1030 "path=\"%s\", target-field-path=\"%s\"",
1031 pathstr
, target_field_path_pretty_str
);
1035 /* Set target field path and target field type */
1037 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1038 ret
= bt_ctf_field_type_common_sequence_set_length_field_path(
1039 type
, target_field_path
);
1041 BT_LOGW("Cannot set sequence field type's length field path: "
1042 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1044 target_field_path_pretty_str
);
1048 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1049 ret
= bt_ctf_field_type_common_variant_set_tag_field_path(
1050 type
, target_field_path
);
1052 BT_LOGW("Cannot set varaint field type's tag field path: "
1053 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1055 target_field_path_pretty_str
);
1059 ret
= bt_ctf_field_type_common_variant_set_tag_field_type(
1062 BT_LOGW("Cannot set varaint field type's tag field type: "
1063 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1065 target_field_path_pretty_str
);
1074 if (target_field_path_pretty
) {
1075 g_string_free(target_field_path_pretty
, TRUE
);
1078 BT_CTF_OBJECT_PUT_REF_AND_RESET(target_field_path
);
1079 BT_CTF_OBJECT_PUT_REF_AND_RESET(target_type
);
1084 * Resolves a field type `type`.
1086 * `type` is owned by the caller.
1089 int resolve_type(struct bt_ctf_field_type_common
*type
, struct resolve_context
*ctx
)
1092 enum bt_ctf_field_type_id type_id
;
1095 /* Type is not available; still valid */
1099 type_id
= bt_ctf_field_type_common_get_type_id(type
);
1100 ctx
->cur_field_type
= type
;
1102 /* Resolve sequence/variant field type */
1104 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1105 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1106 ret
= resolve_sequence_or_variant_type(type
, ctx
);
1108 BT_LOGW("Cannot resolve sequence field type's length or variant field type's tag: "
1109 "ret=%d, ft-addr=%p", ret
, type
);
1117 /* Recurse into compound types */
1119 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1120 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1121 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1122 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1124 int64_t field_count
, f_index
;
1126 ret
= type_stack_push(ctx
->type_stack
, type
);
1128 BT_LOGW("Cannot push field type on context's stack: "
1129 "ft-addr=%p", type
);
1133 field_count
= bt_ctf_field_type_common_get_field_count(type
);
1134 if (field_count
< 0) {
1135 BT_LOGW("Cannot get field type's field count: "
1136 "ret=%" PRId64
", ft-addr=%p",
1142 for (f_index
= 0; f_index
< field_count
; f_index
++) {
1143 struct bt_ctf_field_type_common
*child_type
=
1144 bt_ctf_field_type_common_borrow_field_at_index(type
,
1148 BT_LOGW("Cannot get field type's child field: "
1149 "ft-addr=%p, index=%" PRId64
", "
1150 "count=%" PRId64
, type
, f_index
,
1156 if (type_id
== BT_CTF_FIELD_TYPE_ID_ARRAY
||
1157 type_id
== BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
1158 type_stack_peek(ctx
->type_stack
)->index
= -1;
1160 type_stack_peek(ctx
->type_stack
)->index
=
1164 BT_LOGT("Resolving field type's child field type: "
1165 "parent-ft-addr=%p, child-ft-addr=%p, "
1166 "index=%" PRId64
", count=%" PRId64
,
1167 type
, child_type
, f_index
, field_count
);
1168 ret
= resolve_type(child_type
, ctx
);
1174 type_stack_pop(ctx
->type_stack
);
1186 * Resolves the root field type corresponding to the scope `root_scope`.
1189 int resolve_root_type(enum bt_ctf_scope root_scope
, struct resolve_context
*ctx
)
1193 BT_ASSERT_DBG(type_stack_size(ctx
->type_stack
) == 0);
1194 ctx
->root_scope
= root_scope
;
1195 ret
= resolve_type(get_type_from_ctx(ctx
, root_scope
), ctx
);
1196 ctx
->root_scope
= BT_CTF_SCOPE_UNKNOWN
;
1201 int bt_ctf_resolve_types(
1202 struct bt_ctf_private_value
*environment
,
1203 struct bt_ctf_field_type_common
*packet_header_type
,
1204 struct bt_ctf_field_type_common
*packet_context_type
,
1205 struct bt_ctf_field_type_common
*event_header_type
,
1206 struct bt_ctf_field_type_common
*stream_event_ctx_type
,
1207 struct bt_ctf_field_type_common
*event_context_type
,
1208 struct bt_ctf_field_type_common
*event_payload_type
,
1209 enum bt_ctf_resolve_flag flags
)
1212 struct resolve_context ctx
= {
1213 .environment
= environment
,
1216 packet_context_type
,
1218 stream_event_ctx_type
,
1222 .root_scope
= BT_CTF_SCOPE_UNKNOWN
,
1225 BT_LOGT("Resolving field types: "
1226 "packet-header-ft-addr=%p, "
1227 "packet-context-ft-addr=%p, "
1228 "event-header-ft-addr=%p, "
1229 "stream-event-context-ft-addr=%p, "
1230 "event-context-ft-addr=%p, "
1231 "event-payload-ft-addr=%p",
1232 packet_header_type
, packet_context_type
, event_header_type
,
1233 stream_event_ctx_type
, event_context_type
, event_payload_type
);
1235 /* Initialize type stack */
1236 ctx
.type_stack
= type_stack_create();
1237 if (!ctx
.type_stack
) {
1238 BT_LOGE_STR("Cannot create field type stack.");
1243 /* Resolve packet header type */
1244 if (flags
& BT_CTF_RESOLVE_FLAG_PACKET_HEADER
) {
1245 ret
= resolve_root_type(BT_CTF_SCOPE_TRACE_PACKET_HEADER
, &ctx
);
1247 BT_LOGW("Cannot resolve trace packet header field type: "
1253 /* Resolve packet context type */
1254 if (flags
& BT_CTF_RESOLVE_FLAG_PACKET_CONTEXT
) {
1255 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
, &ctx
);
1257 BT_LOGW("Cannot resolve stream packet context field type: "
1263 /* Resolve event header type */
1264 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_HEADER
) {
1265 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_HEADER
, &ctx
);
1267 BT_LOGW("Cannot resolve stream event header field type: "
1273 /* Resolve stream event context type */
1274 if (flags
& BT_CTF_RESOLVE_FLAG_STREAM_EVENT_CTX
) {
1275 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
, &ctx
);
1277 BT_LOGW("Cannot resolve stream event context field type: "
1283 /* Resolve event context type */
1284 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_CONTEXT
) {
1285 ret
= resolve_root_type(BT_CTF_SCOPE_EVENT_CONTEXT
, &ctx
);
1287 BT_LOGW("Cannot resolve event context field type: "
1293 /* Resolve event payload type */
1294 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_PAYLOAD
) {
1295 ret
= resolve_root_type(BT_CTF_SCOPE_EVENT_FIELDS
, &ctx
);
1297 BT_LOGW("Cannot resolve event payload field type: "
1303 BT_LOGT_STR("Resolved field types.");
1306 type_stack_destroy(ctx
.type_stack
);