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_COMP_LOG_SELF_COMP_CLASS (ctx->self_comp_class)
10 #define BT_LOG_OUTPUT_LEVEL (ctx->log_level)
11 #define BT_LOG_TAG "PLUGIN/CTF/META/RESOLVE"
12 #include "logging/comp-logging.h"
14 #include <babeltrace2/babeltrace.h>
15 #include "common/macros.h"
16 #include "common/assert.h"
17 #include "common/common.h"
27 #include "ctf-meta-visitors.hpp"
28 #include "logging.hpp"
30 using field_class_stack_t
= GPtrArray
;
35 * `fc` contains a compound field class (structure, variant, array,
36 * or sequence) and `index` indicates the index of the field class in
37 * the upper frame (-1 for array and sequence field classes). `name`
38 * indicates the name of the field class in the upper frame (empty
39 * string for array and sequence field classes).
41 struct field_class_stack_frame
{
42 struct ctf_field_class
*fc
;
47 * The current context of the resolving engine.
49 struct resolve_context
{
50 bt_logging_level log_level
;
52 /* Weak, exactly one of these must be set */
53 bt_self_component
*self_comp
;
54 bt_self_component_class
*self_comp_class
;
56 struct ctf_trace_class
*tc
;
57 struct ctf_stream_class
*sc
;
58 struct ctf_event_class
*ec
;
61 struct ctf_field_class
*packet_header
;
62 struct ctf_field_class
*packet_context
;
63 struct ctf_field_class
*event_header
;
64 struct ctf_field_class
*event_common_context
;
65 struct ctf_field_class
*event_spec_context
;
66 struct ctf_field_class
*event_payload
;
69 /* Root scope being visited */
70 enum ctf_scope root_scope
;
71 field_class_stack_t
*field_class_stack
;
72 struct ctf_field_class
*cur_fc
;
75 /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
76 static const char * const absolute_path_prefixes
[] = {
77 /* CTF_SCOPE_PACKET_HEADER */ "trace.packet.header.",
78 /* CTF_SCOPE_PACKET_CONTEXT */ "stream.packet.context.",
79 /* CTF_SCOPE_EVENT_HEADER */ "stream.event.header.",
80 /* CTF_SCOPE_EVENT_COMMON_CONTEXT */ "stream.event.context.",
81 /* CTF_SCOPE_EVENT_SPECIFIC_CONTEXT */ "event.context.",
82 /* CTF_SCOPE_EVENT_PAYLOAD */ "event.fields.",
85 /* Number of path tokens used for the absolute prefixes */
86 static const uint64_t absolute_path_prefix_ptoken_counts
[] = {
87 /* CTF_SCOPE_PACKET_HEADER */ 3,
88 /* CTF_SCOPE_PACKET_CONTEXT */ 3,
89 /* CTF_SCOPE_EVENT_HEADER */ 3,
90 /* CTF_SCOPE_EVENT_COMMON_CONTEXT */ 3,
91 /* CTF_SCOPE_EVENT_SPECIFIC_CONTEXT */ 2,
92 /* CTF_SCOPE_EVENT_PAYLOAD */ 2,
96 void destroy_field_class_stack_frame(struct field_class_stack_frame
*frame
)
106 * Creates a class stack.
109 field_class_stack_t
*field_class_stack_create(void)
111 return g_ptr_array_new_with_free_func(
112 (GDestroyNotify
) destroy_field_class_stack_frame
);
116 * Destroys a class stack.
119 void field_class_stack_destroy(field_class_stack_t
*stack
)
122 g_ptr_array_free(stack
, TRUE
);
127 * Pushes a field class onto a class stack.
130 int field_class_stack_push(field_class_stack_t
*stack
, struct ctf_field_class
*fc
,
131 struct resolve_context
*ctx
)
134 struct field_class_stack_frame
*frame
= NULL
;
137 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid parameter: stack or field class is `NULL`.");
142 frame
= g_new0(struct field_class_stack_frame
, 1);
144 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Failed to allocate one field class stack frame.");
149 BT_COMP_LOGD("Pushing field class on context's stack: "
150 "fc-addr=%p, stack-size-before=%u", fc
, stack
->len
);
152 g_ptr_array_add(stack
, frame
);
159 * Checks whether or not `stack` is empty.
162 bool field_class_stack_empty(field_class_stack_t
*stack
)
164 return stack
->len
== 0;
168 * Returns the number of frames in `stack`.
171 size_t field_class_stack_size(field_class_stack_t
*stack
)
177 * Returns the top frame of `stack`.
180 struct field_class_stack_frame
*field_class_stack_peek(field_class_stack_t
*stack
)
183 BT_ASSERT(!field_class_stack_empty(stack
));
185 return (field_class_stack_frame
*) g_ptr_array_index(stack
, stack
->len
- 1);
189 * Returns the frame at index `index` in `stack`.
192 struct field_class_stack_frame
*field_class_stack_at(field_class_stack_t
*stack
,
196 BT_ASSERT(index
< stack
->len
);
198 return (field_class_stack_frame
*) g_ptr_array_index(stack
, index
);
202 * Removes the top frame of `stack`.
205 void field_class_stack_pop(field_class_stack_t
*stack
,
206 struct resolve_context
*ctx
)
208 if (!field_class_stack_empty(stack
)) {
210 * This will call the frame's destructor and free it, as
211 * well as put its contained field class.
213 BT_COMP_LOGD("Popping context's stack: stack-size-before=%u",
215 g_ptr_array_set_size(stack
, stack
->len
- 1);
220 * Returns the scope field class of `scope` in the context `ctx`.
223 struct ctf_field_class
*borrow_class_from_ctx(struct resolve_context
*ctx
,
224 enum ctf_scope scope
)
227 case CTF_SCOPE_PACKET_HEADER
:
228 return ctx
->scopes
.packet_header
;
229 case CTF_SCOPE_PACKET_CONTEXT
:
230 return ctx
->scopes
.packet_context
;
231 case CTF_SCOPE_EVENT_HEADER
:
232 return ctx
->scopes
.event_header
;
233 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
234 return ctx
->scopes
.event_common_context
;
235 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
236 return ctx
->scopes
.event_spec_context
;
237 case CTF_SCOPE_EVENT_PAYLOAD
:
238 return ctx
->scopes
.event_payload
;
247 * Returns the CTF scope from a path string. May return -1 if the path
248 * is found to be relative.
251 enum ctf_scope
get_root_scope_from_absolute_pathstr(const char *pathstr
,
252 struct resolve_context
*ctx
)
254 enum ctf_scope scope
;
255 enum ctf_scope ret
= CTF_SCOPE_PACKET_UNKNOWN
;
256 const size_t prefixes_count
= sizeof(absolute_path_prefixes
) /
257 sizeof(*absolute_path_prefixes
);
259 for (scope
= CTF_SCOPE_PACKET_HEADER
; scope
< CTF_SCOPE_PACKET_HEADER
+
260 prefixes_count
; scope
= (ctf_scope
) (scope
+ 1)) {
262 * Check if path string starts with a known absolute
265 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
267 if (strncmp(pathstr
, absolute_path_prefixes
[scope
],
268 strlen(absolute_path_prefixes
[scope
]))) {
269 /* Prefix does not match: try the next one */
270 BT_COMP_LOGD("Prefix does not match: trying the next one: "
271 "path=\"%s\", path-prefix=\"%s\", scope=%s",
272 pathstr
, absolute_path_prefixes
[scope
],
273 ctf_scope_string(scope
));
279 BT_COMP_LOGD("Found root scope from absolute path: "
280 "path=\"%s\", scope=%s", pathstr
,
281 ctf_scope_string(scope
));
290 * Destroys a path token.
293 void ptokens_destroy_func(gpointer ptoken
, gpointer data
)
295 g_string_free((GString
*) ptoken
, TRUE
);
299 * Destroys a path token list.
302 void ptokens_destroy(GList
*ptokens
)
308 g_list_foreach(ptokens
, ptokens_destroy_func
, NULL
);
309 g_list_free(ptokens
);
313 * Returns the string contained in a path token.
316 const char *ptoken_get_string(GList
*ptoken
)
318 GString
*tokenstr
= (GString
*) ptoken
->data
;
320 return tokenstr
->str
;
324 * Converts a path string to a path token list, that is, splits the
325 * individual words of a path string into a list of individual
329 GList
*pathstr_to_ptokens(const char *pathstr
, struct resolve_context
*ctx
)
331 const char *at
= pathstr
;
332 const char *last
= at
;
333 GList
*ptokens
= NULL
;
336 if (*at
== '.' || *at
== '\0') {
340 /* Error: empty token */
341 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Empty path token: path=\"%s\", pos=%u",
342 pathstr
, (unsigned int) (at
- pathstr
));
346 tokenstr
= g_string_new(NULL
);
347 g_string_append_len(tokenstr
, last
, at
- last
);
348 ptokens
= g_list_append(ptokens
, tokenstr
);
362 ptokens_destroy(ptokens
);
367 * Converts a path token list to a field path object. The path token
368 * list is relative from `fc`. The index of the source looking for its
369 * target within `fc` is indicated by `src_index`. This can be
370 * `INT64_MAX` if the source is contained in `fc`.
372 * `field_path` is an output parameter owned by the caller that must be
376 int ptokens_to_field_path(GList
*ptokens
, struct ctf_field_path
*field_path
,
377 struct ctf_field_class
*fc
, int64_t src_index
,
378 struct resolve_context
*ctx
)
381 GList
*cur_ptoken
= ptokens
;
382 bool first_level_done
= false;
387 struct ctf_field_class
*child_fc
;
388 const char *ft_name
= ptoken_get_string(cur_ptoken
);
390 BT_COMP_LOGD("Current path token: token=\"%s\"", ft_name
);
392 /* Find to which index corresponds the current path token */
393 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
394 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
398 ctf_field_class_compound_get_field_class_index_from_orig_name(
400 if (child_index
< 0) {
402 * Error: field name does not exist or
403 * wrong current class.
405 BT_COMP_LOGD("Cannot get index of field class: "
406 "field-name=\"%s\", "
407 "src-index=%" PRId64
", "
408 "child-index=%" PRId64
", "
409 "first-level-done=%d",
410 ft_name
, src_index
, child_index
,
414 } else if (child_index
> src_index
&&
416 BT_COMP_LOGD("Child field class is located after source field class: "
417 "field-name=\"%s\", "
418 "src-index=%" PRId64
", "
419 "child-index=%" PRId64
", "
420 "first-level-done=%d",
421 ft_name
, src_index
, child_index
,
427 /* Next path token */
428 cur_ptoken
= g_list_next(cur_ptoken
);
429 first_level_done
= true;
432 /* Create new field path entry */
433 ctf_field_path_append_index(field_path
, child_index
);
435 /* Get child field class */
436 child_fc
= ctf_field_class_compound_borrow_field_class_by_index(
440 /* Move child class to current class */
449 * Converts a known absolute path token list to a field path object
450 * within the resolving context `ctx`.
452 * `field_path` is an output parameter owned by the caller that must be
456 int absolute_ptokens_to_field_path(GList
*ptokens
,
457 struct ctf_field_path
*field_path
,
458 struct resolve_context
*ctx
)
462 struct ctf_field_class
*fc
;
465 * Make sure we're not referring to a scope within a translated
468 switch (field_path
->root
) {
469 case CTF_SCOPE_PACKET_HEADER
:
470 if (ctx
->tc
->is_translated
) {
471 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Trace class is already translated: "
473 ctf_scope_string(field_path
->root
));
479 case CTF_SCOPE_PACKET_CONTEXT
:
480 case CTF_SCOPE_EVENT_HEADER
:
481 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
483 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("No current stream class: "
485 ctf_scope_string(field_path
->root
));
490 if (ctx
->sc
->is_translated
) {
491 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Stream class is already translated: "
493 ctf_scope_string(field_path
->root
));
499 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
500 case CTF_SCOPE_EVENT_PAYLOAD
:
502 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("No current event class: "
504 ctf_scope_string(field_path
->root
));
509 if (ctx
->ec
->is_translated
) {
510 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Event class is already translated: "
512 ctf_scope_string(field_path
->root
));
523 /* Skip absolute path tokens */
524 cur_ptoken
= g_list_nth(ptokens
,
525 absolute_path_prefix_ptoken_counts
[field_path
->root
]);
527 /* Start with root class */
528 fc
= borrow_class_from_ctx(ctx
, field_path
->root
);
530 /* Error: root class is not available */
531 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Root field class is not available: "
533 ctf_scope_string(field_path
->root
));
539 ret
= ptokens_to_field_path(cur_ptoken
, field_path
, fc
, INT64_MAX
, ctx
);
546 * Converts a known relative path token list to a field path object
547 * within the resolving context `ctx`.
549 * `field_path` is an output parameter owned by the caller that must be
553 int relative_ptokens_to_field_path(GList
*ptokens
,
554 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
557 int64_t parent_pos_in_stack
;
558 struct ctf_field_path tail_field_path
;
560 ctf_field_path_init(&tail_field_path
);
561 parent_pos_in_stack
= field_class_stack_size(ctx
->field_class_stack
) - 1;
563 while (parent_pos_in_stack
>= 0) {
564 struct ctf_field_class
*parent_class
=
565 field_class_stack_at(ctx
->field_class_stack
,
566 parent_pos_in_stack
)->fc
;
567 int64_t cur_index
= field_class_stack_at(ctx
->field_class_stack
,
568 parent_pos_in_stack
)->index
;
570 BT_COMP_LOGD("Locating target field class from current parent field class: "
571 "parent-pos=%" PRId64
", parent-fc-addr=%p, "
572 "cur-index=%" PRId64
,
573 parent_pos_in_stack
, parent_class
, cur_index
);
575 /* Locate target from current parent class */
576 ret
= ptokens_to_field_path(ptokens
, &tail_field_path
,
577 parent_class
, cur_index
, ctx
);
579 /* Not found... yet */
580 BT_COMP_LOGD_STR("Not found at this point.");
581 ctf_field_path_clear(&tail_field_path
);
583 /* Found: stitch tail field path to head field path */
585 size_t tail_field_path_len
=
586 tail_field_path
.path
->len
;
589 struct ctf_field_class
*cur_class
=
590 field_class_stack_at(
591 ctx
->field_class_stack
, i
)->fc
;
592 int64_t index
= field_class_stack_at(
593 ctx
->field_class_stack
, i
)->index
;
595 if (cur_class
== parent_class
) {
599 ctf_field_path_append_index(field_path
,
604 for (i
= 0; i
< tail_field_path_len
; i
++) {
606 ctf_field_path_borrow_index_by_index(
607 &tail_field_path
, i
);
609 ctf_field_path_append_index(field_path
,
615 parent_pos_in_stack
--;
618 if (parent_pos_in_stack
< 0) {
623 ctf_field_path_fini(&tail_field_path
);
628 * Converts a path string to a field path object within the resolving
632 int pathstr_to_field_path(const char *pathstr
,
633 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
636 enum ctf_scope root_scope
;
637 GList
*ptokens
= NULL
;
639 /* Convert path string to path tokens */
640 ptokens
= pathstr_to_ptokens(pathstr
, ctx
);
642 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot convert path string to path tokens: "
643 "path=\"%s\"", pathstr
);
648 /* Absolute or relative path? */
649 root_scope
= get_root_scope_from_absolute_pathstr(pathstr
, ctx
);
651 if (root_scope
== CTF_SCOPE_PACKET_UNKNOWN
) {
652 /* Relative path: start with current root scope */
653 field_path
->root
= ctx
->root_scope
;
654 BT_COMP_LOGD("Detected relative path: starting with current root scope: "
655 "scope=%s", ctf_scope_string(field_path
->root
));
656 ret
= relative_ptokens_to_field_path(ptokens
, field_path
, ctx
);
658 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot get relative field path of path string: "
659 "path=\"%s\", start-scope=%s, end-scope=%s",
660 pathstr
, ctf_scope_string(ctx
->root_scope
),
661 ctf_scope_string(field_path
->root
));
665 /* Absolute path: use found root scope */
666 field_path
->root
= root_scope
;
667 BT_COMP_LOGD("Detected absolute path: using root scope: "
668 "scope=%s", ctf_scope_string(field_path
->root
));
669 ret
= absolute_ptokens_to_field_path(ptokens
, field_path
, ctx
);
671 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot get absolute field path of path string: "
672 "path=\"%s\", root-scope=%s",
673 pathstr
, ctf_scope_string(root_scope
));
678 if (BT_LOG_ON_TRACE
&& ret
== 0) {
679 GString
*field_path_pretty
= ctf_field_path_string(field_path
);
680 const char *field_path_pretty_str
=
681 field_path_pretty
? field_path_pretty
->str
: NULL
;
683 BT_COMP_LOGD("Found field path: path=\"%s\", field-path=\"%s\"",
684 pathstr
, field_path_pretty_str
);
686 if (field_path_pretty
) {
687 g_string_free(field_path_pretty
, TRUE
);
692 ptokens_destroy(ptokens
);
697 * Retrieves a field class by following the field path `field_path` in
698 * the resolving context `ctx`.
701 struct ctf_field_class
*field_path_to_field_class(
702 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
705 struct ctf_field_class
*fc
;
707 /* Start with root class */
708 fc
= borrow_class_from_ctx(ctx
, field_path
->root
);
710 /* Error: root class is not available */
711 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Root field class is not available: root-scope=%s",
712 ctf_scope_string(field_path
->root
));
717 for (i
= 0; i
< field_path
->path
->len
; i
++) {
718 struct ctf_field_class
*child_fc
;
719 int64_t child_index
=
720 ctf_field_path_borrow_index_by_index(field_path
, i
);
722 /* Get child field class */
723 child_fc
= ctf_field_class_compound_borrow_field_class_by_index(
727 /* Move child class to current class */
736 * Fills the equivalent field path object of the context class stack.
739 void get_ctx_stack_field_path(struct resolve_context
*ctx
,
740 struct ctf_field_path
*field_path
)
744 BT_ASSERT(field_path
);
745 field_path
->root
= ctx
->root_scope
;
746 ctf_field_path_clear(field_path
);
748 for (i
= 0; i
< field_class_stack_size(ctx
->field_class_stack
); i
++) {
749 struct field_class_stack_frame
*frame
=
750 field_class_stack_at(ctx
->field_class_stack
, i
);
752 ctf_field_path_append_index(field_path
, frame
->index
);
757 * Returns the index of the lowest common ancestor of two field path
758 * objects having the same root scope.
761 int64_t get_field_paths_lca_index(struct ctf_field_path
*field_path1
,
762 struct ctf_field_path
*field_path2
,
763 struct resolve_context
*ctx
)
765 int64_t lca_index
= 0;
766 uint64_t field_path1_len
, field_path2_len
;
768 if (BT_LOG_ON_TRACE
) {
769 GString
*field_path1_pretty
=
770 ctf_field_path_string(field_path1
);
771 GString
*field_path2_pretty
=
772 ctf_field_path_string(field_path2
);
773 const char *field_path1_pretty_str
=
774 field_path1_pretty
? field_path1_pretty
->str
: NULL
;
775 const char *field_path2_pretty_str
=
776 field_path2_pretty
? field_path2_pretty
->str
: NULL
;
778 BT_COMP_LOGD("Finding lowest common ancestor (LCA) between two field paths: "
779 "field-path-1=\"%s\", field-path-2=\"%s\"",
780 field_path1_pretty_str
, field_path2_pretty_str
);
782 if (field_path1_pretty
) {
783 g_string_free(field_path1_pretty
, TRUE
);
786 if (field_path2_pretty
) {
787 g_string_free(field_path2_pretty
, TRUE
);
792 * Start from both roots and find the first mismatch.
794 BT_ASSERT(field_path1
->root
== field_path2
->root
);
795 field_path1_len
= field_path1
->path
->len
;
796 field_path2_len
= field_path2
->path
->len
;
799 int64_t target_index
, ctx_index
;
801 if (lca_index
== (int64_t) field_path2_len
||
802 lca_index
== (int64_t) field_path1_len
) {
804 * This means that both field paths never split.
805 * This is invalid because the target cannot be
806 * an ancestor of the source.
808 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Source field class is an ancestor of target field class or vice versa: "
809 "lca-index=%" PRId64
", "
810 "field-path-1-len=%" PRIu64
", "
811 "field-path-2-len=%" PRIu64
,
812 lca_index
, field_path1_len
, field_path2_len
);
817 target_index
= ctf_field_path_borrow_index_by_index(field_path1
,
819 ctx_index
= ctf_field_path_borrow_index_by_index(field_path2
,
822 if (target_index
!= ctx_index
) {
823 /* LCA index is the previous */
830 BT_COMP_LOGD("Found LCA: lca-index=%" PRId64
, lca_index
);
835 * Validates a target field path.
838 int validate_target_field_path(struct ctf_field_path
*target_field_path
,
839 struct ctf_field_class
*target_fc
,
840 struct resolve_context
*ctx
)
843 struct ctf_field_path ctx_field_path
;
844 uint64_t target_field_path_len
= target_field_path
->path
->len
;
847 /* Get context field path */
848 ctf_field_path_init(&ctx_field_path
);
849 get_ctx_stack_field_path(ctx
, &ctx_field_path
);
852 * Make sure the target is not a root.
854 if (target_field_path_len
== 0) {
855 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Target field path's length is 0 (targeting the root).");
861 * Make sure the root of the target field path is not located
862 * after the context field path's root.
864 if (target_field_path
->root
> ctx_field_path
.root
) {
865 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Target field class is located after source field class: "
866 "target-root=%s, source-root=%s",
867 ctf_scope_string(target_field_path
->root
),
868 ctf_scope_string(ctx_field_path
.root
));
873 if (target_field_path
->root
== ctx_field_path
.root
) {
874 int64_t target_index
, ctx_index
;
877 * Find the index of the lowest common ancestor of both field
880 lca_index
= get_field_paths_lca_index(target_field_path
,
881 &ctx_field_path
, ctx
);
883 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot get least common ancestor.");
889 * Make sure the target field path is located before the
890 * context field path.
892 target_index
= ctf_field_path_borrow_index_by_index(
893 target_field_path
, (uint64_t) lca_index
);
894 ctx_index
= ctf_field_path_borrow_index_by_index(
895 &ctx_field_path
, (uint64_t) lca_index
);
897 if (target_index
>= ctx_index
) {
898 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Target field class's index is greater than or equal to source field class's index in LCA: "
899 "lca-index=%" PRId64
", "
900 "target-index=%" PRId64
", "
901 "source-index=%" PRId64
,
902 lca_index
, target_index
, ctx_index
);
909 * Make sure the target class has the right class and properties.
911 switch (ctx
->cur_fc
->type
) {
912 case CTF_FIELD_CLASS_TYPE_VARIANT
:
913 if (target_fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
914 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Variant field class's tag field class is not an enumeration field class: "
915 "tag-fc-addr=%p, tag-fc-id=%d",
916 target_fc
, target_fc
->type
);
921 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
923 struct ctf_field_class_int
*int_fc
= ctf_field_class_as_int(target_fc
);
925 if (target_fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&&
926 target_fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
927 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Sequence field class's length field class is not an unsigned integer field class: "
928 "length-fc-addr=%p, length-fc-id=%d",
929 target_fc
, target_fc
->type
);
934 if (int_fc
->is_signed
) {
935 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Sequence field class's length field class is not an unsigned integer field class: "
936 "length-fc-addr=%p, length-fc-id=%d",
937 target_fc
, target_fc
->type
);
948 ctf_field_path_fini(&ctx_field_path
);
953 * Resolves a variant or sequence field class `fc`.
956 int resolve_sequence_or_variant_field_class(struct ctf_field_class
*fc
,
957 struct resolve_context
*ctx
)
961 struct ctf_field_path target_field_path
;
962 struct ctf_field_class
*target_fc
= NULL
;
963 GString
*target_field_path_pretty
= NULL
;
964 const char *target_field_path_pretty_str
;
966 ctf_field_path_init(&target_field_path
);
968 /* Get path string */
970 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
972 struct ctf_field_class_sequence
*seq_fc
= ctf_field_class_as_sequence(fc
);
973 pathstr
= seq_fc
->length_ref
->str
;
976 case CTF_FIELD_CLASS_TYPE_VARIANT
:
978 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(fc
);
979 pathstr
= var_fc
->tag_ref
->str
;
987 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot get path string.");
992 /* Get target field path out of path string */
993 ret
= pathstr_to_field_path(pathstr
, &target_field_path
, ctx
);
995 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot get target field path for path string: "
996 "path=\"%s\"", pathstr
);
1000 target_field_path_pretty
= ctf_field_path_string(
1001 &target_field_path
);
1002 target_field_path_pretty_str
=
1003 target_field_path_pretty
? target_field_path_pretty
->str
: NULL
;
1005 /* Get target field class */
1006 target_fc
= field_path_to_field_class(&target_field_path
, ctx
);
1008 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot get target field class for path string: "
1009 "path=\"%s\", target-field-path=\"%s\"",
1010 pathstr
, target_field_path_pretty_str
);
1015 ret
= validate_target_field_path(&target_field_path
,
1018 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid target field path for path string: "
1019 "path=\"%s\", target-field-path=\"%s\"",
1020 pathstr
, target_field_path_pretty_str
);
1024 /* Set target field path and target field class */
1026 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1028 ctf_field_class_sequence
*seq_fc
= ctf_field_class_as_sequence(fc
);
1030 ctf_field_path_copy_content(&seq_fc
->length_path
,
1031 &target_field_path
);
1032 seq_fc
->length_fc
= ctf_field_class_as_int(target_fc
);
1035 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1037 ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(fc
);
1039 ctf_field_path_copy_content(&var_fc
->tag_path
,
1040 &target_field_path
);
1041 ctf_field_class_variant_set_tag_field_class(var_fc
,
1042 ctf_field_class_as_enum(target_fc
));
1050 if (target_field_path_pretty
) {
1051 g_string_free(target_field_path_pretty
, TRUE
);
1054 ctf_field_path_fini(&target_field_path
);
1059 * Resolves a field class `fc`.
1062 int resolve_field_class(struct ctf_field_class
*fc
, struct resolve_context
*ctx
)
1067 /* Field class is not available; still valid */
1073 /* Resolve sequence/variant field class */
1075 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1076 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1077 ret
= resolve_sequence_or_variant_field_class(fc
, ctx
);
1079 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve sequence field class's length or variant field class's tag: "
1080 "ret=%d, fc-addr=%p", ret
, fc
);
1089 /* Recurse into compound classes */
1091 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1092 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1093 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1094 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1097 uint64_t field_count
=
1098 ctf_field_class_compound_get_field_class_count(fc
);
1100 ret
= field_class_stack_push(ctx
->field_class_stack
, fc
, ctx
);
1102 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot push field class on context's stack: "
1107 for (i
= 0; i
< field_count
; i
++) {
1108 struct ctf_field_class
*child_fc
=
1109 ctf_field_class_compound_borrow_field_class_by_index(
1112 BT_ASSERT(child_fc
);
1114 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
1115 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
1116 field_class_stack_peek(
1117 ctx
->field_class_stack
)->index
= -1;
1119 field_class_stack_peek(
1120 ctx
->field_class_stack
)->index
=
1124 BT_COMP_LOGD("Resolving field class's child field class: "
1125 "parent-fc-addr=%p, child-fc-addr=%p, "
1126 "index=%" PRIu64
", count=%" PRIu64
,
1127 fc
, child_fc
, i
, field_count
);
1128 ret
= resolve_field_class(child_fc
, ctx
);
1134 field_class_stack_pop(ctx
->field_class_stack
, ctx
);
1146 * Resolves the root field class corresponding to the scope `root_scope`.
1149 int resolve_root_class(enum ctf_scope root_scope
, struct resolve_context
*ctx
)
1153 BT_ASSERT(field_class_stack_size(ctx
->field_class_stack
) == 0);
1154 ctx
->root_scope
= root_scope
;
1155 ret
= resolve_field_class(borrow_class_from_ctx(ctx
, root_scope
), ctx
);
1156 ctx
->root_scope
= CTF_SCOPE_PACKET_UNKNOWN
;
1161 int resolve_event_class_field_classes(struct resolve_context
*ctx
,
1162 struct ctf_event_class
*ec
)
1166 BT_ASSERT(!ctx
->scopes
.event_spec_context
);
1167 BT_ASSERT(!ctx
->scopes
.event_payload
);
1169 if (ec
->is_translated
) {
1174 ctx
->scopes
.event_spec_context
= ec
->spec_context_fc
;
1175 ret
= resolve_root_class(CTF_SCOPE_EVENT_COMMON_CONTEXT
, ctx
);
1177 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve event specific context field class: "
1182 ctx
->scopes
.event_payload
= ec
->payload_fc
;
1183 ret
= resolve_root_class(CTF_SCOPE_EVENT_PAYLOAD
, ctx
);
1185 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve event payload field class: "
1191 ctx
->scopes
.event_spec_context
= NULL
;
1192 ctx
->scopes
.event_payload
= NULL
;
1198 int resolve_stream_class_field_classes(struct resolve_context
*ctx
,
1199 struct ctf_stream_class
*sc
)
1204 BT_ASSERT(!ctx
->scopes
.packet_context
);
1205 BT_ASSERT(!ctx
->scopes
.event_header
);
1206 BT_ASSERT(!ctx
->scopes
.event_common_context
);
1209 if (!sc
->is_translated
) {
1210 ctx
->scopes
.packet_context
= sc
->packet_context_fc
;
1211 ret
= resolve_root_class(CTF_SCOPE_PACKET_CONTEXT
, ctx
);
1213 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve packet context field class: "
1218 ctx
->scopes
.event_header
= sc
->event_header_fc
;
1219 ret
= resolve_root_class(CTF_SCOPE_EVENT_HEADER
, ctx
);
1221 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve event header field class: "
1226 ctx
->scopes
.event_common_context
= sc
->event_common_context_fc
;
1227 ret
= resolve_root_class(CTF_SCOPE_EVENT_COMMON_CONTEXT
, ctx
);
1229 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve event common context field class: "
1235 ctx
->scopes
.packet_context
= sc
->packet_context_fc
;
1236 ctx
->scopes
.event_header
= sc
->event_header_fc
;
1237 ctx
->scopes
.event_common_context
= sc
->event_common_context_fc
;
1239 for (i
= 0; i
< sc
->event_classes
->len
; i
++) {
1240 ctf_event_class
*ec
= (ctf_event_class
*) sc
->event_classes
->pdata
[i
];
1242 ret
= resolve_event_class_field_classes(ctx
, ec
);
1244 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve event class's field classes: "
1245 "ec-id=%" PRIu64
", ec-name=\"%s\"",
1246 ec
->id
, ec
->name
->str
);
1252 ctx
->scopes
.packet_context
= NULL
;
1253 ctx
->scopes
.event_header
= NULL
;
1254 ctx
->scopes
.event_common_context
= NULL
;
1260 int ctf_trace_class_resolve_field_classes(struct ctf_trace_class
*tc
,
1261 struct meta_log_config
*log_cfg
)
1266 resolve_context local_ctx
{};
1267 local_ctx
.log_level
= log_cfg
->log_level
;
1268 local_ctx
.self_comp
= log_cfg
->self_comp
;
1269 local_ctx
.self_comp_class
= log_cfg
->self_comp_class
;
1271 local_ctx
.scopes
.packet_header
= tc
->packet_header_fc
;
1272 local_ctx
.root_scope
= CTF_SCOPE_PACKET_HEADER
;
1274 struct resolve_context
*ctx
= &local_ctx
;
1276 /* Initialize class stack */
1277 ctx
->field_class_stack
= field_class_stack_create();
1278 if (!ctx
->field_class_stack
) {
1279 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot create field class stack.");
1284 if (!tc
->is_translated
) {
1285 ctx
->scopes
.packet_header
= tc
->packet_header_fc
;
1286 ret
= resolve_root_class(CTF_SCOPE_PACKET_HEADER
, ctx
);
1288 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve packet header field class: "
1294 ctx
->scopes
.packet_header
= tc
->packet_header_fc
;
1296 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1297 ctf_stream_class
*sc
= (ctf_stream_class
*) tc
->stream_classes
->pdata
[i
];
1299 ret
= resolve_stream_class_field_classes(ctx
, sc
);
1301 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve stream class's field classes: "
1302 "sc-id=%" PRIu64
, sc
->id
);
1308 field_class_stack_destroy(ctx
->field_class_stack
);