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_LOG_OUTPUT_LEVEL (ctx->log_level)
17 #define BT_LOG_TAG "PLUGIN/CTF/META/RESOLVE"
18 #include "logging/log.h"
20 #include <babeltrace2/babeltrace.h>
21 #include "common/macros.h"
22 #include "common/assert.h"
23 #include "common/common.h"
32 #include "ctf-meta-visitors.h"
34 typedef GPtrArray field_class_stack
;
39 * `fc` contains a compound field class (structure, variant, array,
40 * or sequence) and `index` indicates the index of the field class in
41 * the upper frame (-1 for array and sequence field classes). `name`
42 * indicates the name of the field class in the upper frame (empty
43 * string for array and sequence field classes).
45 struct field_class_stack_frame
{
46 struct ctf_field_class
*fc
;
51 * The current context of the resolving engine.
53 struct resolve_context
{
54 bt_logging_level log_level
;
55 struct ctf_trace_class
*tc
;
56 struct ctf_stream_class
*sc
;
57 struct ctf_event_class
*ec
;
60 struct ctf_field_class
*packet_header
;
61 struct ctf_field_class
*packet_context
;
62 struct ctf_field_class
*event_header
;
63 struct ctf_field_class
*event_common_context
;
64 struct ctf_field_class
*event_spec_context
;
65 struct ctf_field_class
*event_payload
;
68 /* Root scope being visited */
69 enum ctf_scope root_scope
;
70 field_class_stack
*field_class_stack
;
71 struct ctf_field_class
*cur_fc
;
74 /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
75 static const char * const absolute_path_prefixes
[] = {
76 [CTF_SCOPE_PACKET_HEADER
] = "trace.packet.header.",
77 [CTF_SCOPE_PACKET_CONTEXT
] = "stream.packet.context.",
78 [CTF_SCOPE_EVENT_HEADER
] = "stream.event.header.",
79 [CTF_SCOPE_EVENT_COMMON_CONTEXT
] = "stream.event.context.",
80 [CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
] = "event.context.",
81 [CTF_SCOPE_EVENT_PAYLOAD
] = "event.fields.",
84 /* Number of path tokens used for the absolute prefixes */
85 static const uint64_t absolute_path_prefix_ptoken_counts
[] = {
86 [CTF_SCOPE_PACKET_HEADER
] = 3,
87 [CTF_SCOPE_PACKET_CONTEXT
] = 3,
88 [CTF_SCOPE_EVENT_HEADER
] = 3,
89 [CTF_SCOPE_EVENT_COMMON_CONTEXT
] = 3,
90 [CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
] = 2,
91 [CTF_SCOPE_EVENT_PAYLOAD
] = 2,
95 void destroy_field_class_stack_frame(struct field_class_stack_frame
*frame
)
105 * Creates a class stack.
108 field_class_stack
*field_class_stack_create(void)
110 return g_ptr_array_new_with_free_func(
111 (GDestroyNotify
) destroy_field_class_stack_frame
);
115 * Destroys a class stack.
118 void field_class_stack_destroy(field_class_stack
*stack
)
121 g_ptr_array_free(stack
, TRUE
);
126 * Pushes a field class onto a class stack.
129 int field_class_stack_push(field_class_stack
*stack
, struct ctf_field_class
*fc
,
130 struct resolve_context
*ctx
)
133 struct field_class_stack_frame
*frame
= NULL
;
136 BT_LOGE("Invalid parameter: stack or field class is NULL.");
141 frame
= g_new0(struct field_class_stack_frame
, 1);
143 BT_LOGE_STR("Failed to allocate one field class stack frame.");
148 BT_LOGD("Pushing field class on context's stack: "
149 "fc-addr=%p, stack-size-before=%u", fc
, stack
->len
);
151 g_ptr_array_add(stack
, frame
);
158 * Checks whether or not `stack` is empty.
161 bool field_class_stack_empty(field_class_stack
*stack
)
163 return stack
->len
== 0;
167 * Returns the number of frames in `stack`.
170 size_t field_class_stack_size(field_class_stack
*stack
)
176 * Returns the top frame of `stack`.
179 struct field_class_stack_frame
*field_class_stack_peek(field_class_stack
*stack
)
181 struct field_class_stack_frame
*entry
= NULL
;
183 if (!stack
|| field_class_stack_empty(stack
)) {
187 entry
= g_ptr_array_index(stack
, stack
->len
- 1);
193 * Returns the frame at index `index` in `stack`.
196 struct field_class_stack_frame
*field_class_stack_at(field_class_stack
*stack
,
199 struct field_class_stack_frame
*entry
= NULL
;
201 if (!stack
|| index
>= stack
->len
) {
205 entry
= g_ptr_array_index(stack
, index
);
212 * Removes the top frame of `stack`.
215 void field_class_stack_pop(field_class_stack
*stack
,
216 struct resolve_context
*ctx
)
218 if (!field_class_stack_empty(stack
)) {
220 * This will call the frame's destructor and free it, as
221 * well as put its contained field class.
223 BT_LOGD("Popping context's stack: stack-size-before=%u",
225 g_ptr_array_set_size(stack
, stack
->len
- 1);
230 * Returns the scope field class of `scope` in the context `ctx`.
233 struct ctf_field_class
*borrow_class_from_ctx(struct resolve_context
*ctx
,
234 enum ctf_scope scope
)
237 case CTF_SCOPE_PACKET_HEADER
:
238 return ctx
->scopes
.packet_header
;
239 case CTF_SCOPE_PACKET_CONTEXT
:
240 return ctx
->scopes
.packet_context
;
241 case CTF_SCOPE_EVENT_HEADER
:
242 return ctx
->scopes
.event_header
;
243 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
244 return ctx
->scopes
.event_common_context
;
245 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
246 return ctx
->scopes
.event_spec_context
;
247 case CTF_SCOPE_EVENT_PAYLOAD
:
248 return ctx
->scopes
.event_payload
;
257 * Returns the CTF scope from a path string. May return -1 if the path
258 * is found to be relative.
261 enum ctf_scope
get_root_scope_from_absolute_pathstr(const char *pathstr
,
262 struct resolve_context
*ctx
)
264 enum ctf_scope scope
;
265 enum ctf_scope ret
= -1;
266 const size_t prefixes_count
= sizeof(absolute_path_prefixes
) /
267 sizeof(*absolute_path_prefixes
);
269 for (scope
= CTF_SCOPE_PACKET_HEADER
; scope
< CTF_SCOPE_PACKET_HEADER
+
270 prefixes_count
; scope
++) {
272 * Chech if path string starts with a known absolute
275 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
277 if (strncmp(pathstr
, absolute_path_prefixes
[scope
],
278 strlen(absolute_path_prefixes
[scope
]))) {
279 /* Prefix does not match: try the next one */
280 BT_LOGD("Prefix does not match: trying the next one: "
281 "path=\"%s\", path-prefix=\"%s\", scope=%s",
282 pathstr
, absolute_path_prefixes
[scope
],
283 ctf_scope_string(scope
));
289 BT_LOGD("Found root scope from absolute path: "
290 "path=\"%s\", scope=%s", pathstr
,
291 ctf_scope_string(scope
));
300 * Destroys a path token.
303 void ptokens_destroy_func(gpointer ptoken
, gpointer data
)
305 g_string_free(ptoken
, TRUE
);
309 * Destroys a path token list.
312 void ptokens_destroy(GList
*ptokens
)
318 g_list_foreach(ptokens
, ptokens_destroy_func
, NULL
);
319 g_list_free(ptokens
);
323 * Returns the string contained in a path token.
326 const char *ptoken_get_string(GList
*ptoken
)
328 GString
*tokenstr
= (GString
*) ptoken
->data
;
330 return tokenstr
->str
;
334 * Converts a path string to a path token list, that is, splits the
335 * individual words of a path string into a list of individual
339 GList
*pathstr_to_ptokens(const char *pathstr
, struct resolve_context
*ctx
)
341 const char *at
= pathstr
;
342 const char *last
= at
;
343 GList
*ptokens
= NULL
;
346 if (*at
== '.' || *at
== '\0') {
350 /* Error: empty token */
351 BT_LOGE("Empty path token: path=\"%s\", pos=%u",
352 pathstr
, (unsigned int) (at
- pathstr
));
356 tokenstr
= g_string_new(NULL
);
357 g_string_append_len(tokenstr
, last
, at
- last
);
358 ptokens
= g_list_append(ptokens
, tokenstr
);
372 ptokens_destroy(ptokens
);
377 * Converts a path token list to a field path object. The path token
378 * list is relative from `fc`. The index of the source looking for its
379 * target within `fc` is indicated by `src_index`. This can be `INT64_MAX`
380 * if the source is contained in `fc`.
382 * `field_path` is an output parameter owned by the caller that must be
386 int ptokens_to_field_path(GList
*ptokens
, struct ctf_field_path
*field_path
,
387 struct ctf_field_class
*fc
, int64_t src_index
,
388 struct resolve_context
*ctx
)
391 GList
*cur_ptoken
= ptokens
;
392 bool first_level_done
= false;
397 struct ctf_field_class
*child_fc
;
398 const char *ft_name
= ptoken_get_string(cur_ptoken
);
400 BT_LOGD("Current path token: token=\"%s\"", ft_name
);
402 /* Find to which index corresponds the current path token */
403 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
404 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
408 ctf_field_class_compound_get_field_class_index_from_name(
410 if (child_index
< 0) {
412 * Error: field name does not exist or
413 * wrong current class.
415 BT_LOGD("Cannot get index of field class: "
416 "field-name=\"%s\", "
417 "src-index=%" PRId64
", "
418 "child-index=%" PRId64
", "
419 "first-level-done=%d",
420 ft_name
, src_index
, child_index
,
424 } else if (child_index
> src_index
&&
426 BT_LOGD("Child field class is located after source field class: "
427 "field-name=\"%s\", "
428 "src-index=%" PRId64
", "
429 "child-index=%" PRId64
", "
430 "first-level-done=%d",
431 ft_name
, src_index
, child_index
,
437 /* Next path token */
438 cur_ptoken
= g_list_next(cur_ptoken
);
439 first_level_done
= true;
442 /* Create new field path entry */
443 ctf_field_path_append_index(field_path
, child_index
);
445 /* Get child field class */
446 child_fc
= ctf_field_class_compound_borrow_field_class_by_index(
450 /* Move child class to current class */
459 * Converts a known absolute path token list to a field path object
460 * within the resolving context `ctx`.
462 * `field_path` is an output parameter owned by the caller that must be
466 int absolute_ptokens_to_field_path(GList
*ptokens
,
467 struct ctf_field_path
*field_path
,
468 struct resolve_context
*ctx
)
472 struct ctf_field_class
*fc
;
475 * Make sure we're not referring to a scope within a translated
478 switch (field_path
->root
) {
479 case CTF_SCOPE_PACKET_HEADER
:
480 if (ctx
->tc
->is_translated
) {
481 BT_LOGE("Trace class is already translated: "
483 ctf_scope_string(field_path
->root
));
489 case CTF_SCOPE_PACKET_CONTEXT
:
490 case CTF_SCOPE_EVENT_HEADER
:
491 case CTF_SCOPE_EVENT_COMMON_CONTEXT
:
493 BT_LOGE("No current stream class: "
495 ctf_scope_string(field_path
->root
));
500 if (ctx
->sc
->is_translated
) {
501 BT_LOGE("Stream class is already translated: "
503 ctf_scope_string(field_path
->root
));
509 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
:
510 case CTF_SCOPE_EVENT_PAYLOAD
:
512 BT_LOGE("No current event class: "
514 ctf_scope_string(field_path
->root
));
519 if (ctx
->ec
->is_translated
) {
520 BT_LOGE("Event class is already translated: "
522 ctf_scope_string(field_path
->root
));
533 /* Skip absolute path tokens */
534 cur_ptoken
= g_list_nth(ptokens
,
535 absolute_path_prefix_ptoken_counts
[field_path
->root
]);
537 /* Start with root class */
538 fc
= borrow_class_from_ctx(ctx
, field_path
->root
);
540 /* Error: root class is not available */
541 BT_LOGE("Root field class is not available: "
543 ctf_scope_string(field_path
->root
));
549 ret
= ptokens_to_field_path(cur_ptoken
, field_path
, fc
, INT64_MAX
, ctx
);
556 * Converts a known relative path token list to a field path object
557 * within the resolving context `ctx`.
559 * `field_path` is an output parameter owned by the caller that must be
563 int relative_ptokens_to_field_path(GList
*ptokens
,
564 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
567 int64_t parent_pos_in_stack
;
568 struct ctf_field_path tail_field_path
;
570 ctf_field_path_init(&tail_field_path
);
571 parent_pos_in_stack
= field_class_stack_size(ctx
->field_class_stack
) - 1;
573 while (parent_pos_in_stack
>= 0) {
574 struct ctf_field_class
*parent_class
=
575 field_class_stack_at(ctx
->field_class_stack
,
576 parent_pos_in_stack
)->fc
;
577 int64_t cur_index
= field_class_stack_at(ctx
->field_class_stack
,
578 parent_pos_in_stack
)->index
;
580 BT_LOGD("Locating target field class from current parent field class: "
581 "parent-pos=%" PRId64
", parent-fc-addr=%p, "
582 "cur-index=%" PRId64
,
583 parent_pos_in_stack
, parent_class
, cur_index
);
585 /* Locate target from current parent class */
586 ret
= ptokens_to_field_path(ptokens
, &tail_field_path
,
587 parent_class
, cur_index
, ctx
);
589 /* Not found... yet */
590 BT_LOGD_STR("Not found at this point.");
591 ctf_field_path_clear(&tail_field_path
);
593 /* Found: stitch tail field path to head field path */
595 size_t tail_field_path_len
=
596 tail_field_path
.path
->len
;
599 struct ctf_field_class
*cur_class
=
600 field_class_stack_at(
601 ctx
->field_class_stack
, i
)->fc
;
602 int64_t index
= field_class_stack_at(
603 ctx
->field_class_stack
, i
)->index
;
605 if (cur_class
== parent_class
) {
609 ctf_field_path_append_index(field_path
,
614 for (i
= 0; i
< tail_field_path_len
; i
++) {
616 ctf_field_path_borrow_index_by_index(
617 &tail_field_path
, i
);
619 ctf_field_path_append_index(field_path
,
625 parent_pos_in_stack
--;
628 if (parent_pos_in_stack
< 0) {
633 ctf_field_path_fini(&tail_field_path
);
638 * Converts a path string to a field path object within the resolving
642 int pathstr_to_field_path(const char *pathstr
,
643 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
646 enum ctf_scope root_scope
;
647 GList
*ptokens
= NULL
;
649 /* Convert path string to path tokens */
650 ptokens
= pathstr_to_ptokens(pathstr
, ctx
);
652 BT_LOGE("Cannot convert path string to path tokens: "
653 "path=\"%s\"", pathstr
);
658 /* Absolute or relative path? */
659 root_scope
= get_root_scope_from_absolute_pathstr(pathstr
, ctx
);
661 if (root_scope
== -1) {
662 /* Relative path: start with current root scope */
663 field_path
->root
= ctx
->root_scope
;
664 BT_LOGD("Detected relative path: starting with current root scope: "
665 "scope=%s", ctf_scope_string(field_path
->root
));
666 ret
= relative_ptokens_to_field_path(ptokens
, field_path
, ctx
);
668 BT_LOGE("Cannot get relative field path of path string: "
669 "path=\"%s\", start-scope=%s, end-scope=%s",
670 pathstr
, ctf_scope_string(ctx
->root_scope
),
671 ctf_scope_string(field_path
->root
));
675 /* Absolute path: use found root scope */
676 field_path
->root
= root_scope
;
677 BT_LOGD("Detected absolute path: using root scope: "
678 "scope=%s", ctf_scope_string(field_path
->root
));
679 ret
= absolute_ptokens_to_field_path(ptokens
, field_path
, ctx
);
681 BT_LOGE("Cannot get absolute field path of path string: "
682 "path=\"%s\", root-scope=%s",
683 pathstr
, ctf_scope_string(root_scope
));
688 if (BT_LOG_ON_VERBOSE
&& ret
== 0) {
689 GString
*field_path_pretty
= ctf_field_path_string(field_path
);
690 const char *field_path_pretty_str
=
691 field_path_pretty
? field_path_pretty
->str
: NULL
;
693 BT_LOGD("Found field path: path=\"%s\", field-path=\"%s\"",
694 pathstr
, field_path_pretty_str
);
696 if (field_path_pretty
) {
697 g_string_free(field_path_pretty
, TRUE
);
702 ptokens_destroy(ptokens
);
707 * Retrieves a field class by following the field path `field_path` in
708 * the resolving context `ctx`.
711 struct ctf_field_class
*field_path_to_field_class(
712 struct ctf_field_path
*field_path
, struct resolve_context
*ctx
)
715 struct ctf_field_class
*fc
;
717 /* Start with root class */
718 fc
= borrow_class_from_ctx(ctx
, field_path
->root
);
720 /* Error: root class is not available */
721 BT_LOGE("Root field class is not available: root-scope=%s",
722 ctf_scope_string(field_path
->root
));
727 for (i
= 0; i
< field_path
->path
->len
; i
++) {
728 struct ctf_field_class
*child_fc
;
729 int64_t child_index
=
730 ctf_field_path_borrow_index_by_index(field_path
, i
);
732 /* Get child field class */
733 child_fc
= ctf_field_class_compound_borrow_field_class_by_index(
737 /* Move child class to current class */
746 * Fills the equivalent field path object of the context class stack.
749 void get_ctx_stack_field_path(struct resolve_context
*ctx
,
750 struct ctf_field_path
*field_path
)
754 BT_ASSERT(field_path
);
755 field_path
->root
= ctx
->root_scope
;
756 ctf_field_path_clear(field_path
);
758 for (i
= 0; i
< field_class_stack_size(ctx
->field_class_stack
); i
++) {
759 struct field_class_stack_frame
*frame
=
760 field_class_stack_at(ctx
->field_class_stack
, i
);
762 ctf_field_path_append_index(field_path
, frame
->index
);
767 * Returns the index of the lowest common ancestor of two field path
768 * objects having the same root scope.
770 int64_t get_field_paths_lca_index(struct ctf_field_path
*field_path1
,
771 struct ctf_field_path
*field_path2
,
772 struct resolve_context
*ctx
)
774 int64_t lca_index
= 0;
775 uint64_t field_path1_len
, field_path2_len
;
777 if (BT_LOG_ON_VERBOSE
) {
778 GString
*field_path1_pretty
=
779 ctf_field_path_string(field_path1
);
780 GString
*field_path2_pretty
=
781 ctf_field_path_string(field_path2
);
782 const char *field_path1_pretty_str
=
783 field_path1_pretty
? field_path1_pretty
->str
: NULL
;
784 const char *field_path2_pretty_str
=
785 field_path2_pretty
? field_path2_pretty
->str
: NULL
;
787 BT_LOGD("Finding lowest common ancestor (LCA) between two field paths: "
788 "field-path-1=\"%s\", field-path-2=\"%s\"",
789 field_path1_pretty_str
, field_path2_pretty_str
);
791 if (field_path1_pretty
) {
792 g_string_free(field_path1_pretty
, TRUE
);
795 if (field_path2_pretty
) {
796 g_string_free(field_path2_pretty
, TRUE
);
801 * Start from both roots and find the first mismatch.
803 BT_ASSERT(field_path1
->root
== field_path2
->root
);
804 field_path1_len
= field_path1
->path
->len
;
805 field_path2_len
= field_path2
->path
->len
;
808 int64_t target_index
, ctx_index
;
810 if (lca_index
== (int64_t) field_path2_len
||
811 lca_index
== (int64_t) field_path1_len
) {
813 * This means that both field paths never split.
814 * This is invalid because the target cannot be
815 * an ancestor of the source.
817 BT_LOGE("Source field class is an ancestor of target field class or vice versa: "
818 "lca-index=%" PRId64
", "
819 "field-path-1-len=%" PRIu64
", "
820 "field-path-2-len=%" PRIu64
,
821 lca_index
, field_path1_len
, field_path2_len
);
826 target_index
= ctf_field_path_borrow_index_by_index(field_path1
,
828 ctx_index
= ctf_field_path_borrow_index_by_index(field_path2
,
831 if (target_index
!= ctx_index
) {
832 /* LCA index is the previous */
839 BT_LOGD("Found LCA: lca-index=%" PRId64
, lca_index
);
844 * Validates a target field path.
847 int validate_target_field_path(struct ctf_field_path
*target_field_path
,
848 struct ctf_field_class
*target_fc
,
849 struct resolve_context
*ctx
)
852 struct ctf_field_path ctx_field_path
;
853 uint64_t target_field_path_len
= target_field_path
->path
->len
;
856 /* Get context field path */
857 ctf_field_path_init(&ctx_field_path
);
858 get_ctx_stack_field_path(ctx
, &ctx_field_path
);
861 * Make sure the target is not a root.
863 if (target_field_path_len
== 0) {
864 BT_LOGE_STR("Target field path's length is 0 (targeting the root).");
870 * Make sure the root of the target field path is not located
871 * after the context field path's root.
873 if (target_field_path
->root
> ctx_field_path
.root
) {
874 BT_LOGE("Target field class is located after source field class: "
875 "target-root=%s, source-root=%s",
876 ctf_scope_string(target_field_path
->root
),
877 ctf_scope_string(ctx_field_path
.root
));
882 if (target_field_path
->root
== ctx_field_path
.root
) {
883 int64_t target_index
, ctx_index
;
886 * Find the index of the lowest common ancestor of both field
889 lca_index
= get_field_paths_lca_index(target_field_path
,
890 &ctx_field_path
, ctx
);
892 BT_LOGE_STR("Cannot get least common ancestor.");
898 * Make sure the target field path is located before the
899 * context field path.
901 target_index
= ctf_field_path_borrow_index_by_index(
902 target_field_path
, (uint64_t) lca_index
);
903 ctx_index
= ctf_field_path_borrow_index_by_index(
904 &ctx_field_path
, (uint64_t) lca_index
);
906 if (target_index
>= ctx_index
) {
907 BT_LOGE("Target field class's index is greater than or equal to source field class's index in LCA: "
908 "lca-index=%" PRId64
", "
909 "target-index=%" PRId64
", "
910 "source-index=%" PRId64
,
911 lca_index
, target_index
, ctx_index
);
918 * Make sure the target class has the right class and properties.
920 switch (ctx
->cur_fc
->type
) {
921 case CTF_FIELD_CLASS_TYPE_VARIANT
:
922 if (target_fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
923 BT_LOGE("Variant field class's tag field class is not an enumeration field class: "
924 "tag-fc-addr=%p, tag-fc-id=%d",
925 target_fc
, target_fc
->type
);
930 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
932 struct ctf_field_class_int
*int_fc
= (void *) target_fc
;
934 if (target_fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&&
935 target_fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
936 BT_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
);
943 if (int_fc
->is_signed
) {
944 BT_LOGE("Sequence field class's length field class is not an unsigned integer field class: "
945 "length-fc-addr=%p, length-fc-id=%d",
946 target_fc
, target_fc
->type
);
957 ctf_field_path_fini(&ctx_field_path
);
962 * Resolves a variant or sequence field class `fc`.
965 int resolve_sequence_or_variant_field_class(struct ctf_field_class
*fc
,
966 struct resolve_context
*ctx
)
970 struct ctf_field_path target_field_path
;
971 struct ctf_field_class
*target_fc
= NULL
;
972 GString
*target_field_path_pretty
= NULL
;
973 const char *target_field_path_pretty_str
;
975 ctf_field_path_init(&target_field_path
);
977 /* Get path string */
979 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
981 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
982 pathstr
= seq_fc
->length_ref
->str
;
985 case CTF_FIELD_CLASS_TYPE_VARIANT
:
987 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
988 pathstr
= var_fc
->tag_ref
->str
;
996 BT_LOGE_STR("Cannot get path string.");
1001 /* Get target field path out of path string */
1002 ret
= pathstr_to_field_path(pathstr
, &target_field_path
, ctx
);
1004 BT_LOGE("Cannot get target field path for path string: "
1005 "path=\"%s\"", pathstr
);
1009 target_field_path_pretty
= ctf_field_path_string(
1010 &target_field_path
);
1011 target_field_path_pretty_str
=
1012 target_field_path_pretty
? target_field_path_pretty
->str
: NULL
;
1014 /* Get target field class */
1015 target_fc
= field_path_to_field_class(&target_field_path
, ctx
);
1017 BT_LOGE("Cannot get target field class for path string: "
1018 "path=\"%s\", target-field-path=\"%s\"",
1019 pathstr
, target_field_path_pretty_str
);
1024 ret
= validate_target_field_path(&target_field_path
,
1027 BT_LOGE("Invalid target field path for path string: "
1028 "path=\"%s\", target-field-path=\"%s\"",
1029 pathstr
, target_field_path_pretty_str
);
1033 /* Set target field path and target field class */
1035 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1037 struct ctf_field_class_sequence
*seq_fc
= (void *) fc
;
1039 ctf_field_path_copy_content(&seq_fc
->length_path
,
1040 &target_field_path
);
1041 seq_fc
->length_fc
= (void *) target_fc
;
1044 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1046 struct ctf_field_class_variant
*var_fc
= (void *) fc
;
1048 ctf_field_path_copy_content(&var_fc
->tag_path
,
1049 &target_field_path
);
1050 ctf_field_class_variant_set_tag_field_class(var_fc
,
1051 (void *) target_fc
);
1059 if (target_field_path_pretty
) {
1060 g_string_free(target_field_path_pretty
, TRUE
);
1063 ctf_field_path_fini(&target_field_path
);
1068 * Resolves a field class `fc`.
1071 int resolve_field_class(struct ctf_field_class
*fc
, struct resolve_context
*ctx
)
1076 /* Field class is not available; still valid */
1082 /* Resolve sequence/variant field class */
1084 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1085 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1086 ret
= resolve_sequence_or_variant_field_class(fc
, ctx
);
1088 BT_LOGE("Cannot resolve sequence field class's length or variant field class's tag: "
1089 "ret=%d, fc-addr=%p", ret
, fc
);
1098 /* Recurse into compound classes */
1100 case CTF_FIELD_CLASS_TYPE_STRUCT
:
1101 case CTF_FIELD_CLASS_TYPE_VARIANT
:
1102 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
1103 case CTF_FIELD_CLASS_TYPE_ARRAY
:
1106 uint64_t field_count
=
1107 ctf_field_class_compound_get_field_class_count(fc
);
1109 ret
= field_class_stack_push(ctx
->field_class_stack
, fc
, ctx
);
1111 BT_LOGE("Cannot push field class on context's stack: "
1116 for (i
= 0; i
< field_count
; i
++) {
1117 struct ctf_field_class
*child_fc
=
1118 ctf_field_class_compound_borrow_field_class_by_index(
1121 BT_ASSERT(child_fc
);
1123 if (fc
->type
== CTF_FIELD_CLASS_TYPE_ARRAY
||
1124 fc
->type
== CTF_FIELD_CLASS_TYPE_SEQUENCE
) {
1125 field_class_stack_peek(
1126 ctx
->field_class_stack
)->index
= -1;
1128 field_class_stack_peek(
1129 ctx
->field_class_stack
)->index
=
1133 BT_LOGD("Resolving field class's child field class: "
1134 "parent-fc-addr=%p, child-fc-addr=%p, "
1135 "index=%" PRIu64
", count=%" PRIu64
,
1136 fc
, child_fc
, i
, field_count
);
1137 ret
= resolve_field_class(child_fc
, ctx
);
1143 field_class_stack_pop(ctx
->field_class_stack
, ctx
);
1155 * Resolves the root field class corresponding to the scope `root_scope`.
1158 int resolve_root_class(enum ctf_scope root_scope
, struct resolve_context
*ctx
)
1162 BT_ASSERT(field_class_stack_size(ctx
->field_class_stack
) == 0);
1163 ctx
->root_scope
= root_scope
;
1164 ret
= resolve_field_class(borrow_class_from_ctx(ctx
, root_scope
), ctx
);
1165 ctx
->root_scope
= -1;
1170 int resolve_event_class_field_classes(struct resolve_context
*ctx
,
1171 struct ctf_event_class
*ec
)
1175 BT_ASSERT(!ctx
->scopes
.event_spec_context
);
1176 BT_ASSERT(!ctx
->scopes
.event_payload
);
1178 if (ec
->is_translated
) {
1183 ctx
->scopes
.event_spec_context
= ec
->spec_context_fc
;
1184 ret
= resolve_root_class(CTF_SCOPE_EVENT_COMMON_CONTEXT
, ctx
);
1186 BT_LOGE("Cannot resolve event specific context field class: "
1191 ctx
->scopes
.event_payload
= ec
->payload_fc
;
1192 ret
= resolve_root_class(CTF_SCOPE_EVENT_PAYLOAD
, ctx
);
1194 BT_LOGE("Cannot resolve event payload field class: "
1200 ctx
->scopes
.event_spec_context
= NULL
;
1201 ctx
->scopes
.event_payload
= NULL
;
1207 int resolve_stream_class_field_classes(struct resolve_context
*ctx
,
1208 struct ctf_stream_class
*sc
)
1213 BT_ASSERT(!ctx
->scopes
.packet_context
);
1214 BT_ASSERT(!ctx
->scopes
.event_header
);
1215 BT_ASSERT(!ctx
->scopes
.event_common_context
);
1218 if (!sc
->is_translated
) {
1219 ctx
->scopes
.packet_context
= sc
->packet_context_fc
;
1220 ret
= resolve_root_class(CTF_SCOPE_PACKET_CONTEXT
, ctx
);
1222 BT_LOGE("Cannot resolve packet context field class: "
1227 ctx
->scopes
.event_header
= sc
->event_header_fc
;
1228 ret
= resolve_root_class(CTF_SCOPE_EVENT_HEADER
, ctx
);
1230 BT_LOGE("Cannot resolve event header field class: "
1235 ctx
->scopes
.event_common_context
= sc
->event_common_context_fc
;
1236 ret
= resolve_root_class(CTF_SCOPE_EVENT_SPECIFIC_CONTEXT
, ctx
);
1238 BT_LOGE("Cannot resolve event common context field class: "
1244 ctx
->scopes
.packet_context
= sc
->packet_context_fc
;
1245 ctx
->scopes
.event_header
= sc
->event_header_fc
;
1246 ctx
->scopes
.event_common_context
= sc
->event_common_context_fc
;
1248 for (i
= 0; i
< sc
->event_classes
->len
; i
++) {
1249 struct ctf_event_class
*ec
= sc
->event_classes
->pdata
[i
];
1251 ret
= resolve_event_class_field_classes(ctx
, ec
);
1253 BT_LOGE("Cannot resolve event class's field classes: "
1254 "ec-id=%" PRIu64
", ec-name=\"%s\"",
1255 ec
->id
, ec
->name
->str
);
1261 ctx
->scopes
.packet_context
= NULL
;
1262 ctx
->scopes
.event_header
= NULL
;
1263 ctx
->scopes
.event_common_context
= NULL
;
1269 int ctf_trace_class_resolve_field_classes(struct ctf_trace_class
*tc
,
1270 bt_logging_level log_level
)
1274 struct resolve_context local_ctx
= {
1275 .log_level
= log_level
,
1280 .packet_header
= tc
->packet_header_fc
,
1281 .packet_context
= NULL
,
1282 .event_header
= NULL
,
1283 .event_common_context
= NULL
,
1284 .event_spec_context
= NULL
,
1285 .event_payload
= NULL
,
1287 .root_scope
= CTF_SCOPE_PACKET_HEADER
,
1290 struct resolve_context
*ctx
= &local_ctx
;
1292 /* Initialize class stack */
1293 ctx
->field_class_stack
= field_class_stack_create();
1294 if (!ctx
->field_class_stack
) {
1295 BT_LOGE_STR("Cannot create field class stack.");
1300 if (!tc
->is_translated
) {
1301 ctx
->scopes
.packet_header
= tc
->packet_header_fc
;
1302 ret
= resolve_root_class(CTF_SCOPE_PACKET_HEADER
, ctx
);
1304 BT_LOGE("Cannot resolve packet header field class: "
1310 ctx
->scopes
.packet_header
= tc
->packet_header_fc
;
1312 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
1313 struct ctf_stream_class
*sc
= tc
->stream_classes
->pdata
[i
];
1315 ret
= resolve_stream_class_field_classes(ctx
, sc
);
1317 BT_LOGE("Cannot resolve stream class's field classes: "
1318 "sc-id=%" PRIu64
, sc
->id
);
1324 field_class_stack_destroy(ctx
->field_class_stack
);