2 * SPDX-License-Identifier: MIT
4 * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_COMP_LOG_SELF_COMP (ctx->self_comp)
9 #define BT_LOG_OUTPUT_LEVEL (ctx->log_level)
10 #define BT_LOG_TAG "PLUGIN/CTF/META/RESOLVE"
11 #include "logging/comp-logging.h"
13 #include <babeltrace2/babeltrace.h>
14 #include "common/macros.h"
15 #include "common/assert.h"
16 #include "common/common.h"
26 #include "ctf-meta-visitors.h"
29 typedef GPtrArray field_class_stack
;
34 * `fc` contains a compound field class (structure, variant, array,
35 * or sequence) and `index` indicates the index of the field class in
36 * the upper frame (-1 for array and sequence field classes). `name`
37 * indicates the name of the field class in the upper frame (empty
38 * string for array and sequence field classes).
40 struct field_class_stack_frame
{
41 struct ctf_field_class
*fc
;
46 * The current context of the resolving engine.
48 struct resolve_context
{
49 bt_logging_level log_level
;
50 bt_self_component
*self_comp
;
51 struct ctf_trace_class
*tc
;
52 struct ctf_stream_class
*sc
;
53 struct ctf_event_class
*ec
;
56 struct ctf_field_class
*packet_header
;
57 struct ctf_field_class
*packet_context
;
58 struct ctf_field_class
*event_header
;
59 struct ctf_field_class
*event_common_context
;
60 struct ctf_field_class
*event_spec_context
;
61 struct ctf_field_class
*event_payload
;
64 /* Root scope being visited */
65 enum ctf_scope root_scope
;
66 field_class_stack
*field_class_stack
;
67 struct ctf_field_class
*cur_fc
;
70 /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
71 static const char * const absolute_path_prefixes
[] = {
72 [CTF_SCOPE_PACKET_HEADER
] = "trace.packet.header.",
73 [CTF_SCOPE_PACKET_CONTEXT
] = "stream.packet.context.",
74 [CTF_SCOPE_EVENT_HEADER
] = "stream.event.header.",
75 [CTF_SCOPE_EVENT_COMMON_CONTEXT
] = "stream.event.context.",
76 [CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
] = "event.context.",
77 [CTF_SCOPE_EVENT_PAYLOAD
] = "event.fields.",
80 /* Number of path tokens used for the absolute prefixes */
81 static const uint64_t absolute_path_prefix_ptoken_counts
[] = {
82 [CTF_SCOPE_PACKET_HEADER
] = 3,
83 [CTF_SCOPE_PACKET_CONTEXT
] = 3,
84 [CTF_SCOPE_EVENT_HEADER
] = 3,
85 [CTF_SCOPE_EVENT_COMMON_CONTEXT
] = 3,
86 [CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
] = 2,
87 [CTF_SCOPE_EVENT_PAYLOAD
] = 2,
91 void destroy_field_class_stack_frame(struct field_class_stack_frame
*frame
)
101 * Creates a class stack.
104 field_class_stack
*field_class_stack_create(void)
106 return g_ptr_array_new_with_free_func(
107 (GDestroyNotify
) destroy_field_class_stack_frame
);
111 * Destroys a class stack.
114 void field_class_stack_destroy(field_class_stack
*stack
)
117 g_ptr_array_free(stack
, TRUE
);
122 * Pushes a field class onto a class stack.
125 int field_class_stack_push(field_class_stack
*stack
, struct ctf_field_class
*fc
,
126 struct resolve_context
*ctx
)
129 struct field_class_stack_frame
*frame
= NULL
;
132 BT_COMP_LOGE("Invalid parameter: stack or field class is NULL.");
137 frame
= g_new0(struct field_class_stack_frame
, 1);
139 BT_COMP_LOGE_STR("Failed to allocate one field class stack frame.");
144 BT_COMP_LOGD("Pushing field class on context's stack: "
145 "fc-addr=%p, stack-size-before=%u", fc
, stack
->len
);
147 g_ptr_array_add(stack
, frame
);
154 * Checks whether or not `stack` is empty.
157 bool field_class_stack_empty(field_class_stack
*stack
)
159 return stack
->len
== 0;
163 * Returns the number of frames in `stack`.
166 size_t field_class_stack_size(field_class_stack
*stack
)
172 * Returns the top frame of `stack`.
175 struct field_class_stack_frame
*field_class_stack_peek(field_class_stack
*stack
)
178 BT_ASSERT(!field_class_stack_empty(stack
));
180 return g_ptr_array_index(stack
, stack
->len
- 1);
184 * Returns the frame at index `index` in `stack`.
187 struct field_class_stack_frame
*field_class_stack_at(field_class_stack
*stack
,
191 BT_ASSERT(index
< stack
->len
);
193 return g_ptr_array_index(stack
, index
);
197 * Removes the top frame of `stack`.
200 void field_class_stack_pop(field_class_stack
*stack
,
201 struct resolve_context
*ctx
)
203 if (!field_class_stack_empty(stack
)) {
205 * This will call the frame's destructor and free it, as
206 * well as put its contained field class.
208 BT_COMP_LOGD("Popping context's stack: stack-size-before=%u",
210 g_ptr_array_set_size(stack
, stack
->len
- 1);
215 * Returns the scope field class of `scope` in the context `ctx`.
218 struct ctf_field_class
*borrow_class_from_ctx(struct resolve_context
*ctx
,
219 enum ctf_scope scope
)
222 case CTF_SCOPE_PACKET_HEADER
:
223 return ctx
->scopes
.packet_header
;
224 case CTF_SCOPE_PACKET_CONTEXT
:
225 return ctx
->scopes
.packet_context
;
226 case CTF_SCOPE_EVENT_HEADER
:
227 return ctx
->scopes
.event_header
;
228 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
229 return ctx
->scopes
.event_common_context
;
230 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
231 return ctx
->scopes
.event_spec_context
;
232 case CTF_SCOPE_EVENT_PAYLOAD
:
233 return ctx
->scopes
.event_payload
;
242 * Returns the CTF scope from a path string. May return -1 if the path
243 * is found to be relative.
246 enum ctf_scope
get_root_scope_from_absolute_pathstr(const char *pathstr
,
247 struct resolve_context
*ctx
)
249 enum ctf_scope scope
;
250 enum ctf_scope ret
= CTF_SCOPE_PACKET_UNKNOWN
;
251 const size_t prefixes_count
= sizeof(absolute_path_prefixes
) /
252 sizeof(*absolute_path_prefixes
);
254 for (scope
= CTF_SCOPE_PACKET_HEADER
; scope
< CTF_SCOPE_PACKET_HEADER
+
255 prefixes_count
; scope
++) {
257 * Check if path string starts with a known absolute
260 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
262 if (strncmp(pathstr
, absolute_path_prefixes
[scope
],
263 strlen(absolute_path_prefixes
[scope
]))) {
264 /* Prefix does not match: try the next one */
265 BT_COMP_LOGD("Prefix does not match: trying the next one: "
266 "path=\"%s\", path-prefix=\"%s\", scope=%s",
267 pathstr
, absolute_path_prefixes
[scope
],
268 ctf_scope_string(scope
));
274 BT_COMP_LOGD("Found root scope from absolute path: "
275 "path=\"%s\", scope=%s", pathstr
,
276 ctf_scope_string(scope
));
285 * Destroys a path token.
288 void ptokens_destroy_func(gpointer ptoken
, gpointer data
)
290 g_string_free(ptoken
, TRUE
);
294 * Destroys a path token list.
297 void ptokens_destroy(GList
*ptokens
)
303 g_list_foreach(ptokens
, ptokens_destroy_func
, NULL
);
304 g_list_free(ptokens
);
308 * Returns the string contained in a path token.
311 const char *ptoken_get_string(GList
*ptoken
)
313 GString
*tokenstr
= (GString
*) ptoken
->data
;
315 return tokenstr
->str
;
319 * Converts a path string to a path token list, that is, splits the
320 * individual words of a path string into a list of individual
324 GList
*pathstr_to_ptokens(const char *pathstr
, struct resolve_context
*ctx
)
326 const char *at
= pathstr
;
327 const char *last
= at
;
328 GList
*ptokens
= NULL
;
331 if (*at
== '.' || *at
== '\0') {
335 /* Error: empty token */
336 BT_COMP_LOGE("Empty path token: path=\"%s\", pos=%u",
337 pathstr
, (unsigned int) (at
- pathstr
));
341 tokenstr
= g_string_new(NULL
);
342 g_string_append_len(tokenstr
, last
, at
- last
);
343 ptokens
= g_list_append(ptokens
, tokenstr
);
357 ptokens_destroy(ptokens
);
362 * Converts a path token list to a field path object. The path token
363 * list is relative from `fc`. The index of the source looking for its
364 * target within `fc` is indicated by `src_index`. This can be
365 * `INT64_MAX` if the source is contained in `fc`.
367 * `field_path` is an output parameter owned by the caller that must be
371 int ptokens_to_field_path(GList
*ptokens
, struct ctf_field_path
*field_path
,
372 struct ctf_field_class
*fc
, int64_t src_index
,
373 struct resolve_context
*ctx
)
376 GList
*cur_ptoken
= ptokens
;
377 bool first_level_done
= false;
382 struct ctf_field_class
*child_fc
;
383 const char *ft_name
= ptoken_get_string(cur_ptoken
);
385 BT_COMP_LOGD("Current path token: token=\"%s\"", ft_name
);
387 /* Find to which index corresponds the current path token */
388 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
389 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
393 ctf_field_class_compound_get_field_class_index_from_orig_name(
395 if (child_index
< 0) {
397 * Error: field name does not exist or
398 * wrong current class.
400 BT_COMP_LOGD("Cannot get index of field class: "
401 "field-name=\"%s\", "
402 "src-index=%" PRId64
", "
403 "child-index=%" PRId64
", "
404 "first-level-done=%d",
405 ft_name
, src_index
, child_index
,
409 } else if (child_index
> src_index
&&
411 BT_COMP_LOGD("Child field class is located after source field class: "
412 "field-name=\"%s\", "
413 "src-index=%" PRId64
", "
414 "child-index=%" PRId64
", "
415 "first-level-done=%d",
416 ft_name
, src_index
, child_index
,
422 /* Next path token */
423 cur_ptoken
= g_list_next(cur_ptoken
);
424 first_level_done
= true;
427 /* Create new field path entry */
428 ctf_field_path_append_index(field_path
, child_index
);
430 /* Get child field class */
431 child_fc
= ctf_field_class_compound_borrow_field_class_by_index(
435 /* Move child class to current class */
444 * Converts a known absolute path token list to a field path object
445 * within the resolving context `ctx`.
447 * `field_path` is an output parameter owned by the caller that must be
451 int absolute_ptokens_to_field_path(GList
*ptokens
,
452 struct ctf_field_path
*field_path
,
453 struct resolve_context
*ctx
)
457 struct ctf_field_class
*fc
;
460 * Make sure we're not referring to a scope within a translated
463 switch (field_path
->root
) {
464 case CTF_SCOPE_PACKET_HEADER
:
465 if (ctx
->tc
->is_translated
) {
466 BT_COMP_LOGE("Trace class is already translated: "
468 ctf_scope_string(field_path
->root
));
474 case CTF_SCOPE_PACKET_CONTEXT
:
475 case CTF_SCOPE_EVENT_HEADER
:
476 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
478 BT_COMP_LOGE("No current stream class: "
480 ctf_scope_string(field_path
->root
));
485 if (ctx
->sc
->is_translated
) {
486 BT_COMP_LOGE("Stream class is already translated: "
488 ctf_scope_string(field_path
->root
));
494 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
495 case CTF_SCOPE_EVENT_PAYLOAD
:
497 BT_COMP_LOGE("No current event class: "
499 ctf_scope_string(field_path
->root
));
504 if (ctx
->ec
->is_translated
) {
505 BT_COMP_LOGE("Event class is already translated: "
507 ctf_scope_string(field_path
->root
));
518 /* Skip absolute path tokens */
519 cur_ptoken
= g_list_nth(ptokens
,
520 absolute_path_prefix_ptoken_counts
[field_path
->root
]);
522 /* Start with root class */
523 fc
= borrow_class_from_ctx(ctx
, field_path
->root
);
525 /* Error: root class is not available */
526 BT_COMP_LOGE("Root field class is not available: "
528 ctf_scope_string(field_path
->root
));
534 ret
= ptokens_to_field_path(cur_ptoken
, field_path
, fc
, INT64_MAX
, ctx
);
541 * Converts a known relative path token list to a field path object
542 * within the resolving context `ctx`.
544 * `field_path` is an output parameter owned by the caller that must be
548 int relative_ptokens_to_field_path(GList
*ptokens
,
549 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
552 int64_t parent_pos_in_stack
;
553 struct ctf_field_path tail_field_path
;
555 ctf_field_path_init(&tail_field_path
);
556 parent_pos_in_stack
= field_class_stack_size(ctx
->field_class_stack
) - 1;
558 while (parent_pos_in_stack
>= 0) {
559 struct ctf_field_class
*parent_class
=
560 field_class_stack_at(ctx
->field_class_stack
,
561 parent_pos_in_stack
)->fc
;
562 int64_t cur_index
= field_class_stack_at(ctx
->field_class_stack
,
563 parent_pos_in_stack
)->index
;
565 BT_COMP_LOGD("Locating target field class from current parent field class: "
566 "parent-pos=%" PRId64
", parent-fc-addr=%p, "
567 "cur-index=%" PRId64
,
568 parent_pos_in_stack
, parent_class
, cur_index
);
570 /* Locate target from current parent class */
571 ret
= ptokens_to_field_path(ptokens
, &tail_field_path
,
572 parent_class
, cur_index
, ctx
);
574 /* Not found... yet */
575 BT_COMP_LOGD_STR("Not found at this point.");
576 ctf_field_path_clear(&tail_field_path
);
578 /* Found: stitch tail field path to head field path */
580 size_t tail_field_path_len
=
581 tail_field_path
.path
->len
;
584 struct ctf_field_class
*cur_class
=
585 field_class_stack_at(
586 ctx
->field_class_stack
, i
)->fc
;
587 int64_t index
= field_class_stack_at(
588 ctx
->field_class_stack
, i
)->index
;
590 if (cur_class
== parent_class
) {
594 ctf_field_path_append_index(field_path
,
599 for (i
= 0; i
< tail_field_path_len
; i
++) {
601 ctf_field_path_borrow_index_by_index(
602 &tail_field_path
, i
);
604 ctf_field_path_append_index(field_path
,
610 parent_pos_in_stack
--;
613 if (parent_pos_in_stack
< 0) {
618 ctf_field_path_fini(&tail_field_path
);
623 * Converts a path string to a field path object within the resolving
627 int pathstr_to_field_path(const char *pathstr
,
628 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
631 enum ctf_scope root_scope
;
632 GList
*ptokens
= NULL
;
634 /* Convert path string to path tokens */
635 ptokens
= pathstr_to_ptokens(pathstr
, ctx
);
637 BT_COMP_LOGE("Cannot convert path string to path tokens: "
638 "path=\"%s\"", pathstr
);
643 /* Absolute or relative path? */
644 root_scope
= get_root_scope_from_absolute_pathstr(pathstr
, ctx
);
646 if (root_scope
== CTF_SCOPE_PACKET_UNKNOWN
) {
647 /* Relative path: start with current root scope */
648 field_path
->root
= ctx
->root_scope
;
649 BT_COMP_LOGD("Detected relative path: starting with current root scope: "
650 "scope=%s", ctf_scope_string(field_path
->root
));
651 ret
= relative_ptokens_to_field_path(ptokens
, field_path
, ctx
);
653 BT_COMP_LOGE("Cannot get relative field path of path string: "
654 "path=\"%s\", start-scope=%s, end-scope=%s",
655 pathstr
, ctf_scope_string(ctx
->root_scope
),
656 ctf_scope_string(field_path
->root
));
660 /* Absolute path: use found root scope */
661 field_path
->root
= root_scope
;
662 BT_COMP_LOGD("Detected absolute path: using root scope: "
663 "scope=%s", ctf_scope_string(field_path
->root
));
664 ret
= absolute_ptokens_to_field_path(ptokens
, field_path
, ctx
);
666 BT_COMP_LOGE("Cannot get absolute field path of path string: "
667 "path=\"%s\", root-scope=%s",
668 pathstr
, ctf_scope_string(root_scope
));
673 if (BT_LOG_ON_TRACE
&& ret
== 0) {
674 GString
*field_path_pretty
= ctf_field_path_string(field_path
);
675 const char *field_path_pretty_str
=
676 field_path_pretty
? field_path_pretty
->str
: NULL
;
678 BT_COMP_LOGD("Found field path: path=\"%s\", field-path=\"%s\"",
679 pathstr
, field_path_pretty_str
);
681 if (field_path_pretty
) {
682 g_string_free(field_path_pretty
, TRUE
);
687 ptokens_destroy(ptokens
);
692 * Retrieves a field class by following the field path `field_path` in
693 * the resolving context `ctx`.
696 struct ctf_field_class
*field_path_to_field_class(
697 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
700 struct ctf_field_class
*fc
;
702 /* Start with root class */
703 fc
= borrow_class_from_ctx(ctx
, field_path
->root
);
705 /* Error: root class is not available */
706 BT_COMP_LOGE("Root field class is not available: root-scope=%s",
707 ctf_scope_string(field_path
->root
));
712 for (i
= 0; i
< field_path
->path
->len
; i
++) {
713 struct ctf_field_class
*child_fc
;
714 int64_t child_index
=
715 ctf_field_path_borrow_index_by_index(field_path
, i
);
717 /* Get child field class */
718 child_fc
= ctf_field_class_compound_borrow_field_class_by_index(
722 /* Move child class to current class */
731 * Fills the equivalent field path object of the context class stack.
734 void get_ctx_stack_field_path(struct resolve_context
*ctx
,
735 struct ctf_field_path
*field_path
)
739 BT_ASSERT(field_path
);
740 field_path
->root
= ctx
->root_scope
;
741 ctf_field_path_clear(field_path
);
743 for (i
= 0; i
< field_class_stack_size(ctx
->field_class_stack
); i
++) {
744 struct field_class_stack_frame
*frame
=
745 field_class_stack_at(ctx
->field_class_stack
, i
);
747 ctf_field_path_append_index(field_path
, frame
->index
);
752 * Returns the index of the lowest common ancestor of two field path
753 * objects having the same root scope.
756 int64_t get_field_paths_lca_index(struct ctf_field_path
*field_path1
,
757 struct ctf_field_path
*field_path2
,
758 struct resolve_context
*ctx
)
760 int64_t lca_index
= 0;
761 uint64_t field_path1_len
, field_path2_len
;
763 if (BT_LOG_ON_TRACE
) {
764 GString
*field_path1_pretty
=
765 ctf_field_path_string(field_path1
);
766 GString
*field_path2_pretty
=
767 ctf_field_path_string(field_path2
);
768 const char *field_path1_pretty_str
=
769 field_path1_pretty
? field_path1_pretty
->str
: NULL
;
770 const char *field_path2_pretty_str
=
771 field_path2_pretty
? field_path2_pretty
->str
: NULL
;
773 BT_COMP_LOGD("Finding lowest common ancestor (LCA) between two field paths: "
774 "field-path-1=\"%s\", field-path-2=\"%s\"",
775 field_path1_pretty_str
, field_path2_pretty_str
);
777 if (field_path1_pretty
) {
778 g_string_free(field_path1_pretty
, TRUE
);
781 if (field_path2_pretty
) {
782 g_string_free(field_path2_pretty
, TRUE
);
787 * Start from both roots and find the first mismatch.
789 BT_ASSERT(field_path1
->root
== field_path2
->root
);
790 field_path1_len
= field_path1
->path
->len
;
791 field_path2_len
= field_path2
->path
->len
;
794 int64_t target_index
, ctx_index
;
796 if (lca_index
== (int64_t) field_path2_len
||
797 lca_index
== (int64_t) field_path1_len
) {
799 * This means that both field paths never split.
800 * This is invalid because the target cannot be
801 * an ancestor of the source.
803 BT_COMP_LOGE("Source field class is an ancestor of target field class or vice versa: "
804 "lca-index=%" PRId64
", "
805 "field-path-1-len=%" PRIu64
", "
806 "field-path-2-len=%" PRIu64
,
807 lca_index
, field_path1_len
, field_path2_len
);
812 target_index
= ctf_field_path_borrow_index_by_index(field_path1
,
814 ctx_index
= ctf_field_path_borrow_index_by_index(field_path2
,
817 if (target_index
!= ctx_index
) {
818 /* LCA index is the previous */
825 BT_COMP_LOGD("Found LCA: lca-index=%" PRId64
, lca_index
);
830 * Validates a target field path.
833 int validate_target_field_path(struct ctf_field_path
*target_field_path
,
834 struct ctf_field_class
*target_fc
,
835 struct resolve_context
*ctx
)
838 struct ctf_field_path ctx_field_path
;
839 uint64_t target_field_path_len
= target_field_path
->path
->len
;
842 /* Get context field path */
843 ctf_field_path_init(&ctx_field_path
);
844 get_ctx_stack_field_path(ctx
, &ctx_field_path
);
847 * Make sure the target is not a root.
849 if (target_field_path_len
== 0) {
850 BT_COMP_LOGE_STR("Target field path's length is 0 (targeting the root).");
856 * Make sure the root of the target field path is not located
857 * after the context field path's root.
859 if (target_field_path
->root
> ctx_field_path
.root
) {
860 BT_COMP_LOGE("Target field class is located after source field class: "
861 "target-root=%s, source-root=%s",
862 ctf_scope_string(target_field_path
->root
),
863 ctf_scope_string(ctx_field_path
.root
));
868 if (target_field_path
->root
== ctx_field_path
.root
) {
869 int64_t target_index
, ctx_index
;
872 * Find the index of the lowest common ancestor of both field
875 lca_index
= get_field_paths_lca_index(target_field_path
,
876 &ctx_field_path
, ctx
);
878 BT_COMP_LOGE_STR("Cannot get least common ancestor.");
884 * Make sure the target field path is located before the
885 * context field path.
887 target_index
= ctf_field_path_borrow_index_by_index(
888 target_field_path
, (uint64_t) lca_index
);
889 ctx_index
= ctf_field_path_borrow_index_by_index(
890 &ctx_field_path
, (uint64_t) lca_index
);
892 if (target_index
>= ctx_index
) {
893 BT_COMP_LOGE("Target field class's index is greater than or equal to source field class's index in LCA: "
894 "lca-index=%" PRId64
", "
895 "target-index=%" PRId64
", "
896 "source-index=%" PRId64
,
897 lca_index
, target_index
, ctx_index
);
904 * Make sure the target class has the right class and properties.
906 switch (ctx
->cur_fc
->type
) {
907 case CTF_FIELD_CLASS_TYPE_VARIANT
:
908 if (target_fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
909 BT_COMP_LOGE("Variant field class's tag field class is not an enumeration field class: "
910 "tag-fc-addr=%p, tag-fc-id=%d",
911 target_fc
, target_fc
->type
);
916 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
918 struct ctf_field_class_int
*int_fc
= (void *) target_fc
;
920 if (target_fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&&
921 target_fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
922 BT_COMP_LOGE("Sequence field class's length field class is not an unsigned integer field class: "
923 "length-fc-addr=%p, length-fc-id=%d",
924 target_fc
, target_fc
->type
);
929 if (int_fc
->is_signed
) {
930 BT_COMP_LOGE("Sequence field class's length field class is not an unsigned integer field class: "
931 "length-fc-addr=%p, length-fc-id=%d",
932 target_fc
, target_fc
->type
);
943 ctf_field_path_fini(&ctx_field_path
);
948 * Resolves a variant or sequence field class `fc`.
951 int resolve_sequence_or_variant_field_class(struct ctf_field_class
*fc
,
952 struct resolve_context
*ctx
)
956 struct ctf_field_path target_field_path
;
957 struct ctf_field_class
*target_fc
= NULL
;
958 GString
*target_field_path_pretty
= NULL
;
959 const char *target_field_path_pretty_str
;
961 ctf_field_path_init(&target_field_path
);
963 /* Get path string */
965 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
967 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
968 pathstr
= seq_fc
->length_ref
->str
;
971 case CTF_FIELD_CLASS_TYPE_VARIANT
:
973 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
974 pathstr
= var_fc
->tag_ref
->str
;
982 BT_COMP_LOGE_STR("Cannot get path string.");
987 /* Get target field path out of path string */
988 ret
= pathstr_to_field_path(pathstr
, &target_field_path
, ctx
);
990 BT_COMP_LOGE("Cannot get target field path for path string: "
991 "path=\"%s\"", pathstr
);
995 target_field_path_pretty
= ctf_field_path_string(
997 target_field_path_pretty_str
=
998 target_field_path_pretty
? target_field_path_pretty
->str
: NULL
;
1000 /* Get target field class */
1001 target_fc
= field_path_to_field_class(&target_field_path
, ctx
);
1003 BT_COMP_LOGE("Cannot get target field class for path string: "
1004 "path=\"%s\", target-field-path=\"%s\"",
1005 pathstr
, target_field_path_pretty_str
);
1010 ret
= validate_target_field_path(&target_field_path
,
1013 BT_COMP_LOGE("Invalid target field path for path string: "
1014 "path=\"%s\", target-field-path=\"%s\"",
1015 pathstr
, target_field_path_pretty_str
);
1019 /* Set target field path and target field class */
1021 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1023 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
1025 ctf_field_path_copy_content(&seq_fc
->length_path
,
1026 &target_field_path
);
1027 seq_fc
->length_fc
= (void *) target_fc
;
1030 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1032 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1034 ctf_field_path_copy_content(&var_fc
->tag_path
,
1035 &target_field_path
);
1036 ctf_field_class_variant_set_tag_field_class(var_fc
,
1037 (void *) target_fc
);
1045 if (target_field_path_pretty
) {
1046 g_string_free(target_field_path_pretty
, TRUE
);
1049 ctf_field_path_fini(&target_field_path
);
1054 * Resolves a field class `fc`.
1057 int resolve_field_class(struct ctf_field_class
*fc
, struct resolve_context
*ctx
)
1062 /* Field class is not available; still valid */
1068 /* Resolve sequence/variant field class */
1070 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1071 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1072 ret
= resolve_sequence_or_variant_field_class(fc
, ctx
);
1074 BT_COMP_LOGE("Cannot resolve sequence field class's length or variant field class's tag: "
1075 "ret=%d, fc-addr=%p", ret
, fc
);
1084 /* Recurse into compound classes */
1086 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1087 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1088 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1089 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1092 uint64_t field_count
=
1093 ctf_field_class_compound_get_field_class_count(fc
);
1095 ret
= field_class_stack_push(ctx
->field_class_stack
, fc
, ctx
);
1097 BT_COMP_LOGE("Cannot push field class on context's stack: "
1102 for (i
= 0; i
< field_count
; i
++) {
1103 struct ctf_field_class
*child_fc
=
1104 ctf_field_class_compound_borrow_field_class_by_index(
1107 BT_ASSERT(child_fc
);
1109 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
1110 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
1111 field_class_stack_peek(
1112 ctx
->field_class_stack
)->index
= -1;
1114 field_class_stack_peek(
1115 ctx
->field_class_stack
)->index
=
1119 BT_COMP_LOGD("Resolving field class's child field class: "
1120 "parent-fc-addr=%p, child-fc-addr=%p, "
1121 "index=%" PRIu64
", count=%" PRIu64
,
1122 fc
, child_fc
, i
, field_count
);
1123 ret
= resolve_field_class(child_fc
, ctx
);
1129 field_class_stack_pop(ctx
->field_class_stack
, ctx
);
1141 * Resolves the root field class corresponding to the scope `root_scope`.
1144 int resolve_root_class(enum ctf_scope root_scope
, struct resolve_context
*ctx
)
1148 BT_ASSERT(field_class_stack_size(ctx
->field_class_stack
) == 0);
1149 ctx
->root_scope
= root_scope
;
1150 ret
= resolve_field_class(borrow_class_from_ctx(ctx
, root_scope
), ctx
);
1151 ctx
->root_scope
= -1;
1156 int resolve_event_class_field_classes(struct resolve_context
*ctx
,
1157 struct ctf_event_class
*ec
)
1161 BT_ASSERT(!ctx
->scopes
.event_spec_context
);
1162 BT_ASSERT(!ctx
->scopes
.event_payload
);
1164 if (ec
->is_translated
) {
1169 ctx
->scopes
.event_spec_context
= ec
->spec_context_fc
;
1170 ret
= resolve_root_class(CTF_SCOPE_EVENT_COMMON_CONTEXT
, ctx
);
1172 BT_COMP_LOGE("Cannot resolve event specific context field class: "
1177 ctx
->scopes
.event_payload
= ec
->payload_fc
;
1178 ret
= resolve_root_class(CTF_SCOPE_EVENT_PAYLOAD
, ctx
);
1180 BT_COMP_LOGE("Cannot resolve event payload field class: "
1186 ctx
->scopes
.event_spec_context
= NULL
;
1187 ctx
->scopes
.event_payload
= NULL
;
1193 int resolve_stream_class_field_classes(struct resolve_context
*ctx
,
1194 struct ctf_stream_class
*sc
)
1199 BT_ASSERT(!ctx
->scopes
.packet_context
);
1200 BT_ASSERT(!ctx
->scopes
.event_header
);
1201 BT_ASSERT(!ctx
->scopes
.event_common_context
);
1204 if (!sc
->is_translated
) {
1205 ctx
->scopes
.packet_context
= sc
->packet_context_fc
;
1206 ret
= resolve_root_class(CTF_SCOPE_PACKET_CONTEXT
, ctx
);
1208 BT_COMP_LOGE("Cannot resolve packet context field class: "
1213 ctx
->scopes
.event_header
= sc
->event_header_fc
;
1214 ret
= resolve_root_class(CTF_SCOPE_EVENT_HEADER
, ctx
);
1216 BT_COMP_LOGE("Cannot resolve event header field class: "
1221 ctx
->scopes
.event_common_context
= sc
->event_common_context_fc
;
1222 ret
= resolve_root_class(CTF_SCOPE_EVENT_COMMON_CONTEXT
, ctx
);
1224 BT_COMP_LOGE("Cannot resolve event common context field class: "
1230 ctx
->scopes
.packet_context
= sc
->packet_context_fc
;
1231 ctx
->scopes
.event_header
= sc
->event_header_fc
;
1232 ctx
->scopes
.event_common_context
= sc
->event_common_context_fc
;
1234 for (i
= 0; i
< sc
->event_classes
->len
; i
++) {
1235 struct ctf_event_class
*ec
= sc
->event_classes
->pdata
[i
];
1237 ret
= resolve_event_class_field_classes(ctx
, ec
);
1239 BT_COMP_LOGE("Cannot resolve event class's field classes: "
1240 "ec-id=%" PRIu64
", ec-name=\"%s\"",
1241 ec
->id
, ec
->name
->str
);
1247 ctx
->scopes
.packet_context
= NULL
;
1248 ctx
->scopes
.event_header
= NULL
;
1249 ctx
->scopes
.event_common_context
= NULL
;
1255 int ctf_trace_class_resolve_field_classes(struct ctf_trace_class
*tc
,
1256 struct meta_log_config
*log_cfg
)
1260 struct resolve_context local_ctx
= {
1261 .log_level
= log_cfg
->log_level
,
1262 .self_comp
= log_cfg
->self_comp
,
1267 .packet_header
= tc
->packet_header_fc
,
1268 .packet_context
= NULL
,
1269 .event_header
= NULL
,
1270 .event_common_context
= NULL
,
1271 .event_spec_context
= NULL
,
1272 .event_payload
= NULL
,
1274 .root_scope
= CTF_SCOPE_PACKET_HEADER
,
1277 struct resolve_context
*ctx
= &local_ctx
;
1279 /* Initialize class stack */
1280 ctx
->field_class_stack
= field_class_stack_create();
1281 if (!ctx
->field_class_stack
) {
1282 BT_COMP_LOGE_STR("Cannot create field class stack.");
1287 if (!tc
->is_translated
) {
1288 ctx
->scopes
.packet_header
= tc
->packet_header_fc
;
1289 ret
= resolve_root_class(CTF_SCOPE_PACKET_HEADER
, ctx
);
1291 BT_COMP_LOGE("Cannot resolve packet header field class: "
1297 ctx
->scopes
.packet_header
= tc
->packet_header_fc
;
1299 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1300 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1302 ret
= resolve_stream_class_field_classes(ctx
, sc
);
1304 BT_COMP_LOGE("Cannot resolve stream class's field classes: "
1305 "sc-id=%" PRIu64
, sc
->id
);
1311 field_class_stack_destroy(ctx
->field_class_stack
);