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>
41 #include <babeltrace/types.h>
45 #define _printf_error(fmt, args...) \
46 printf_verbose("[resolving] " fmt, ## args)
48 typedef GPtrArray type_stack
;
53 * `type` contains a compound field type (structure, variant, array,
54 * or sequence) and `index` indicates the index of the field type in
55 * the upper frame (-1 for array and sequence field types).
57 * `type` is owned by the stack frame.
59 struct type_stack_frame
{
60 struct bt_ctf_field_type
*type
;
65 * The current context of the resolving engine.
67 * `scopes` contain the 6 CTF scope field types (see CTF, sect. 7.3.2)
68 * in the following order:
73 * * Stream event context
77 struct resolve_context
{
78 struct bt_value
*environment
;
79 struct bt_ctf_field_type
*scopes
[6];
81 /* Root scope being visited */
82 enum bt_ctf_scope root_scope
;
83 type_stack
*type_stack
;
84 struct bt_ctf_field_type
*cur_field_type
;
87 /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
88 static const char * const absolute_path_prefixes
[] = {
89 [BT_CTF_SCOPE_ENV
] = "env.",
90 [BT_CTF_SCOPE_TRACE_PACKET_HEADER
] = "trace.packet.header.",
91 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
] = "stream.packet.context.",
92 [BT_CTF_SCOPE_STREAM_EVENT_HEADER
] = "stream.event.header.",
93 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
] = "stream.event.context.",
94 [BT_CTF_SCOPE_EVENT_CONTEXT
] = "event.context.",
95 [BT_CTF_SCOPE_EVENT_FIELDS
] = "event.fields.",
98 /* Number of path tokens used for the absolute prefixes */
99 static const int absolute_path_prefix_ptoken_counts
[] = {
100 [BT_CTF_SCOPE_ENV
] = 1,
101 [BT_CTF_SCOPE_TRACE_PACKET_HEADER
] = 3,
102 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
] = 3,
103 [BT_CTF_SCOPE_STREAM_EVENT_HEADER
] = 3,
104 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
] = 3,
105 [BT_CTF_SCOPE_EVENT_CONTEXT
] = 2,
106 [BT_CTF_SCOPE_EVENT_FIELDS
] = 2,
110 * Destroys a type stack frame.
113 void type_stack_destroy_notify(gpointer data
)
115 struct type_stack_frame
*frame
= data
;
122 * Creates a type stack.
124 * Return value is owned by the caller.
127 type_stack
*type_stack_create(void)
129 return g_ptr_array_new_with_free_func(type_stack_destroy_notify
);
133 * Destroys a type stack.
136 void type_stack_destroy(type_stack
*stack
)
138 g_ptr_array_free(stack
, TRUE
);
142 * Pushes a field type onto a type stack.
144 * `type` is owned by the caller (stack frame gets a new reference).
147 int type_stack_push(type_stack
*stack
, struct bt_ctf_field_type
*type
)
150 struct type_stack_frame
*frame
= NULL
;
152 if (!stack
|| !type
) {
157 frame
= g_new0(struct type_stack_frame
, 1);
163 frame
->type
= bt_get(type
);
164 g_ptr_array_add(stack
, frame
);
171 * Checks whether or not `stack` is empty.
174 bt_bool
type_stack_empty(type_stack
*stack
)
176 return stack
->len
== 0;
180 * Returns the number of frames in `stack`.
183 size_t type_stack_size(type_stack
*stack
)
189 * Returns the top frame of `stack`.
191 * Return value is owned by `stack`.
194 struct type_stack_frame
*type_stack_peek(type_stack
*stack
)
196 struct type_stack_frame
*entry
= NULL
;
198 if (!stack
|| type_stack_empty(stack
)) {
202 entry
= g_ptr_array_index(stack
, stack
->len
- 1);
208 * Returns the frame at index `index` in `stack`.
210 * Return value is owned by `stack`.
213 struct type_stack_frame
*type_stack_at(type_stack
*stack
,
216 struct type_stack_frame
*entry
= NULL
;
218 if (!stack
|| index
>= stack
->len
) {
222 entry
= g_ptr_array_index(stack
, index
);
229 * Removes the top frame of `stack`.
232 void type_stack_pop(type_stack
*stack
)
234 if (!type_stack_empty(stack
)) {
236 * This will call the frame's destructor and free it, as
237 * well as put its contained field type.
239 g_ptr_array_set_size(stack
, stack
->len
- 1);
244 * Returns the scope field type of `scope` in the context `ctx`.
246 * Return value is owned by `ctx` on success.
249 struct bt_ctf_field_type
*get_type_from_ctx(struct resolve_context
*ctx
,
250 enum bt_ctf_scope scope
)
252 assert(scope
>= BT_CTF_SCOPE_TRACE_PACKET_HEADER
&&
253 scope
<= BT_CTF_SCOPE_EVENT_FIELDS
);
255 return ctx
->scopes
[scope
- BT_CTF_SCOPE_TRACE_PACKET_HEADER
];
259 * Returns the CTF scope from a path string. May return
260 * CTF_NODE_UNKNOWN if the path is found to be relative.
263 enum bt_ctf_scope
get_root_scope_from_absolute_pathstr(const char *pathstr
)
265 enum bt_ctf_scope scope
;
266 enum bt_ctf_scope ret
= BT_CTF_SCOPE_UNKNOWN
;
267 const size_t prefixes_count
= sizeof(absolute_path_prefixes
) /
268 sizeof(*absolute_path_prefixes
);
270 for (scope
= BT_CTF_SCOPE_ENV
; scope
< BT_CTF_SCOPE_ENV
+
271 prefixes_count
; scope
++) {
273 * Chech if path string starts with a known absolute
276 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
278 if (strncmp(pathstr
, absolute_path_prefixes
[scope
],
279 strlen(absolute_path_prefixes
[scope
]))) {
280 /* Prefix does not match: try the next one */
294 * Destroys a path token.
297 void ptokens_destroy_func(gpointer ptoken
, gpointer data
)
299 g_string_free(ptoken
, TRUE
);
303 * Destroys a path token list.
306 void ptokens_destroy(GList
*ptokens
)
312 g_list_foreach(ptokens
, ptokens_destroy_func
, NULL
);
313 g_list_free(ptokens
);
317 * Returns the string contained in a path token.
320 const char *ptoken_get_string(GList
*ptoken
)
322 GString
*tokenstr
= (GString
*) ptoken
->data
;
324 return tokenstr
->str
;
328 * Converts a path string to a path token list, that is, splits the
329 * individual words of a path string into a list of individual
332 * Return value is owned by the caller on success.
335 GList
*pathstr_to_ptokens(const char *pathstr
)
337 const char *at
= pathstr
;
338 const char *last
= at
;
339 GList
*ptokens
= NULL
;
342 if (*at
== '.' || *at
== '\0') {
346 /* Error: empty token */
347 _printf_error("Empty token in path string at position %d\n",
348 (int) (at
- pathstr
));
352 tokenstr
= g_string_new(NULL
);
353 g_string_append_len(tokenstr
, last
, at
- last
);
354 ptokens
= g_list_append(ptokens
, tokenstr
);
368 ptokens_destroy(ptokens
);
373 * Converts a path token list to a field path object. The path token
374 * list is relative from `type`. The index of the source looking for
375 * its target within `type` is indicated by `src_index`. This can be
376 * `INT_MAX` if the source is contained in `type`.
378 * `ptokens` is owned by the caller. `field_path` is an output parameter
379 * owned by the caller that must be filled here. `type` is owned by the
383 int ptokens_to_field_path(GList
*ptokens
, struct bt_ctf_field_path
*field_path
,
384 struct bt_ctf_field_type
*type
, int src_index
)
387 GList
*cur_ptoken
= ptokens
;
388 bt_bool first_level_done
= BT_FALSE
;
390 /* Get our own reference */
396 struct bt_ctf_field_type
*child_type
;
397 const char *field_name
= ptoken_get_string(cur_ptoken
);
398 enum bt_ctf_field_type_id type_id
=
399 bt_ctf_field_type_get_type_id(type
);
401 /* Find to which index corresponds the current path token */
402 if (type_id
== CTF_TYPE_ARRAY
|| type_id
== CTF_TYPE_SEQUENCE
) {
405 child_index
= bt_ctf_field_type_get_field_index(type
,
407 if (child_index
< 0) {
409 * Error: field name does not exist or
410 * wrong current type.
412 _printf_error("Cannot get index of field type named \"%s\"\n",
416 } else if (child_index
> src_index
&&
418 _printf_error("Child type is located after source index (%d)\n",
424 /* Next path token */
425 cur_ptoken
= g_list_next(cur_ptoken
);
426 first_level_done
= BT_TRUE
;
429 /* Create new field path entry */
430 g_array_append_val(field_path
->indexes
, child_index
);
432 /* Get child field type */
433 child_type
= bt_ctf_field_type_get_field_at_index(type
,
436 _printf_error("Cannot get child type at index %d (field \"%s\")\n",
437 child_index
, field_name
);
442 /* Move child type to current type */
443 BT_MOVE(type
, child_type
);
452 * Converts a known absolute path token list to a field path object
453 * within the resolving context `ctx`.
455 * `ptokens` is owned by the caller. `field_path` is an output parameter
456 * owned by the caller that must be filled here.
459 int absolute_ptokens_to_field_path(GList
*ptokens
,
460 struct bt_ctf_field_path
*field_path
,
461 struct resolve_context
*ctx
)
465 struct bt_ctf_field_type
*type
;
467 /* Skip absolute path tokens */
468 cur_ptoken
= g_list_nth(ptokens
,
469 absolute_path_prefix_ptoken_counts
[field_path
->root
]);
471 /* Start with root type */
472 type
= get_type_from_ctx(ctx
, field_path
->root
);
474 /* Error: root type is not available */
475 _printf_error("Root type with scope type %d is not available\n",
482 ret
= ptokens_to_field_path(cur_ptoken
, field_path
, type
, INT_MAX
);
489 * Converts a known relative path token list to a field path object
490 * within the resolving context `ctx`.
492 * `ptokens` is owned by the caller. `field_path` is an output parameter
493 * owned by the caller that must be filled here.
496 int relative_ptokens_to_field_path(GList
*ptokens
,
497 struct bt_ctf_field_path
*field_path
,
498 struct resolve_context
*ctx
)
501 int parent_pos_in_stack
;
502 struct bt_ctf_field_path
*tail_field_path
= bt_ctf_field_path_create();
504 if (!tail_field_path
) {
505 _printf_error("Cannot create field path\n");
510 parent_pos_in_stack
= type_stack_size(ctx
->type_stack
) - 1;
512 while (parent_pos_in_stack
>= 0) {
513 struct bt_ctf_field_type
*parent_type
=
514 type_stack_at(ctx
->type_stack
,
515 parent_pos_in_stack
)->type
;
516 int cur_index
= type_stack_at(ctx
->type_stack
,
517 parent_pos_in_stack
)->index
;
519 /* Locate target from current parent type */
520 ret
= ptokens_to_field_path(ptokens
, tail_field_path
,
521 parent_type
, cur_index
);
523 /* Not found... yet */
524 bt_ctf_field_path_clear(tail_field_path
);
526 /* Found: stitch tail field path to head field path */
528 int tail_field_path_len
=
529 tail_field_path
->indexes
->len
;
532 struct bt_ctf_field_type
*cur_type
=
533 type_stack_at(ctx
->type_stack
, i
)->type
;
534 int index
= type_stack_at(
535 ctx
->type_stack
, i
)->index
;
537 if (cur_type
== parent_type
) {
541 g_array_append_val(field_path
->indexes
,
546 for (i
= 0; i
< tail_field_path_len
; i
++) {
547 int index
= g_array_index(
548 tail_field_path
->indexes
,
551 g_array_append_val(field_path
->indexes
,
557 parent_pos_in_stack
--;
560 if (parent_pos_in_stack
< 0) {
561 /* Not found: look in previous scopes */
564 while (field_path
->root
>= BT_CTF_SCOPE_TRACE_PACKET_HEADER
) {
565 struct bt_ctf_field_type
*root_type
;
566 bt_ctf_field_path_clear(field_path
);
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
,
589 BT_PUT(tail_field_path
);
594 * Converts a path string to a field path object within the resolving
597 * Return value is owned by the caller on success.
600 struct bt_ctf_field_path
*pathstr_to_field_path(const char *pathstr
,
601 struct resolve_context
*ctx
)
604 enum bt_ctf_scope root_scope
;
605 GList
*ptokens
= NULL
;
606 struct bt_ctf_field_path
*field_path
= NULL
;
608 /* Create field path */
609 field_path
= bt_ctf_field_path_create();
611 _printf_error("Cannot create field path\n");
616 /* Convert path string to path tokens */
617 ptokens
= pathstr_to_ptokens(pathstr
);
619 _printf_error("Cannot convert path string \"%s\" to path tokens\n",
625 /* Absolute or relative path? */
626 root_scope
= get_root_scope_from_absolute_pathstr(pathstr
);
628 if (root_scope
== BT_CTF_SCOPE_UNKNOWN
) {
629 /* Relative path: start with current root scope */
630 field_path
->root
= ctx
->root_scope
;
631 ret
= relative_ptokens_to_field_path(ptokens
, field_path
, ctx
);
633 _printf_error("Cannot get relative field path of path string \"%s\"\n",
635 _printf_error(" Starting at root scope %d, finished at root scope %d\n",
636 ctx
->root_scope
, field_path
->root
);
639 } else if (root_scope
== BT_CTF_SCOPE_ENV
) {
640 _printf_error("Sequence field types referring the trace environment are not supported as of this version\n");
644 /* Absolute path: use found root scope */
645 field_path
->root
= root_scope
;
646 ret
= absolute_ptokens_to_field_path(ptokens
, field_path
, ctx
);
648 _printf_error("Cannot get absolute field path of path string \"%s\"\n",
650 _printf_error(" Looking in root scope %d\n", root_scope
);
660 ptokens_destroy(ptokens
);
666 * Retrieves a field type by following the field path `field_path` in
667 * the resolving context `ctx`.
669 * Return value is owned by the caller on success.
672 struct bt_ctf_field_type
*field_path_to_field_type(
673 struct bt_ctf_field_path
*field_path
,
674 struct resolve_context
*ctx
)
677 struct bt_ctf_field_type
*type
;
679 /* Start with root type */
680 type
= get_type_from_ctx(ctx
, field_path
->root
);
683 /* Error: root type is not available */
684 _printf_error("Root type with scope type %d is not available\n",
690 for (i
= 0; i
< field_path
->indexes
->len
; i
++) {
691 struct bt_ctf_field_type
*child_type
;
693 g_array_index(field_path
->indexes
, int, i
);
695 /* Get child field type */
696 child_type
= bt_ctf_field_type_get_field_at_index(type
,
699 _printf_error("Cannot get field type field at index %d\n",
704 /* Move child type to current type */
705 BT_MOVE(type
, child_type
);
716 * Returns the equivalent field path object of the context type stack.
718 * Return value is owned by the caller on success.
721 struct bt_ctf_field_path
*get_ctx_stack_field_path(struct resolve_context
*ctx
)
724 struct bt_ctf_field_path
*field_path
;
726 /* Create field path */
727 field_path
= bt_ctf_field_path_create();
729 _printf_error("Cannot create field path\n");
733 field_path
->root
= ctx
->root_scope
;
735 for (i
= 0; i
< type_stack_size(ctx
->type_stack
); i
++) {
736 struct type_stack_frame
*frame
;
738 frame
= type_stack_at(ctx
->type_stack
, i
);
739 g_array_append_val(field_path
->indexes
, frame
->index
);
750 * Returns the lowest common ancestor of two field path objects
751 * having the same root scope.
753 * `field_path1` and `field_path2` are owned by the caller.
755 int get_field_paths_lca_index(struct bt_ctf_field_path
*field_path1
,
756 struct bt_ctf_field_path
*field_path2
)
759 int field_path1_len
, field_path2_len
;
762 * Start from both roots and find the first mismatch.
764 assert(field_path1
->root
== field_path2
->root
);
765 field_path1_len
= field_path1
->indexes
->len
;
766 field_path2_len
= field_path2
->indexes
->len
;
769 int target_index
, ctx_index
;
771 if (lca_index
== field_path2_len
||
772 lca_index
== field_path1_len
) {
774 * This means that both field paths never split.
775 * This is invalid because the target cannot be
776 * an ancestor of the source.
778 _printf_error("In source and target: one is an ancestor of the other\n");
783 target_index
= g_array_index(field_path1
->indexes
, int,
785 ctx_index
= g_array_index(field_path2
->indexes
, int,
788 if (target_index
!= ctx_index
) {
789 /* LCA index is the previous */
800 * Validates a target field path.
802 * `target_field_path` and `target_type` are owned by the caller.
805 int validate_target_field_path(struct bt_ctf_field_path
*target_field_path
,
806 struct bt_ctf_field_type
*target_type
,
807 struct resolve_context
*ctx
)
810 struct bt_ctf_field_path
*ctx_field_path
;
811 int target_field_path_len
= target_field_path
->indexes
->len
;
813 int ctx_cur_field_type_id
;
816 /* Get context field path */
817 ctx_field_path
= get_ctx_stack_field_path(ctx
);
818 if (!ctx_field_path
) {
819 _printf_error("Cannot get source field path\n");
825 * Make sure the target is not a root.
827 if (target_field_path_len
== 0) {
828 _printf_error("Target field path's length is 0 (targeting the root)\n");
834 * Make sure the root of the target field path is not located
835 * after the context field path's root.
837 if (target_field_path
->root
> ctx_field_path
->root
) {
838 _printf_error("Target is located after source\n");
843 if (target_field_path
->root
== ctx_field_path
->root
) {
844 int target_index
, ctx_index
;
847 * Find the index of the lowest common ancestor of both field
850 lca_index
= get_field_paths_lca_index(target_field_path
,
853 _printf_error("Cannot get least common ancestor\n");
859 * Make sure the target field path is located before the
860 * context field path.
862 target_index
= g_array_index(target_field_path
->indexes
,
864 ctx_index
= g_array_index(ctx_field_path
->indexes
,
867 if (target_index
>= ctx_index
) {
868 _printf_error("Target index (%d) is greater or equal to source index (%d) in LCA\n",
869 target_index
, ctx_index
);
876 * Make sure the target type has the right type and properties.
878 ctx_cur_field_type_id
= bt_ctf_field_type_get_type_id(
879 ctx
->cur_field_type
);
880 target_type_id
= bt_ctf_field_type_get_type_id(target_type
);
882 if (ctx_cur_field_type_id
== CTF_TYPE_VARIANT
) {
883 if (target_type_id
!= CTF_TYPE_ENUM
) {
884 _printf_error("Variant type's tag field type is not an enumeration\n");
888 } else if (ctx_cur_field_type_id
== CTF_TYPE_SEQUENCE
) {
889 if (target_type_id
!= CTF_TYPE_INTEGER
||
890 bt_ctf_field_type_integer_get_signed(
892 _printf_error("Sequence type's length field type is not an unsigned integer\n");
901 BT_PUT(ctx_field_path
);
906 * Resolves a variant or sequence field type `type`.
908 * `type` is owned by the caller.
911 int resolve_sequence_or_variant_type(struct bt_ctf_field_type
*type
,
912 struct resolve_context
*ctx
)
916 int type_id
= bt_ctf_field_type_get_type_id(type
);
917 struct bt_ctf_field_path
*target_field_path
= NULL
;
918 struct bt_ctf_field_type
*target_type
= NULL
;
920 /* Get path string */
922 case CTF_TYPE_SEQUENCE
:
924 bt_ctf_field_type_sequence_get_length_field_name(type
);
926 case CTF_TYPE_VARIANT
:
928 bt_ctf_field_type_variant_get_tag_name(type
);
936 /* Get target field path out of path string */
937 target_field_path
= pathstr_to_field_path(pathstr
, ctx
);
938 if (!target_field_path
) {
939 _printf_error("Cannot get target field path for path string \"%s\"\n",
945 /* Get target field type */
946 target_type
= field_path_to_field_type(target_field_path
, ctx
);
948 _printf_error("Cannot get target field type for path string \"%s\"\n",
954 ret
= validate_target_field_path(target_field_path
, target_type
, ctx
);
956 _printf_error("Invalid target field path for path string \"%s\"\n",
961 /* Set target field path and target field type */
962 if (type_id
== CTF_TYPE_SEQUENCE
) {
963 ret
= bt_ctf_field_type_sequence_set_length_field_path(
964 type
, target_field_path
);
966 _printf_error("Cannot set sequence field type's length field path\n");
969 } else if (type_id
== CTF_TYPE_VARIANT
) {
970 ret
= bt_ctf_field_type_variant_set_tag_field_path(
971 type
, target_field_path
);
973 _printf_error("Cannot set variant field type's tag field path\n");
977 ret
= bt_ctf_field_type_variant_set_tag_field_type(
980 _printf_error("Cannot set variant field type's tag field type\n");
988 BT_PUT(target_field_path
);
994 * Resolves a field type `type`.
996 * `type` is owned by the caller.
999 int resolve_type(struct bt_ctf_field_type
*type
, struct resolve_context
*ctx
)
1005 /* Type is not available; still valid */
1009 type_id
= bt_ctf_field_type_get_type_id(type
);
1010 ctx
->cur_field_type
= type
;
1012 /* Resolve sequence/variant field type */
1014 case CTF_TYPE_SEQUENCE
:
1015 case CTF_TYPE_VARIANT
:
1016 ret
= resolve_sequence_or_variant_type(type
, ctx
);
1018 _printf_error("Cannot resolve sequence or variant field type's length/tag\n");
1026 /* Recurse into compound types */
1028 case CTF_TYPE_STRUCT
:
1029 case CTF_TYPE_VARIANT
:
1030 case CTF_TYPE_SEQUENCE
:
1031 case CTF_TYPE_ARRAY
:
1033 int field_count
, f_index
;
1035 ret
= type_stack_push(ctx
->type_stack
, type
);
1037 _printf_error("Cannot push field type on type stack\n");
1038 _printf_error(" Stack size: %zu\n",
1039 type_stack_size(ctx
->type_stack
));
1043 field_count
= bt_ctf_field_type_get_field_count(type
);
1044 if (field_count
< 0) {
1045 _printf_error("Cannot get field type field count\n");
1050 for (f_index
= 0; f_index
< field_count
; f_index
++) {
1051 struct bt_ctf_field_type
*child_type
=
1052 bt_ctf_field_type_get_field_at_index(type
,
1056 _printf_error("Cannot get field type field at index %d/%d\n",
1057 f_index
, field_count
);
1062 if (type_id
== CTF_TYPE_ARRAY
||
1063 type_id
== CTF_TYPE_SEQUENCE
) {
1064 type_stack_peek(ctx
->type_stack
)->index
= -1;
1066 type_stack_peek(ctx
->type_stack
)->index
=
1070 ret
= resolve_type(child_type
, ctx
);
1077 type_stack_pop(ctx
->type_stack
);
1089 * Resolves the root field type corresponding to the scope `root_scope`.
1092 int resolve_root_type(enum bt_ctf_scope root_scope
, struct resolve_context
*ctx
)
1096 assert(type_stack_size(ctx
->type_stack
) == 0);
1097 ctx
->root_scope
= root_scope
;
1098 ret
= resolve_type(get_type_from_ctx(ctx
, root_scope
), ctx
);
1099 ctx
->root_scope
= BT_CTF_SCOPE_UNKNOWN
;
1105 int bt_ctf_resolve_types(
1106 struct bt_value
*environment
,
1107 struct bt_ctf_field_type
*packet_header_type
,
1108 struct bt_ctf_field_type
*packet_context_type
,
1109 struct bt_ctf_field_type
*event_header_type
,
1110 struct bt_ctf_field_type
*stream_event_ctx_type
,
1111 struct bt_ctf_field_type
*event_context_type
,
1112 struct bt_ctf_field_type
*event_payload_type
,
1113 enum bt_ctf_resolve_flag flags
)
1116 struct resolve_context ctx
= {
1117 .environment
= environment
,
1120 packet_context_type
,
1122 stream_event_ctx_type
,
1126 .root_scope
= BT_CTF_SCOPE_UNKNOWN
,
1129 /* Initialize type stack */
1130 ctx
.type_stack
= type_stack_create();
1131 if (!ctx
.type_stack
) {
1132 printf_error("Cannot create type stack\n");
1137 /* Resolve packet header type */
1138 if (flags
& BT_CTF_RESOLVE_FLAG_PACKET_HEADER
) {
1139 ret
= resolve_root_type(BT_CTF_SCOPE_TRACE_PACKET_HEADER
, &ctx
);
1141 _printf_error("Cannot resolve trace packet header type\n");
1146 /* Resolve packet context type */
1147 if (flags
& BT_CTF_RESOLVE_FLAG_PACKET_CONTEXT
) {
1148 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
, &ctx
);
1150 _printf_error("Cannot resolve stream packet context type\n");
1155 /* Resolve event header type */
1156 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_HEADER
) {
1157 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_HEADER
, &ctx
);
1159 _printf_error("Cannot resolve stream event header type\n");
1164 /* Resolve stream event context type */
1165 if (flags
& BT_CTF_RESOLVE_FLAG_STREAM_EVENT_CTX
) {
1166 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
, &ctx
);
1168 _printf_error("Cannot resolve stream event context type\n");
1173 /* Resolve event context type */
1174 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_CONTEXT
) {
1175 ret
= resolve_root_type(BT_CTF_SCOPE_EVENT_CONTEXT
, &ctx
);
1177 _printf_error("Cannot resolve event context type\n");
1182 /* Resolve event payload type */
1183 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_PAYLOAD
) {
1184 ret
= resolve_root_type(BT_CTF_SCOPE_EVENT_FIELDS
, &ctx
);
1186 _printf_error("Cannot resolve event payload type\n");
1192 type_stack_destroy(ctx
.type_stack
);