2 * Copyright 2016-2018 - Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
16 #define BT_COMP_LOG_SELF_COMP (ctx->self_comp)
17 #define BT_LOG_OUTPUT_LEVEL (ctx->log_level)
18 #define BT_LOG_TAG "PLUGIN/CTF/META/RESOLVE"
19 #include "logging/comp-logging.h"
21 #include <babeltrace2/babeltrace.h>
22 #include "common/macros.h"
23 #include "common/assert.h"
24 #include "common/common.h"
33 #include "ctf-meta-visitors.h"
36 typedef GPtrArray field_class_stack
;
41 * `fc` contains a compound field class (structure, variant, array,
42 * or sequence) and `index` indicates the index of the field class in
43 * the upper frame (-1 for array and sequence field classes). `name`
44 * indicates the name of the field class in the upper frame (empty
45 * string for array and sequence field classes).
47 struct field_class_stack_frame
{
48 struct ctf_field_class
*fc
;
53 * The current context of the resolving engine.
55 struct resolve_context
{
56 bt_logging_level log_level
;
57 bt_self_component
*self_comp
;
58 struct ctf_trace_class
*tc
;
59 struct ctf_stream_class
*sc
;
60 struct ctf_event_class
*ec
;
63 struct ctf_field_class
*packet_header
;
64 struct ctf_field_class
*packet_context
;
65 struct ctf_field_class
*event_header
;
66 struct ctf_field_class
*event_common_context
;
67 struct ctf_field_class
*event_spec_context
;
68 struct ctf_field_class
*event_payload
;
71 /* Root scope being visited */
72 enum ctf_scope root_scope
;
73 field_class_stack
*field_class_stack
;
74 struct ctf_field_class
*cur_fc
;
77 /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
78 static const char * const absolute_path_prefixes
[] = {
79 [CTF_SCOPE_PACKET_HEADER
] = "trace.packet.header.",
80 [CTF_SCOPE_PACKET_CONTEXT
] = "stream.packet.context.",
81 [CTF_SCOPE_EVENT_HEADER
] = "stream.event.header.",
82 [CTF_SCOPE_EVENT_COMMON_CONTEXT
] = "stream.event.context.",
83 [CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
] = "event.context.",
84 [CTF_SCOPE_EVENT_PAYLOAD
] = "event.fields.",
87 /* Number of path tokens used for the absolute prefixes */
88 static const uint64_t absolute_path_prefix_ptoken_counts
[] = {
89 [CTF_SCOPE_PACKET_HEADER
] = 3,
90 [CTF_SCOPE_PACKET_CONTEXT
] = 3,
91 [CTF_SCOPE_EVENT_HEADER
] = 3,
92 [CTF_SCOPE_EVENT_COMMON_CONTEXT
] = 3,
93 [CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
] = 2,
94 [CTF_SCOPE_EVENT_PAYLOAD
] = 2,
98 void destroy_field_class_stack_frame(struct field_class_stack_frame
*frame
)
108 * Creates a class stack.
111 field_class_stack
*field_class_stack_create(void)
113 return g_ptr_array_new_with_free_func(
114 (GDestroyNotify
) destroy_field_class_stack_frame
);
118 * Destroys a class stack.
121 void field_class_stack_destroy(field_class_stack
*stack
)
124 g_ptr_array_free(stack
, TRUE
);
129 * Pushes a field class onto a class stack.
132 int field_class_stack_push(field_class_stack
*stack
, struct ctf_field_class
*fc
,
133 struct resolve_context
*ctx
)
136 struct field_class_stack_frame
*frame
= NULL
;
139 BT_COMP_LOGE("Invalid parameter: stack or field class is NULL.");
144 frame
= g_new0(struct field_class_stack_frame
, 1);
146 BT_COMP_LOGE_STR("Failed to allocate one field class stack frame.");
151 BT_COMP_LOGD("Pushing field class on context's stack: "
152 "fc-addr=%p, stack-size-before=%u", fc
, stack
->len
);
154 g_ptr_array_add(stack
, frame
);
161 * Checks whether or not `stack` is empty.
164 bool field_class_stack_empty(field_class_stack
*stack
)
166 return stack
->len
== 0;
170 * Returns the number of frames in `stack`.
173 size_t field_class_stack_size(field_class_stack
*stack
)
179 * Returns the top frame of `stack`.
182 struct field_class_stack_frame
*field_class_stack_peek(field_class_stack
*stack
)
185 BT_ASSERT(!field_class_stack_empty(stack
));
187 return g_ptr_array_index(stack
, stack
->len
- 1);
191 * Returns the frame at index `index` in `stack`.
194 struct field_class_stack_frame
*field_class_stack_at(field_class_stack
*stack
,
198 BT_ASSERT(index
< stack
->len
);
200 return g_ptr_array_index(stack
, index
);
204 * Removes the top frame of `stack`.
207 void field_class_stack_pop(field_class_stack
*stack
,
208 struct resolve_context
*ctx
)
210 if (!field_class_stack_empty(stack
)) {
212 * This will call the frame's destructor and free it, as
213 * well as put its contained field class.
215 BT_COMP_LOGD("Popping context's stack: stack-size-before=%u",
217 g_ptr_array_set_size(stack
, stack
->len
- 1);
222 * Returns the scope field class of `scope` in the context `ctx`.
225 struct ctf_field_class
*borrow_class_from_ctx(struct resolve_context
*ctx
,
226 enum ctf_scope scope
)
229 case CTF_SCOPE_PACKET_HEADER
:
230 return ctx
->scopes
.packet_header
;
231 case CTF_SCOPE_PACKET_CONTEXT
:
232 return ctx
->scopes
.packet_context
;
233 case CTF_SCOPE_EVENT_HEADER
:
234 return ctx
->scopes
.event_header
;
235 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
236 return ctx
->scopes
.event_common_context
;
237 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
238 return ctx
->scopes
.event_spec_context
;
239 case CTF_SCOPE_EVENT_PAYLOAD
:
240 return ctx
->scopes
.event_payload
;
249 * Returns the CTF scope from a path string. May return -1 if the path
250 * is found to be relative.
253 enum ctf_scope
get_root_scope_from_absolute_pathstr(const char *pathstr
,
254 struct resolve_context
*ctx
)
256 enum ctf_scope scope
;
257 enum ctf_scope ret
= CTF_SCOPE_PACKET_UNKNOWN
;
258 const size_t prefixes_count
= sizeof(absolute_path_prefixes
) /
259 sizeof(*absolute_path_prefixes
);
261 for (scope
= CTF_SCOPE_PACKET_HEADER
; scope
< CTF_SCOPE_PACKET_HEADER
+
262 prefixes_count
; scope
++) {
264 * Check if path string starts with a known absolute
267 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
269 if (strncmp(pathstr
, absolute_path_prefixes
[scope
],
270 strlen(absolute_path_prefixes
[scope
]))) {
271 /* Prefix does not match: try the next one */
272 BT_COMP_LOGD("Prefix does not match: trying the next one: "
273 "path=\"%s\", path-prefix=\"%s\", scope=%s",
274 pathstr
, absolute_path_prefixes
[scope
],
275 ctf_scope_string(scope
));
281 BT_COMP_LOGD("Found root scope from absolute path: "
282 "path=\"%s\", scope=%s", pathstr
,
283 ctf_scope_string(scope
));
292 * Destroys a path token.
295 void ptokens_destroy_func(gpointer ptoken
, gpointer data
)
297 g_string_free(ptoken
, TRUE
);
301 * Destroys a path token list.
304 void ptokens_destroy(GList
*ptokens
)
310 g_list_foreach(ptokens
, ptokens_destroy_func
, NULL
);
311 g_list_free(ptokens
);
315 * Returns the string contained in a path token.
318 const char *ptoken_get_string(GList
*ptoken
)
320 GString
*tokenstr
= (GString
*) ptoken
->data
;
322 return tokenstr
->str
;
326 * Converts a path string to a path token list, that is, splits the
327 * individual words of a path string into a list of individual
331 GList
*pathstr_to_ptokens(const char *pathstr
, struct resolve_context
*ctx
)
333 const char *at
= pathstr
;
334 const char *last
= at
;
335 GList
*ptokens
= NULL
;
338 if (*at
== '.' || *at
== '\0') {
342 /* Error: empty token */
343 BT_COMP_LOGE("Empty path token: path=\"%s\", pos=%u",
344 pathstr
, (unsigned int) (at
- pathstr
));
348 tokenstr
= g_string_new(NULL
);
349 g_string_append_len(tokenstr
, last
, at
- last
);
350 ptokens
= g_list_append(ptokens
, tokenstr
);
364 ptokens_destroy(ptokens
);
369 * Converts a path token list to a field path object. The path token
370 * list is relative from `fc`. The index of the source looking for its
371 * target within `fc` is indicated by `src_index`. This can be
372 * `INT64_MAX` if the source is contained in `fc`.
374 * `field_path` is an output parameter owned by the caller that must be
378 int ptokens_to_field_path(GList
*ptokens
, struct ctf_field_path
*field_path
,
379 struct ctf_field_class
*fc
, int64_t src_index
,
380 struct resolve_context
*ctx
)
383 GList
*cur_ptoken
= ptokens
;
384 bool first_level_done
= false;
389 struct ctf_field_class
*child_fc
;
390 const char *ft_name
= ptoken_get_string(cur_ptoken
);
392 BT_COMP_LOGD("Current path token: token=\"%s\"", ft_name
);
394 /* Find to which index corresponds the current path token */
395 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
396 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
400 ctf_field_class_compound_get_field_class_index_from_orig_name(
402 if (child_index
< 0) {
404 * Error: field name does not exist or
405 * wrong current class.
407 BT_COMP_LOGD("Cannot get index of field class: "
408 "field-name=\"%s\", "
409 "src-index=%" PRId64
", "
410 "child-index=%" PRId64
", "
411 "first-level-done=%d",
412 ft_name
, src_index
, child_index
,
416 } else if (child_index
> src_index
&&
418 BT_COMP_LOGD("Child field class is located after source field class: "
419 "field-name=\"%s\", "
420 "src-index=%" PRId64
", "
421 "child-index=%" PRId64
", "
422 "first-level-done=%d",
423 ft_name
, src_index
, child_index
,
429 /* Next path token */
430 cur_ptoken
= g_list_next(cur_ptoken
);
431 first_level_done
= true;
434 /* Create new field path entry */
435 ctf_field_path_append_index(field_path
, child_index
);
437 /* Get child field class */
438 child_fc
= ctf_field_class_compound_borrow_field_class_by_index(
442 /* Move child class to current class */
451 * Converts a known absolute path token list to a field path object
452 * within the resolving context `ctx`.
454 * `field_path` is an output parameter owned by the caller that must be
458 int absolute_ptokens_to_field_path(GList
*ptokens
,
459 struct ctf_field_path
*field_path
,
460 struct resolve_context
*ctx
)
464 struct ctf_field_class
*fc
;
467 * Make sure we're not referring to a scope within a translated
470 switch (field_path
->root
) {
471 case CTF_SCOPE_PACKET_HEADER
:
472 if (ctx
->tc
->is_translated
) {
473 BT_COMP_LOGE("Trace class is already translated: "
475 ctf_scope_string(field_path
->root
));
481 case CTF_SCOPE_PACKET_CONTEXT
:
482 case CTF_SCOPE_EVENT_HEADER
:
483 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
485 BT_COMP_LOGE("No current stream class: "
487 ctf_scope_string(field_path
->root
));
492 if (ctx
->sc
->is_translated
) {
493 BT_COMP_LOGE("Stream class is already translated: "
495 ctf_scope_string(field_path
->root
));
501 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
502 case CTF_SCOPE_EVENT_PAYLOAD
:
504 BT_COMP_LOGE("No current event class: "
506 ctf_scope_string(field_path
->root
));
511 if (ctx
->ec
->is_translated
) {
512 BT_COMP_LOGE("Event class is already translated: "
514 ctf_scope_string(field_path
->root
));
525 /* Skip absolute path tokens */
526 cur_ptoken
= g_list_nth(ptokens
,
527 absolute_path_prefix_ptoken_counts
[field_path
->root
]);
529 /* Start with root class */
530 fc
= borrow_class_from_ctx(ctx
, field_path
->root
);
532 /* Error: root class is not available */
533 BT_COMP_LOGE("Root field class is not available: "
535 ctf_scope_string(field_path
->root
));
541 ret
= ptokens_to_field_path(cur_ptoken
, field_path
, fc
, INT64_MAX
, ctx
);
548 * Converts a known relative path token list to a field path object
549 * within the resolving context `ctx`.
551 * `field_path` is an output parameter owned by the caller that must be
555 int relative_ptokens_to_field_path(GList
*ptokens
,
556 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
559 int64_t parent_pos_in_stack
;
560 struct ctf_field_path tail_field_path
;
562 ctf_field_path_init(&tail_field_path
);
563 parent_pos_in_stack
= field_class_stack_size(ctx
->field_class_stack
) - 1;
565 while (parent_pos_in_stack
>= 0) {
566 struct ctf_field_class
*parent_class
=
567 field_class_stack_at(ctx
->field_class_stack
,
568 parent_pos_in_stack
)->fc
;
569 int64_t cur_index
= field_class_stack_at(ctx
->field_class_stack
,
570 parent_pos_in_stack
)->index
;
572 BT_COMP_LOGD("Locating target field class from current parent field class: "
573 "parent-pos=%" PRId64
", parent-fc-addr=%p, "
574 "cur-index=%" PRId64
,
575 parent_pos_in_stack
, parent_class
, cur_index
);
577 /* Locate target from current parent class */
578 ret
= ptokens_to_field_path(ptokens
, &tail_field_path
,
579 parent_class
, cur_index
, ctx
);
581 /* Not found... yet */
582 BT_COMP_LOGD_STR("Not found at this point.");
583 ctf_field_path_clear(&tail_field_path
);
585 /* Found: stitch tail field path to head field path */
587 size_t tail_field_path_len
=
588 tail_field_path
.path
->len
;
591 struct ctf_field_class
*cur_class
=
592 field_class_stack_at(
593 ctx
->field_class_stack
, i
)->fc
;
594 int64_t index
= field_class_stack_at(
595 ctx
->field_class_stack
, i
)->index
;
597 if (cur_class
== parent_class
) {
601 ctf_field_path_append_index(field_path
,
606 for (i
= 0; i
< tail_field_path_len
; i
++) {
608 ctf_field_path_borrow_index_by_index(
609 &tail_field_path
, i
);
611 ctf_field_path_append_index(field_path
,
617 parent_pos_in_stack
--;
620 if (parent_pos_in_stack
< 0) {
625 ctf_field_path_fini(&tail_field_path
);
630 * Converts a path string to a field path object within the resolving
634 int pathstr_to_field_path(const char *pathstr
,
635 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
638 enum ctf_scope root_scope
;
639 GList
*ptokens
= NULL
;
641 /* Convert path string to path tokens */
642 ptokens
= pathstr_to_ptokens(pathstr
, ctx
);
644 BT_COMP_LOGE("Cannot convert path string to path tokens: "
645 "path=\"%s\"", pathstr
);
650 /* Absolute or relative path? */
651 root_scope
= get_root_scope_from_absolute_pathstr(pathstr
, ctx
);
653 if (root_scope
== CTF_SCOPE_PACKET_UNKNOWN
) {
654 /* Relative path: start with current root scope */
655 field_path
->root
= ctx
->root_scope
;
656 BT_COMP_LOGD("Detected relative path: starting with current root scope: "
657 "scope=%s", ctf_scope_string(field_path
->root
));
658 ret
= relative_ptokens_to_field_path(ptokens
, field_path
, ctx
);
660 BT_COMP_LOGE("Cannot get relative field path of path string: "
661 "path=\"%s\", start-scope=%s, end-scope=%s",
662 pathstr
, ctf_scope_string(ctx
->root_scope
),
663 ctf_scope_string(field_path
->root
));
667 /* Absolute path: use found root scope */
668 field_path
->root
= root_scope
;
669 BT_COMP_LOGD("Detected absolute path: using root scope: "
670 "scope=%s", ctf_scope_string(field_path
->root
));
671 ret
= absolute_ptokens_to_field_path(ptokens
, field_path
, ctx
);
673 BT_COMP_LOGE("Cannot get absolute field path of path string: "
674 "path=\"%s\", root-scope=%s",
675 pathstr
, ctf_scope_string(root_scope
));
680 if (BT_LOG_ON_TRACE
&& ret
== 0) {
681 GString
*field_path_pretty
= ctf_field_path_string(field_path
);
682 const char *field_path_pretty_str
=
683 field_path_pretty
? field_path_pretty
->str
: NULL
;
685 BT_COMP_LOGD("Found field path: path=\"%s\", field-path=\"%s\"",
686 pathstr
, field_path_pretty_str
);
688 if (field_path_pretty
) {
689 g_string_free(field_path_pretty
, TRUE
);
694 ptokens_destroy(ptokens
);
699 * Retrieves a field class by following the field path `field_path` in
700 * the resolving context `ctx`.
703 struct ctf_field_class
*field_path_to_field_class(
704 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
707 struct ctf_field_class
*fc
;
709 /* Start with root class */
710 fc
= borrow_class_from_ctx(ctx
, field_path
->root
);
712 /* Error: root class is not available */
713 BT_COMP_LOGE("Root field class is not available: root-scope=%s",
714 ctf_scope_string(field_path
->root
));
719 for (i
= 0; i
< field_path
->path
->len
; i
++) {
720 struct ctf_field_class
*child_fc
;
721 int64_t child_index
=
722 ctf_field_path_borrow_index_by_index(field_path
, i
);
724 /* Get child field class */
725 child_fc
= ctf_field_class_compound_borrow_field_class_by_index(
729 /* Move child class to current class */
738 * Fills the equivalent field path object of the context class stack.
741 void get_ctx_stack_field_path(struct resolve_context
*ctx
,
742 struct ctf_field_path
*field_path
)
746 BT_ASSERT(field_path
);
747 field_path
->root
= ctx
->root_scope
;
748 ctf_field_path_clear(field_path
);
750 for (i
= 0; i
< field_class_stack_size(ctx
->field_class_stack
); i
++) {
751 struct field_class_stack_frame
*frame
=
752 field_class_stack_at(ctx
->field_class_stack
, i
);
754 ctf_field_path_append_index(field_path
, frame
->index
);
759 * Returns the index of the lowest common ancestor of two field path
760 * objects having the same root scope.
762 int64_t get_field_paths_lca_index(struct ctf_field_path
*field_path1
,
763 struct ctf_field_path
*field_path2
,
764 struct resolve_context
*ctx
)
766 int64_t lca_index
= 0;
767 uint64_t field_path1_len
, field_path2_len
;
769 if (BT_LOG_ON_TRACE
) {
770 GString
*field_path1_pretty
=
771 ctf_field_path_string(field_path1
);
772 GString
*field_path2_pretty
=
773 ctf_field_path_string(field_path2
);
774 const char *field_path1_pretty_str
=
775 field_path1_pretty
? field_path1_pretty
->str
: NULL
;
776 const char *field_path2_pretty_str
=
777 field_path2_pretty
? field_path2_pretty
->str
: NULL
;
779 BT_COMP_LOGD("Finding lowest common ancestor (LCA) between two field paths: "
780 "field-path-1=\"%s\", field-path-2=\"%s\"",
781 field_path1_pretty_str
, field_path2_pretty_str
);
783 if (field_path1_pretty
) {
784 g_string_free(field_path1_pretty
, TRUE
);
787 if (field_path2_pretty
) {
788 g_string_free(field_path2_pretty
, TRUE
);
793 * Start from both roots and find the first mismatch.
795 BT_ASSERT(field_path1
->root
== field_path2
->root
);
796 field_path1_len
= field_path1
->path
->len
;
797 field_path2_len
= field_path2
->path
->len
;
800 int64_t target_index
, ctx_index
;
802 if (lca_index
== (int64_t) field_path2_len
||
803 lca_index
== (int64_t) field_path1_len
) {
805 * This means that both field paths never split.
806 * This is invalid because the target cannot be
807 * an ancestor of the source.
809 BT_COMP_LOGE("Source field class is an ancestor of target field class or vice versa: "
810 "lca-index=%" PRId64
", "
811 "field-path-1-len=%" PRIu64
", "
812 "field-path-2-len=%" PRIu64
,
813 lca_index
, field_path1_len
, field_path2_len
);
818 target_index
= ctf_field_path_borrow_index_by_index(field_path1
,
820 ctx_index
= ctf_field_path_borrow_index_by_index(field_path2
,
823 if (target_index
!= ctx_index
) {
824 /* LCA index is the previous */
831 BT_COMP_LOGD("Found LCA: lca-index=%" PRId64
, lca_index
);
836 * Validates a target field path.
839 int validate_target_field_path(struct ctf_field_path
*target_field_path
,
840 struct ctf_field_class
*target_fc
,
841 struct resolve_context
*ctx
)
844 struct ctf_field_path ctx_field_path
;
845 uint64_t target_field_path_len
= target_field_path
->path
->len
;
848 /* Get context field path */
849 ctf_field_path_init(&ctx_field_path
);
850 get_ctx_stack_field_path(ctx
, &ctx_field_path
);
853 * Make sure the target is not a root.
855 if (target_field_path_len
== 0) {
856 BT_COMP_LOGE_STR("Target field path's length is 0 (targeting the root).");
862 * Make sure the root of the target field path is not located
863 * after the context field path's root.
865 if (target_field_path
->root
> ctx_field_path
.root
) {
866 BT_COMP_LOGE("Target field class is located after source field class: "
867 "target-root=%s, source-root=%s",
868 ctf_scope_string(target_field_path
->root
),
869 ctf_scope_string(ctx_field_path
.root
));
874 if (target_field_path
->root
== ctx_field_path
.root
) {
875 int64_t target_index
, ctx_index
;
878 * Find the index of the lowest common ancestor of both field
881 lca_index
= get_field_paths_lca_index(target_field_path
,
882 &ctx_field_path
, ctx
);
884 BT_COMP_LOGE_STR("Cannot get least common ancestor.");
890 * Make sure the target field path is located before the
891 * context field path.
893 target_index
= ctf_field_path_borrow_index_by_index(
894 target_field_path
, (uint64_t) lca_index
);
895 ctx_index
= ctf_field_path_borrow_index_by_index(
896 &ctx_field_path
, (uint64_t) lca_index
);
898 if (target_index
>= ctx_index
) {
899 BT_COMP_LOGE("Target field class's index is greater than or equal to source field class's index in LCA: "
900 "lca-index=%" PRId64
", "
901 "target-index=%" PRId64
", "
902 "source-index=%" PRId64
,
903 lca_index
, target_index
, ctx_index
);
910 * Make sure the target class has the right class and properties.
912 switch (ctx
->cur_fc
->type
) {
913 case CTF_FIELD_CLASS_TYPE_VARIANT
:
914 if (target_fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
915 BT_COMP_LOGE("Variant field class's tag field class is not an enumeration field class: "
916 "tag-fc-addr=%p, tag-fc-id=%d",
917 target_fc
, target_fc
->type
);
922 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
924 struct ctf_field_class_int
*int_fc
= (void *) target_fc
;
926 if (target_fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&&
927 target_fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
928 BT_COMP_LOGE("Sequence field class's length field class is not an unsigned integer field class: "
929 "length-fc-addr=%p, length-fc-id=%d",
930 target_fc
, target_fc
->type
);
935 if (int_fc
->is_signed
) {
936 BT_COMP_LOGE("Sequence field class's length field class is not an unsigned integer field class: "
937 "length-fc-addr=%p, length-fc-id=%d",
938 target_fc
, target_fc
->type
);
949 ctf_field_path_fini(&ctx_field_path
);
954 * Resolves a variant or sequence field class `fc`.
957 int resolve_sequence_or_variant_field_class(struct ctf_field_class
*fc
,
958 struct resolve_context
*ctx
)
962 struct ctf_field_path target_field_path
;
963 struct ctf_field_class
*target_fc
= NULL
;
964 GString
*target_field_path_pretty
= NULL
;
965 const char *target_field_path_pretty_str
;
967 ctf_field_path_init(&target_field_path
);
969 /* Get path string */
971 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
973 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
974 pathstr
= seq_fc
->length_ref
->str
;
977 case CTF_FIELD_CLASS_TYPE_VARIANT
:
979 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
980 pathstr
= var_fc
->tag_ref
->str
;
988 BT_COMP_LOGE_STR("Cannot get path string.");
993 /* Get target field path out of path string */
994 ret
= pathstr_to_field_path(pathstr
, &target_field_path
, ctx
);
996 BT_COMP_LOGE("Cannot get target field path for path string: "
997 "path=\"%s\"", pathstr
);
1001 target_field_path_pretty
= ctf_field_path_string(
1002 &target_field_path
);
1003 target_field_path_pretty_str
=
1004 target_field_path_pretty
? target_field_path_pretty
->str
: NULL
;
1006 /* Get target field class */
1007 target_fc
= field_path_to_field_class(&target_field_path
, ctx
);
1009 BT_COMP_LOGE("Cannot get target field class for path string: "
1010 "path=\"%s\", target-field-path=\"%s\"",
1011 pathstr
, target_field_path_pretty_str
);
1016 ret
= validate_target_field_path(&target_field_path
,
1019 BT_COMP_LOGE("Invalid target field path for path string: "
1020 "path=\"%s\", target-field-path=\"%s\"",
1021 pathstr
, target_field_path_pretty_str
);
1025 /* Set target field path and target field class */
1027 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1029 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
1031 ctf_field_path_copy_content(&seq_fc
->length_path
,
1032 &target_field_path
);
1033 seq_fc
->length_fc
= (void *) target_fc
;
1036 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1038 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1040 ctf_field_path_copy_content(&var_fc
->tag_path
,
1041 &target_field_path
);
1042 ctf_field_class_variant_set_tag_field_class(var_fc
,
1043 (void *) target_fc
);
1051 if (target_field_path_pretty
) {
1052 g_string_free(target_field_path_pretty
, TRUE
);
1055 ctf_field_path_fini(&target_field_path
);
1060 * Resolves a field class `fc`.
1063 int resolve_field_class(struct ctf_field_class
*fc
, struct resolve_context
*ctx
)
1068 /* Field class is not available; still valid */
1074 /* Resolve sequence/variant field class */
1076 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1077 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1078 ret
= resolve_sequence_or_variant_field_class(fc
, ctx
);
1080 BT_COMP_LOGE("Cannot resolve sequence field class's length or variant field class's tag: "
1081 "ret=%d, fc-addr=%p", ret
, fc
);
1090 /* Recurse into compound classes */
1092 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1093 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1094 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1095 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1098 uint64_t field_count
=
1099 ctf_field_class_compound_get_field_class_count(fc
);
1101 ret
= field_class_stack_push(ctx
->field_class_stack
, fc
, ctx
);
1103 BT_COMP_LOGE("Cannot push field class on context's stack: "
1108 for (i
= 0; i
< field_count
; i
++) {
1109 struct ctf_field_class
*child_fc
=
1110 ctf_field_class_compound_borrow_field_class_by_index(
1113 BT_ASSERT(child_fc
);
1115 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
1116 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
1117 field_class_stack_peek(
1118 ctx
->field_class_stack
)->index
= -1;
1120 field_class_stack_peek(
1121 ctx
->field_class_stack
)->index
=
1125 BT_COMP_LOGD("Resolving field class's child field class: "
1126 "parent-fc-addr=%p, child-fc-addr=%p, "
1127 "index=%" PRIu64
", count=%" PRIu64
,
1128 fc
, child_fc
, i
, field_count
);
1129 ret
= resolve_field_class(child_fc
, ctx
);
1135 field_class_stack_pop(ctx
->field_class_stack
, ctx
);
1147 * Resolves the root field class corresponding to the scope `root_scope`.
1150 int resolve_root_class(enum ctf_scope root_scope
, struct resolve_context
*ctx
)
1154 BT_ASSERT(field_class_stack_size(ctx
->field_class_stack
) == 0);
1155 ctx
->root_scope
= root_scope
;
1156 ret
= resolve_field_class(borrow_class_from_ctx(ctx
, root_scope
), ctx
);
1157 ctx
->root_scope
= -1;
1162 int resolve_event_class_field_classes(struct resolve_context
*ctx
,
1163 struct ctf_event_class
*ec
)
1167 BT_ASSERT(!ctx
->scopes
.event_spec_context
);
1168 BT_ASSERT(!ctx
->scopes
.event_payload
);
1170 if (ec
->is_translated
) {
1175 ctx
->scopes
.event_spec_context
= ec
->spec_context_fc
;
1176 ret
= resolve_root_class(CTF_SCOPE_EVENT_COMMON_CONTEXT
, ctx
);
1178 BT_COMP_LOGE("Cannot resolve event specific context field class: "
1183 ctx
->scopes
.event_payload
= ec
->payload_fc
;
1184 ret
= resolve_root_class(CTF_SCOPE_EVENT_PAYLOAD
, ctx
);
1186 BT_COMP_LOGE("Cannot resolve event payload field class: "
1192 ctx
->scopes
.event_spec_context
= NULL
;
1193 ctx
->scopes
.event_payload
= NULL
;
1199 int resolve_stream_class_field_classes(struct resolve_context
*ctx
,
1200 struct ctf_stream_class
*sc
)
1205 BT_ASSERT(!ctx
->scopes
.packet_context
);
1206 BT_ASSERT(!ctx
->scopes
.event_header
);
1207 BT_ASSERT(!ctx
->scopes
.event_common_context
);
1210 if (!sc
->is_translated
) {
1211 ctx
->scopes
.packet_context
= sc
->packet_context_fc
;
1212 ret
= resolve_root_class(CTF_SCOPE_PACKET_CONTEXT
, ctx
);
1214 BT_COMP_LOGE("Cannot resolve packet context field class: "
1219 ctx
->scopes
.event_header
= sc
->event_header_fc
;
1220 ret
= resolve_root_class(CTF_SCOPE_EVENT_HEADER
, ctx
);
1222 BT_COMP_LOGE("Cannot resolve event header field class: "
1227 ctx
->scopes
.event_common_context
= sc
->event_common_context_fc
;
1228 ret
= resolve_root_class(CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
, ctx
);
1230 BT_COMP_LOGE("Cannot resolve event common context field class: "
1236 ctx
->scopes
.packet_context
= sc
->packet_context_fc
;
1237 ctx
->scopes
.event_header
= sc
->event_header_fc
;
1238 ctx
->scopes
.event_common_context
= sc
->event_common_context_fc
;
1240 for (i
= 0; i
< sc
->event_classes
->len
; i
++) {
1241 struct ctf_event_class
*ec
= sc
->event_classes
->pdata
[i
];
1243 ret
= resolve_event_class_field_classes(ctx
, ec
);
1245 BT_COMP_LOGE("Cannot resolve event class's field classes: "
1246 "ec-id=%" PRIu64
", ec-name=\"%s\"",
1247 ec
->id
, ec
->name
->str
);
1253 ctx
->scopes
.packet_context
= NULL
;
1254 ctx
->scopes
.event_header
= NULL
;
1255 ctx
->scopes
.event_common_context
= NULL
;
1261 int ctf_trace_class_resolve_field_classes(struct ctf_trace_class
*tc
,
1262 struct meta_log_config
*log_cfg
)
1266 struct resolve_context local_ctx
= {
1267 .log_level
= log_cfg
->log_level
,
1268 .self_comp
= log_cfg
->self_comp
,
1273 .packet_header
= tc
->packet_header_fc
,
1274 .packet_context
= NULL
,
1275 .event_header
= NULL
,
1276 .event_common_context
= NULL
,
1277 .event_spec_context
= NULL
,
1278 .event_payload
= NULL
,
1280 .root_scope
= CTF_SCOPE_PACKET_HEADER
,
1283 struct resolve_context
*ctx
= &local_ctx
;
1285 /* Initialize class stack */
1286 ctx
->field_class_stack
= field_class_stack_create();
1287 if (!ctx
->field_class_stack
) {
1288 BT_COMP_LOGE_STR("Cannot create field class stack.");
1293 if (!tc
->is_translated
) {
1294 ctx
->scopes
.packet_header
= tc
->packet_header_fc
;
1295 ret
= resolve_root_class(CTF_SCOPE_PACKET_HEADER
, ctx
);
1297 BT_COMP_LOGE("Cannot resolve packet header field class: "
1303 ctx
->scopes
.packet_header
= tc
->packet_header_fc
;
1305 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1306 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1308 ret
= resolve_stream_class_field_classes(ctx
, sc
);
1310 BT_COMP_LOGE("Cannot resolve stream class's field classes: "
1311 "sc-id=%" PRIu64
, sc
->id
);
1317 field_class_stack_destroy(ctx
->field_class_stack
);