2 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "LIB/RESOLVE-FIELD-PATH"
24 #include "lib/logging.h"
26 #include "lib/assert-pre.h"
27 #include "common/assert.h"
28 #include <babeltrace2/trace-ir/field-path-const.h>
34 #include "field-class.h"
35 #include "field-path.h"
36 #include "resolve-field-path.h"
39 bool find_field_class_recursive(struct bt_field_class
*fc
,
40 struct bt_field_class
*tgt_fc
, struct bt_field_path
*field_path
)
50 case BT_FIELD_CLASS_TYPE_OPTION
:
52 struct bt_field_class_option
*opt_fc
= (void *) fc
;
53 struct bt_field_path_item item
= {
54 .type
= BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT
,
55 .index
= UINT64_C(-1),
58 bt_field_path_append_item(field_path
, &item
);
59 found
= find_field_class_recursive(opt_fc
->content_fc
,
65 bt_field_path_remove_last_item(field_path
);
68 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
69 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
:
70 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
:
71 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
:
73 struct bt_field_class_named_field_class_container
*container_fc
=
77 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
78 struct bt_named_field_class
*named_fc
=
79 container_fc
->named_fcs
->pdata
[i
];
80 struct bt_field_path_item item
= {
81 .type
= BT_FIELD_PATH_ITEM_TYPE_INDEX
,
85 bt_field_path_append_item(field_path
, &item
);
86 found
= find_field_class_recursive(named_fc
->fc
,
92 bt_field_path_remove_last_item(field_path
);
97 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
98 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
100 struct bt_field_class_array
*array_fc
= (void *) fc
;
101 struct bt_field_path_item item
= {
102 .type
= BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
,
103 .index
= UINT64_C(-1),
106 bt_field_path_append_item(field_path
, &item
);
107 found
= find_field_class_recursive(array_fc
->element_fc
,
113 bt_field_path_remove_last_item(field_path
);
125 int find_field_class(struct bt_field_class
*root_fc
,
126 enum bt_field_path_scope root_scope
, struct bt_field_class
*tgt_fc
,
127 struct bt_field_path
**ret_field_path
)
130 struct bt_field_path
*field_path
= NULL
;
136 field_path
= bt_field_path_create();
142 field_path
->root
= root_scope
;
143 if (!find_field_class_recursive(root_fc
, tgt_fc
, field_path
)) {
145 BT_OBJECT_PUT_REF_AND_RESET(field_path
);
149 *ret_field_path
= field_path
;
154 struct bt_field_path
*find_field_class_in_ctx(struct bt_field_class
*fc
,
155 struct bt_resolve_field_path_context
*ctx
)
157 struct bt_field_path
*field_path
= NULL
;
160 ret
= find_field_class(ctx
->packet_context
, BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
,
162 if (ret
|| field_path
) {
166 ret
= find_field_class(ctx
->event_common_context
,
167 BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
, fc
, &field_path
);
168 if (ret
|| field_path
) {
172 ret
= find_field_class(ctx
->event_specific_context
,
173 BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
, fc
, &field_path
);
174 if (ret
|| field_path
) {
178 ret
= find_field_class(ctx
->event_payload
, BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
,
180 if (ret
|| field_path
) {
188 BT_ASSERT_PRE_DEV_FUNC
190 bool target_is_before_source(struct bt_field_path
*src_field_path
,
191 struct bt_field_path
*tgt_field_path
)
193 bool is_valid
= true;
194 uint64_t src_i
= 0, tgt_i
= 0;
196 if (tgt_field_path
->root
< src_field_path
->root
) {
200 if (tgt_field_path
->root
> src_field_path
->root
) {
205 BT_ASSERT(tgt_field_path
->root
== src_field_path
->root
);
207 for (src_i
= 0, tgt_i
= 0; src_i
< src_field_path
->items
->len
&&
208 tgt_i
< tgt_field_path
->items
->len
; src_i
++, tgt_i
++) {
209 struct bt_field_path_item
*src_fp_item
=
210 bt_field_path_borrow_item_by_index_inline(
211 src_field_path
, src_i
);
212 struct bt_field_path_item
*tgt_fp_item
=
213 bt_field_path_borrow_item_by_index_inline(
214 tgt_field_path
, tgt_i
);
216 if (src_fp_item
->type
== BT_FIELD_PATH_ITEM_TYPE_INDEX
&&
217 tgt_fp_item
->type
== BT_FIELD_PATH_ITEM_TYPE_INDEX
) {
218 if (tgt_fp_item
->index
> src_fp_item
->index
) {
232 BT_ASSERT_PRE_DEV_FUNC
234 struct bt_field_class
*borrow_root_field_class(
235 struct bt_resolve_field_path_context
*ctx
, enum bt_field_path_scope scope
)
238 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
:
239 return ctx
->packet_context
;
240 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
:
241 return ctx
->event_common_context
;
242 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
:
243 return ctx
->event_specific_context
;
244 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
:
245 return ctx
->event_payload
;
253 BT_ASSERT_PRE_DEV_FUNC
255 struct bt_field_class
*borrow_child_field_class(
256 struct bt_field_class
*parent_fc
,
257 struct bt_field_path_item
*fp_item
)
259 struct bt_field_class
*child_fc
= NULL
;
261 switch (parent_fc
->type
) {
262 case BT_FIELD_CLASS_TYPE_OPTION
:
264 struct bt_field_class_option
*opt_fc
= (void *) parent_fc
;
266 BT_ASSERT(fp_item
->type
==
267 BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT
);
268 child_fc
= opt_fc
->content_fc
;
271 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
272 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
:
273 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
:
274 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
:
276 struct bt_field_class_named_field_class_container
*container_fc
=
278 struct bt_named_field_class
*named_fc
;
280 BT_ASSERT(fp_item
->type
== BT_FIELD_PATH_ITEM_TYPE_INDEX
);
281 named_fc
= container_fc
->named_fcs
->pdata
[fp_item
->index
];
282 child_fc
= named_fc
->fc
;
285 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
286 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
288 struct bt_field_class_array
*array_fc
= (void *) parent_fc
;
290 BT_ASSERT(fp_item
->type
==
291 BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
);
292 child_fc
= array_fc
->element_fc
;
302 BT_ASSERT_PRE_DEV_FUNC
304 bool target_field_path_in_different_scope_has_struct_fc_only(
305 struct bt_field_path
*src_field_path
,
306 struct bt_field_path
*tgt_field_path
,
307 struct bt_resolve_field_path_context
*ctx
)
309 bool is_valid
= true;
311 struct bt_field_class
*fc
;
313 if (src_field_path
->root
== tgt_field_path
->root
) {
317 fc
= borrow_root_field_class(ctx
, tgt_field_path
->root
);
319 for (i
= 0; i
< tgt_field_path
->items
->len
; i
++) {
320 struct bt_field_path_item
*fp_item
=
321 bt_field_path_borrow_item_by_index_inline(
324 if (fc
->type
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
||
325 fc
->type
== BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
||
326 fc
->type
== BT_FIELD_CLASS_TYPE_OPTION
||
327 fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
||
328 fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
||
329 fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
) {
334 BT_ASSERT(fp_item
->type
== BT_FIELD_PATH_ITEM_TYPE_INDEX
);
335 fc
= borrow_child_field_class(fc
, fp_item
);
342 BT_ASSERT_PRE_DEV_FUNC
344 bool lca_is_structure_field_class(struct bt_field_path
*src_field_path
,
345 struct bt_field_path
*tgt_field_path
,
346 struct bt_resolve_field_path_context
*ctx
)
348 bool is_valid
= true;
349 struct bt_field_class
*src_fc
;
350 struct bt_field_class
*tgt_fc
;
351 struct bt_field_class
*prev_fc
= NULL
;
352 uint64_t src_i
= 0, tgt_i
= 0;
354 if (src_field_path
->root
!= tgt_field_path
->root
) {
358 src_fc
= borrow_root_field_class(ctx
, src_field_path
->root
);
359 tgt_fc
= borrow_root_field_class(ctx
, tgt_field_path
->root
);
363 for (src_i
= 0, tgt_i
= 0; src_i
< src_field_path
->items
->len
&&
364 tgt_i
< tgt_field_path
->items
->len
; src_i
++, tgt_i
++) {
365 struct bt_field_path_item
*src_fp_item
=
366 bt_field_path_borrow_item_by_index_inline(
367 src_field_path
, src_i
);
368 struct bt_field_path_item
*tgt_fp_item
=
369 bt_field_path_borrow_item_by_index_inline(
370 tgt_field_path
, tgt_i
);
372 if (src_fc
!= tgt_fc
) {
375 * This is correct: the LCA is the root
376 * scope field class, which must be a
377 * structure field class.
382 if (prev_fc
->type
!= BT_FIELD_CLASS_TYPE_STRUCTURE
) {
390 src_fc
= borrow_child_field_class(src_fc
, src_fp_item
);
391 tgt_fc
= borrow_child_field_class(tgt_fc
, tgt_fp_item
);
398 BT_ASSERT_PRE_DEV_FUNC
400 bool lca_to_target_has_struct_fc_only(struct bt_field_path
*src_field_path
,
401 struct bt_field_path
*tgt_field_path
,
402 struct bt_resolve_field_path_context
*ctx
)
404 bool is_valid
= true;
405 struct bt_field_class
*src_fc
;
406 struct bt_field_class
*tgt_fc
;
407 uint64_t src_i
= 0, tgt_i
= 0;
409 if (src_field_path
->root
!= tgt_field_path
->root
) {
413 src_fc
= borrow_root_field_class(ctx
, src_field_path
->root
);
414 tgt_fc
= borrow_root_field_class(ctx
, tgt_field_path
->root
);
417 BT_ASSERT(src_fc
== tgt_fc
);
420 for (src_i
= 0, tgt_i
= 0; src_i
< src_field_path
->items
->len
&&
421 tgt_i
< tgt_field_path
->items
->len
; src_i
++, tgt_i
++) {
422 struct bt_field_path_item
*src_fp_item
=
423 bt_field_path_borrow_item_by_index_inline(
424 src_field_path
, src_i
);
425 struct bt_field_path_item
*tgt_fp_item
=
426 bt_field_path_borrow_item_by_index_inline(
427 tgt_field_path
, tgt_i
);
429 if (src_i
!= tgt_i
) {
430 /* Next field class is different: LCA is `tgt_fc` */
434 src_fc
= borrow_child_field_class(src_fc
, src_fp_item
);
435 tgt_fc
= borrow_child_field_class(tgt_fc
, tgt_fp_item
);
438 /* Only structure field classes to the target */
439 for (; tgt_i
< tgt_field_path
->items
->len
; tgt_i
++) {
440 struct bt_field_path_item
*tgt_fp_item
=
441 bt_field_path_borrow_item_by_index_inline(
442 tgt_field_path
, tgt_i
);
444 if (tgt_fc
->type
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
||
445 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
||
446 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_OPTION
||
447 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
||
448 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
||
449 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
) {
454 tgt_fc
= borrow_child_field_class(tgt_fc
, tgt_fp_item
);
461 BT_ASSERT_PRE_DEV_FUNC
463 bool field_path_is_valid(struct bt_field_class
*src_fc
,
464 struct bt_field_class
*tgt_fc
,
465 struct bt_resolve_field_path_context
*ctx
)
467 bool is_valid
= true;
468 struct bt_field_path
*src_field_path
= find_field_class_in_ctx(
470 struct bt_field_path
*tgt_field_path
= find_field_class_in_ctx(
473 if (!src_field_path
) {
474 BT_ASSERT_PRE_DEV_MSG("Cannot find requesting field class in "
475 "resolving context: %!+F", src_fc
);
480 if (!tgt_field_path
) {
481 BT_ASSERT_PRE_DEV_MSG("Cannot find target field class in "
482 "resolving context: %!+F", tgt_fc
);
487 /* Target must be before source */
488 if (!target_is_before_source(src_field_path
, tgt_field_path
)) {
489 BT_ASSERT_PRE_DEV_MSG("Target field class is located after "
490 "requesting field class: %![req-fc-]+F, %![tgt-fc-]+F",
497 * If target is in a different scope than source, there are no
498 * array or variant field classes on the way to the target.
500 if (!target_field_path_in_different_scope_has_struct_fc_only(
501 src_field_path
, tgt_field_path
, ctx
)) {
502 BT_ASSERT_PRE_DEV_MSG("Target field class is located in a "
503 "different scope than requesting field class, "
504 "but within an array or a variant field class: "
505 "%![req-fc-]+F, %![tgt-fc-]+F",
511 /* Same scope: LCA must be a structure field class */
512 if (!lca_is_structure_field_class(src_field_path
, tgt_field_path
, ctx
)) {
513 BT_ASSERT_PRE_DEV_MSG("Lowest common ancestor of target and "
514 "requesting field classes is not a structure field class: "
515 "%![req-fc-]+F, %![tgt-fc-]+F",
521 /* Same scope: path from LCA to target has no array/variant FTs */
522 if (!lca_to_target_has_struct_fc_only(src_field_path
, tgt_field_path
,
524 BT_ASSERT_PRE_DEV_MSG("Path from lowest common ancestor of target "
525 "and requesting field classes to target field class "
526 "contains an array or a variant field class: "
527 "%![req-fc-]+F, %![tgt-fc-]+F", src_fc
, tgt_fc
);
533 bt_object_put_ref(src_field_path
);
534 bt_object_put_ref(tgt_field_path
);
539 struct bt_field_path
*resolve_field_path(struct bt_field_class
*src_fc
,
540 struct bt_field_class
*tgt_fc
,
541 struct bt_resolve_field_path_context
*ctx
)
543 BT_ASSERT_PRE_DEV(field_path_is_valid(src_fc
, tgt_fc
, ctx
),
544 "Invalid target field class: %![req-fc-]+F, %![tgt-fc-]+F",
546 return find_field_class_in_ctx(tgt_fc
, ctx
);
550 int bt_resolve_field_paths(struct bt_field_class
*fc
,
551 struct bt_resolve_field_path_context
*ctx
)
557 /* Resolving part for dynamic array and variant field classes */
559 case BT_FIELD_CLASS_TYPE_OPTION
:
561 struct bt_field_class_option
*opt_fc
= (void *) fc
;
563 if (opt_fc
->selector_fc
) {
564 BT_ASSERT(!opt_fc
->selector_field_path
);
565 opt_fc
->selector_field_path
= resolve_field_path(
566 fc
, opt_fc
->selector_fc
, ctx
);
567 if (!opt_fc
->selector_field_path
) {
575 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
577 struct bt_field_class_array_dynamic
*dyn_array_fc
= (void *) fc
;
579 if (dyn_array_fc
->length_fc
) {
580 BT_ASSERT(!dyn_array_fc
->length_field_path
);
581 dyn_array_fc
->length_field_path
= resolve_field_path(
582 fc
, dyn_array_fc
->length_fc
, ctx
);
583 if (!dyn_array_fc
->length_field_path
) {
591 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
:
592 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
:
594 struct bt_field_class_variant_with_selector
*var_fc
=
597 if (var_fc
->selector_fc
) {
598 BT_ASSERT(!var_fc
->selector_field_path
);
599 var_fc
->selector_field_path
=
600 resolve_field_path(fc
,
601 (void *) var_fc
->selector_fc
, ctx
);
602 if (!var_fc
->selector_field_path
) {
614 case BT_FIELD_CLASS_TYPE_OPTION
:
616 struct bt_field_class_option
*opt_fc
= (void *) fc
;
618 ret
= bt_resolve_field_paths(opt_fc
->content_fc
, ctx
);
621 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
622 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
:
623 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
:
624 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
:
626 struct bt_field_class_named_field_class_container
*container_fc
=
630 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
631 struct bt_named_field_class
*named_fc
=
632 container_fc
->named_fcs
->pdata
[i
];
634 ret
= bt_resolve_field_paths(named_fc
->fc
, ctx
);
642 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
643 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
645 struct bt_field_class_array
*array_fc
= (void *) fc
;
647 ret
= bt_resolve_field_paths(array_fc
->element_fc
, ctx
);