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"
34 #include "ctf-meta-visitors.h"
37 typedef GPtrArray field_class_stack
;
42 * `fc` contains a compound field class (structure, variant, array,
43 * or sequence) and `index` indicates the index of the field class in
44 * the upper frame (-1 for array and sequence field classes). `name`
45 * indicates the name of the field class in the upper frame (empty
46 * string for array and sequence field classes).
48 struct field_class_stack_frame
{
49 struct ctf_field_class
*fc
;
54 * The current context of the resolving engine.
56 struct resolve_context
{
57 bt_logging_level log_level
;
58 bt_self_component
*self_comp
;
59 struct ctf_trace_class
*tc
;
60 struct ctf_stream_class
*sc
;
61 struct ctf_event_class
*ec
;
64 struct ctf_field_class
*packet_header
;
65 struct ctf_field_class
*packet_context
;
66 struct ctf_field_class
*event_header
;
67 struct ctf_field_class
*event_common_context
;
68 struct ctf_field_class
*event_spec_context
;
69 struct ctf_field_class
*event_payload
;
72 /* Root scope being visited */
73 enum ctf_scope root_scope
;
74 field_class_stack
*field_class_stack
;
75 struct ctf_field_class
*cur_fc
;
78 /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
79 static const char * const absolute_path_prefixes
[] = {
80 [CTF_SCOPE_PACKET_HEADER
] = "trace.packet.header.",
81 [CTF_SCOPE_PACKET_CONTEXT
] = "stream.packet.context.",
82 [CTF_SCOPE_EVENT_HEADER
] = "stream.event.header.",
83 [CTF_SCOPE_EVENT_COMMON_CONTEXT
] = "stream.event.context.",
84 [CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
] = "event.context.",
85 [CTF_SCOPE_EVENT_PAYLOAD
] = "event.fields.",
88 /* Number of path tokens used for the absolute prefixes */
89 static const uint64_t absolute_path_prefix_ptoken_counts
[] = {
90 [CTF_SCOPE_PACKET_HEADER
] = 3,
91 [CTF_SCOPE_PACKET_CONTEXT
] = 3,
92 [CTF_SCOPE_EVENT_HEADER
] = 3,
93 [CTF_SCOPE_EVENT_COMMON_CONTEXT
] = 3,
94 [CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
] = 2,
95 [CTF_SCOPE_EVENT_PAYLOAD
] = 2,
99 void destroy_field_class_stack_frame(struct field_class_stack_frame
*frame
)
109 * Creates a class stack.
112 field_class_stack
*field_class_stack_create(void)
114 return g_ptr_array_new_with_free_func(
115 (GDestroyNotify
) destroy_field_class_stack_frame
);
119 * Destroys a class stack.
122 void field_class_stack_destroy(field_class_stack
*stack
)
125 g_ptr_array_free(stack
, TRUE
);
130 * Pushes a field class onto a class stack.
133 int field_class_stack_push(field_class_stack
*stack
, struct ctf_field_class
*fc
,
134 struct resolve_context
*ctx
)
137 struct field_class_stack_frame
*frame
= NULL
;
140 BT_COMP_LOGE("Invalid parameter: stack or field class is NULL.");
145 frame
= g_new0(struct field_class_stack_frame
, 1);
147 BT_COMP_LOGE_STR("Failed to allocate one field class stack frame.");
152 BT_COMP_LOGD("Pushing field class on context's stack: "
153 "fc-addr=%p, stack-size-before=%u", fc
, stack
->len
);
155 g_ptr_array_add(stack
, frame
);
162 * Checks whether or not `stack` is empty.
165 bool field_class_stack_empty(field_class_stack
*stack
)
167 return stack
->len
== 0;
171 * Returns the number of frames in `stack`.
174 size_t field_class_stack_size(field_class_stack
*stack
)
180 * Returns the top frame of `stack`.
183 struct field_class_stack_frame
*field_class_stack_peek(field_class_stack
*stack
)
186 BT_ASSERT(!field_class_stack_empty(stack
));
188 return g_ptr_array_index(stack
, stack
->len
- 1);
192 * Returns the frame at index `index` in `stack`.
195 struct field_class_stack_frame
*field_class_stack_at(field_class_stack
*stack
,
199 BT_ASSERT(index
< stack
->len
);
201 return g_ptr_array_index(stack
, index
);
205 * Removes the top frame of `stack`.
208 void field_class_stack_pop(field_class_stack
*stack
,
209 struct resolve_context
*ctx
)
211 if (!field_class_stack_empty(stack
)) {
213 * This will call the frame's destructor and free it, as
214 * well as put its contained field class.
216 BT_COMP_LOGD("Popping context's stack: stack-size-before=%u",
218 g_ptr_array_set_size(stack
, stack
->len
- 1);
223 * Returns the scope field class of `scope` in the context `ctx`.
226 struct ctf_field_class
*borrow_class_from_ctx(struct resolve_context
*ctx
,
227 enum ctf_scope scope
)
230 case CTF_SCOPE_PACKET_HEADER
:
231 return ctx
->scopes
.packet_header
;
232 case CTF_SCOPE_PACKET_CONTEXT
:
233 return ctx
->scopes
.packet_context
;
234 case CTF_SCOPE_EVENT_HEADER
:
235 return ctx
->scopes
.event_header
;
236 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
237 return ctx
->scopes
.event_common_context
;
238 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
239 return ctx
->scopes
.event_spec_context
;
240 case CTF_SCOPE_EVENT_PAYLOAD
:
241 return ctx
->scopes
.event_payload
;
250 * Returns the CTF scope from a path string. May return -1 if the path
251 * is found to be relative.
254 enum ctf_scope
get_root_scope_from_absolute_pathstr(const char *pathstr
,
255 struct resolve_context
*ctx
)
257 enum ctf_scope scope
;
258 enum ctf_scope ret
= CTF_SCOPE_PACKET_UNKNOWN
;
259 const size_t prefixes_count
= sizeof(absolute_path_prefixes
) /
260 sizeof(*absolute_path_prefixes
);
262 for (scope
= CTF_SCOPE_PACKET_HEADER
; scope
< CTF_SCOPE_PACKET_HEADER
+
263 prefixes_count
; scope
++) {
265 * Check if path string starts with a known absolute
268 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
270 if (strncmp(pathstr
, absolute_path_prefixes
[scope
],
271 strlen(absolute_path_prefixes
[scope
]))) {
272 /* Prefix does not match: try the next one */
273 BT_COMP_LOGD("Prefix does not match: trying the next one: "
274 "path=\"%s\", path-prefix=\"%s\", scope=%s",
275 pathstr
, absolute_path_prefixes
[scope
],
276 ctf_scope_string(scope
));
282 BT_COMP_LOGD("Found root scope from absolute path: "
283 "path=\"%s\", scope=%s", pathstr
,
284 ctf_scope_string(scope
));
293 * Destroys a path token.
296 void ptokens_destroy_func(gpointer ptoken
, gpointer data
)
298 g_string_free(ptoken
, TRUE
);
302 * Destroys a path token list.
305 void ptokens_destroy(GList
*ptokens
)
311 g_list_foreach(ptokens
, ptokens_destroy_func
, NULL
);
312 g_list_free(ptokens
);
316 * Returns the string contained in a path token.
319 const char *ptoken_get_string(GList
*ptoken
)
321 GString
*tokenstr
= (GString
*) ptoken
->data
;
323 return tokenstr
->str
;
327 * Converts a path string to a path token list, that is, splits the
328 * individual words of a path string into a list of individual
332 GList
*pathstr_to_ptokens(const char *pathstr
, struct resolve_context
*ctx
)
334 const char *at
= pathstr
;
335 const char *last
= at
;
336 GList
*ptokens
= NULL
;
339 if (*at
== '.' || *at
== '\0') {
343 /* Error: empty token */
344 BT_COMP_LOGE("Empty path token: path=\"%s\", pos=%u",
345 pathstr
, (unsigned int) (at
- pathstr
));
349 tokenstr
= g_string_new(NULL
);
350 g_string_append_len(tokenstr
, last
, at
- last
);
351 ptokens
= g_list_append(ptokens
, tokenstr
);
365 ptokens_destroy(ptokens
);
370 * Converts a path token list to a field path object. The path token
371 * list is relative from `fc`. The index of the source looking for its
372 * target within `fc` is indicated by `src_index`. This can be
373 * `INT64_MAX` if the source is contained in `fc`.
375 * `field_path` is an output parameter owned by the caller that must be
379 int ptokens_to_field_path(GList
*ptokens
, struct ctf_field_path
*field_path
,
380 struct ctf_field_class
*fc
, int64_t src_index
,
381 struct resolve_context
*ctx
)
384 GList
*cur_ptoken
= ptokens
;
385 bool first_level_done
= false;
390 struct ctf_field_class
*child_fc
;
391 const char *ft_name
= ptoken_get_string(cur_ptoken
);
393 BT_COMP_LOGD("Current path token: token=\"%s\"", ft_name
);
395 /* Find to which index corresponds the current path token */
396 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
397 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
401 ctf_field_class_compound_get_field_class_index_from_orig_name(
403 if (child_index
< 0) {
405 * Error: field name does not exist or
406 * wrong current class.
408 BT_COMP_LOGD("Cannot get index of field class: "
409 "field-name=\"%s\", "
410 "src-index=%" PRId64
", "
411 "child-index=%" PRId64
", "
412 "first-level-done=%d",
413 ft_name
, src_index
, child_index
,
417 } else if (child_index
> src_index
&&
419 BT_COMP_LOGD("Child field class is located after source field class: "
420 "field-name=\"%s\", "
421 "src-index=%" PRId64
", "
422 "child-index=%" PRId64
", "
423 "first-level-done=%d",
424 ft_name
, src_index
, child_index
,
430 /* Next path token */
431 cur_ptoken
= g_list_next(cur_ptoken
);
432 first_level_done
= true;
435 /* Create new field path entry */
436 ctf_field_path_append_index(field_path
, child_index
);
438 /* Get child field class */
439 child_fc
= ctf_field_class_compound_borrow_field_class_by_index(
443 /* Move child class to current class */
452 * Converts a known absolute path token list to a field path object
453 * within the resolving context `ctx`.
455 * `field_path` is an output parameter owned by the caller that must be
459 int absolute_ptokens_to_field_path(GList
*ptokens
,
460 struct ctf_field_path
*field_path
,
461 struct resolve_context
*ctx
)
465 struct ctf_field_class
*fc
;
468 * Make sure we're not referring to a scope within a translated
471 switch (field_path
->root
) {
472 case CTF_SCOPE_PACKET_HEADER
:
473 if (ctx
->tc
->is_translated
) {
474 BT_COMP_LOGE("Trace class is already translated: "
476 ctf_scope_string(field_path
->root
));
482 case CTF_SCOPE_PACKET_CONTEXT
:
483 case CTF_SCOPE_EVENT_HEADER
:
484 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
486 BT_COMP_LOGE("No current stream class: "
488 ctf_scope_string(field_path
->root
));
493 if (ctx
->sc
->is_translated
) {
494 BT_COMP_LOGE("Stream class is already translated: "
496 ctf_scope_string(field_path
->root
));
502 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
503 case CTF_SCOPE_EVENT_PAYLOAD
:
505 BT_COMP_LOGE("No current event class: "
507 ctf_scope_string(field_path
->root
));
512 if (ctx
->ec
->is_translated
) {
513 BT_COMP_LOGE("Event class is already translated: "
515 ctf_scope_string(field_path
->root
));
526 /* Skip absolute path tokens */
527 cur_ptoken
= g_list_nth(ptokens
,
528 absolute_path_prefix_ptoken_counts
[field_path
->root
]);
530 /* Start with root class */
531 fc
= borrow_class_from_ctx(ctx
, field_path
->root
);
533 /* Error: root class is not available */
534 BT_COMP_LOGE("Root field class is not available: "
536 ctf_scope_string(field_path
->root
));
542 ret
= ptokens_to_field_path(cur_ptoken
, field_path
, fc
, INT64_MAX
, ctx
);
549 * Converts a known relative path token list to a field path object
550 * within the resolving context `ctx`.
552 * `field_path` is an output parameter owned by the caller that must be
556 int relative_ptokens_to_field_path(GList
*ptokens
,
557 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
560 int64_t parent_pos_in_stack
;
561 struct ctf_field_path tail_field_path
;
563 ctf_field_path_init(&tail_field_path
);
564 parent_pos_in_stack
= field_class_stack_size(ctx
->field_class_stack
) - 1;
566 while (parent_pos_in_stack
>= 0) {
567 struct ctf_field_class
*parent_class
=
568 field_class_stack_at(ctx
->field_class_stack
,
569 parent_pos_in_stack
)->fc
;
570 int64_t cur_index
= field_class_stack_at(ctx
->field_class_stack
,
571 parent_pos_in_stack
)->index
;
573 BT_COMP_LOGD("Locating target field class from current parent field class: "
574 "parent-pos=%" PRId64
", parent-fc-addr=%p, "
575 "cur-index=%" PRId64
,
576 parent_pos_in_stack
, parent_class
, cur_index
);
578 /* Locate target from current parent class */
579 ret
= ptokens_to_field_path(ptokens
, &tail_field_path
,
580 parent_class
, cur_index
, ctx
);
582 /* Not found... yet */
583 BT_COMP_LOGD_STR("Not found at this point.");
584 ctf_field_path_clear(&tail_field_path
);
586 /* Found: stitch tail field path to head field path */
588 size_t tail_field_path_len
=
589 tail_field_path
.path
->len
;
592 struct ctf_field_class
*cur_class
=
593 field_class_stack_at(
594 ctx
->field_class_stack
, i
)->fc
;
595 int64_t index
= field_class_stack_at(
596 ctx
->field_class_stack
, i
)->index
;
598 if (cur_class
== parent_class
) {
602 ctf_field_path_append_index(field_path
,
607 for (i
= 0; i
< tail_field_path_len
; i
++) {
609 ctf_field_path_borrow_index_by_index(
610 &tail_field_path
, i
);
612 ctf_field_path_append_index(field_path
,
618 parent_pos_in_stack
--;
621 if (parent_pos_in_stack
< 0) {
626 ctf_field_path_fini(&tail_field_path
);
631 * Converts a path string to a field path object within the resolving
635 int pathstr_to_field_path(const char *pathstr
,
636 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
639 enum ctf_scope root_scope
;
640 GList
*ptokens
= NULL
;
642 /* Convert path string to path tokens */
643 ptokens
= pathstr_to_ptokens(pathstr
, ctx
);
645 BT_COMP_LOGE("Cannot convert path string to path tokens: "
646 "path=\"%s\"", pathstr
);
651 /* Absolute or relative path? */
652 root_scope
= get_root_scope_from_absolute_pathstr(pathstr
, ctx
);
654 if (root_scope
== CTF_SCOPE_PACKET_UNKNOWN
) {
655 /* Relative path: start with current root scope */
656 field_path
->root
= ctx
->root_scope
;
657 BT_COMP_LOGD("Detected relative path: starting with current root scope: "
658 "scope=%s", ctf_scope_string(field_path
->root
));
659 ret
= relative_ptokens_to_field_path(ptokens
, field_path
, ctx
);
661 BT_COMP_LOGE("Cannot get relative field path of path string: "
662 "path=\"%s\", start-scope=%s, end-scope=%s",
663 pathstr
, ctf_scope_string(ctx
->root_scope
),
664 ctf_scope_string(field_path
->root
));
668 /* Absolute path: use found root scope */
669 field_path
->root
= root_scope
;
670 BT_COMP_LOGD("Detected absolute path: using root scope: "
671 "scope=%s", ctf_scope_string(field_path
->root
));
672 ret
= absolute_ptokens_to_field_path(ptokens
, field_path
, ctx
);
674 BT_COMP_LOGE("Cannot get absolute field path of path string: "
675 "path=\"%s\", root-scope=%s",
676 pathstr
, ctf_scope_string(root_scope
));
681 if (BT_LOG_ON_TRACE
&& ret
== 0) {
682 GString
*field_path_pretty
= ctf_field_path_string(field_path
);
683 const char *field_path_pretty_str
=
684 field_path_pretty
? field_path_pretty
->str
: NULL
;
686 BT_COMP_LOGD("Found field path: path=\"%s\", field-path=\"%s\"",
687 pathstr
, field_path_pretty_str
);
689 if (field_path_pretty
) {
690 g_string_free(field_path_pretty
, TRUE
);
695 ptokens_destroy(ptokens
);
700 * Retrieves a field class by following the field path `field_path` in
701 * the resolving context `ctx`.
704 struct ctf_field_class
*field_path_to_field_class(
705 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
708 struct ctf_field_class
*fc
;
710 /* Start with root class */
711 fc
= borrow_class_from_ctx(ctx
, field_path
->root
);
713 /* Error: root class is not available */
714 BT_COMP_LOGE("Root field class is not available: root-scope=%s",
715 ctf_scope_string(field_path
->root
));
720 for (i
= 0; i
< field_path
->path
->len
; i
++) {
721 struct ctf_field_class
*child_fc
;
722 int64_t child_index
=
723 ctf_field_path_borrow_index_by_index(field_path
, i
);
725 /* Get child field class */
726 child_fc
= ctf_field_class_compound_borrow_field_class_by_index(
730 /* Move child class to current class */
739 * Fills the equivalent field path object of the context class stack.
742 void get_ctx_stack_field_path(struct resolve_context
*ctx
,
743 struct ctf_field_path
*field_path
)
747 BT_ASSERT(field_path
);
748 field_path
->root
= ctx
->root_scope
;
749 ctf_field_path_clear(field_path
);
751 for (i
= 0; i
< field_class_stack_size(ctx
->field_class_stack
); i
++) {
752 struct field_class_stack_frame
*frame
=
753 field_class_stack_at(ctx
->field_class_stack
, i
);
755 ctf_field_path_append_index(field_path
, frame
->index
);
760 * Returns the index of the lowest common ancestor of two field path
761 * objects having the same root scope.
764 int64_t get_field_paths_lca_index(struct ctf_field_path
*field_path1
,
765 struct ctf_field_path
*field_path2
,
766 struct resolve_context
*ctx
)
768 int64_t lca_index
= 0;
769 uint64_t field_path1_len
, field_path2_len
;
771 if (BT_LOG_ON_TRACE
) {
772 GString
*field_path1_pretty
=
773 ctf_field_path_string(field_path1
);
774 GString
*field_path2_pretty
=
775 ctf_field_path_string(field_path2
);
776 const char *field_path1_pretty_str
=
777 field_path1_pretty
? field_path1_pretty
->str
: NULL
;
778 const char *field_path2_pretty_str
=
779 field_path2_pretty
? field_path2_pretty
->str
: NULL
;
781 BT_COMP_LOGD("Finding lowest common ancestor (LCA) between two field paths: "
782 "field-path-1=\"%s\", field-path-2=\"%s\"",
783 field_path1_pretty_str
, field_path2_pretty_str
);
785 if (field_path1_pretty
) {
786 g_string_free(field_path1_pretty
, TRUE
);
789 if (field_path2_pretty
) {
790 g_string_free(field_path2_pretty
, TRUE
);
795 * Start from both roots and find the first mismatch.
797 BT_ASSERT(field_path1
->root
== field_path2
->root
);
798 field_path1_len
= field_path1
->path
->len
;
799 field_path2_len
= field_path2
->path
->len
;
802 int64_t target_index
, ctx_index
;
804 if (lca_index
== (int64_t) field_path2_len
||
805 lca_index
== (int64_t) field_path1_len
) {
807 * This means that both field paths never split.
808 * This is invalid because the target cannot be
809 * an ancestor of the source.
811 BT_COMP_LOGE("Source field class is an ancestor of target field class or vice versa: "
812 "lca-index=%" PRId64
", "
813 "field-path-1-len=%" PRIu64
", "
814 "field-path-2-len=%" PRIu64
,
815 lca_index
, field_path1_len
, field_path2_len
);
820 target_index
= ctf_field_path_borrow_index_by_index(field_path1
,
822 ctx_index
= ctf_field_path_borrow_index_by_index(field_path2
,
825 if (target_index
!= ctx_index
) {
826 /* LCA index is the previous */
833 BT_COMP_LOGD("Found LCA: lca-index=%" PRId64
, lca_index
);
838 * Validates a target field path.
841 int validate_target_field_path(struct ctf_field_path
*target_field_path
,
842 struct ctf_field_class
*target_fc
,
843 struct resolve_context
*ctx
)
846 struct ctf_field_path ctx_field_path
;
847 uint64_t target_field_path_len
= target_field_path
->path
->len
;
850 /* Get context field path */
851 ctf_field_path_init(&ctx_field_path
);
852 get_ctx_stack_field_path(ctx
, &ctx_field_path
);
855 * Make sure the target is not a root.
857 if (target_field_path_len
== 0) {
858 BT_COMP_LOGE_STR("Target field path's length is 0 (targeting the root).");
864 * Make sure the root of the target field path is not located
865 * after the context field path's root.
867 if (target_field_path
->root
> ctx_field_path
.root
) {
868 BT_COMP_LOGE("Target field class is located after source field class: "
869 "target-root=%s, source-root=%s",
870 ctf_scope_string(target_field_path
->root
),
871 ctf_scope_string(ctx_field_path
.root
));
876 if (target_field_path
->root
== ctx_field_path
.root
) {
877 int64_t target_index
, ctx_index
;
880 * Find the index of the lowest common ancestor of both field
883 lca_index
= get_field_paths_lca_index(target_field_path
,
884 &ctx_field_path
, ctx
);
886 BT_COMP_LOGE_STR("Cannot get least common ancestor.");
892 * Make sure the target field path is located before the
893 * context field path.
895 target_index
= ctf_field_path_borrow_index_by_index(
896 target_field_path
, (uint64_t) lca_index
);
897 ctx_index
= ctf_field_path_borrow_index_by_index(
898 &ctx_field_path
, (uint64_t) lca_index
);
900 if (target_index
>= ctx_index
) {
901 BT_COMP_LOGE("Target field class's index is greater than or equal to source field class's index in LCA: "
902 "lca-index=%" PRId64
", "
903 "target-index=%" PRId64
", "
904 "source-index=%" PRId64
,
905 lca_index
, target_index
, ctx_index
);
912 * Make sure the target class has the right class and properties.
914 switch (ctx
->cur_fc
->type
) {
915 case CTF_FIELD_CLASS_TYPE_VARIANT
:
916 if (target_fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
917 BT_COMP_LOGE("Variant field class's tag field class is not an enumeration field class: "
918 "tag-fc-addr=%p, tag-fc-id=%d",
919 target_fc
, target_fc
->type
);
924 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
926 struct ctf_field_class_int
*int_fc
= (void *) target_fc
;
928 if (target_fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&&
929 target_fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
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
);
937 if (int_fc
->is_signed
) {
938 BT_COMP_LOGE("Sequence field class's length field class is not an unsigned integer field class: "
939 "length-fc-addr=%p, length-fc-id=%d",
940 target_fc
, target_fc
->type
);
951 ctf_field_path_fini(&ctx_field_path
);
956 * Resolves a variant or sequence field class `fc`.
959 int resolve_sequence_or_variant_field_class(struct ctf_field_class
*fc
,
960 struct resolve_context
*ctx
)
964 struct ctf_field_path target_field_path
;
965 struct ctf_field_class
*target_fc
= NULL
;
966 GString
*target_field_path_pretty
= NULL
;
967 const char *target_field_path_pretty_str
;
969 ctf_field_path_init(&target_field_path
);
971 /* Get path string */
973 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
975 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
976 pathstr
= seq_fc
->length_ref
->str
;
979 case CTF_FIELD_CLASS_TYPE_VARIANT
:
981 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
982 pathstr
= var_fc
->tag_ref
->str
;
990 BT_COMP_LOGE_STR("Cannot get path string.");
995 /* Get target field path out of path string */
996 ret
= pathstr_to_field_path(pathstr
, &target_field_path
, ctx
);
998 BT_COMP_LOGE("Cannot get target field path for path string: "
999 "path=\"%s\"", pathstr
);
1003 target_field_path_pretty
= ctf_field_path_string(
1004 &target_field_path
);
1005 target_field_path_pretty_str
=
1006 target_field_path_pretty
? target_field_path_pretty
->str
: NULL
;
1008 /* Get target field class */
1009 target_fc
= field_path_to_field_class(&target_field_path
, ctx
);
1011 BT_COMP_LOGE("Cannot get target field class for path string: "
1012 "path=\"%s\", target-field-path=\"%s\"",
1013 pathstr
, target_field_path_pretty_str
);
1018 ret
= validate_target_field_path(&target_field_path
,
1021 BT_COMP_LOGE("Invalid target field path for path string: "
1022 "path=\"%s\", target-field-path=\"%s\"",
1023 pathstr
, target_field_path_pretty_str
);
1027 /* Set target field path and target field class */
1029 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1031 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
1033 ctf_field_path_copy_content(&seq_fc
->length_path
,
1034 &target_field_path
);
1035 seq_fc
->length_fc
= (void *) target_fc
;
1038 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1040 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1042 ctf_field_path_copy_content(&var_fc
->tag_path
,
1043 &target_field_path
);
1044 ctf_field_class_variant_set_tag_field_class(var_fc
,
1045 (void *) target_fc
);
1053 if (target_field_path_pretty
) {
1054 g_string_free(target_field_path_pretty
, TRUE
);
1057 ctf_field_path_fini(&target_field_path
);
1062 * Resolves a field class `fc`.
1065 int resolve_field_class(struct ctf_field_class
*fc
, struct resolve_context
*ctx
)
1070 /* Field class is not available; still valid */
1076 /* Resolve sequence/variant field class */
1078 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1079 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1080 ret
= resolve_sequence_or_variant_field_class(fc
, ctx
);
1082 BT_COMP_LOGE("Cannot resolve sequence field class's length or variant field class's tag: "
1083 "ret=%d, fc-addr=%p", ret
, fc
);
1092 /* Recurse into compound classes */
1094 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1095 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1096 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1097 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1100 uint64_t field_count
=
1101 ctf_field_class_compound_get_field_class_count(fc
);
1103 ret
= field_class_stack_push(ctx
->field_class_stack
, fc
, ctx
);
1105 BT_COMP_LOGE("Cannot push field class on context's stack: "
1110 for (i
= 0; i
< field_count
; i
++) {
1111 struct ctf_field_class
*child_fc
=
1112 ctf_field_class_compound_borrow_field_class_by_index(
1115 BT_ASSERT(child_fc
);
1117 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
1118 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
1119 field_class_stack_peek(
1120 ctx
->field_class_stack
)->index
= -1;
1122 field_class_stack_peek(
1123 ctx
->field_class_stack
)->index
=
1127 BT_COMP_LOGD("Resolving field class's child field class: "
1128 "parent-fc-addr=%p, child-fc-addr=%p, "
1129 "index=%" PRIu64
", count=%" PRIu64
,
1130 fc
, child_fc
, i
, field_count
);
1131 ret
= resolve_field_class(child_fc
, ctx
);
1137 field_class_stack_pop(ctx
->field_class_stack
, ctx
);
1149 * Resolves the root field class corresponding to the scope `root_scope`.
1152 int resolve_root_class(enum ctf_scope root_scope
, struct resolve_context
*ctx
)
1156 BT_ASSERT(field_class_stack_size(ctx
->field_class_stack
) == 0);
1157 ctx
->root_scope
= root_scope
;
1158 ret
= resolve_field_class(borrow_class_from_ctx(ctx
, root_scope
), ctx
);
1159 ctx
->root_scope
= -1;
1164 int resolve_event_class_field_classes(struct resolve_context
*ctx
,
1165 struct ctf_event_class
*ec
)
1169 BT_ASSERT(!ctx
->scopes
.event_spec_context
);
1170 BT_ASSERT(!ctx
->scopes
.event_payload
);
1172 if (ec
->is_translated
) {
1177 ctx
->scopes
.event_spec_context
= ec
->spec_context_fc
;
1178 ret
= resolve_root_class(CTF_SCOPE_EVENT_COMMON_CONTEXT
, ctx
);
1180 BT_COMP_LOGE("Cannot resolve event specific context field class: "
1185 ctx
->scopes
.event_payload
= ec
->payload_fc
;
1186 ret
= resolve_root_class(CTF_SCOPE_EVENT_PAYLOAD
, ctx
);
1188 BT_COMP_LOGE("Cannot resolve event payload field class: "
1194 ctx
->scopes
.event_spec_context
= NULL
;
1195 ctx
->scopes
.event_payload
= NULL
;
1201 int resolve_stream_class_field_classes(struct resolve_context
*ctx
,
1202 struct ctf_stream_class
*sc
)
1207 BT_ASSERT(!ctx
->scopes
.packet_context
);
1208 BT_ASSERT(!ctx
->scopes
.event_header
);
1209 BT_ASSERT(!ctx
->scopes
.event_common_context
);
1212 if (!sc
->is_translated
) {
1213 ctx
->scopes
.packet_context
= sc
->packet_context_fc
;
1214 ret
= resolve_root_class(CTF_SCOPE_PACKET_CONTEXT
, ctx
);
1216 BT_COMP_LOGE("Cannot resolve packet context field class: "
1221 ctx
->scopes
.event_header
= sc
->event_header_fc
;
1222 ret
= resolve_root_class(CTF_SCOPE_EVENT_HEADER
, ctx
);
1224 BT_COMP_LOGE("Cannot resolve event header field class: "
1229 ctx
->scopes
.event_common_context
= sc
->event_common_context_fc
;
1230 ret
= resolve_root_class(CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
, ctx
);
1232 BT_COMP_LOGE("Cannot resolve event common context field class: "
1238 ctx
->scopes
.packet_context
= sc
->packet_context_fc
;
1239 ctx
->scopes
.event_header
= sc
->event_header_fc
;
1240 ctx
->scopes
.event_common_context
= sc
->event_common_context_fc
;
1242 for (i
= 0; i
< sc
->event_classes
->len
; i
++) {
1243 struct ctf_event_class
*ec
= sc
->event_classes
->pdata
[i
];
1245 ret
= resolve_event_class_field_classes(ctx
, ec
);
1247 BT_COMP_LOGE("Cannot resolve event class's field classes: "
1248 "ec-id=%" PRIu64
", ec-name=\"%s\"",
1249 ec
->id
, ec
->name
->str
);
1255 ctx
->scopes
.packet_context
= NULL
;
1256 ctx
->scopes
.event_header
= NULL
;
1257 ctx
->scopes
.event_common_context
= NULL
;
1263 int ctf_trace_class_resolve_field_classes(struct ctf_trace_class
*tc
,
1264 struct meta_log_config
*log_cfg
)
1268 struct resolve_context local_ctx
= {
1269 .log_level
= log_cfg
->log_level
,
1270 .self_comp
= log_cfg
->self_comp
,
1275 .packet_header
= tc
->packet_header_fc
,
1276 .packet_context
= NULL
,
1277 .event_header
= NULL
,
1278 .event_common_context
= NULL
,
1279 .event_spec_context
= NULL
,
1280 .event_payload
= NULL
,
1282 .root_scope
= CTF_SCOPE_PACKET_HEADER
,
1285 struct resolve_context
*ctx
= &local_ctx
;
1287 /* Initialize class stack */
1288 ctx
->field_class_stack
= field_class_stack_create();
1289 if (!ctx
->field_class_stack
) {
1290 BT_COMP_LOGE_STR("Cannot create field class stack.");
1295 if (!tc
->is_translated
) {
1296 ctx
->scopes
.packet_header
= tc
->packet_header_fc
;
1297 ret
= resolve_root_class(CTF_SCOPE_PACKET_HEADER
, ctx
);
1299 BT_COMP_LOGE("Cannot resolve packet header field class: "
1305 ctx
->scopes
.packet_header
= tc
->packet_header_fc
;
1307 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1308 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1310 ret
= resolve_stream_class_field_classes(ctx
, sc
);
1312 BT_COMP_LOGE("Cannot resolve stream class's field classes: "
1313 "sc-id=%" PRIu64
, sc
->id
);
1319 field_class_stack_destroy(ctx
->field_class_stack
);