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 "plugins/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
)
184 struct field_class_stack_frame
*entry
= NULL
;
186 if (!stack
|| field_class_stack_empty(stack
)) {
190 entry
= g_ptr_array_index(stack
, stack
->len
- 1);
196 * Returns the frame at index `index` in `stack`.
199 struct field_class_stack_frame
*field_class_stack_at(field_class_stack
*stack
,
202 struct field_class_stack_frame
*entry
= NULL
;
204 if (!stack
|| index
>= stack
->len
) {
208 entry
= g_ptr_array_index(stack
, index
);
215 * Removes the top frame of `stack`.
218 void field_class_stack_pop(field_class_stack
*stack
,
219 struct resolve_context
*ctx
)
221 if (!field_class_stack_empty(stack
)) {
223 * This will call the frame's destructor and free it, as
224 * well as put its contained field class.
226 BT_COMP_LOGD("Popping context's stack: stack-size-before=%u",
228 g_ptr_array_set_size(stack
, stack
->len
- 1);
233 * Returns the scope field class of `scope` in the context `ctx`.
236 struct ctf_field_class
*borrow_class_from_ctx(struct resolve_context
*ctx
,
237 enum ctf_scope scope
)
240 case CTF_SCOPE_PACKET_HEADER
:
241 return ctx
->scopes
.packet_header
;
242 case CTF_SCOPE_PACKET_CONTEXT
:
243 return ctx
->scopes
.packet_context
;
244 case CTF_SCOPE_EVENT_HEADER
:
245 return ctx
->scopes
.event_header
;
246 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
247 return ctx
->scopes
.event_common_context
;
248 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
249 return ctx
->scopes
.event_spec_context
;
250 case CTF_SCOPE_EVENT_PAYLOAD
:
251 return ctx
->scopes
.event_payload
;
260 * Returns the CTF scope from a path string. May return -1 if the path
261 * is found to be relative.
264 enum ctf_scope
get_root_scope_from_absolute_pathstr(const char *pathstr
,
265 struct resolve_context
*ctx
)
267 enum ctf_scope scope
;
268 enum ctf_scope ret
= -1;
269 const size_t prefixes_count
= sizeof(absolute_path_prefixes
) /
270 sizeof(*absolute_path_prefixes
);
272 for (scope
= CTF_SCOPE_PACKET_HEADER
; scope
< CTF_SCOPE_PACKET_HEADER
+
273 prefixes_count
; scope
++) {
275 * Chech if path string starts with a known absolute
278 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
280 if (strncmp(pathstr
, absolute_path_prefixes
[scope
],
281 strlen(absolute_path_prefixes
[scope
]))) {
282 /* Prefix does not match: try the next one */
283 BT_COMP_LOGD("Prefix does not match: trying the next one: "
284 "path=\"%s\", path-prefix=\"%s\", scope=%s",
285 pathstr
, absolute_path_prefixes
[scope
],
286 ctf_scope_string(scope
));
292 BT_COMP_LOGD("Found root scope from absolute path: "
293 "path=\"%s\", scope=%s", pathstr
,
294 ctf_scope_string(scope
));
303 * Destroys a path token.
306 void ptokens_destroy_func(gpointer ptoken
, gpointer data
)
308 g_string_free(ptoken
, TRUE
);
312 * Destroys a path token list.
315 void ptokens_destroy(GList
*ptokens
)
321 g_list_foreach(ptokens
, ptokens_destroy_func
, NULL
);
322 g_list_free(ptokens
);
326 * Returns the string contained in a path token.
329 const char *ptoken_get_string(GList
*ptoken
)
331 GString
*tokenstr
= (GString
*) ptoken
->data
;
333 return tokenstr
->str
;
337 * Converts a path string to a path token list, that is, splits the
338 * individual words of a path string into a list of individual
342 GList
*pathstr_to_ptokens(const char *pathstr
, struct resolve_context
*ctx
)
344 const char *at
= pathstr
;
345 const char *last
= at
;
346 GList
*ptokens
= NULL
;
349 if (*at
== '.' || *at
== '\0') {
353 /* Error: empty token */
354 BT_COMP_LOGE("Empty path token: path=\"%s\", pos=%u",
355 pathstr
, (unsigned int) (at
- pathstr
));
359 tokenstr
= g_string_new(NULL
);
360 g_string_append_len(tokenstr
, last
, at
- last
);
361 ptokens
= g_list_append(ptokens
, tokenstr
);
375 ptokens_destroy(ptokens
);
380 * Converts a path token list to a field path object. The path token
381 * list is relative from `fc`. The index of the source looking for its
382 * target within `fc` is indicated by `src_index`. This can be
383 * `INT64_MAX` if the source is contained in `fc`.
385 * `field_path` is an output parameter owned by the caller that must be
389 int ptokens_to_field_path(GList
*ptokens
, struct ctf_field_path
*field_path
,
390 struct ctf_field_class
*fc
, int64_t src_index
,
391 struct resolve_context
*ctx
)
394 GList
*cur_ptoken
= ptokens
;
395 bool first_level_done
= false;
400 struct ctf_field_class
*child_fc
;
401 const char *ft_name
= ptoken_get_string(cur_ptoken
);
403 BT_COMP_LOGD("Current path token: token=\"%s\"", ft_name
);
405 /* Find to which index corresponds the current path token */
406 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
407 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
411 ctf_field_class_compound_get_field_class_index_from_orig_name(
413 if (child_index
< 0) {
415 * Error: field name does not exist or
416 * wrong current class.
418 BT_COMP_LOGD("Cannot get index of 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
,
427 } else if (child_index
> src_index
&&
429 BT_COMP_LOGD("Child field class is located after source field class: "
430 "field-name=\"%s\", "
431 "src-index=%" PRId64
", "
432 "child-index=%" PRId64
", "
433 "first-level-done=%d",
434 ft_name
, src_index
, child_index
,
440 /* Next path token */
441 cur_ptoken
= g_list_next(cur_ptoken
);
442 first_level_done
= true;
445 /* Create new field path entry */
446 ctf_field_path_append_index(field_path
, child_index
);
448 /* Get child field class */
449 child_fc
= ctf_field_class_compound_borrow_field_class_by_index(
453 /* Move child class to current class */
462 * Converts a known absolute path token list to a field path object
463 * within the resolving context `ctx`.
465 * `field_path` is an output parameter owned by the caller that must be
469 int absolute_ptokens_to_field_path(GList
*ptokens
,
470 struct ctf_field_path
*field_path
,
471 struct resolve_context
*ctx
)
475 struct ctf_field_class
*fc
;
478 * Make sure we're not referring to a scope within a translated
481 switch (field_path
->root
) {
482 case CTF_SCOPE_PACKET_HEADER
:
483 if (ctx
->tc
->is_translated
) {
484 BT_COMP_LOGE("Trace class is already translated: "
486 ctf_scope_string(field_path
->root
));
492 case CTF_SCOPE_PACKET_CONTEXT
:
493 case CTF_SCOPE_EVENT_HEADER
:
494 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
496 BT_COMP_LOGE("No current stream class: "
498 ctf_scope_string(field_path
->root
));
503 if (ctx
->sc
->is_translated
) {
504 BT_COMP_LOGE("Stream class is already translated: "
506 ctf_scope_string(field_path
->root
));
512 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
513 case CTF_SCOPE_EVENT_PAYLOAD
:
515 BT_COMP_LOGE("No current event class: "
517 ctf_scope_string(field_path
->root
));
522 if (ctx
->ec
->is_translated
) {
523 BT_COMP_LOGE("Event class is already translated: "
525 ctf_scope_string(field_path
->root
));
536 /* Skip absolute path tokens */
537 cur_ptoken
= g_list_nth(ptokens
,
538 absolute_path_prefix_ptoken_counts
[field_path
->root
]);
540 /* Start with root class */
541 fc
= borrow_class_from_ctx(ctx
, field_path
->root
);
543 /* Error: root class is not available */
544 BT_COMP_LOGE("Root field class is not available: "
546 ctf_scope_string(field_path
->root
));
552 ret
= ptokens_to_field_path(cur_ptoken
, field_path
, fc
, INT64_MAX
, ctx
);
559 * Converts a known relative path token list to a field path object
560 * within the resolving context `ctx`.
562 * `field_path` is an output parameter owned by the caller that must be
566 int relative_ptokens_to_field_path(GList
*ptokens
,
567 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
570 int64_t parent_pos_in_stack
;
571 struct ctf_field_path tail_field_path
;
573 ctf_field_path_init(&tail_field_path
);
574 parent_pos_in_stack
= field_class_stack_size(ctx
->field_class_stack
) - 1;
576 while (parent_pos_in_stack
>= 0) {
577 struct ctf_field_class
*parent_class
=
578 field_class_stack_at(ctx
->field_class_stack
,
579 parent_pos_in_stack
)->fc
;
580 int64_t cur_index
= field_class_stack_at(ctx
->field_class_stack
,
581 parent_pos_in_stack
)->index
;
583 BT_COMP_LOGD("Locating target field class from current parent field class: "
584 "parent-pos=%" PRId64
", parent-fc-addr=%p, "
585 "cur-index=%" PRId64
,
586 parent_pos_in_stack
, parent_class
, cur_index
);
588 /* Locate target from current parent class */
589 ret
= ptokens_to_field_path(ptokens
, &tail_field_path
,
590 parent_class
, cur_index
, ctx
);
592 /* Not found... yet */
593 BT_COMP_LOGD_STR("Not found at this point.");
594 ctf_field_path_clear(&tail_field_path
);
596 /* Found: stitch tail field path to head field path */
598 size_t tail_field_path_len
=
599 tail_field_path
.path
->len
;
602 struct ctf_field_class
*cur_class
=
603 field_class_stack_at(
604 ctx
->field_class_stack
, i
)->fc
;
605 int64_t index
= field_class_stack_at(
606 ctx
->field_class_stack
, i
)->index
;
608 if (cur_class
== parent_class
) {
612 ctf_field_path_append_index(field_path
,
617 for (i
= 0; i
< tail_field_path_len
; i
++) {
619 ctf_field_path_borrow_index_by_index(
620 &tail_field_path
, i
);
622 ctf_field_path_append_index(field_path
,
628 parent_pos_in_stack
--;
631 if (parent_pos_in_stack
< 0) {
636 ctf_field_path_fini(&tail_field_path
);
641 * Converts a path string to a field path object within the resolving
645 int pathstr_to_field_path(const char *pathstr
,
646 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
649 enum ctf_scope root_scope
;
650 GList
*ptokens
= NULL
;
652 /* Convert path string to path tokens */
653 ptokens
= pathstr_to_ptokens(pathstr
, ctx
);
655 BT_COMP_LOGE("Cannot convert path string to path tokens: "
656 "path=\"%s\"", pathstr
);
661 /* Absolute or relative path? */
662 root_scope
= get_root_scope_from_absolute_pathstr(pathstr
, ctx
);
664 if (root_scope
== -1) {
665 /* Relative path: start with current root scope */
666 field_path
->root
= ctx
->root_scope
;
667 BT_COMP_LOGD("Detected relative path: starting with current root scope: "
668 "scope=%s", ctf_scope_string(field_path
->root
));
669 ret
= relative_ptokens_to_field_path(ptokens
, field_path
, ctx
);
671 BT_COMP_LOGE("Cannot get relative field path of path string: "
672 "path=\"%s\", start-scope=%s, end-scope=%s",
673 pathstr
, ctf_scope_string(ctx
->root_scope
),
674 ctf_scope_string(field_path
->root
));
678 /* Absolute path: use found root scope */
679 field_path
->root
= root_scope
;
680 BT_COMP_LOGD("Detected absolute path: using root scope: "
681 "scope=%s", ctf_scope_string(field_path
->root
));
682 ret
= absolute_ptokens_to_field_path(ptokens
, field_path
, ctx
);
684 BT_COMP_LOGE("Cannot get absolute field path of path string: "
685 "path=\"%s\", root-scope=%s",
686 pathstr
, ctf_scope_string(root_scope
));
691 if (BT_LOG_ON_TRACE
&& ret
== 0) {
692 GString
*field_path_pretty
= ctf_field_path_string(field_path
);
693 const char *field_path_pretty_str
=
694 field_path_pretty
? field_path_pretty
->str
: NULL
;
696 BT_COMP_LOGD("Found field path: path=\"%s\", field-path=\"%s\"",
697 pathstr
, field_path_pretty_str
);
699 if (field_path_pretty
) {
700 g_string_free(field_path_pretty
, TRUE
);
705 ptokens_destroy(ptokens
);
710 * Retrieves a field class by following the field path `field_path` in
711 * the resolving context `ctx`.
714 struct ctf_field_class
*field_path_to_field_class(
715 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
718 struct ctf_field_class
*fc
;
720 /* Start with root class */
721 fc
= borrow_class_from_ctx(ctx
, field_path
->root
);
723 /* Error: root class is not available */
724 BT_COMP_LOGE("Root field class is not available: root-scope=%s",
725 ctf_scope_string(field_path
->root
));
730 for (i
= 0; i
< field_path
->path
->len
; i
++) {
731 struct ctf_field_class
*child_fc
;
732 int64_t child_index
=
733 ctf_field_path_borrow_index_by_index(field_path
, i
);
735 /* Get child field class */
736 child_fc
= ctf_field_class_compound_borrow_field_class_by_index(
740 /* Move child class to current class */
749 * Fills the equivalent field path object of the context class stack.
752 void get_ctx_stack_field_path(struct resolve_context
*ctx
,
753 struct ctf_field_path
*field_path
)
757 BT_ASSERT(field_path
);
758 field_path
->root
= ctx
->root_scope
;
759 ctf_field_path_clear(field_path
);
761 for (i
= 0; i
< field_class_stack_size(ctx
->field_class_stack
); i
++) {
762 struct field_class_stack_frame
*frame
=
763 field_class_stack_at(ctx
->field_class_stack
, i
);
765 ctf_field_path_append_index(field_path
, frame
->index
);
770 * Returns the index of the lowest common ancestor of two field path
771 * objects having the same root scope.
773 int64_t get_field_paths_lca_index(struct ctf_field_path
*field_path1
,
774 struct ctf_field_path
*field_path2
,
775 struct resolve_context
*ctx
)
777 int64_t lca_index
= 0;
778 uint64_t field_path1_len
, field_path2_len
;
780 if (BT_LOG_ON_TRACE
) {
781 GString
*field_path1_pretty
=
782 ctf_field_path_string(field_path1
);
783 GString
*field_path2_pretty
=
784 ctf_field_path_string(field_path2
);
785 const char *field_path1_pretty_str
=
786 field_path1_pretty
? field_path1_pretty
->str
: NULL
;
787 const char *field_path2_pretty_str
=
788 field_path2_pretty
? field_path2_pretty
->str
: NULL
;
790 BT_COMP_LOGD("Finding lowest common ancestor (LCA) between two field paths: "
791 "field-path-1=\"%s\", field-path-2=\"%s\"",
792 field_path1_pretty_str
, field_path2_pretty_str
);
794 if (field_path1_pretty
) {
795 g_string_free(field_path1_pretty
, TRUE
);
798 if (field_path2_pretty
) {
799 g_string_free(field_path2_pretty
, TRUE
);
804 * Start from both roots and find the first mismatch.
806 BT_ASSERT(field_path1
->root
== field_path2
->root
);
807 field_path1_len
= field_path1
->path
->len
;
808 field_path2_len
= field_path2
->path
->len
;
811 int64_t target_index
, ctx_index
;
813 if (lca_index
== (int64_t) field_path2_len
||
814 lca_index
== (int64_t) field_path1_len
) {
816 * This means that both field paths never split.
817 * This is invalid because the target cannot be
818 * an ancestor of the source.
820 BT_COMP_LOGE("Source field class is an ancestor of target field class or vice versa: "
821 "lca-index=%" PRId64
", "
822 "field-path-1-len=%" PRIu64
", "
823 "field-path-2-len=%" PRIu64
,
824 lca_index
, field_path1_len
, field_path2_len
);
829 target_index
= ctf_field_path_borrow_index_by_index(field_path1
,
831 ctx_index
= ctf_field_path_borrow_index_by_index(field_path2
,
834 if (target_index
!= ctx_index
) {
835 /* LCA index is the previous */
842 BT_COMP_LOGD("Found LCA: lca-index=%" PRId64
, lca_index
);
847 * Validates a target field path.
850 int validate_target_field_path(struct ctf_field_path
*target_field_path
,
851 struct ctf_field_class
*target_fc
,
852 struct resolve_context
*ctx
)
855 struct ctf_field_path ctx_field_path
;
856 uint64_t target_field_path_len
= target_field_path
->path
->len
;
859 /* Get context field path */
860 ctf_field_path_init(&ctx_field_path
);
861 get_ctx_stack_field_path(ctx
, &ctx_field_path
);
864 * Make sure the target is not a root.
866 if (target_field_path_len
== 0) {
867 BT_COMP_LOGE_STR("Target field path's length is 0 (targeting the root).");
873 * Make sure the root of the target field path is not located
874 * after the context field path's root.
876 if (target_field_path
->root
> ctx_field_path
.root
) {
877 BT_COMP_LOGE("Target field class is located after source field class: "
878 "target-root=%s, source-root=%s",
879 ctf_scope_string(target_field_path
->root
),
880 ctf_scope_string(ctx_field_path
.root
));
885 if (target_field_path
->root
== ctx_field_path
.root
) {
886 int64_t target_index
, ctx_index
;
889 * Find the index of the lowest common ancestor of both field
892 lca_index
= get_field_paths_lca_index(target_field_path
,
893 &ctx_field_path
, ctx
);
895 BT_COMP_LOGE_STR("Cannot get least common ancestor.");
901 * Make sure the target field path is located before the
902 * context field path.
904 target_index
= ctf_field_path_borrow_index_by_index(
905 target_field_path
, (uint64_t) lca_index
);
906 ctx_index
= ctf_field_path_borrow_index_by_index(
907 &ctx_field_path
, (uint64_t) lca_index
);
909 if (target_index
>= ctx_index
) {
910 BT_COMP_LOGE("Target field class's index is greater than or equal to source field class's index in LCA: "
911 "lca-index=%" PRId64
", "
912 "target-index=%" PRId64
", "
913 "source-index=%" PRId64
,
914 lca_index
, target_index
, ctx_index
);
921 * Make sure the target class has the right class and properties.
923 switch (ctx
->cur_fc
->type
) {
924 case CTF_FIELD_CLASS_TYPE_VARIANT
:
925 if (target_fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
926 BT_COMP_LOGE("Variant field class's tag field class is not an enumeration field class: "
927 "tag-fc-addr=%p, tag-fc-id=%d",
928 target_fc
, target_fc
->type
);
933 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
935 struct ctf_field_class_int
*int_fc
= (void *) target_fc
;
937 if (target_fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&&
938 target_fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
939 BT_COMP_LOGE("Sequence field class's length field class is not an unsigned integer field class: "
940 "length-fc-addr=%p, length-fc-id=%d",
941 target_fc
, target_fc
->type
);
946 if (int_fc
->is_signed
) {
947 BT_COMP_LOGE("Sequence field class's length field class is not an unsigned integer field class: "
948 "length-fc-addr=%p, length-fc-id=%d",
949 target_fc
, target_fc
->type
);
960 ctf_field_path_fini(&ctx_field_path
);
965 * Resolves a variant or sequence field class `fc`.
968 int resolve_sequence_or_variant_field_class(struct ctf_field_class
*fc
,
969 struct resolve_context
*ctx
)
973 struct ctf_field_path target_field_path
;
974 struct ctf_field_class
*target_fc
= NULL
;
975 GString
*target_field_path_pretty
= NULL
;
976 const char *target_field_path_pretty_str
;
978 ctf_field_path_init(&target_field_path
);
980 /* Get path string */
982 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
984 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
985 pathstr
= seq_fc
->length_ref
->str
;
988 case CTF_FIELD_CLASS_TYPE_VARIANT
:
990 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
991 pathstr
= var_fc
->tag_ref
->str
;
999 BT_COMP_LOGE_STR("Cannot get path string.");
1004 /* Get target field path out of path string */
1005 ret
= pathstr_to_field_path(pathstr
, &target_field_path
, ctx
);
1007 BT_COMP_LOGE("Cannot get target field path for path string: "
1008 "path=\"%s\"", pathstr
);
1012 target_field_path_pretty
= ctf_field_path_string(
1013 &target_field_path
);
1014 target_field_path_pretty_str
=
1015 target_field_path_pretty
? target_field_path_pretty
->str
: NULL
;
1017 /* Get target field class */
1018 target_fc
= field_path_to_field_class(&target_field_path
, ctx
);
1020 BT_COMP_LOGE("Cannot get target field class for path string: "
1021 "path=\"%s\", target-field-path=\"%s\"",
1022 pathstr
, target_field_path_pretty_str
);
1027 ret
= validate_target_field_path(&target_field_path
,
1030 BT_COMP_LOGE("Invalid target field path for path string: "
1031 "path=\"%s\", target-field-path=\"%s\"",
1032 pathstr
, target_field_path_pretty_str
);
1036 /* Set target field path and target field class */
1038 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1040 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
1042 ctf_field_path_copy_content(&seq_fc
->length_path
,
1043 &target_field_path
);
1044 seq_fc
->length_fc
= (void *) target_fc
;
1047 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1049 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1051 ctf_field_path_copy_content(&var_fc
->tag_path
,
1052 &target_field_path
);
1053 ctf_field_class_variant_set_tag_field_class(var_fc
,
1054 (void *) target_fc
);
1062 if (target_field_path_pretty
) {
1063 g_string_free(target_field_path_pretty
, TRUE
);
1066 ctf_field_path_fini(&target_field_path
);
1071 * Resolves a field class `fc`.
1074 int resolve_field_class(struct ctf_field_class
*fc
, struct resolve_context
*ctx
)
1079 /* Field class is not available; still valid */
1085 /* Resolve sequence/variant field class */
1087 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1088 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1089 ret
= resolve_sequence_or_variant_field_class(fc
, ctx
);
1091 BT_COMP_LOGE("Cannot resolve sequence field class's length or variant field class's tag: "
1092 "ret=%d, fc-addr=%p", ret
, fc
);
1101 /* Recurse into compound classes */
1103 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1104 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1105 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1106 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1109 uint64_t field_count
=
1110 ctf_field_class_compound_get_field_class_count(fc
);
1112 ret
= field_class_stack_push(ctx
->field_class_stack
, fc
, ctx
);
1114 BT_COMP_LOGE("Cannot push field class on context's stack: "
1119 for (i
= 0; i
< field_count
; i
++) {
1120 struct ctf_field_class
*child_fc
=
1121 ctf_field_class_compound_borrow_field_class_by_index(
1124 BT_ASSERT(child_fc
);
1126 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
1127 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
1128 field_class_stack_peek(
1129 ctx
->field_class_stack
)->index
= -1;
1131 field_class_stack_peek(
1132 ctx
->field_class_stack
)->index
=
1136 BT_COMP_LOGD("Resolving field class's child field class: "
1137 "parent-fc-addr=%p, child-fc-addr=%p, "
1138 "index=%" PRIu64
", count=%" PRIu64
,
1139 fc
, child_fc
, i
, field_count
);
1140 ret
= resolve_field_class(child_fc
, ctx
);
1146 field_class_stack_pop(ctx
->field_class_stack
, ctx
);
1158 * Resolves the root field class corresponding to the scope `root_scope`.
1161 int resolve_root_class(enum ctf_scope root_scope
, struct resolve_context
*ctx
)
1165 BT_ASSERT(field_class_stack_size(ctx
->field_class_stack
) == 0);
1166 ctx
->root_scope
= root_scope
;
1167 ret
= resolve_field_class(borrow_class_from_ctx(ctx
, root_scope
), ctx
);
1168 ctx
->root_scope
= -1;
1173 int resolve_event_class_field_classes(struct resolve_context
*ctx
,
1174 struct ctf_event_class
*ec
)
1178 BT_ASSERT(!ctx
->scopes
.event_spec_context
);
1179 BT_ASSERT(!ctx
->scopes
.event_payload
);
1181 if (ec
->is_translated
) {
1186 ctx
->scopes
.event_spec_context
= ec
->spec_context_fc
;
1187 ret
= resolve_root_class(CTF_SCOPE_EVENT_COMMON_CONTEXT
, ctx
);
1189 BT_COMP_LOGE("Cannot resolve event specific context field class: "
1194 ctx
->scopes
.event_payload
= ec
->payload_fc
;
1195 ret
= resolve_root_class(CTF_SCOPE_EVENT_PAYLOAD
, ctx
);
1197 BT_COMP_LOGE("Cannot resolve event payload field class: "
1203 ctx
->scopes
.event_spec_context
= NULL
;
1204 ctx
->scopes
.event_payload
= NULL
;
1210 int resolve_stream_class_field_classes(struct resolve_context
*ctx
,
1211 struct ctf_stream_class
*sc
)
1216 BT_ASSERT(!ctx
->scopes
.packet_context
);
1217 BT_ASSERT(!ctx
->scopes
.event_header
);
1218 BT_ASSERT(!ctx
->scopes
.event_common_context
);
1221 if (!sc
->is_translated
) {
1222 ctx
->scopes
.packet_context
= sc
->packet_context_fc
;
1223 ret
= resolve_root_class(CTF_SCOPE_PACKET_CONTEXT
, ctx
);
1225 BT_COMP_LOGE("Cannot resolve packet context field class: "
1230 ctx
->scopes
.event_header
= sc
->event_header_fc
;
1231 ret
= resolve_root_class(CTF_SCOPE_EVENT_HEADER
, ctx
);
1233 BT_COMP_LOGE("Cannot resolve event header field class: "
1238 ctx
->scopes
.event_common_context
= sc
->event_common_context_fc
;
1239 ret
= resolve_root_class(CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
, ctx
);
1241 BT_COMP_LOGE("Cannot resolve event common context field class: "
1247 ctx
->scopes
.packet_context
= sc
->packet_context_fc
;
1248 ctx
->scopes
.event_header
= sc
->event_header_fc
;
1249 ctx
->scopes
.event_common_context
= sc
->event_common_context_fc
;
1251 for (i
= 0; i
< sc
->event_classes
->len
; i
++) {
1252 struct ctf_event_class
*ec
= sc
->event_classes
->pdata
[i
];
1254 ret
= resolve_event_class_field_classes(ctx
, ec
);
1256 BT_COMP_LOGE("Cannot resolve event class's field classes: "
1257 "ec-id=%" PRIu64
", ec-name=\"%s\"",
1258 ec
->id
, ec
->name
->str
);
1264 ctx
->scopes
.packet_context
= NULL
;
1265 ctx
->scopes
.event_header
= NULL
;
1266 ctx
->scopes
.event_common_context
= NULL
;
1272 int ctf_trace_class_resolve_field_classes(struct ctf_trace_class
*tc
,
1273 struct meta_log_config
*log_cfg
)
1277 struct resolve_context local_ctx
= {
1278 .log_level
= log_cfg
->log_level
,
1279 .self_comp
= log_cfg
->self_comp
,
1284 .packet_header
= tc
->packet_header_fc
,
1285 .packet_context
= NULL
,
1286 .event_header
= NULL
,
1287 .event_common_context
= NULL
,
1288 .event_spec_context
= NULL
,
1289 .event_payload
= NULL
,
1291 .root_scope
= CTF_SCOPE_PACKET_HEADER
,
1294 struct resolve_context
*ctx
= &local_ctx
;
1296 /* Initialize class stack */
1297 ctx
->field_class_stack
= field_class_stack_create();
1298 if (!ctx
->field_class_stack
) {
1299 BT_COMP_LOGE_STR("Cannot create field class stack.");
1304 if (!tc
->is_translated
) {
1305 ctx
->scopes
.packet_header
= tc
->packet_header_fc
;
1306 ret
= resolve_root_class(CTF_SCOPE_PACKET_HEADER
, ctx
);
1308 BT_COMP_LOGE("Cannot resolve packet header field class: "
1314 ctx
->scopes
.packet_header
= tc
->packet_header_fc
;
1316 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1317 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1319 ret
= resolve_stream_class_field_classes(ctx
, sc
);
1321 BT_COMP_LOGE("Cannot resolve stream class's field classes: "
1322 "sc-id=%" PRIu64
, sc
->id
);
1328 field_class_stack_destroy(ctx
->field_class_stack
);