4 * Babeltrace - CTF writer: Type resolving internal
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
9 * Authors: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Philippe Proulx <pproulx@efficios.com>
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #define BT_LOG_TAG "CTF-WRITER-RESOLVE"
39 #include <babeltrace2/ctf-writer/field-types.h>
40 #include <babeltrace2/ctf-writer/object.h>
41 #include <babeltrace2/ctf-writer/stream-class.h>
42 #include <babeltrace2/types.h>
44 #include "common/macros.h"
45 #include "common/assert.h"
47 #include "field-path.h"
52 typedef GPtrArray type_stack
;
57 * `type` contains a compound field type (structure, variant, array,
58 * or sequence) and `index` indicates the index of the field type in
59 * the upper frame (-1 for array and sequence field types).
61 * `type` is owned by the stack frame.
63 struct type_stack_frame
{
64 struct bt_ctf_field_type_common
*type
;
69 * The current context of the resolving engine.
71 * `scopes` contain the 6 CTF scope field types (see CTF, sect. 7.3.2)
72 * in the following order:
77 * * Stream event context
81 struct resolve_context
{
82 struct bt_ctf_private_value
*environment
;
83 struct bt_ctf_field_type_common
*scopes
[6];
85 /* Root scope being visited */
86 enum bt_ctf_scope root_scope
;
87 type_stack
*type_stack
;
88 struct bt_ctf_field_type_common
*cur_field_type
;
91 /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
92 static const char * const absolute_path_prefixes
[] = {
93 [BT_CTF_SCOPE_ENV
] = "env.",
94 [BT_CTF_SCOPE_TRACE_PACKET_HEADER
] = "trace.packet.header.",
95 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
] = "stream.packet.context.",
96 [BT_CTF_SCOPE_STREAM_EVENT_HEADER
] = "stream.event.header.",
97 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
] = "stream.event.context.",
98 [BT_CTF_SCOPE_EVENT_CONTEXT
] = "event.context.",
99 [BT_CTF_SCOPE_EVENT_FIELDS
] = "event.fields.",
102 /* Number of path tokens used for the absolute prefixes */
103 static const int absolute_path_prefix_ptoken_counts
[] = {
104 [BT_CTF_SCOPE_ENV
] = 1,
105 [BT_CTF_SCOPE_TRACE_PACKET_HEADER
] = 3,
106 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
] = 3,
107 [BT_CTF_SCOPE_STREAM_EVENT_HEADER
] = 3,
108 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
] = 3,
109 [BT_CTF_SCOPE_EVENT_CONTEXT
] = 2,
110 [BT_CTF_SCOPE_EVENT_FIELDS
] = 2,
114 * Destroys a type stack frame.
117 void type_stack_destroy_notify(gpointer data
)
119 struct type_stack_frame
*frame
= data
;
121 BT_CTF_OBJECT_PUT_REF_AND_RESET(frame
->type
);
126 * Creates a type stack.
128 * Return value is owned by the caller.
131 type_stack
*type_stack_create(void)
133 return g_ptr_array_new_with_free_func(type_stack_destroy_notify
);
137 * Destroys a type stack.
140 void type_stack_destroy(type_stack
*stack
)
142 g_ptr_array_free(stack
, TRUE
);
146 * Pushes a field type onto a type stack.
148 * `type` is owned by the caller (stack frame gets a new reference).
151 int type_stack_push(type_stack
*stack
, struct bt_ctf_field_type_common
*type
)
154 struct type_stack_frame
*frame
= NULL
;
156 if (!stack
|| !type
) {
157 BT_LOGW("Invalid parameter: stack or type is NULL.");
162 frame
= g_new0(struct type_stack_frame
, 1);
164 BT_LOGE_STR("Failed to allocate one field type stack frame.");
169 BT_LOGV("Pushing field type on context's stack: "
170 "ft-addr=%p, stack-size-before=%u", type
, stack
->len
);
171 frame
->type
= bt_ctf_object_get_ref(type
);
172 g_ptr_array_add(stack
, frame
);
179 * Checks whether or not `stack` is empty.
182 bt_bool
type_stack_empty(type_stack
*stack
)
184 return stack
->len
== 0;
188 * Returns the number of frames in `stack`.
191 size_t type_stack_size(type_stack
*stack
)
197 * Returns the top frame of `stack`.
199 * Return value is owned by `stack`.
202 struct type_stack_frame
*type_stack_peek(type_stack
*stack
)
204 struct type_stack_frame
*entry
= NULL
;
206 if (!stack
|| type_stack_empty(stack
)) {
210 entry
= g_ptr_array_index(stack
, stack
->len
- 1);
216 * Returns the frame at index `index` in `stack`.
218 * Return value is owned by `stack`.
221 struct type_stack_frame
*type_stack_at(type_stack
*stack
,
224 struct type_stack_frame
*entry
= NULL
;
226 if (!stack
|| index
>= stack
->len
) {
230 entry
= g_ptr_array_index(stack
, index
);
237 * Removes the top frame of `stack`.
240 void type_stack_pop(type_stack
*stack
)
242 if (!type_stack_empty(stack
)) {
244 * This will call the frame's destructor and free it, as
245 * well as put its contained field type.
247 BT_LOGV("Popping context's stack: stack-size-before=%u",
249 g_ptr_array_set_size(stack
, stack
->len
- 1);
254 * Returns the scope field type of `scope` in the context `ctx`.
256 * Return value is owned by `ctx` on success.
259 struct bt_ctf_field_type_common
*get_type_from_ctx(struct resolve_context
*ctx
,
260 enum bt_ctf_scope scope
)
262 BT_ASSERT(scope
>= BT_CTF_SCOPE_TRACE_PACKET_HEADER
&&
263 scope
<= BT_CTF_SCOPE_EVENT_FIELDS
);
265 return ctx
->scopes
[scope
- BT_CTF_SCOPE_TRACE_PACKET_HEADER
];
269 * Returns the CTF scope from a path string. May return
270 * CTF_NODE_UNKNOWN if the path is found to be relative.
273 enum bt_ctf_scope
get_root_scope_from_absolute_pathstr(const char *pathstr
)
275 enum bt_ctf_scope scope
;
276 enum bt_ctf_scope ret
= BT_CTF_SCOPE_UNKNOWN
;
277 const size_t prefixes_count
= sizeof(absolute_path_prefixes
) /
278 sizeof(*absolute_path_prefixes
);
280 for (scope
= BT_CTF_SCOPE_ENV
; scope
< BT_CTF_SCOPE_ENV
+
281 prefixes_count
; scope
++) {
283 * Chech if path string starts with a known absolute
286 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
288 if (strncmp(pathstr
, absolute_path_prefixes
[scope
],
289 strlen(absolute_path_prefixes
[scope
]))) {
290 /* Prefix does not match: try the next one */
291 BT_LOGV("Prefix does not match: trying the next one: "
292 "path=\"%s\", path-prefix=\"%s\", scope=%s",
293 pathstr
, absolute_path_prefixes
[scope
],
294 bt_ctf_scope_string(scope
));
300 BT_LOGV("Found root scope from absolute path: "
301 "path=\"%s\", scope=%s", pathstr
,
302 bt_ctf_scope_string(scope
));
311 * Destroys a path token.
314 void ptokens_destroy_func(gpointer ptoken
, gpointer data
)
316 g_string_free(ptoken
, TRUE
);
320 * Destroys a path token list.
323 void ptokens_destroy(GList
*ptokens
)
329 g_list_foreach(ptokens
, ptokens_destroy_func
, NULL
);
330 g_list_free(ptokens
);
334 * Returns the string contained in a path token.
337 const char *ptoken_get_string(GList
*ptoken
)
339 GString
*tokenstr
= (GString
*) ptoken
->data
;
341 return tokenstr
->str
;
345 * Converts a path string to a path token list, that is, splits the
346 * individual words of a path string into a list of individual
349 * Return value is owned by the caller on success.
352 GList
*pathstr_to_ptokens(const char *pathstr
)
354 const char *at
= pathstr
;
355 const char *last
= at
;
356 GList
*ptokens
= NULL
;
359 if (*at
== '.' || *at
== '\0') {
363 /* Error: empty token */
364 BT_LOGW("Empty path token: path=\"%s\", pos=%u",
365 pathstr
, (int) (at
- pathstr
));
369 tokenstr
= g_string_new(NULL
);
370 g_string_append_len(tokenstr
, last
, at
- last
);
371 ptokens
= g_list_append(ptokens
, tokenstr
);
385 ptokens_destroy(ptokens
);
390 * Converts a path token list to a field path object. The path token
391 * list is relative from `type`. The index of the source looking for
392 * its target within `type` is indicated by `src_index`. This can be
393 * `INT_MAX` if the source is contained in `type`.
395 * `ptokens` is owned by the caller. `field_path` is an output parameter
396 * owned by the caller that must be filled here. `type` is owned by the
400 int ptokens_to_field_path(GList
*ptokens
, struct bt_ctf_field_path
*field_path
,
401 struct bt_ctf_field_type_common
*type
, int src_index
)
404 GList
*cur_ptoken
= ptokens
;
405 bt_bool first_level_done
= BT_FALSE
;
407 /* Get our own reference */
408 bt_ctf_object_get_ref(type
);
413 struct bt_ctf_field_type_common
*child_type
;
414 const char *field_name
= ptoken_get_string(cur_ptoken
);
415 enum bt_ctf_field_type_id type_id
=
416 bt_ctf_field_type_common_get_type_id(type
);
418 BT_LOGV("Current path token: token=\"%s\"", field_name
);
420 /* Find to which index corresponds the current path token */
421 if (type_id
== BT_CTF_FIELD_TYPE_ID_ARRAY
||
422 type_id
== BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
425 child_index
= bt_ctf_field_type_common_get_field_index(type
,
427 if (child_index
< 0) {
429 * Error: field name does not exist or
430 * wrong current type.
432 BT_LOGW("Cannot get index of field type: "
433 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
434 field_name
, src_index
, child_index
, first_level_done
);
437 } else if (child_index
> src_index
&&
439 BT_LOGW("Child field type is located after source field type: "
440 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
441 field_name
, src_index
, child_index
, first_level_done
);
446 /* Next path token */
447 cur_ptoken
= g_list_next(cur_ptoken
);
448 first_level_done
= BT_TRUE
;
451 /* Create new field path entry */
452 g_array_append_val(field_path
->indexes
, child_index
);
454 /* Get child field type */
455 child_type
= bt_ctf_field_type_common_borrow_field_at_index(type
,
458 BT_LOGW("Cannot get child field type: "
459 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
460 field_name
, src_index
, child_index
, first_level_done
);
465 /* Move child type to current type */
466 bt_ctf_object_get_ref(child_type
);
467 BT_CTF_OBJECT_MOVE_REF(type
, child_type
);
471 bt_ctf_object_put_ref(type
);
476 * Converts a known absolute path token list to a field path object
477 * within the resolving context `ctx`.
479 * `ptokens` is owned by the caller. `field_path` is an output parameter
480 * owned by the caller that must be filled here.
483 int absolute_ptokens_to_field_path(GList
*ptokens
,
484 struct bt_ctf_field_path
*field_path
,
485 struct resolve_context
*ctx
)
489 struct bt_ctf_field_type_common
*type
;
491 /* Skip absolute path tokens */
492 cur_ptoken
= g_list_nth(ptokens
,
493 absolute_path_prefix_ptoken_counts
[field_path
->root
]);
495 /* Start with root type */
496 type
= get_type_from_ctx(ctx
, field_path
->root
);
498 /* Error: root type is not available */
499 BT_LOGW("Root field type is not available: "
501 bt_ctf_scope_string(field_path
->root
));
507 ret
= ptokens_to_field_path(cur_ptoken
, field_path
, type
, INT_MAX
);
514 * Converts a known relative path token list to a field path object
515 * within the resolving context `ctx`.
517 * `ptokens` is owned by the caller. `field_path` is an output parameter
518 * owned by the caller that must be filled here.
521 int relative_ptokens_to_field_path(GList
*ptokens
,
522 struct bt_ctf_field_path
*field_path
,
523 struct resolve_context
*ctx
)
526 int parent_pos_in_stack
;
527 struct bt_ctf_field_path
*tail_field_path
= bt_ctf_field_path_create();
529 if (!tail_field_path
) {
530 BT_LOGE_STR("Cannot create empty field path.");
535 parent_pos_in_stack
= type_stack_size(ctx
->type_stack
) - 1;
537 while (parent_pos_in_stack
>= 0) {
538 struct bt_ctf_field_type_common
*parent_type
=
539 type_stack_at(ctx
->type_stack
,
540 parent_pos_in_stack
)->type
;
541 int cur_index
= type_stack_at(ctx
->type_stack
,
542 parent_pos_in_stack
)->index
;
544 BT_LOGV("Locating target field type from current parent field type: "
545 "parent-pos=%d, parent-ft-addr=%p, cur-index=%d",
546 parent_pos_in_stack
, parent_type
, cur_index
);
548 /* Locate target from current parent type */
549 ret
= ptokens_to_field_path(ptokens
, tail_field_path
,
550 parent_type
, cur_index
);
552 /* Not found... yet */
553 BT_LOGV_STR("Not found at this point.");
554 bt_ctf_field_path_clear(tail_field_path
);
556 /* Found: stitch tail field path to head field path */
558 int tail_field_path_len
=
559 tail_field_path
->indexes
->len
;
562 struct bt_ctf_field_type_common
*cur_type
=
563 type_stack_at(ctx
->type_stack
, i
)->type
;
564 int index
= type_stack_at(
565 ctx
->type_stack
, i
)->index
;
567 if (cur_type
== parent_type
) {
571 g_array_append_val(field_path
->indexes
,
576 for (i
= 0; i
< tail_field_path_len
; i
++) {
577 int index
= g_array_index(
578 tail_field_path
->indexes
,
581 g_array_append_val(field_path
->indexes
,
587 parent_pos_in_stack
--;
590 if (parent_pos_in_stack
< 0) {
591 /* Not found: look in previous scopes */
594 while (field_path
->root
>= BT_CTF_SCOPE_TRACE_PACKET_HEADER
) {
595 struct bt_ctf_field_type_common
*root_type
;
596 bt_ctf_field_path_clear(field_path
);
598 BT_LOGV("Looking into potential root scope: scope=%s",
599 bt_ctf_scope_string(field_path
->root
));
600 root_type
= get_type_from_ctx(ctx
, field_path
->root
);
606 /* Locate target in previous scope */
607 ret
= ptokens_to_field_path(ptokens
, field_path
,
611 BT_LOGV_STR("Not found in this scope.");
617 BT_LOGV_STR("Found in this scope.");
623 BT_CTF_OBJECT_PUT_REF_AND_RESET(tail_field_path
);
628 * Converts a path string to a field path object within the resolving
631 * Return value is owned by the caller on success.
634 struct bt_ctf_field_path
*pathstr_to_field_path(const char *pathstr
,
635 struct resolve_context
*ctx
)
638 enum bt_ctf_scope root_scope
;
639 GList
*ptokens
= NULL
;
640 struct bt_ctf_field_path
*field_path
= NULL
;
642 /* Create field path */
643 field_path
= bt_ctf_field_path_create();
645 BT_LOGE_STR("Cannot create empty field path.");
650 /* Convert path string to path tokens */
651 ptokens
= pathstr_to_ptokens(pathstr
);
653 BT_LOGW("Cannot convert path string to path tokens: "
654 "path=\"%s\"", pathstr
);
659 /* Absolute or relative path? */
660 root_scope
= get_root_scope_from_absolute_pathstr(pathstr
);
662 if (root_scope
== BT_CTF_SCOPE_UNKNOWN
) {
663 /* Relative path: start with current root scope */
664 field_path
->root
= ctx
->root_scope
;
665 BT_LOGV("Detected relative path: starting with current root scope: "
666 "scope=%s", bt_ctf_scope_string(field_path
->root
));
667 ret
= relative_ptokens_to_field_path(ptokens
, field_path
, ctx
);
669 BT_LOGW("Cannot get relative field path of path string: "
670 "path=\"%s\", start-scope=%s, end-scope=%s",
671 pathstr
, bt_ctf_scope_string(ctx
->root_scope
),
672 bt_ctf_scope_string(field_path
->root
));
675 } else if (root_scope
== BT_CTF_SCOPE_ENV
) {
676 BT_LOGW("Sequence field types referring the trace environment are not supported as of this version: "
677 "path=\"%s\"", pathstr
);
681 /* Absolute path: use found root scope */
682 field_path
->root
= root_scope
;
683 BT_LOGV("Detected absolute path: using root scope: "
684 "scope=%s", bt_ctf_scope_string(field_path
->root
));
685 ret
= absolute_ptokens_to_field_path(ptokens
, field_path
, ctx
);
687 BT_LOGW("Cannot get absolute field path of path string: "
688 "path=\"%s\", root-scope=%s",
689 pathstr
, bt_ctf_scope_string(root_scope
));
695 GString
*field_path_pretty
=
696 bt_ctf_field_path_string(field_path
);
697 const char *field_path_pretty_str
=
698 field_path_pretty
? field_path_pretty
->str
: NULL
;
700 BT_LOGV("Found field path: path=\"%s\", field-path=\"%s\"",
701 pathstr
, field_path_pretty_str
);
703 if (field_path_pretty
) {
704 g_string_free(field_path_pretty
, TRUE
);
710 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_path
);
713 ptokens_destroy(ptokens
);
718 * Retrieves a field type by following the field path `field_path` in
719 * the resolving context `ctx`.
721 * Return value is owned by the caller on success.
724 struct bt_ctf_field_type_common
*field_path_to_field_type(
725 struct bt_ctf_field_path
*field_path
,
726 struct resolve_context
*ctx
)
729 struct bt_ctf_field_type_common
*type
;
731 /* Start with root type */
732 type
= get_type_from_ctx(ctx
, field_path
->root
);
733 bt_ctf_object_get_ref(type
);
735 /* Error: root type is not available */
736 BT_LOGW("Root field type is not available: root-scope=%s",
737 bt_ctf_scope_string(field_path
->root
));
742 for (i
= 0; i
< field_path
->indexes
->len
; i
++) {
743 struct bt_ctf_field_type_common
*child_type
;
745 g_array_index(field_path
->indexes
, int, i
);
747 /* Get child field type */
748 child_type
= bt_ctf_field_type_common_borrow_field_at_index(type
,
751 BT_LOGW("Cannot get field type: "
752 "parent-ft-addr=%p, index=%d", type
, i
);
756 /* Move child type to current type */
757 bt_ctf_object_get_ref(child_type
);
758 BT_CTF_OBJECT_MOVE_REF(type
, child_type
);
764 BT_CTF_OBJECT_PUT_REF_AND_RESET(type
);
769 * Returns the equivalent field path object of the context type stack.
771 * Return value is owned by the caller on success.
774 struct bt_ctf_field_path
*get_ctx_stack_field_path(struct resolve_context
*ctx
)
777 struct bt_ctf_field_path
*field_path
;
779 /* Create field path */
780 field_path
= bt_ctf_field_path_create();
782 BT_LOGE_STR("Cannot create empty field path.");
786 field_path
->root
= ctx
->root_scope
;
788 for (i
= 0; i
< type_stack_size(ctx
->type_stack
); i
++) {
789 struct type_stack_frame
*frame
;
791 frame
= type_stack_at(ctx
->type_stack
, i
);
792 g_array_append_val(field_path
->indexes
, frame
->index
);
798 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_path
);
803 * Returns the lowest common ancestor of two field path objects
804 * having the same root scope.
806 * `field_path1` and `field_path2` are owned by the caller.
809 int get_field_paths_lca_index(struct bt_ctf_field_path
*field_path1
,
810 struct bt_ctf_field_path
*field_path2
)
813 int field_path1_len
, field_path2_len
;
815 if (BT_LOG_ON_VERBOSE
) {
816 GString
*field_path1_pretty
=
817 bt_ctf_field_path_string(field_path1
);
818 GString
*field_path2_pretty
=
819 bt_ctf_field_path_string(field_path2
);
820 const char *field_path1_pretty_str
=
821 field_path1_pretty
? field_path1_pretty
->str
: NULL
;
822 const char *field_path2_pretty_str
=
823 field_path2_pretty
? field_path2_pretty
->str
: NULL
;
825 BT_LOGV("Finding lowest common ancestor (LCA) between two field paths: "
826 "field-path-1=\"%s\", field-path-2=\"%s\"",
827 field_path1_pretty_str
, field_path2_pretty_str
);
829 if (field_path1_pretty
) {
830 g_string_free(field_path1_pretty
, TRUE
);
833 if (field_path2_pretty
) {
834 g_string_free(field_path2_pretty
, TRUE
);
839 * Start from both roots and find the first mismatch.
841 BT_ASSERT(field_path1
->root
== field_path2
->root
);
842 field_path1_len
= field_path1
->indexes
->len
;
843 field_path2_len
= field_path2
->indexes
->len
;
846 int target_index
, ctx_index
;
848 if (lca_index
== field_path2_len
||
849 lca_index
== field_path1_len
) {
851 * This means that both field paths never split.
852 * This is invalid because the target cannot be
853 * an ancestor of the source.
855 BT_LOGW("Source field type is an ancestor of target field type or vice versa: "
856 "lca-index=%d, field-path-1-len=%d, "
857 "field-path-2-len=%d",
858 lca_index
, field_path1_len
, field_path2_len
);
863 target_index
= g_array_index(field_path1
->indexes
, int,
865 ctx_index
= g_array_index(field_path2
->indexes
, int,
868 if (target_index
!= ctx_index
) {
869 /* LCA index is the previous */
876 BT_LOGV("Found LCA: lca-index=%d", lca_index
);
881 * Validates a target field path.
883 * `target_field_path` and `target_type` are owned by the caller.
886 int validate_target_field_path(struct bt_ctf_field_path
*target_field_path
,
887 struct bt_ctf_field_type_common
*target_type
,
888 struct resolve_context
*ctx
)
891 struct bt_ctf_field_path
*ctx_field_path
;
892 int target_field_path_len
= target_field_path
->indexes
->len
;
894 enum bt_ctf_field_type_id ctx_cur_field_type_id
;
895 enum bt_ctf_field_type_id target_type_id
;
897 /* Get context field path */
898 ctx_field_path
= get_ctx_stack_field_path(ctx
);
899 if (!ctx_field_path
) {
900 BT_LOGW_STR("Cannot get field path from context's stack.");
906 * Make sure the target is not a root.
908 if (target_field_path_len
== 0) {
909 BT_LOGW_STR("Target field path's length is 0 (targeting the root).");
915 * Make sure the root of the target field path is not located
916 * after the context field path's root.
918 if (target_field_path
->root
> ctx_field_path
->root
) {
919 BT_LOGW("Target field type is located after source field type: "
920 "target-root=%s, source-root=%s",
921 bt_ctf_scope_string(target_field_path
->root
),
922 bt_ctf_scope_string(ctx_field_path
->root
));
927 if (target_field_path
->root
== ctx_field_path
->root
) {
928 int target_index
, ctx_index
;
931 * Find the index of the lowest common ancestor of both field
934 lca_index
= get_field_paths_lca_index(target_field_path
,
937 BT_LOGW_STR("Cannot get least common ancestor.");
943 * Make sure the target field path is located before the
944 * context field path.
946 target_index
= g_array_index(target_field_path
->indexes
,
948 ctx_index
= g_array_index(ctx_field_path
->indexes
,
951 if (target_index
>= ctx_index
) {
952 BT_LOGW("Target field type's index is greater than or equal to source field type's index in LCA: "
953 "lca-index=%d, target-index=%d, source-index=%d",
954 lca_index
, target_index
, ctx_index
);
961 * Make sure the target type has the right type and properties.
963 ctx_cur_field_type_id
= bt_ctf_field_type_common_get_type_id(
964 ctx
->cur_field_type
);
965 target_type_id
= bt_ctf_field_type_common_get_type_id(target_type
);
967 switch (ctx_cur_field_type_id
) {
968 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
969 if (target_type_id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) {
970 BT_LOGW("Variant field type's tag field type is not an enumeration field type: "
971 "tag-ft-addr=%p, tag-ft-id=%s",
973 bt_ctf_field_type_id_string(target_type_id
));
978 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
979 if (target_type_id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
||
980 bt_ctf_field_type_common_integer_is_signed(target_type
)) {
981 BT_LOGW("Sequence field type's length field type is not an unsigned integer field type: "
982 "length-ft-addr=%p, length-ft-id=%s",
984 bt_ctf_field_type_id_string(target_type_id
));
994 BT_CTF_OBJECT_PUT_REF_AND_RESET(ctx_field_path
);
999 * Resolves a variant or sequence field type `type`.
1001 * `type` is owned by the caller.
1004 int resolve_sequence_or_variant_type(struct bt_ctf_field_type_common
*type
,
1005 struct resolve_context
*ctx
)
1008 const char *pathstr
;
1009 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_common_get_type_id(type
);
1010 struct bt_ctf_field_path
*target_field_path
= NULL
;
1011 struct bt_ctf_field_type_common
*target_type
= NULL
;
1012 GString
*target_field_path_pretty
= NULL
;
1013 const char *target_field_path_pretty_str
;
1016 /* Get path string */
1018 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1020 bt_ctf_field_type_common_sequence_get_length_field_name(type
);
1022 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1024 bt_ctf_field_type_common_variant_get_tag_name(type
);
1031 BT_LOGW_STR("Cannot get path string.");
1036 /* Get target field path out of path string */
1037 target_field_path
= pathstr_to_field_path(pathstr
, ctx
);
1038 if (!target_field_path
) {
1039 BT_LOGW("Cannot get target field path for path string: "
1040 "path=\"%s\"", pathstr
);
1045 target_field_path_pretty
= bt_ctf_field_path_string(target_field_path
);
1046 target_field_path_pretty_str
=
1047 target_field_path_pretty
? target_field_path_pretty
->str
: NULL
;
1049 /* Get target field type */
1050 target_type
= field_path_to_field_type(target_field_path
, ctx
);
1052 BT_LOGW("Cannot get target field type for path string: "
1053 "path=\"%s\", target-field-path=\"%s\"",
1054 pathstr
, target_field_path_pretty_str
);
1059 ret
= validate_target_field_path(target_field_path
, target_type
, ctx
);
1061 BT_LOGW("Invalid target field path for path string: "
1062 "path=\"%s\", target-field-path=\"%s\"",
1063 pathstr
, target_field_path_pretty_str
);
1067 /* Set target field path and target field type */
1069 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1070 ret
= bt_ctf_field_type_common_sequence_set_length_field_path(
1071 type
, target_field_path
);
1073 BT_LOGW("Cannot set sequence field type's length field path: "
1074 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1076 target_field_path_pretty_str
);
1080 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1081 ret
= bt_ctf_field_type_common_variant_set_tag_field_path(
1082 type
, target_field_path
);
1084 BT_LOGW("Cannot set varaint field type's tag field path: "
1085 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1087 target_field_path_pretty_str
);
1091 ret
= bt_ctf_field_type_common_variant_set_tag_field_type(
1094 BT_LOGW("Cannot set varaint field type's tag field type: "
1095 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1097 target_field_path_pretty_str
);
1106 if (target_field_path_pretty
) {
1107 g_string_free(target_field_path_pretty
, TRUE
);
1110 BT_CTF_OBJECT_PUT_REF_AND_RESET(target_field_path
);
1111 BT_CTF_OBJECT_PUT_REF_AND_RESET(target_type
);
1116 * Resolves a field type `type`.
1118 * `type` is owned by the caller.
1121 int resolve_type(struct bt_ctf_field_type_common
*type
, struct resolve_context
*ctx
)
1124 enum bt_ctf_field_type_id type_id
;
1127 /* Type is not available; still valid */
1131 type_id
= bt_ctf_field_type_common_get_type_id(type
);
1132 ctx
->cur_field_type
= type
;
1134 /* Resolve sequence/variant field type */
1136 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1137 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1138 ret
= resolve_sequence_or_variant_type(type
, ctx
);
1140 BT_LOGW("Cannot resolve sequence field type's length or variant field type's tag: "
1141 "ret=%d, ft-addr=%p", ret
, type
);
1149 /* Recurse into compound types */
1151 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1152 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1153 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1154 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1156 int64_t field_count
, f_index
;
1158 ret
= type_stack_push(ctx
->type_stack
, type
);
1160 BT_LOGW("Cannot push field type on context's stack: "
1161 "ft-addr=%p", type
);
1165 field_count
= bt_ctf_field_type_common_get_field_count(type
);
1166 if (field_count
< 0) {
1167 BT_LOGW("Cannot get field type's field count: "
1168 "ret=%" PRId64
", ft-addr=%p",
1174 for (f_index
= 0; f_index
< field_count
; f_index
++) {
1175 struct bt_ctf_field_type_common
*child_type
=
1176 bt_ctf_field_type_common_borrow_field_at_index(type
,
1180 BT_LOGW("Cannot get field type's child field: "
1181 "ft-addr=%p, index=%" PRId64
", "
1182 "count=%" PRId64
, type
, f_index
,
1188 if (type_id
== BT_CTF_FIELD_TYPE_ID_ARRAY
||
1189 type_id
== BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
1190 type_stack_peek(ctx
->type_stack
)->index
= -1;
1192 type_stack_peek(ctx
->type_stack
)->index
=
1196 BT_LOGV("Resolving field type's child field type: "
1197 "parent-ft-addr=%p, child-ft-addr=%p, "
1198 "index=%" PRId64
", count=%" PRId64
,
1199 type
, child_type
, f_index
, field_count
);
1200 ret
= resolve_type(child_type
, ctx
);
1206 type_stack_pop(ctx
->type_stack
);
1218 * Resolves the root field type corresponding to the scope `root_scope`.
1221 int resolve_root_type(enum bt_ctf_scope root_scope
, struct resolve_context
*ctx
)
1225 BT_ASSERT(type_stack_size(ctx
->type_stack
) == 0);
1226 ctx
->root_scope
= root_scope
;
1227 ret
= resolve_type(get_type_from_ctx(ctx
, root_scope
), ctx
);
1228 ctx
->root_scope
= BT_CTF_SCOPE_UNKNOWN
;
1234 int bt_ctf_resolve_types(
1235 struct bt_ctf_private_value
*environment
,
1236 struct bt_ctf_field_type_common
*packet_header_type
,
1237 struct bt_ctf_field_type_common
*packet_context_type
,
1238 struct bt_ctf_field_type_common
*event_header_type
,
1239 struct bt_ctf_field_type_common
*stream_event_ctx_type
,
1240 struct bt_ctf_field_type_common
*event_context_type
,
1241 struct bt_ctf_field_type_common
*event_payload_type
,
1242 enum bt_ctf_resolve_flag flags
)
1245 struct resolve_context ctx
= {
1246 .environment
= environment
,
1249 packet_context_type
,
1251 stream_event_ctx_type
,
1255 .root_scope
= BT_CTF_SCOPE_UNKNOWN
,
1258 BT_LOGV("Resolving field types: "
1259 "packet-header-ft-addr=%p, "
1260 "packet-context-ft-addr=%p, "
1261 "event-header-ft-addr=%p, "
1262 "stream-event-context-ft-addr=%p, "
1263 "event-context-ft-addr=%p, "
1264 "event-payload-ft-addr=%p",
1265 packet_header_type
, packet_context_type
, event_header_type
,
1266 stream_event_ctx_type
, event_context_type
, event_payload_type
);
1268 /* Initialize type stack */
1269 ctx
.type_stack
= type_stack_create();
1270 if (!ctx
.type_stack
) {
1271 BT_LOGE_STR("Cannot create field type stack.");
1276 /* Resolve packet header type */
1277 if (flags
& BT_CTF_RESOLVE_FLAG_PACKET_HEADER
) {
1278 ret
= resolve_root_type(BT_CTF_SCOPE_TRACE_PACKET_HEADER
, &ctx
);
1280 BT_LOGW("Cannot resolve trace packet header field type: "
1286 /* Resolve packet context type */
1287 if (flags
& BT_CTF_RESOLVE_FLAG_PACKET_CONTEXT
) {
1288 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
, &ctx
);
1290 BT_LOGW("Cannot resolve stream packet context field type: "
1296 /* Resolve event header type */
1297 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_HEADER
) {
1298 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_HEADER
, &ctx
);
1300 BT_LOGW("Cannot resolve stream event header field type: "
1306 /* Resolve stream event context type */
1307 if (flags
& BT_CTF_RESOLVE_FLAG_STREAM_EVENT_CTX
) {
1308 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
, &ctx
);
1310 BT_LOGW("Cannot resolve stream event context field type: "
1316 /* Resolve event context type */
1317 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_CONTEXT
) {
1318 ret
= resolve_root_type(BT_CTF_SCOPE_EVENT_CONTEXT
, &ctx
);
1320 BT_LOGW("Cannot resolve event context field type: "
1326 /* Resolve event payload type */
1327 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_PAYLOAD
) {
1328 ret
= resolve_root_type(BT_CTF_SCOPE_EVENT_FIELDS
, &ctx
);
1330 BT_LOGW("Cannot resolve event payload field type: "
1336 BT_LOGV_STR("Resolved field types.");
1339 type_stack_destroy(ctx
.type_stack
);