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 #include <babeltrace/ctf-ir/event.h>
32 #include <babeltrace/ctf-ir/stream-class.h>
33 #include <babeltrace/ctf-ir/resolve-internal.h>
34 #include <babeltrace/ctf-ir/field-types.h>
35 #include <babeltrace/ctf-ir/field-path.h>
36 #include <babeltrace/ctf-ir/field-path-internal.h>
37 #include <babeltrace/ctf-ir/event-internal.h>
38 #include <babeltrace/ref.h>
39 #include <babeltrace/babeltrace-internal.h>
40 #include <babeltrace/values.h>
44 #define _printf_error(fmt, args...) \
45 printf_verbose("[resolving] " fmt, ## args)
47 typedef GPtrArray type_stack
;
52 * `type` contains a compound field type (structure, variant, array,
53 * or sequence) and `index` indicates the index of the field type in
54 * the upper frame (-1 for array and sequence field types).
56 * `type` is owned by the stack frame.
58 struct type_stack_frame
{
59 struct bt_ctf_field_type
*type
;
64 * The current context of the resolving engine.
66 * `scopes` contain the 6 CTF scope field types (see CTF, sect. 7.3.2)
67 * in the following order:
72 * * Stream event context
76 struct resolve_context
{
77 struct bt_value
*environment
;
78 struct bt_ctf_field_type
*scopes
[6];
80 /* Root scope being visited */
81 enum bt_ctf_scope root_scope
;
82 type_stack
*type_stack
;
83 struct bt_ctf_field_type
*cur_field_type
;
86 /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
87 static const char * const absolute_path_prefixes
[] = {
88 [BT_CTF_SCOPE_ENV
] = "env.",
89 [BT_CTF_SCOPE_TRACE_PACKET_HEADER
] = "trace.packet.header.",
90 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
] = "stream.packet.context.",
91 [BT_CTF_SCOPE_STREAM_EVENT_HEADER
] = "stream.event.header.",
92 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
] = "stream.event.context.",
93 [BT_CTF_SCOPE_EVENT_CONTEXT
] = "event.context.",
94 [BT_CTF_SCOPE_EVENT_FIELDS
] = "event.fields.",
97 /* Number of path tokens used for the absolute prefixes */
98 static const int absolute_path_prefix_ptoken_counts
[] = {
99 [BT_CTF_SCOPE_ENV
] = 1,
100 [BT_CTF_SCOPE_TRACE_PACKET_HEADER
] = 3,
101 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
] = 3,
102 [BT_CTF_SCOPE_STREAM_EVENT_HEADER
] = 3,
103 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
] = 3,
104 [BT_CTF_SCOPE_EVENT_CONTEXT
] = 2,
105 [BT_CTF_SCOPE_EVENT_FIELDS
] = 2,
109 * Destroys a type stack frame.
112 void type_stack_destroy_notify(gpointer data
)
114 struct type_stack_frame
*frame
= data
;
121 * Creates a type stack.
123 * Return value is owned by the caller.
126 type_stack
*type_stack_create(void)
128 return g_ptr_array_new_with_free_func(type_stack_destroy_notify
);
132 * Destroys a type stack.
135 void type_stack_destroy(type_stack
*stack
)
137 g_ptr_array_free(stack
, TRUE
);
141 * Pushes a field type onto a type stack.
143 * `type` is owned by the caller (stack frame gets a new reference).
146 int type_stack_push(type_stack
*stack
, struct bt_ctf_field_type
*type
)
149 struct type_stack_frame
*frame
= NULL
;
151 if (!stack
|| !type
) {
156 frame
= g_new0(struct type_stack_frame
, 1);
162 frame
->type
= bt_get(type
);
163 g_ptr_array_add(stack
, frame
);
170 * Checks whether or not `stack` is empty.
173 bool type_stack_empty(type_stack
*stack
)
175 return stack
->len
== 0;
179 * Returns the number of frames in `stack`.
182 size_t type_stack_size(type_stack
*stack
)
188 * Returns the top frame of `stack`.
190 * Return value is owned by `stack`.
193 struct type_stack_frame
*type_stack_peek(type_stack
*stack
)
195 struct type_stack_frame
*entry
= NULL
;
197 if (!stack
|| type_stack_empty(stack
)) {
201 entry
= g_ptr_array_index(stack
, stack
->len
- 1);
207 * Returns the frame at index `index` in `stack`.
209 * Return value is owned by `stack`.
212 struct type_stack_frame
*type_stack_at(type_stack
*stack
,
215 struct type_stack_frame
*entry
= NULL
;
217 if (!stack
|| index
>= stack
->len
) {
221 entry
= g_ptr_array_index(stack
, index
);
228 * Removes the top frame of `stack`.
231 void type_stack_pop(type_stack
*stack
)
233 if (!type_stack_empty(stack
)) {
235 * This will call the frame's destructor and free it, as
236 * well as put its contained field type.
238 g_ptr_array_set_size(stack
, stack
->len
- 1);
243 * Returns the scope field type of `scope` in the context `ctx`.
245 * Return value is owned by `ctx` on success.
248 struct bt_ctf_field_type
*get_type_from_ctx(struct resolve_context
*ctx
,
249 enum bt_ctf_scope scope
)
251 assert(scope
>= BT_CTF_SCOPE_TRACE_PACKET_HEADER
&&
252 scope
<= BT_CTF_SCOPE_EVENT_FIELDS
);
254 return ctx
->scopes
[scope
- BT_CTF_SCOPE_TRACE_PACKET_HEADER
];
258 * Returns the CTF scope from a path string. May return
259 * CTF_NODE_UNKNOWN if the path is found to be relative.
262 enum bt_ctf_scope
get_root_scope_from_absolute_pathstr(const char *pathstr
)
264 enum bt_ctf_scope scope
;
265 enum bt_ctf_scope ret
= BT_CTF_SCOPE_UNKNOWN
;
266 const size_t prefixes_count
= sizeof(absolute_path_prefixes
) /
267 sizeof(*absolute_path_prefixes
);
269 for (scope
= BT_CTF_SCOPE_ENV
; scope
< BT_CTF_SCOPE_ENV
+
270 prefixes_count
; scope
++) {
272 * Chech if path string starts with a known absolute
275 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
277 if (strncmp(pathstr
, absolute_path_prefixes
[scope
],
278 strlen(absolute_path_prefixes
[scope
]))) {
279 /* Prefix does not match: try the next one */
293 * Destroys a path token.
296 void ptokens_destroy_func(gpointer ptoken
, gpointer data
)
298 g_string_free(ptoken
, TRUE
);
302 * Destroys a path token list.
305 void ptokens_destroy(GList
*ptokens
)
311 g_list_foreach(ptokens
, ptokens_destroy_func
, NULL
);
312 g_list_free(ptokens
);
316 * Returns the string contained in a path token.
319 const char *ptoken_get_string(GList
*ptoken
)
321 GString
*tokenstr
= (GString
*) ptoken
->data
;
323 return tokenstr
->str
;
327 * Converts a path string to a path token list, that is, splits the
328 * individual words of a path string into a list of individual
331 * Return value is owned by the caller on success.
334 GList
*pathstr_to_ptokens(const char *pathstr
)
336 const char *at
= pathstr
;
337 const char *last
= at
;
338 GList
*ptokens
= NULL
;
341 if (*at
== '.' || *at
== '\0') {
345 /* Error: empty token */
346 _printf_error("Empty token in path string at position %d\n",
347 (int) (at
- pathstr
));
351 tokenstr
= g_string_new(NULL
);
352 g_string_append_len(tokenstr
, last
, at
- last
);
353 ptokens
= g_list_append(ptokens
, tokenstr
);
367 ptokens_destroy(ptokens
);
372 * Converts a path token list to a field path object. The path token
373 * list is relative from `type`. The index of the source looking for
374 * its target within `type` is indicated by `src_index`. This can be
375 * `INT_MAX` if the source is contained in `type`.
377 * `ptokens` is owned by the caller. `field_path` is an output parameter
378 * owned by the caller that must be filled here. `type` is owned by the
382 int ptokens_to_field_path(GList
*ptokens
, struct bt_ctf_field_path
*field_path
,
383 struct bt_ctf_field_type
*type
, int src_index
)
386 GList
*cur_ptoken
= ptokens
;
387 bool first_level_done
= false;
389 /* Get our own reference */
395 struct bt_ctf_field_type
*child_type
;
396 const char *field_name
= ptoken_get_string(cur_ptoken
);
397 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(type
);
399 /* Find to which index corresponds the current path token */
400 if (type_id
== CTF_TYPE_ARRAY
|| type_id
== CTF_TYPE_SEQUENCE
) {
403 child_index
= bt_ctf_field_type_get_field_index(type
,
405 if (child_index
< 0) {
407 * Error: field name does not exist or
408 * wrong current type.
410 _printf_error("Cannot get index of field type named \"%s\"\n",
414 } else if (child_index
> src_index
&&
416 _printf_error("Child type is located after source index (%d)\n",
422 /* Next path token */
423 cur_ptoken
= g_list_next(cur_ptoken
);
424 first_level_done
= true;
427 /* Create new field path entry */
428 g_array_append_val(field_path
->indexes
, child_index
);
430 /* Get child field type */
431 child_type
= bt_ctf_field_type_get_field_at_index(type
,
434 _printf_error("Cannot get child type at index %d (field \"%s\")\n",
435 child_index
, field_name
);
440 /* Move child type to current type */
441 BT_MOVE(type
, child_type
);
450 * Converts a known absolute path token list to a field path object
451 * within the resolving context `ctx`.
453 * `ptokens` is owned by the caller. `field_path` is an output parameter
454 * owned by the caller that must be filled here.
457 int absolute_ptokens_to_field_path(GList
*ptokens
,
458 struct bt_ctf_field_path
*field_path
,
459 struct resolve_context
*ctx
)
463 struct bt_ctf_field_type
*type
;
465 /* Skip absolute path tokens */
466 cur_ptoken
= g_list_nth(ptokens
,
467 absolute_path_prefix_ptoken_counts
[field_path
->root
]);
469 /* Start with root type */
470 type
= get_type_from_ctx(ctx
, field_path
->root
);
472 /* Error: root type is not available */
473 _printf_error("Root type with scope type %d is not available\n",
480 ret
= ptokens_to_field_path(cur_ptoken
, field_path
, type
, INT_MAX
);
487 * Converts a known relative path token list to a field path object
488 * within the resolving context `ctx`.
490 * `ptokens` is owned by the caller. `field_path` is an output parameter
491 * owned by the caller that must be filled here.
494 int relative_ptokens_to_field_path(GList
*ptokens
,
495 struct bt_ctf_field_path
*field_path
,
496 struct resolve_context
*ctx
)
499 int parent_pos_in_stack
;
500 struct bt_ctf_field_path
*tail_field_path
= bt_ctf_field_path_create();
502 if (!tail_field_path
) {
503 _printf_error("Cannot create field path\n");
508 parent_pos_in_stack
= type_stack_size(ctx
->type_stack
) - 1;
510 while (parent_pos_in_stack
>= 0) {
511 struct bt_ctf_field_type
*parent_type
=
512 type_stack_at(ctx
->type_stack
,
513 parent_pos_in_stack
)->type
;
514 int cur_index
= type_stack_at(ctx
->type_stack
,
515 parent_pos_in_stack
)->index
;
517 /* Locate target from current parent type */
518 ret
= ptokens_to_field_path(ptokens
, tail_field_path
,
519 parent_type
, cur_index
);
521 /* Not found... yet */
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
;
530 struct bt_ctf_field_type
*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
= 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
*root_type
;
564 bt_ctf_field_path_clear(field_path
);
566 root_type
= get_type_from_ctx(ctx
, field_path
->root
);
572 /* Locate target in previous scope */
573 ret
= ptokens_to_field_path(ptokens
, field_path
,
587 BT_PUT(tail_field_path
);
592 * Converts a path string to a field path object within the resolving
595 * Return value is owned by the caller on success.
598 struct bt_ctf_field_path
*pathstr_to_field_path(const char *pathstr
,
599 struct resolve_context
*ctx
)
602 enum bt_ctf_scope root_scope
;
603 GList
*ptokens
= NULL
;
604 struct bt_ctf_field_path
*field_path
= NULL
;
606 /* Create field path */
607 field_path
= bt_ctf_field_path_create();
609 _printf_error("Cannot create field path\n");
614 /* Convert path string to path tokens */
615 ptokens
= pathstr_to_ptokens(pathstr
);
617 _printf_error("Cannot convert path string \"%s\" to path tokens\n",
623 /* Absolute or relative path? */
624 root_scope
= get_root_scope_from_absolute_pathstr(pathstr
);
626 if (root_scope
== BT_CTF_SCOPE_UNKNOWN
) {
627 /* Relative path: start with current root scope */
628 field_path
->root
= ctx
->root_scope
;
629 ret
= relative_ptokens_to_field_path(ptokens
, field_path
, ctx
);
631 _printf_error("Cannot get relative field path of path string \"%s\"\n",
633 _printf_error(" Starting at root scope %d, finished at root scope %d\n",
634 ctx
->root_scope
, field_path
->root
);
637 } else if (root_scope
== BT_CTF_SCOPE_ENV
) {
638 _printf_error("Sequence field types referring the trace environment are not supported as of this version\n");
642 /* Absolute path: use found root scope */
643 field_path
->root
= root_scope
;
644 ret
= absolute_ptokens_to_field_path(ptokens
, field_path
, ctx
);
646 _printf_error("Cannot get absolute field path of path string \"%s\"\n",
648 _printf_error(" Looking in root scope %d\n", root_scope
);
658 ptokens_destroy(ptokens
);
664 * Retrieves a field type by following the field path `field_path` in
665 * the resolving context `ctx`.
667 * Return value is owned by the caller on success.
670 struct bt_ctf_field_type
*field_path_to_field_type(
671 struct bt_ctf_field_path
*field_path
,
672 struct resolve_context
*ctx
)
675 struct bt_ctf_field_type
*type
;
677 /* Start with root type */
678 type
= get_type_from_ctx(ctx
, field_path
->root
);
681 /* Error: root type is not available */
682 _printf_error("Root type with scope type %d is not available\n",
688 for (i
= 0; i
< field_path
->indexes
->len
; i
++) {
689 struct bt_ctf_field_type
*child_type
;
691 g_array_index(field_path
->indexes
, int, i
);
693 /* Get child field type */
694 child_type
= bt_ctf_field_type_get_field_at_index(type
,
697 _printf_error("Cannot get field type field at index %d\n",
702 /* Move child type to current type */
703 BT_MOVE(type
, child_type
);
714 * Returns the equivalent field path object of the context type stack.
716 * Return value is owned by the caller on success.
719 struct bt_ctf_field_path
*get_ctx_stack_field_path(struct resolve_context
*ctx
)
722 struct bt_ctf_field_path
*field_path
;
724 /* Create field path */
725 field_path
= bt_ctf_field_path_create();
727 _printf_error("Cannot create field path\n");
731 field_path
->root
= ctx
->root_scope
;
733 for (i
= 0; i
< type_stack_size(ctx
->type_stack
); i
++) {
734 struct type_stack_frame
*frame
;
736 frame
= type_stack_at(ctx
->type_stack
, i
);
737 g_array_append_val(field_path
->indexes
, frame
->index
);
748 * Returns the lowest common ancestor of two field path objects
749 * having the same root scope.
751 * `field_path1` and `field_path2` are owned by the caller.
753 int get_field_paths_lca_index(struct bt_ctf_field_path
*field_path1
,
754 struct bt_ctf_field_path
*field_path2
)
757 int field_path1_len
, field_path2_len
;
760 * Start from both roots and find the first mismatch.
762 assert(field_path1
->root
== field_path2
->root
);
763 field_path1_len
= field_path1
->indexes
->len
;
764 field_path2_len
= field_path2
->indexes
->len
;
767 int target_index
, ctx_index
;
769 if (lca_index
== field_path2_len
||
770 lca_index
== field_path1_len
) {
772 * This means that both field paths never split.
773 * This is invalid because the target cannot be
774 * an ancestor of the source.
776 _printf_error("In source and target: one is an ancestor of the other\n");
781 target_index
= g_array_index(field_path1
->indexes
, int,
783 ctx_index
= g_array_index(field_path2
->indexes
, int,
786 if (target_index
!= ctx_index
) {
787 /* LCA index is the previous */
798 * Validates a target field path.
800 * `target_field_path` and `target_type` are owned by the caller.
803 int validate_target_field_path(struct bt_ctf_field_path
*target_field_path
,
804 struct bt_ctf_field_type
*target_type
,
805 struct resolve_context
*ctx
)
808 struct bt_ctf_field_path
*ctx_field_path
;
809 int target_field_path_len
= target_field_path
->indexes
->len
;
811 int ctx_cur_field_type_id
;
814 /* Get context field path */
815 ctx_field_path
= get_ctx_stack_field_path(ctx
);
816 if (!ctx_field_path
) {
817 _printf_error("Cannot get source field path\n");
823 * Make sure the target is not a root.
825 if (target_field_path_len
== 0) {
826 _printf_error("Target field path's length is 0 (targeting the root)\n");
832 * Make sure the root of the target field path is not located
833 * after the context field path's root.
835 if (target_field_path
->root
> ctx_field_path
->root
) {
836 _printf_error("Target is located after source\n");
841 if (target_field_path
->root
== ctx_field_path
->root
) {
842 int target_index
, ctx_index
;
845 * Find the index of the lowest common ancestor of both field
848 lca_index
= get_field_paths_lca_index(target_field_path
,
851 _printf_error("Cannot get least common ancestor\n");
857 * Make sure the target field path is located before the
858 * context field path.
860 target_index
= g_array_index(target_field_path
->indexes
,
862 ctx_index
= g_array_index(ctx_field_path
->indexes
,
865 if (target_index
>= ctx_index
) {
866 _printf_error("Target index (%d) is greater or equal to source index (%d) in LCA\n",
867 target_index
, ctx_index
);
874 * Make sure the target type has the right type and properties.
876 ctx_cur_field_type_id
= bt_ctf_field_type_get_type_id(
877 ctx
->cur_field_type
);
878 target_type_id
= bt_ctf_field_type_get_type_id(target_type
);
880 if (ctx_cur_field_type_id
== CTF_TYPE_VARIANT
) {
881 if (target_type_id
!= CTF_TYPE_ENUM
) {
882 _printf_error("Variant type's tag field type is not an enumeration\n");
886 } else if (ctx_cur_field_type_id
== CTF_TYPE_SEQUENCE
) {
887 if (target_type_id
!= CTF_TYPE_INTEGER
||
888 bt_ctf_field_type_integer_get_signed(
890 _printf_error("Sequence type's length field type is not an unsigned integer\n");
899 BT_PUT(ctx_field_path
);
904 * Resolves a variant or sequence field type `type`.
906 * `type` is owned by the caller.
909 int resolve_sequence_or_variant_type(struct bt_ctf_field_type
*type
,
910 struct resolve_context
*ctx
)
914 int type_id
= bt_ctf_field_type_get_type_id(type
);
915 struct bt_ctf_field_path
*target_field_path
= NULL
;
916 struct bt_ctf_field_type
*target_type
= NULL
;
918 /* Get path string */
920 case CTF_TYPE_SEQUENCE
:
922 bt_ctf_field_type_sequence_get_length_field_name(type
);
924 case CTF_TYPE_VARIANT
:
926 bt_ctf_field_type_variant_get_tag_name(type
);
934 /* Get target field path out of path string */
935 target_field_path
= pathstr_to_field_path(pathstr
, ctx
);
936 if (!target_field_path
) {
937 _printf_error("Cannot get target field path for path string \"%s\"\n",
943 /* Get target field type */
944 target_type
= field_path_to_field_type(target_field_path
, ctx
);
946 _printf_error("Cannot get target field type for path string \"%s\"\n",
952 ret
= validate_target_field_path(target_field_path
, target_type
, ctx
);
954 _printf_error("Invalid target field path for path string \"%s\"\n",
959 /* Set target field path and target field type */
960 if (type_id
== CTF_TYPE_SEQUENCE
) {
961 ret
= bt_ctf_field_type_sequence_set_length_field_path(
962 type
, target_field_path
);
964 _printf_error("Cannot set sequence field type's length field path\n");
967 } else if (type_id
== CTF_TYPE_VARIANT
) {
968 ret
= bt_ctf_field_type_variant_set_tag_field_path(
969 type
, target_field_path
);
971 _printf_error("Cannot set variant field type's tag field path\n");
975 ret
= bt_ctf_field_type_variant_set_tag_field_type(
978 _printf_error("Cannot set variant field type's tag field type\n");
986 BT_PUT(target_field_path
);
992 * Resolves a field type `type`.
994 * `type` is owned by the caller.
997 int resolve_type(struct bt_ctf_field_type
*type
, struct resolve_context
*ctx
)
1003 /* Type is not available; still valid */
1007 type_id
= bt_ctf_field_type_get_type_id(type
);
1008 ctx
->cur_field_type
= type
;
1010 /* Resolve sequence/variant field type */
1012 case CTF_TYPE_SEQUENCE
:
1013 case CTF_TYPE_VARIANT
:
1014 ret
= resolve_sequence_or_variant_type(type
, ctx
);
1016 _printf_error("Cannot resolve sequence or variant field type's length/tag\n");
1024 /* Recurse into compound types */
1026 case CTF_TYPE_STRUCT
:
1027 case CTF_TYPE_VARIANT
:
1028 case CTF_TYPE_SEQUENCE
:
1029 case CTF_TYPE_ARRAY
:
1031 int field_count
, f_index
;
1033 ret
= type_stack_push(ctx
->type_stack
, type
);
1035 _printf_error("Cannot push field type on type stack\n");
1036 _printf_error(" Stack size: %zu\n",
1037 type_stack_size(ctx
->type_stack
));
1041 field_count
= bt_ctf_field_type_get_field_count(type
);
1042 if (field_count
< 0) {
1043 _printf_error("Cannot get field type field count\n");
1048 for (f_index
= 0; f_index
< field_count
; f_index
++) {
1049 struct bt_ctf_field_type
*child_type
=
1050 bt_ctf_field_type_get_field_at_index(type
,
1054 _printf_error("Cannot get field type field at index %d/%d\n",
1055 f_index
, field_count
);
1060 if (type_id
== CTF_TYPE_ARRAY
||
1061 type_id
== CTF_TYPE_SEQUENCE
) {
1062 type_stack_peek(ctx
->type_stack
)->index
= -1;
1064 type_stack_peek(ctx
->type_stack
)->index
=
1068 ret
= resolve_type(child_type
, ctx
);
1075 type_stack_pop(ctx
->type_stack
);
1087 * Resolves the root field type corresponding to the scope `root_scope`.
1090 int resolve_root_type(enum bt_ctf_scope root_scope
, struct resolve_context
*ctx
)
1094 assert(type_stack_size(ctx
->type_stack
) == 0);
1095 ctx
->root_scope
= root_scope
;
1096 ret
= resolve_type(get_type_from_ctx(ctx
, root_scope
), ctx
);
1097 ctx
->root_scope
= BT_CTF_SCOPE_UNKNOWN
;
1103 int bt_ctf_resolve_types(
1104 struct bt_value
*environment
,
1105 struct bt_ctf_field_type
*packet_header_type
,
1106 struct bt_ctf_field_type
*packet_context_type
,
1107 struct bt_ctf_field_type
*event_header_type
,
1108 struct bt_ctf_field_type
*stream_event_ctx_type
,
1109 struct bt_ctf_field_type
*event_context_type
,
1110 struct bt_ctf_field_type
*event_payload_type
,
1111 enum bt_ctf_resolve_flag flags
)
1114 struct resolve_context ctx
= {
1115 .environment
= environment
,
1118 packet_context_type
,
1120 stream_event_ctx_type
,
1124 .root_scope
= BT_CTF_SCOPE_UNKNOWN
,
1127 /* Initialize type stack */
1128 ctx
.type_stack
= type_stack_create();
1129 if (!ctx
.type_stack
) {
1130 printf_error("Cannot create type stack\n");
1135 /* Resolve packet header type */
1136 if (flags
& BT_CTF_RESOLVE_FLAG_PACKET_HEADER
) {
1137 ret
= resolve_root_type(BT_CTF_SCOPE_TRACE_PACKET_HEADER
, &ctx
);
1139 _printf_error("Cannot resolve trace packet header type\n");
1144 /* Resolve packet context type */
1145 if (flags
& BT_CTF_RESOLVE_FLAG_PACKET_CONTEXT
) {
1146 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
, &ctx
);
1148 _printf_error("Cannot resolve stream packet context type\n");
1153 /* Resolve event header type */
1154 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_HEADER
) {
1155 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_HEADER
, &ctx
);
1157 _printf_error("Cannot resolve stream event header type\n");
1162 /* Resolve stream event context type */
1163 if (flags
& BT_CTF_RESOLVE_FLAG_STREAM_EVENT_CTX
) {
1164 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
, &ctx
);
1166 _printf_error("Cannot resolve stream event context type\n");
1171 /* Resolve event context type */
1172 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_CONTEXT
) {
1173 ret
= resolve_root_type(BT_CTF_SCOPE_EVENT_CONTEXT
, &ctx
);
1175 _printf_error("Cannot resolve event context type\n");
1180 /* Resolve event payload type */
1181 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_PAYLOAD
) {
1182 ret
= resolve_root_type(BT_CTF_SCOPE_EVENT_FIELDS
, &ctx
);
1184 _printf_error("Cannot resolve event payload type\n");
1190 type_stack_destroy(ctx
.type_stack
);