2 * SPDX-License-Identifier: MIT
4 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
7 * Babeltrace - CTF writer: Type resolving internal
10 #define BT_LOG_TAG "CTF-WRITER/RESOLVE"
18 #include <babeltrace2-ctf-writer/field-types.h>
19 #include <babeltrace2-ctf-writer/object.h>
20 #include <babeltrace2-ctf-writer/stream-class.h>
21 #include <babeltrace2/types.h>
23 #include "common/macros.h"
24 #include "common/assert.h"
26 #include "field-path.h"
31 typedef GPtrArray type_stack
;
36 * `type` contains a compound field type (structure, variant, array,
37 * or sequence) and `index` indicates the index of the field type in
38 * the upper frame (-1 for array and sequence field types).
40 * `type` is owned by the stack frame.
42 struct type_stack_frame
{
43 struct bt_ctf_field_type_common
*type
;
48 * The current context of the resolving engine.
50 * `scopes` contain the 6 CTF scope field types (see CTF, sect. 7.3.2)
51 * in the following order:
56 * * Stream event context
60 struct resolve_context
{
61 struct bt_ctf_private_value
*environment
;
62 struct bt_ctf_field_type_common
*scopes
[6];
64 /* Root scope being visited */
65 enum bt_ctf_scope root_scope
;
66 type_stack
*type_stack
;
67 struct bt_ctf_field_type_common
*cur_field_type
;
70 /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
71 static const char * const absolute_path_prefixes
[] = {
72 [BT_CTF_SCOPE_ENV
] = "env.",
73 [BT_CTF_SCOPE_TRACE_PACKET_HEADER
] = "trace.packet.header.",
74 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
] = "stream.packet.context.",
75 [BT_CTF_SCOPE_STREAM_EVENT_HEADER
] = "stream.event.header.",
76 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
] = "stream.event.context.",
77 [BT_CTF_SCOPE_EVENT_CONTEXT
] = "event.context.",
78 [BT_CTF_SCOPE_EVENT_FIELDS
] = "event.fields.",
81 /* Number of path tokens used for the absolute prefixes */
82 static const int absolute_path_prefix_ptoken_counts
[] = {
83 [BT_CTF_SCOPE_ENV
] = 1,
84 [BT_CTF_SCOPE_TRACE_PACKET_HEADER
] = 3,
85 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
] = 3,
86 [BT_CTF_SCOPE_STREAM_EVENT_HEADER
] = 3,
87 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
] = 3,
88 [BT_CTF_SCOPE_EVENT_CONTEXT
] = 2,
89 [BT_CTF_SCOPE_EVENT_FIELDS
] = 2,
93 * Destroys a type stack frame.
96 void type_stack_destroy_notify(gpointer data
)
98 struct type_stack_frame
*frame
= data
;
100 BT_CTF_OBJECT_PUT_REF_AND_RESET(frame
->type
);
105 * Creates a type stack.
107 * Return value is owned by the caller.
110 type_stack
*type_stack_create(void)
112 return g_ptr_array_new_with_free_func(type_stack_destroy_notify
);
116 * Destroys a type stack.
119 void type_stack_destroy(type_stack
*stack
)
121 g_ptr_array_free(stack
, TRUE
);
125 * Pushes a field type onto a type stack.
127 * `type` is owned by the caller (stack frame gets a new reference).
130 int type_stack_push(type_stack
*stack
, struct bt_ctf_field_type_common
*type
)
133 struct type_stack_frame
*frame
= NULL
;
135 if (!stack
|| !type
) {
136 BT_LOGW("Invalid parameter: stack or type is NULL.");
141 frame
= g_new0(struct type_stack_frame
, 1);
143 BT_LOGE_STR("Failed to allocate one field type stack frame.");
148 BT_LOGT("Pushing field type on context's stack: "
149 "ft-addr=%p, stack-size-before=%u", type
, stack
->len
);
150 frame
->type
= bt_ctf_object_get_ref(type
);
151 g_ptr_array_add(stack
, frame
);
158 * Checks whether or not `stack` is empty.
161 bt_ctf_bool
type_stack_empty(type_stack
*stack
)
163 return stack
->len
== 0;
167 * Returns the number of frames in `stack`.
170 size_t type_stack_size(type_stack
*stack
)
176 * Returns the top frame of `stack`.
178 * Return value is owned by `stack`.
181 struct type_stack_frame
*type_stack_peek(type_stack
*stack
)
184 BT_ASSERT(!type_stack_empty(stack
));
186 return g_ptr_array_index(stack
, stack
->len
- 1);
190 * Returns the frame at index `index` in `stack`.
192 * Return value is owned by `stack`.
195 struct type_stack_frame
*type_stack_at(type_stack
*stack
, size_t index
)
198 BT_ASSERT(index
< stack
->len
);
200 return g_ptr_array_index(stack
, index
);
204 * Removes the top frame of `stack`.
207 void type_stack_pop(type_stack
*stack
)
209 if (!type_stack_empty(stack
)) {
211 * This will call the frame's destructor and free it, as
212 * well as put its contained field type.
214 BT_LOGT("Popping context's stack: stack-size-before=%u",
216 g_ptr_array_set_size(stack
, stack
->len
- 1);
221 * Returns the scope field type of `scope` in the context `ctx`.
223 * Return value is owned by `ctx` on success.
226 struct bt_ctf_field_type_common
*get_type_from_ctx(struct resolve_context
*ctx
,
227 enum bt_ctf_scope scope
)
229 BT_ASSERT_DBG(scope
>= BT_CTF_SCOPE_TRACE_PACKET_HEADER
&&
230 scope
<= BT_CTF_SCOPE_EVENT_FIELDS
);
232 return ctx
->scopes
[scope
- BT_CTF_SCOPE_TRACE_PACKET_HEADER
];
236 * Returns the CTF scope from a path string. May return
237 * CTF_NODE_UNKNOWN if the path is found to be relative.
240 enum bt_ctf_scope
get_root_scope_from_absolute_pathstr(const char *pathstr
)
242 enum bt_ctf_scope scope
;
243 enum bt_ctf_scope ret
= BT_CTF_SCOPE_UNKNOWN
;
244 const size_t prefixes_count
= sizeof(absolute_path_prefixes
) /
245 sizeof(*absolute_path_prefixes
);
247 for (scope
= BT_CTF_SCOPE_ENV
; scope
< BT_CTF_SCOPE_ENV
+
248 prefixes_count
; scope
++) {
250 * Chech if path string starts with a known absolute
253 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
255 if (strncmp(pathstr
, absolute_path_prefixes
[scope
],
256 strlen(absolute_path_prefixes
[scope
]))) {
257 /* Prefix does not match: try the next one */
258 BT_LOGT("Prefix does not match: trying the next one: "
259 "path=\"%s\", path-prefix=\"%s\", scope=%s",
260 pathstr
, absolute_path_prefixes
[scope
],
261 bt_ctf_scope_string(scope
));
267 BT_LOGT("Found root scope from absolute path: "
268 "path=\"%s\", scope=%s", pathstr
,
269 bt_ctf_scope_string(scope
));
278 * Destroys a path token.
281 void ptokens_destroy_func(gpointer ptoken
, gpointer data
)
283 g_string_free(ptoken
, TRUE
);
287 * Destroys a path token list.
290 void ptokens_destroy(GList
*ptokens
)
296 g_list_foreach(ptokens
, ptokens_destroy_func
, NULL
);
297 g_list_free(ptokens
);
301 * Returns the string contained in a path token.
304 const char *ptoken_get_string(GList
*ptoken
)
306 GString
*tokenstr
= (GString
*) ptoken
->data
;
308 return tokenstr
->str
;
312 * Converts a path string to a path token list, that is, splits the
313 * individual words of a path string into a list of individual
316 * Return value is owned by the caller on success.
319 GList
*pathstr_to_ptokens(const char *pathstr
)
321 const char *at
= pathstr
;
322 const char *last
= at
;
323 GList
*ptokens
= NULL
;
326 if (*at
== '.' || *at
== '\0') {
330 /* Error: empty token */
331 BT_LOGW("Empty path token: path=\"%s\", pos=%u",
332 pathstr
, (int) (at
- pathstr
));
336 tokenstr
= g_string_new(NULL
);
337 g_string_append_len(tokenstr
, last
, at
- last
);
338 ptokens
= g_list_append(ptokens
, tokenstr
);
352 ptokens_destroy(ptokens
);
357 * Converts a path token list to a field path object. The path token
358 * list is relative from `type`. The index of the source looking for
359 * its target within `type` is indicated by `src_index`. This can be
360 * `INT_MAX` if the source is contained in `type`.
362 * `ptokens` is owned by the caller. `field_path` is an output parameter
363 * owned by the caller that must be filled here. `type` is owned by the
367 int ptokens_to_field_path(GList
*ptokens
, struct bt_ctf_field_path
*field_path
,
368 struct bt_ctf_field_type_common
*type
, int src_index
)
371 GList
*cur_ptoken
= ptokens
;
372 bt_ctf_bool first_level_done
= BT_CTF_FALSE
;
374 /* Get our own reference */
375 bt_ctf_object_get_ref(type
);
380 struct bt_ctf_field_type_common
*child_type
;
381 const char *field_name
= ptoken_get_string(cur_ptoken
);
382 enum bt_ctf_field_type_id type_id
=
383 bt_ctf_field_type_common_get_type_id(type
);
385 BT_LOGT("Current path token: token=\"%s\"", field_name
);
387 /* Find to which index corresponds the current path token */
388 if (type_id
== BT_CTF_FIELD_TYPE_ID_ARRAY
||
389 type_id
== BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
392 child_index
= bt_ctf_field_type_common_get_field_index(type
,
394 if (child_index
< 0) {
396 * Error: field name does not exist or
397 * wrong current type.
399 BT_LOGW("Cannot get index of field type: "
400 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
401 field_name
, src_index
, child_index
, first_level_done
);
404 } else if (child_index
> src_index
&&
406 BT_LOGW("Child field type is located after source field type: "
407 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
408 field_name
, src_index
, child_index
, first_level_done
);
413 /* Next path token */
414 cur_ptoken
= g_list_next(cur_ptoken
);
415 first_level_done
= BT_CTF_TRUE
;
418 /* Create new field path entry */
419 g_array_append_val(field_path
->indexes
, child_index
);
421 /* Get child field type */
422 child_type
= bt_ctf_field_type_common_borrow_field_at_index(type
,
425 BT_LOGW("Cannot get child field type: "
426 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
427 field_name
, src_index
, child_index
, first_level_done
);
432 /* Move child type to current type */
433 bt_ctf_object_get_ref(child_type
);
434 BT_CTF_OBJECT_MOVE_REF(type
, child_type
);
438 bt_ctf_object_put_ref(type
);
443 * Converts a known absolute path token list to a field path object
444 * within the resolving context `ctx`.
446 * `ptokens` is owned by the caller. `field_path` is an output parameter
447 * owned by the caller that must be filled here.
450 int absolute_ptokens_to_field_path(GList
*ptokens
,
451 struct bt_ctf_field_path
*field_path
,
452 struct resolve_context
*ctx
)
456 struct bt_ctf_field_type_common
*type
;
458 /* Skip absolute path tokens */
459 cur_ptoken
= g_list_nth(ptokens
,
460 absolute_path_prefix_ptoken_counts
[field_path
->root
]);
462 /* Start with root type */
463 type
= get_type_from_ctx(ctx
, field_path
->root
);
465 /* Error: root type is not available */
466 BT_LOGW("Root field type is not available: "
468 bt_ctf_scope_string(field_path
->root
));
474 ret
= ptokens_to_field_path(cur_ptoken
, field_path
, type
, INT_MAX
);
481 * Converts a known relative path token list to a field path object
482 * within the resolving context `ctx`.
484 * `ptokens` is owned by the caller. `field_path` is an output parameter
485 * owned by the caller that must be filled here.
488 int relative_ptokens_to_field_path(GList
*ptokens
,
489 struct bt_ctf_field_path
*field_path
,
490 struct resolve_context
*ctx
)
493 int parent_pos_in_stack
;
494 struct bt_ctf_field_path
*tail_field_path
= bt_ctf_field_path_create();
496 if (!tail_field_path
) {
497 BT_LOGE_STR("Cannot create empty field path.");
502 parent_pos_in_stack
= type_stack_size(ctx
->type_stack
) - 1;
504 while (parent_pos_in_stack
>= 0) {
505 struct bt_ctf_field_type_common
*parent_type
=
506 type_stack_at(ctx
->type_stack
,
507 parent_pos_in_stack
)->type
;
508 int cur_index
= type_stack_at(ctx
->type_stack
,
509 parent_pos_in_stack
)->index
;
511 BT_LOGT("Locating target field type from current parent field type: "
512 "parent-pos=%d, parent-ft-addr=%p, cur-index=%d",
513 parent_pos_in_stack
, parent_type
, cur_index
);
515 /* Locate target from current parent type */
516 ret
= ptokens_to_field_path(ptokens
, tail_field_path
,
517 parent_type
, cur_index
);
519 /* Not found... yet */
520 BT_LOGT_STR("Not found at this point.");
521 bt_ctf_field_path_clear(tail_field_path
);
523 /* Found: stitch tail field path to head field path */
525 int tail_field_path_len
=
526 tail_field_path
->indexes
->len
;
528 while (BT_CTF_TRUE
) {
529 struct bt_ctf_field_type_common
*cur_type
=
530 type_stack_at(ctx
->type_stack
, i
)->type
;
531 int index
= type_stack_at(
532 ctx
->type_stack
, i
)->index
;
534 if (cur_type
== parent_type
) {
538 g_array_append_val(field_path
->indexes
,
543 for (i
= 0; i
< tail_field_path_len
; i
++) {
544 int index
= bt_g_array_index(
545 tail_field_path
->indexes
,
548 g_array_append_val(field_path
->indexes
,
554 parent_pos_in_stack
--;
557 if (parent_pos_in_stack
< 0) {
558 /* Not found: look in previous scopes */
561 while (field_path
->root
>= BT_CTF_SCOPE_TRACE_PACKET_HEADER
) {
562 struct bt_ctf_field_type_common
*root_type
;
563 bt_ctf_field_path_clear(field_path
);
565 BT_LOGT("Looking into potential root scope: scope=%s",
566 bt_ctf_scope_string(field_path
->root
));
567 root_type
= get_type_from_ctx(ctx
, field_path
->root
);
573 /* Locate target in previous scope */
574 ret
= ptokens_to_field_path(ptokens
, field_path
,
578 BT_LOGT_STR("Not found in this scope.");
584 BT_LOGT_STR("Found in this scope.");
590 BT_CTF_OBJECT_PUT_REF_AND_RESET(tail_field_path
);
595 * Converts a path string to a field path object within the resolving
598 * Return value is owned by the caller on success.
601 struct bt_ctf_field_path
*pathstr_to_field_path(const char *pathstr
,
602 struct resolve_context
*ctx
)
605 enum bt_ctf_scope root_scope
;
606 GList
*ptokens
= NULL
;
607 struct bt_ctf_field_path
*field_path
= NULL
;
609 /* Create field path */
610 field_path
= bt_ctf_field_path_create();
612 BT_LOGE_STR("Cannot create empty field path.");
617 /* Convert path string to path tokens */
618 ptokens
= pathstr_to_ptokens(pathstr
);
620 BT_LOGW("Cannot convert path string to path tokens: "
621 "path=\"%s\"", pathstr
);
626 /* Absolute or relative path? */
627 root_scope
= get_root_scope_from_absolute_pathstr(pathstr
);
629 if (root_scope
== BT_CTF_SCOPE_UNKNOWN
) {
630 /* Relative path: start with current root scope */
631 field_path
->root
= ctx
->root_scope
;
632 BT_LOGT("Detected relative path: starting with current root scope: "
633 "scope=%s", bt_ctf_scope_string(field_path
->root
));
634 ret
= relative_ptokens_to_field_path(ptokens
, field_path
, ctx
);
636 BT_LOGW("Cannot get relative field path of path string: "
637 "path=\"%s\", start-scope=%s, end-scope=%s",
638 pathstr
, bt_ctf_scope_string(ctx
->root_scope
),
639 bt_ctf_scope_string(field_path
->root
));
642 } else if (root_scope
== BT_CTF_SCOPE_ENV
) {
643 BT_LOGW("Sequence field types referring the trace environment are not supported as of this version: "
644 "path=\"%s\"", pathstr
);
648 /* Absolute path: use found root scope */
649 field_path
->root
= root_scope
;
650 BT_LOGT("Detected absolute path: using root scope: "
651 "scope=%s", bt_ctf_scope_string(field_path
->root
));
652 ret
= absolute_ptokens_to_field_path(ptokens
, field_path
, ctx
);
654 BT_LOGW("Cannot get absolute field path of path string: "
655 "path=\"%s\", root-scope=%s",
656 pathstr
, bt_ctf_scope_string(root_scope
));
662 GString
*field_path_pretty
=
663 bt_ctf_field_path_string(field_path
);
664 const char *field_path_pretty_str
=
665 field_path_pretty
? field_path_pretty
->str
: "(null)";
667 BT_LOGT("Found field path: path=\"%s\", field-path=\"%s\"",
668 pathstr
, field_path_pretty_str
);
670 if (field_path_pretty
) {
671 g_string_free(field_path_pretty
, TRUE
);
677 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_path
);
680 ptokens_destroy(ptokens
);
685 * Retrieves a field type by following the field path `field_path` in
686 * the resolving context `ctx`.
688 * Return value is owned by the caller on success.
691 struct bt_ctf_field_type_common
*field_path_to_field_type(
692 struct bt_ctf_field_path
*field_path
,
693 struct resolve_context
*ctx
)
696 struct bt_ctf_field_type_common
*type
;
698 /* Start with root type */
699 type
= get_type_from_ctx(ctx
, field_path
->root
);
700 bt_ctf_object_get_ref(type
);
702 /* Error: root type is not available */
703 BT_LOGW("Root field type is not available: root-scope=%s",
704 bt_ctf_scope_string(field_path
->root
));
709 for (i
= 0; i
< field_path
->indexes
->len
; i
++) {
710 struct bt_ctf_field_type_common
*child_type
;
712 bt_g_array_index(field_path
->indexes
, int, i
);
714 /* Get child field type */
715 child_type
= bt_ctf_field_type_common_borrow_field_at_index(type
,
718 BT_LOGW("Cannot get field type: "
719 "parent-ft-addr=%p, index=%d", type
, i
);
723 /* Move child type to current type */
724 bt_ctf_object_get_ref(child_type
);
725 BT_CTF_OBJECT_MOVE_REF(type
, child_type
);
731 BT_CTF_OBJECT_PUT_REF_AND_RESET(type
);
736 * Returns the equivalent field path object of the context type stack.
738 * Return value is owned by the caller on success.
741 struct bt_ctf_field_path
*get_ctx_stack_field_path(struct resolve_context
*ctx
)
744 struct bt_ctf_field_path
*field_path
;
746 /* Create field path */
747 field_path
= bt_ctf_field_path_create();
749 BT_LOGE_STR("Cannot create empty field path.");
753 field_path
->root
= ctx
->root_scope
;
755 for (i
= 0; i
< type_stack_size(ctx
->type_stack
); i
++) {
756 struct type_stack_frame
*frame
;
758 frame
= type_stack_at(ctx
->type_stack
, i
);
759 g_array_append_val(field_path
->indexes
, frame
->index
);
765 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_path
);
770 * Returns the lowest common ancestor of two field path objects
771 * having the same root scope.
773 * `field_path1` and `field_path2` are owned by the caller.
776 int get_field_paths_lca_index(struct bt_ctf_field_path
*field_path1
,
777 struct bt_ctf_field_path
*field_path2
)
780 int field_path1_len
, field_path2_len
;
782 if (BT_LOG_ON_TRACE
) {
783 GString
*field_path1_pretty
=
784 bt_ctf_field_path_string(field_path1
);
785 GString
*field_path2_pretty
=
786 bt_ctf_field_path_string(field_path2
);
787 const char *field_path1_pretty_str
=
788 field_path1_pretty
? field_path1_pretty
->str
: "(null)";
789 const char *field_path2_pretty_str
=
790 field_path2_pretty
? field_path2_pretty
->str
: "(null)";
792 BT_LOGT("Finding lowest common ancestor (LCA) between two field paths: "
793 "field-path-1=\"%s\", field-path-2=\"%s\"",
794 field_path1_pretty_str
, field_path2_pretty_str
);
796 if (field_path1_pretty
) {
797 g_string_free(field_path1_pretty
, TRUE
);
800 if (field_path2_pretty
) {
801 g_string_free(field_path2_pretty
, TRUE
);
806 * Start from both roots and find the first mismatch.
808 BT_ASSERT_DBG(field_path1
->root
== field_path2
->root
);
809 field_path1_len
= field_path1
->indexes
->len
;
810 field_path2_len
= field_path2
->indexes
->len
;
812 while (BT_CTF_TRUE
) {
813 int target_index
, ctx_index
;
815 if (lca_index
== field_path2_len
||
816 lca_index
== field_path1_len
) {
818 * This means that both field paths never split.
819 * This is invalid because the target cannot be
820 * an ancestor of the source.
822 BT_LOGW("Source field type is an ancestor of target field type or vice versa: "
823 "lca-index=%d, field-path-1-len=%d, "
824 "field-path-2-len=%d",
825 lca_index
, field_path1_len
, field_path2_len
);
830 target_index
= bt_g_array_index(field_path1
->indexes
, int,
832 ctx_index
= bt_g_array_index(field_path2
->indexes
, int,
835 if (target_index
!= ctx_index
) {
836 /* LCA index is the previous */
843 BT_LOGT("Found LCA: lca-index=%d", lca_index
);
848 * Validates a target field path.
850 * `target_field_path` and `target_type` are owned by the caller.
853 int validate_target_field_path(struct bt_ctf_field_path
*target_field_path
,
854 struct bt_ctf_field_type_common
*target_type
,
855 struct resolve_context
*ctx
)
858 struct bt_ctf_field_path
*ctx_field_path
;
859 int target_field_path_len
= target_field_path
->indexes
->len
;
861 enum bt_ctf_field_type_id ctx_cur_field_type_id
;
862 enum bt_ctf_field_type_id target_type_id
;
864 /* Get context field path */
865 ctx_field_path
= get_ctx_stack_field_path(ctx
);
866 if (!ctx_field_path
) {
867 BT_LOGW_STR("Cannot get field path from context's stack.");
873 * Make sure the target is not a root.
875 if (target_field_path_len
== 0) {
876 BT_LOGW_STR("Target field path's length is 0 (targeting the root).");
882 * Make sure the root of the target field path is not located
883 * after the context field path's root.
885 if (target_field_path
->root
> ctx_field_path
->root
) {
886 BT_LOGW("Target field type is located after source field type: "
887 "target-root=%s, source-root=%s",
888 bt_ctf_scope_string(target_field_path
->root
),
889 bt_ctf_scope_string(ctx_field_path
->root
));
894 if (target_field_path
->root
== ctx_field_path
->root
) {
895 int target_index
, ctx_index
;
898 * Find the index of the lowest common ancestor of both field
901 lca_index
= get_field_paths_lca_index(target_field_path
,
904 BT_LOGW_STR("Cannot get least common ancestor.");
910 * Make sure the target field path is located before the
911 * context field path.
913 target_index
= bt_g_array_index(target_field_path
->indexes
,
915 ctx_index
= bt_g_array_index(ctx_field_path
->indexes
,
918 if (target_index
>= ctx_index
) {
919 BT_LOGW("Target field type's index is greater than or equal to source field type's index in LCA: "
920 "lca-index=%d, target-index=%d, source-index=%d",
921 lca_index
, target_index
, ctx_index
);
928 * Make sure the target type has the right type and properties.
930 ctx_cur_field_type_id
= bt_ctf_field_type_common_get_type_id(
931 ctx
->cur_field_type
);
932 target_type_id
= bt_ctf_field_type_common_get_type_id(target_type
);
934 switch (ctx_cur_field_type_id
) {
935 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
936 if (target_type_id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) {
937 BT_LOGW("Variant field type's tag field type is not an enumeration field type: "
938 "tag-ft-addr=%p, tag-ft-id=%s",
940 bt_ctf_field_type_id_string(target_type_id
));
945 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
946 if (target_type_id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
||
947 bt_ctf_field_type_common_integer_is_signed(target_type
)) {
948 BT_LOGW("Sequence field type's length field type is not an unsigned integer field type: "
949 "length-ft-addr=%p, length-ft-id=%s",
951 bt_ctf_field_type_id_string(target_type_id
));
961 BT_CTF_OBJECT_PUT_REF_AND_RESET(ctx_field_path
);
966 * Resolves a variant or sequence field type `type`.
968 * `type` is owned by the caller.
971 int resolve_sequence_or_variant_type(struct bt_ctf_field_type_common
*type
,
972 struct resolve_context
*ctx
)
976 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_common_get_type_id(type
);
977 struct bt_ctf_field_path
*target_field_path
= NULL
;
978 struct bt_ctf_field_type_common
*target_type
= NULL
;
979 GString
*target_field_path_pretty
= NULL
;
980 const char *target_field_path_pretty_str
;
983 /* Get path string */
985 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
987 bt_ctf_field_type_common_sequence_get_length_field_name(type
);
989 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
991 bt_ctf_field_type_common_variant_get_tag_name(type
);
998 BT_LOGW_STR("Cannot get path string.");
1003 /* Get target field path out of path string */
1004 target_field_path
= pathstr_to_field_path(pathstr
, ctx
);
1005 if (!target_field_path
) {
1006 BT_LOGW("Cannot get target field path for path string: "
1007 "path=\"%s\"", pathstr
);
1012 target_field_path_pretty
= bt_ctf_field_path_string(target_field_path
);
1013 target_field_path_pretty_str
=
1014 target_field_path_pretty
? target_field_path_pretty
->str
: NULL
;
1016 /* Get target field type */
1017 target_type
= field_path_to_field_type(target_field_path
, ctx
);
1019 BT_LOGW("Cannot get target field type for path string: "
1020 "path=\"%s\", target-field-path=\"%s\"",
1021 pathstr
, target_field_path_pretty_str
);
1026 ret
= validate_target_field_path(target_field_path
, target_type
, ctx
);
1028 BT_LOGW("Invalid target field path for path string: "
1029 "path=\"%s\", target-field-path=\"%s\"",
1030 pathstr
, target_field_path_pretty_str
);
1034 /* Set target field path and target field type */
1036 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1037 ret
= bt_ctf_field_type_common_sequence_set_length_field_path(
1038 type
, target_field_path
);
1040 BT_LOGW("Cannot set sequence field type's length field path: "
1041 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1043 target_field_path_pretty_str
);
1047 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1048 ret
= bt_ctf_field_type_common_variant_set_tag_field_path(
1049 type
, target_field_path
);
1051 BT_LOGW("Cannot set varaint field type's tag field path: "
1052 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1054 target_field_path_pretty_str
);
1058 ret
= bt_ctf_field_type_common_variant_set_tag_field_type(
1061 BT_LOGW("Cannot set varaint field type's tag field type: "
1062 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1064 target_field_path_pretty_str
);
1073 if (target_field_path_pretty
) {
1074 g_string_free(target_field_path_pretty
, TRUE
);
1077 BT_CTF_OBJECT_PUT_REF_AND_RESET(target_field_path
);
1078 BT_CTF_OBJECT_PUT_REF_AND_RESET(target_type
);
1083 * Resolves a field type `type`.
1085 * `type` is owned by the caller.
1088 int resolve_type(struct bt_ctf_field_type_common
*type
, struct resolve_context
*ctx
)
1091 enum bt_ctf_field_type_id type_id
;
1094 /* Type is not available; still valid */
1098 type_id
= bt_ctf_field_type_common_get_type_id(type
);
1099 ctx
->cur_field_type
= type
;
1101 /* Resolve sequence/variant field type */
1103 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1104 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1105 ret
= resolve_sequence_or_variant_type(type
, ctx
);
1107 BT_LOGW("Cannot resolve sequence field type's length or variant field type's tag: "
1108 "ret=%d, ft-addr=%p", ret
, type
);
1116 /* Recurse into compound types */
1118 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
1119 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
1120 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
1121 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
1123 int64_t field_count
, f_index
;
1125 ret
= type_stack_push(ctx
->type_stack
, type
);
1127 BT_LOGW("Cannot push field type on context's stack: "
1128 "ft-addr=%p", type
);
1132 field_count
= bt_ctf_field_type_common_get_field_count(type
);
1133 if (field_count
< 0) {
1134 BT_LOGW("Cannot get field type's field count: "
1135 "ret=%" PRId64
", ft-addr=%p",
1141 for (f_index
= 0; f_index
< field_count
; f_index
++) {
1142 struct bt_ctf_field_type_common
*child_type
=
1143 bt_ctf_field_type_common_borrow_field_at_index(type
,
1147 BT_LOGW("Cannot get field type's child field: "
1148 "ft-addr=%p, index=%" PRId64
", "
1149 "count=%" PRId64
, type
, f_index
,
1155 if (type_id
== BT_CTF_FIELD_TYPE_ID_ARRAY
||
1156 type_id
== BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
1157 type_stack_peek(ctx
->type_stack
)->index
= -1;
1159 type_stack_peek(ctx
->type_stack
)->index
=
1163 BT_LOGT("Resolving field type's child field type: "
1164 "parent-ft-addr=%p, child-ft-addr=%p, "
1165 "index=%" PRId64
", count=%" PRId64
,
1166 type
, child_type
, f_index
, field_count
);
1167 ret
= resolve_type(child_type
, ctx
);
1173 type_stack_pop(ctx
->type_stack
);
1185 * Resolves the root field type corresponding to the scope `root_scope`.
1188 int resolve_root_type(enum bt_ctf_scope root_scope
, struct resolve_context
*ctx
)
1192 BT_ASSERT_DBG(type_stack_size(ctx
->type_stack
) == 0);
1193 ctx
->root_scope
= root_scope
;
1194 ret
= resolve_type(get_type_from_ctx(ctx
, root_scope
), ctx
);
1195 ctx
->root_scope
= BT_CTF_SCOPE_UNKNOWN
;
1200 int bt_ctf_resolve_types(
1201 struct bt_ctf_private_value
*environment
,
1202 struct bt_ctf_field_type_common
*packet_header_type
,
1203 struct bt_ctf_field_type_common
*packet_context_type
,
1204 struct bt_ctf_field_type_common
*event_header_type
,
1205 struct bt_ctf_field_type_common
*stream_event_ctx_type
,
1206 struct bt_ctf_field_type_common
*event_context_type
,
1207 struct bt_ctf_field_type_common
*event_payload_type
,
1208 enum bt_ctf_resolve_flag flags
)
1211 struct resolve_context ctx
= {
1212 .environment
= environment
,
1215 packet_context_type
,
1217 stream_event_ctx_type
,
1221 .root_scope
= BT_CTF_SCOPE_UNKNOWN
,
1224 BT_LOGT("Resolving field types: "
1225 "packet-header-ft-addr=%p, "
1226 "packet-context-ft-addr=%p, "
1227 "event-header-ft-addr=%p, "
1228 "stream-event-context-ft-addr=%p, "
1229 "event-context-ft-addr=%p, "
1230 "event-payload-ft-addr=%p",
1231 packet_header_type
, packet_context_type
, event_header_type
,
1232 stream_event_ctx_type
, event_context_type
, event_payload_type
);
1234 /* Initialize type stack */
1235 ctx
.type_stack
= type_stack_create();
1236 if (!ctx
.type_stack
) {
1237 BT_LOGE_STR("Cannot create field type stack.");
1242 /* Resolve packet header type */
1243 if (flags
& BT_CTF_RESOLVE_FLAG_PACKET_HEADER
) {
1244 ret
= resolve_root_type(BT_CTF_SCOPE_TRACE_PACKET_HEADER
, &ctx
);
1246 BT_LOGW("Cannot resolve trace packet header field type: "
1252 /* Resolve packet context type */
1253 if (flags
& BT_CTF_RESOLVE_FLAG_PACKET_CONTEXT
) {
1254 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
, &ctx
);
1256 BT_LOGW("Cannot resolve stream packet context field type: "
1262 /* Resolve event header type */
1263 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_HEADER
) {
1264 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_HEADER
, &ctx
);
1266 BT_LOGW("Cannot resolve stream event header field type: "
1272 /* Resolve stream event context type */
1273 if (flags
& BT_CTF_RESOLVE_FLAG_STREAM_EVENT_CTX
) {
1274 ret
= resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
, &ctx
);
1276 BT_LOGW("Cannot resolve stream event context field type: "
1282 /* Resolve event context type */
1283 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_CONTEXT
) {
1284 ret
= resolve_root_type(BT_CTF_SCOPE_EVENT_CONTEXT
, &ctx
);
1286 BT_LOGW("Cannot resolve event context field type: "
1292 /* Resolve event payload type */
1293 if (flags
& BT_CTF_RESOLVE_FLAG_EVENT_PAYLOAD
) {
1294 ret
= resolve_root_type(BT_CTF_SCOPE_EVENT_FIELDS
, &ctx
);
1296 BT_LOGW("Cannot resolve event payload field type: "
1302 BT_LOGT_STR("Resolved field types.");
1305 type_stack_destroy(ctx
.type_stack
);