4 * Babeltrace - CTF IR: 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 "RESOLVE"
32 #include <babeltrace/lib-logging-internal.h>
34 #include <babeltrace/ctf-ir/event.h>
35 #include <babeltrace/ctf-ir/stream-class.h>
36 #include <babeltrace/ctf-ir/resolve-internal.h>
37 #include <babeltrace/ctf-ir/field-types.h>
38 #include <babeltrace/ctf-ir/field-path.h>
39 #include <babeltrace/ctf-ir/field-path-internal.h>
40 #include <babeltrace/ctf-ir/event-internal.h>
41 #include <babeltrace/ref.h>
42 #include <babeltrace/babeltrace-internal.h>
43 #include <babeltrace/values.h>
44 #include <babeltrace/types.h>
49 typedef GPtrArray type_stack
;
54 * `type` contains a compound field type (structure, variant, array,
55 * or sequence) and `index` indicates the index of the field type in
56 * the upper frame (-1 for array and sequence field types).
58 * `type` is owned by the stack frame.
60 struct type_stack_frame
{
61 struct bt_ctf_field_type
*type
;
66 * The current context of the resolving engine.
68 * `scopes` contain the 6 CTF scope field types (see CTF, sect. 7.3.2)
69 * in the following order:
74 * * Stream event context
78 struct resolve_context
{
79 struct bt_value
*environment
;
80 struct bt_ctf_field_type
*scopes
[6];
82 /* Root scope being visited */
83 enum bt_ctf_scope root_scope
;
84 type_stack
*type_stack
;
85 struct bt_ctf_field_type
*cur_field_type
;
88 /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
89 static const char * const absolute_path_prefixes
[] = {
90 [BT_CTF_SCOPE_ENV
] = "env.",
91 [BT_CTF_SCOPE_TRACE_PACKET_HEADER
] = "trace.packet.header.",
92 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
] = "stream.packet.context.",
93 [BT_CTF_SCOPE_STREAM_EVENT_HEADER
] = "stream.event.header.",
94 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
] = "stream.event.context.",
95 [BT_CTF_SCOPE_EVENT_CONTEXT
] = "event.context.",
96 [BT_CTF_SCOPE_EVENT_FIELDS
] = "event.fields.",
99 /* Number of path tokens used for the absolute prefixes */
100 static const int absolute_path_prefix_ptoken_counts
[] = {
101 [BT_CTF_SCOPE_ENV
] = 1,
102 [BT_CTF_SCOPE_TRACE_PACKET_HEADER
] = 3,
103 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
] = 3,
104 [BT_CTF_SCOPE_STREAM_EVENT_HEADER
] = 3,
105 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
] = 3,
106 [BT_CTF_SCOPE_EVENT_CONTEXT
] = 2,
107 [BT_CTF_SCOPE_EVENT_FIELDS
] = 2,
111 * Destroys a type stack frame.
114 void type_stack_destroy_notify(gpointer data
)
116 struct type_stack_frame
*frame
= data
;
123 * Creates a type stack.
125 * Return value is owned by the caller.
128 type_stack
*type_stack_create(void)
130 return g_ptr_array_new_with_free_func(type_stack_destroy_notify
);
134 * Destroys a type stack.
137 void type_stack_destroy(type_stack
*stack
)
139 g_ptr_array_free(stack
, TRUE
);
143 * Pushes a field type onto a type stack.
145 * `type` is owned by the caller (stack frame gets a new reference).
148 int type_stack_push(type_stack
*stack
, struct bt_ctf_field_type
*type
)
151 struct type_stack_frame
*frame
= NULL
;
153 if (!stack
|| !type
) {
154 BT_LOGW("Invalid parameter: stack or type is NULL.");
159 frame
= g_new0(struct type_stack_frame
, 1);
161 BT_LOGE_STR("Failed to allocate one field type stack frame.");
166 BT_LOGV("Pushing field type on context's stack: "
167 "ft-addr=%p, stack-size-before=%u", type
, stack
->len
);
168 frame
->type
= bt_get(type
);
169 g_ptr_array_add(stack
, frame
);
176 * Checks whether or not `stack` is empty.
179 bt_bool
type_stack_empty(type_stack
*stack
)
181 return stack
->len
== 0;
185 * Returns the number of frames in `stack`.
188 size_t type_stack_size(type_stack
*stack
)
194 * Returns the top frame of `stack`.
196 * Return value is owned by `stack`.
199 struct type_stack_frame
*type_stack_peek(type_stack
*stack
)
201 struct type_stack_frame
*entry
= NULL
;
203 if (!stack
|| type_stack_empty(stack
)) {
207 entry
= g_ptr_array_index(stack
, stack
->len
- 1);
213 * Returns the frame at index `index` in `stack`.
215 * Return value is owned by `stack`.
218 struct type_stack_frame
*type_stack_at(type_stack
*stack
,
221 struct type_stack_frame
*entry
= NULL
;
223 if (!stack
|| index
>= stack
->len
) {
227 entry
= g_ptr_array_index(stack
, index
);
234 * Removes the top frame of `stack`.
237 void type_stack_pop(type_stack
*stack
)
239 if (!type_stack_empty(stack
)) {
241 * This will call the frame's destructor and free it, as
242 * well as put its contained field type.
244 BT_LOGV("Popping context's stack: stack-size-before=%u",
246 g_ptr_array_set_size(stack
, stack
->len
- 1);
251 * Returns the scope field type of `scope` in the context `ctx`.
253 * Return value is owned by `ctx` on success.
256 struct bt_ctf_field_type
*get_type_from_ctx(struct resolve_context
*ctx
,
257 enum bt_ctf_scope scope
)
259 assert(scope
>= BT_CTF_SCOPE_TRACE_PACKET_HEADER
&&
260 scope
<= BT_CTF_SCOPE_EVENT_FIELDS
);
262 return ctx
->scopes
[scope
- BT_CTF_SCOPE_TRACE_PACKET_HEADER
];
266 * Returns the CTF scope from a path string. May return
267 * CTF_NODE_UNKNOWN if the path is found to be relative.
270 enum bt_ctf_scope
get_root_scope_from_absolute_pathstr(const char *pathstr
)
272 enum bt_ctf_scope scope
;
273 enum bt_ctf_scope ret
= BT_CTF_SCOPE_UNKNOWN
;
274 const size_t prefixes_count
= sizeof(absolute_path_prefixes
) /
275 sizeof(*absolute_path_prefixes
);
277 for (scope
= BT_CTF_SCOPE_ENV
; scope
< BT_CTF_SCOPE_ENV
+
278 prefixes_count
; scope
++) {
280 * Chech if path string starts with a known absolute
283 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
285 if (strncmp(pathstr
, absolute_path_prefixes
[scope
],
286 strlen(absolute_path_prefixes
[scope
]))) {
287 /* Prefix does not match: try the next one */
288 BT_LOGV("Prefix does not match: trying the next one: "
289 "path=\"%s\", path-prefix=\"%s\", scope=%s",
290 pathstr
, absolute_path_prefixes
[scope
],
291 bt_ctf_scope_string(scope
));
297 BT_LOGV("Found root scope from absolute path: "
298 "path=\"%s\", scope=%s", pathstr
,
299 bt_ctf_scope_string(scope
));
308 * Destroys a path token.
311 void ptokens_destroy_func(gpointer ptoken
, gpointer data
)
313 g_string_free(ptoken
, TRUE
);
317 * Destroys a path token list.
320 void ptokens_destroy(GList
*ptokens
)
326 g_list_foreach(ptokens
, ptokens_destroy_func
, NULL
);
327 g_list_free(ptokens
);
331 * Returns the string contained in a path token.
334 const char *ptoken_get_string(GList
*ptoken
)
336 GString
*tokenstr
= (GString
*) ptoken
->data
;
338 return tokenstr
->str
;
342 * Converts a path string to a path token list, that is, splits the
343 * individual words of a path string into a list of individual
346 * Return value is owned by the caller on success.
349 GList
*pathstr_to_ptokens(const char *pathstr
)
351 const char *at
= pathstr
;
352 const char *last
= at
;
353 GList
*ptokens
= NULL
;
356 if (*at
== '.' || *at
== '\0') {
360 /* Error: empty token */
361 BT_LOGW("Empty path token: path=\"%s\", pos=%u",
362 pathstr
, (int) (at
- pathstr
));
366 tokenstr
= g_string_new(NULL
);
367 g_string_append_len(tokenstr
, last
, at
- last
);
368 ptokens
= g_list_append(ptokens
, tokenstr
);
382 ptokens_destroy(ptokens
);
387 * Converts a path token list to a field path object. The path token
388 * list is relative from `type`. The index of the source looking for
389 * its target within `type` is indicated by `src_index`. This can be
390 * `INT_MAX` if the source is contained in `type`.
392 * `ptokens` is owned by the caller. `field_path` is an output parameter
393 * owned by the caller that must be filled here. `type` is owned by the
397 int ptokens_to_field_path(GList
*ptokens
, struct bt_ctf_field_path
*field_path
,
398 struct bt_ctf_field_type
*type
, int src_index
)
401 GList
*cur_ptoken
= ptokens
;
402 bt_bool first_level_done
= BT_FALSE
;
404 /* Get our own reference */
410 struct bt_ctf_field_type
*child_type
;
411 const char *field_name
= ptoken_get_string(cur_ptoken
);
412 enum bt_ctf_field_type_id type_id
=
413 bt_ctf_field_type_get_type_id(type
);
415 BT_LOGV("Current path token: token=\"%s\"", field_name
);
417 /* Find to which index corresponds the current path token */
418 if (type_id
== BT_CTF_FIELD_TYPE_ID_ARRAY
||
419 type_id
== BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
422 child_index
= bt_ctf_field_type_get_field_index(type
,
424 if (child_index
< 0) {
426 * Error: field name does not exist or
427 * wrong current type.
429 BT_LOGW("Cannot get index of field type: "
430 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
431 field_name
, src_index
, child_index
, first_level_done
);
434 } else if (child_index
> src_index
&&
436 BT_LOGW("Child field type is located after source field type: "
437 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
438 field_name
, src_index
, child_index
, first_level_done
);
443 /* Next path token */
444 cur_ptoken
= g_list_next(cur_ptoken
);
445 first_level_done
= BT_TRUE
;
448 /* Create new field path entry */
449 g_array_append_val(field_path
->indexes
, child_index
);
451 /* Get child field type */
452 child_type
= bt_ctf_field_type_get_field_at_index(type
,
455 BT_LOGW("Cannot get child field type: "
456 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
457 field_name
, src_index
, child_index
, first_level_done
);
462 /* Move child type to current type */
463 BT_MOVE(type
, child_type
);
472 * Converts a known absolute path token list to a field path object
473 * within the resolving context `ctx`.
475 * `ptokens` is owned by the caller. `field_path` is an output parameter
476 * owned by the caller that must be filled here.
479 int absolute_ptokens_to_field_path(GList
*ptokens
,
480 struct bt_ctf_field_path
*field_path
,
481 struct resolve_context
*ctx
)
485 struct bt_ctf_field_type
*type
;
487 /* Skip absolute path tokens */
488 cur_ptoken
= g_list_nth(ptokens
,
489 absolute_path_prefix_ptoken_counts
[field_path
->root
]);
491 /* Start with root type */
492 type
= get_type_from_ctx(ctx
, field_path
->root
);
494 /* Error: root type is not available */
495 BT_LOGW("Root field type is not available: "
497 bt_ctf_scope_string(field_path
->root
));
503 ret
= ptokens_to_field_path(cur_ptoken
, field_path
, type
, INT_MAX
);
510 * Converts a known relative path token list to a field path object
511 * within the resolving context `ctx`.
513 * `ptokens` is owned by the caller. `field_path` is an output parameter
514 * owned by the caller that must be filled here.
517 int relative_ptokens_to_field_path(GList
*ptokens
,
518 struct bt_ctf_field_path
*field_path
,
519 struct resolve_context
*ctx
)
522 int parent_pos_in_stack
;
523 struct bt_ctf_field_path
*tail_field_path
= bt_ctf_field_path_create();
525 if (!tail_field_path
) {
526 BT_LOGE_STR("Cannot create empty field path.");
531 parent_pos_in_stack
= type_stack_size(ctx
->type_stack
) - 1;
533 while (parent_pos_in_stack
>= 0) {
534 struct bt_ctf_field_type
*parent_type
=
535 type_stack_at(ctx
->type_stack
,
536 parent_pos_in_stack
)->type
;
537 int cur_index
= type_stack_at(ctx
->type_stack
,
538 parent_pos_in_stack
)->index
;
540 BT_LOGV("Locating target field type from current parent field type: "
541 "parent-pos=%d, parent-ft-addr=%p, cur-index=%d",
542 parent_pos_in_stack
, parent_type
, cur_index
);
544 /* Locate target from current parent type */
545 ret
= ptokens_to_field_path(ptokens
, tail_field_path
,
546 parent_type
, cur_index
);
548 /* Not found... yet */
549 BT_LOGV_STR("Not found at this point.");
550 bt_ctf_field_path_clear(tail_field_path
);
552 /* Found: stitch tail field path to head field path */
554 int tail_field_path_len
=
555 tail_field_path
->indexes
->len
;
558 struct bt_ctf_field_type
*cur_type
=
559 type_stack_at(ctx
->type_stack
, i
)->type
;
560 int index
= type_stack_at(
561 ctx
->type_stack
, i
)->index
;
563 if (cur_type
== parent_type
) {
567 g_array_append_val(field_path
->indexes
,
572 for (i
= 0; i
< tail_field_path_len
; i
++) {
573 int index
= g_array_index(
574 tail_field_path
->indexes
,
577 g_array_append_val(field_path
->indexes
,
583 parent_pos_in_stack
--;
586 if (parent_pos_in_stack
< 0) {
587 /* Not found: look in previous scopes */
590 while (field_path
->root
>= BT_CTF_SCOPE_TRACE_PACKET_HEADER
) {
591 struct bt_ctf_field_type
*root_type
;
592 bt_ctf_field_path_clear(field_path
);
594 BT_LOGV("Looking into potential root scope: scope=%s",
595 bt_ctf_scope_string(field_path
->root
));
596 root_type
= get_type_from_ctx(ctx
, field_path
->root
);
602 /* Locate target in previous scope */
603 ret
= ptokens_to_field_path(ptokens
, field_path
,
607 BT_LOGV_STR("Not found in this scope.");
613 BT_LOGV_STR("Found in this scope.");
619 BT_PUT(tail_field_path
);
624 * Converts a path string to a field path object within the resolving
627 * Return value is owned by the caller on success.
630 struct bt_ctf_field_path
*pathstr_to_field_path(const char *pathstr
,
631 struct resolve_context
*ctx
)
634 enum bt_ctf_scope root_scope
;
635 GList
*ptokens
= NULL
;
636 struct bt_ctf_field_path
*field_path
= NULL
;
638 /* Create field path */
639 field_path
= bt_ctf_field_path_create();
641 BT_LOGE_STR("Cannot create empty field path.");
646 /* Convert path string to path tokens */
647 ptokens
= pathstr_to_ptokens(pathstr
);
649 BT_LOGW("Cannot convert path string to path tokens: "
650 "path=\"%s\"", pathstr
);
655 /* Absolute or relative path? */
656 root_scope
= get_root_scope_from_absolute_pathstr(pathstr
);
658 if (root_scope
== BT_CTF_SCOPE_UNKNOWN
) {
659 /* Relative path: start with current root scope */
660 field_path
->root
= ctx
->root_scope
;
661 BT_LOGV("Detected relative path: starting with current root scope: "
662 "scope=%s", bt_ctf_scope_string(field_path
->root
));
663 ret
= relative_ptokens_to_field_path(ptokens
, field_path
, ctx
);
665 BT_LOGW("Cannot get relative field path of path string: "
666 "path=\"%s\", start-scope=%s, end-scope=%s",
667 pathstr
, bt_ctf_scope_string(ctx
->root_scope
),
668 bt_ctf_scope_string(field_path
->root
));
671 } else if (root_scope
== BT_CTF_SCOPE_ENV
) {
672 BT_LOGW("Sequence field types referring the trace environment are not supported as of this version: "
673 "path=\"%s\"", pathstr
);
677 /* Absolute path: use found root scope */
678 field_path
->root
= root_scope
;
679 BT_LOGV("Detected absolute path: using root scope: "
680 "scope=%s", bt_ctf_scope_string(field_path
->root
));
681 ret
= absolute_ptokens_to_field_path(ptokens
, field_path
, ctx
);
683 BT_LOGW("Cannot get absolute field path of path string: "
684 "path=\"%s\", root-scope=%s",
685 pathstr
, bt_ctf_scope_string(root_scope
));
691 GString
*field_path_pretty
=
692 bt_ctf_field_path_string(field_path
);
693 const char *field_path_pretty_str
=
694 field_path_pretty
? field_path_pretty
->str
: NULL
;
696 BT_LOGV("Found field path: path=\"%s\", field-path=\"%s\"",
697 pathstr
, field_path_pretty_str
);
699 if (field_path_pretty
) {
700 g_string_free(field_path_pretty
, TRUE
);
709 ptokens_destroy(ptokens
);
714 * Retrieves a field type by following the field path `field_path` in
715 * the resolving context `ctx`.
717 * Return value is owned by the caller on success.
720 struct bt_ctf_field_type
*field_path_to_field_type(
721 struct bt_ctf_field_path
*field_path
,
722 struct resolve_context
*ctx
)
725 struct bt_ctf_field_type
*type
;
727 /* Start with root type */
728 type
= get_type_from_ctx(ctx
, field_path
->root
);
731 /* Error: root type is not available */
732 BT_LOGW("Root field type is not available: root-scope=%s",
733 bt_ctf_scope_string(field_path
->root
));
738 for (i
= 0; i
< field_path
->indexes
->len
; i
++) {
739 struct bt_ctf_field_type
*child_type
;
741 g_array_index(field_path
->indexes
, int, i
);
743 /* Get child field type */
744 child_type
= bt_ctf_field_type_get_field_at_index(type
,
747 BT_LOGW("Cannot get field type: "
748 "parent-ft-addr=%p, index=%d", type
, i
);
752 /* Move child type to current type */
753 BT_MOVE(type
, child_type
);
764 * Returns the equivalent field path object of the context type stack.
766 * Return value is owned by the caller on success.
769 struct bt_ctf_field_path
*get_ctx_stack_field_path(struct resolve_context
*ctx
)
772 struct bt_ctf_field_path
*field_path
;
774 /* Create field path */
775 field_path
= bt_ctf_field_path_create();
777 BT_LOGE_STR("Cannot create empty field path.");
781 field_path
->root
= ctx
->root_scope
;
783 for (i
= 0; i
< type_stack_size(ctx
->type_stack
); i
++) {
784 struct type_stack_frame
*frame
;
786 frame
= type_stack_at(ctx
->type_stack
, i
);
787 g_array_append_val(field_path
->indexes
, frame
->index
);
798 * Returns the lowest common ancestor of two field path objects
799 * having the same root scope.
801 * `field_path1` and `field_path2` are owned by the caller.
803 int get_field_paths_lca_index(struct bt_ctf_field_path
*field_path1
,
804 struct bt_ctf_field_path
*field_path2
)
807 int field_path1_len
, field_path2_len
;
809 if (BT_LOG_ON_VERBOSE
) {
810 GString
*field_path1_pretty
=
811 bt_ctf_field_path_string(field_path1
);
812 GString
*field_path2_pretty
=
813 bt_ctf_field_path_string(field_path2
);
814 const char *field_path1_pretty_str
=
815 field_path1_pretty
? field_path1_pretty
->str
: NULL
;
816 const char *field_path2_pretty_str
=
817 field_path2_pretty
? field_path2_pretty
->str
: NULL
;
819 BT_LOGV("Finding lowest common ancestor (LCA) between two field paths: "
820 "field-path-1=\"%s\", field-path-2=\"%s\"",
821 field_path1_pretty_str
, field_path2_pretty_str
);
823 if (field_path1_pretty
) {
824 g_string_free(field_path1_pretty
, TRUE
);
827 if (field_path2_pretty
) {
828 g_string_free(field_path2_pretty
, TRUE
);
833 * Start from both roots and find the first mismatch.
835 assert(field_path1
->root
== field_path2
->root
);
836 field_path1_len
= field_path1
->indexes
->len
;
837 field_path2_len
= field_path2
->indexes
->len
;
840 int target_index
, ctx_index
;
842 if (lca_index
== field_path2_len
||
843 lca_index
== field_path1_len
) {
845 * This means that both field paths never split.
846 * This is invalid because the target cannot be
847 * an ancestor of the source.
849 BT_LOGW("Source field type is an ancestor of target field type or vice versa: "
850 "lca-index=%d, field-path-1-len=%d, "
851 "field-path-2-len=%d",
852 lca_index
, field_path1_len
, field_path2_len
);
857 target_index
= g_array_index(field_path1
->indexes
, int,
859 ctx_index
= g_array_index(field_path2
->indexes
, int,
862 if (target_index
!= ctx_index
) {
863 /* LCA index is the previous */
870 BT_LOGV("Found LCA: lca-index=%d", lca_index
);
875 * Validates a target field path.
877 * `target_field_path` and `target_type` are owned by the caller.
880 int validate_target_field_path(struct bt_ctf_field_path
*target_field_path
,
881 struct bt_ctf_field_type
*target_type
,
882 struct resolve_context
*ctx
)
885 struct bt_ctf_field_path
*ctx_field_path
;
886 int target_field_path_len
= target_field_path
->indexes
->len
;
888 int ctx_cur_field_type_id
;
891 /* Get context field path */
892 ctx_field_path
= get_ctx_stack_field_path(ctx
);
893 if (!ctx_field_path
) {
894 BT_LOGW_STR("Cannot get field path from context's stack.");
900 * Make sure the target is not a root.
902 if (target_field_path_len
== 0) {
903 BT_LOGW_STR("Target field path's length is 0 (targeting the root).");
909 * Make sure the root of the target field path is not located
910 * after the context field path's root.
912 if (target_field_path
->root
> ctx_field_path
->root
) {
913 BT_LOGW("Target field type is located after source field type: "
914 "target-root=%s, source-root=%s",
915 bt_ctf_scope_string(target_field_path
->root
),
916 bt_ctf_scope_string(ctx_field_path
->root
));
921 if (target_field_path
->root
== ctx_field_path
->root
) {
922 int target_index
, ctx_index
;
925 * Find the index of the lowest common ancestor of both field
928 lca_index
= get_field_paths_lca_index(target_field_path
,
931 BT_LOGW_STR("Cannot get least common ancestor.");
937 * Make sure the target field path is located before the
938 * context field path.
940 target_index
= g_array_index(target_field_path
->indexes
,
942 ctx_index
= g_array_index(ctx_field_path
->indexes
,
945 if (target_index
>= ctx_index
) {
946 BT_LOGW("Target field type's index is greater than or equal to source field type's index in LCA: "
947 "lca-index=%d, target-index=%d, source-index=%d",
948 lca_index
, target_index
, ctx_index
);
955 * Make sure the target type has the right type and properties.
957 ctx_cur_field_type_id
= bt_ctf_field_type_get_type_id(
958 ctx
->cur_field_type
);
959 target_type_id
= bt_ctf_field_type_get_type_id(target_type
);
961 if (ctx_cur_field_type_id
== CTF_TYPE_VARIANT
) {
962 if (target_type_id
!= CTF_TYPE_ENUM
) {
963 BT_LOGW("Variant field type's tag field type is not an enumeration field type: "
964 "tag-ft-addr=%p, tag-ft-id=%s",
966 bt_ctf_field_type_id_string(target_type_id
));
970 } else if (ctx_cur_field_type_id
== CTF_TYPE_SEQUENCE
) {
971 if (target_type_id
!= CTF_TYPE_INTEGER
||
972 bt_ctf_field_type_integer_get_signed(
974 BT_LOGW("Sequence field type's length field type is not an unsigned integer field type: "
975 "length-ft-addr=%p, length-ft-id=%s",
977 bt_ctf_field_type_id_string(target_type_id
));
986 BT_PUT(ctx_field_path
);
991 * Resolves a variant or sequence field type `type`.
993 * `type` is owned by the caller.
996 int resolve_sequence_or_variant_type(struct bt_ctf_field_type
*type
,
997 struct resolve_context
*ctx
)
1000 const char *pathstr
;
1001 int type_id
= bt_ctf_field_type_get_type_id(type
);
1002 struct bt_ctf_field_path
*target_field_path
= NULL
;
1003 struct bt_ctf_field_type
*target_type
= NULL
;
1004 GString
*target_field_path_pretty
= NULL
;
1005 const char *target_field_path_pretty_str
;
1008 /* Get path string */
1010 case CTF_TYPE_SEQUENCE
:
1012 bt_ctf_field_type_sequence_get_length_field_name(type
);
1014 case CTF_TYPE_VARIANT
:
1016 bt_ctf_field_type_variant_get_tag_name(type
);
1024 /* Get target field path out of path string */
1025 target_field_path
= pathstr_to_field_path(pathstr
, ctx
);
1026 if (!target_field_path
) {
1027 BT_LOGW("Cannot get target field path for path string: "
1028 "path=\"%s\"", pathstr
);
1033 target_field_path_pretty
= bt_ctf_field_path_string(target_field_path
);
1034 target_field_path_pretty_str
=
1035 target_field_path_pretty
? target_field_path_pretty
->str
: NULL
;
1037 /* Get target field type */
1038 target_type
= field_path_to_field_type(target_field_path
, ctx
);
1040 BT_LOGW("Cannot get target field type for path string: "
1041 "path=\"%s\", target-field-path=\"%s\"",
1042 pathstr
, target_field_path_pretty_str
);
1047 ret
= validate_target_field_path(target_field_path
, target_type
, ctx
);
1049 BT_LOGW("Invalid target field path for path string: "
1050 "path=\"%s\", target-field-path=\"%s\"",
1051 pathstr
, target_field_path_pretty_str
);
1055 /* Set target field path and target field type */
1056 if (type_id
== CTF_TYPE_SEQUENCE
) {
1057 ret
= bt_ctf_field_type_sequence_set_length_field_path(
1058 type
, target_field_path
);
1060 BT_LOGW("Cannot set sequence field type's length field path: "
1061 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1063 target_field_path_pretty_str
);
1066 } else if (type_id
== CTF_TYPE_VARIANT
) {
1067 ret
= bt_ctf_field_type_variant_set_tag_field_path(
1068 type
, target_field_path
);
1070 BT_LOGW("Cannot set varaint field type's tag field path: "
1071 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1073 target_field_path_pretty_str
);
1077 ret
= bt_ctf_field_type_variant_set_tag_field_type(
1080 BT_LOGW("Cannot set varaint field type's tag field type: "
1081 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1083 target_field_path_pretty_str
);
1091 if (target_field_path_pretty
) {
1092 g_string_free(target_field_path_pretty
, TRUE
);
1095 BT_PUT(target_field_path
);
1096 BT_PUT(target_type
);
1101 * Resolves a field type `type`.
1103 * `type` is owned by the caller.
1106 int resolve_type(struct bt_ctf_field_type
*type
, struct resolve_context
*ctx
)
1112 /* Type is not available; still valid */
1116 type_id
= bt_ctf_field_type_get_type_id(type
);
1117 ctx
->cur_field_type
= type
;
1119 /* Resolve sequence/variant field type */
1121 case CTF_TYPE_SEQUENCE
:
1122 case CTF_TYPE_VARIANT
:
1123 ret
= resolve_sequence_or_variant_type(type
, ctx
);
1125 BT_LOGW("Cannot resolve sequence field type's length or variant field type's tag: "
1126 "ret=%d, ft-addr=%p", ret
, type
);
1134 /* Recurse into compound types */
1136 case CTF_TYPE_STRUCT
:
1137 case CTF_TYPE_VARIANT
:
1138 case CTF_TYPE_SEQUENCE
:
1139 case CTF_TYPE_ARRAY
:
1141 int64_t field_count
, f_index
;
1143 ret
= type_stack_push(ctx
->type_stack
, type
);
1145 BT_LOGW("Cannot push field type on context's stack: "
1146 "ft-addr=%p", type
);
1150 field_count
= bt_ctf_field_type_get_field_count(type
);
1151 if (field_count
< 0) {
1152 BT_LOGW("Cannot get field type's field count: "
1153 "ret=%" PRId64
", ft-addr=%p",
1159 for (f_index
= 0; f_index
< field_count
; f_index
++) {
1160 struct bt_ctf_field_type
*child_type
=
1161 bt_ctf_field_type_get_field_at_index(type
,
1165 BT_LOGW("Cannot get field type's child field: "
1166 "ft-addr=%p, index=%" PRId64
", "
1167 "count=%" PRId64
, type
, f_index
,
1173 if (type_id
== CTF_TYPE_ARRAY
||
1174 type_id
== CTF_TYPE_SEQUENCE
) {
1175 type_stack_peek(ctx
->type_stack
)->index
= -1;
1177 type_stack_peek(ctx
->type_stack
)->index
=
1181 BT_LOGV("Resolving field type's child field type: "
1182 "parent-ft-addr=%p, child-ft-addr=%p, "
1183 "index=%" PRId64
", count=%" PRId64
,
1184 type
, child_type
, f_index
, field_count
);
1185 ret
= resolve_type(child_type
, ctx
);
1192 type_stack_pop(ctx
->type_stack
);
1204 * Resolves the root field type corresponding to the scope `root_scope`.
1207 int resolve_root_type(enum bt_ctf_scope root_scope
, struct resolve_context
*ctx
)
1211 assert(type_stack_size(ctx
->type_stack
) == 0);
1212 ctx
->root_scope
= root_scope
;
1213 ret
= resolve_type(get_type_from_ctx(ctx
, root_scope
), ctx
);
1214 ctx
->root_scope
= BT_CTF_SCOPE_UNKNOWN
;
1220 int bt_ctf_resolve_types(
1221 struct bt_value
*environment
,
1222 struct bt_ctf_field_type
*packet_header_type
,
1223 struct bt_ctf_field_type
*packet_context_type
,
1224 struct bt_ctf_field_type
*event_header_type
,
1225 struct bt_ctf_field_type
*stream_event_ctx_type
,
1226 struct bt_ctf_field_type
*event_context_type
,
1227 struct bt_ctf_field_type
*event_payload_type
,
1228 enum bt_ctf_resolve_flag flags
)
1231 struct resolve_context ctx
= {
1232 .environment
= environment
,
1235 packet_context_type
,
1237 stream_event_ctx_type
,
1241 .root_scope
= BT_CTF_SCOPE_UNKNOWN
,
1244 BT_LOGV("Resolving field types: "
1245 "packet-header-ft-addr=%p, "
1246 "packet-context-ft-addr=%p, "
1247 "event-header-ft-addr=%p, "
1248 "stream-event-context-ft-addr=%p, "
1249 "event-context-ft-addr=%p, "
1250 "event-payload-ft-addr=%p",
1251 packet_header_type
, packet_context_type
, event_header_type
,
1252 stream_event_ctx_type
, event_context_type
, event_payload_type
);
1254 /* Initialize type stack */
1255 ctx
.type_stack
= type_stack_create();
1256 if (!ctx
.type_stack
) {
1257 BT_LOGE_STR("Cannot create field type stack.");
1262 /* Resolve packet header type */
1263 if (flags
& BT_CTF_RESOLVE_FLAG_PACKET_HEADER
) {
1264 ret
= resolve_root_type(BT_CTF_SCOPE_TRACE_PACKET_HEADER
, &ctx
);
1266 BT_LOGW("Cannot resolve trace packet header field type: "
1272 /* Resolve packet context type */
1273 if (flags
& BT_CTF_RESOLVE_FLAG_PACKET_CONTEXT
) {
1274 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
, &ctx
);
1276 BT_LOGW("Cannot resolve stream packet context field type: "
1282 /* Resolve event header type */
1283 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_HEADER
) {
1284 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_HEADER
, &ctx
);
1286 BT_LOGW("Cannot resolve stream event header field type: "
1292 /* Resolve stream event context type */
1293 if (flags
& BT_CTF_RESOLVE_FLAG_STREAM_EVENT_CTX
) {
1294 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
, &ctx
);
1296 BT_LOGW("Cannot resolve stream event context field type: "
1302 /* Resolve event context type */
1303 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_CONTEXT
) {
1304 ret
= resolve_root_type(BT_CTF_SCOPE_EVENT_CONTEXT
, &ctx
);
1306 BT_LOGW("Cannot resolve event context field type: "
1312 /* Resolve event payload type */
1313 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_PAYLOAD
) {
1314 ret
= resolve_root_type(BT_CTF_SCOPE_EVENT_FIELDS
, &ctx
);
1316 BT_LOGW("Cannot resolve event payload field type: "
1322 BT_LOGV_STR("Resolved field types.");
1325 type_stack_destroy(ctx
.type_stack
);