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_WITHOUT_SELECTOR
:
51 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR
:
52 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR
:
53 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR
:
55 struct bt_field_class_option
*opt_fc
= (void *) fc
;
56 struct bt_field_path_item item
= {
57 .type
= BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT
,
58 .index
= UINT64_C(-1),
61 bt_field_path_append_item(field_path
, &item
);
62 found
= find_field_class_recursive(opt_fc
->content_fc
,
68 bt_field_path_remove_last_item(field_path
);
71 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
72 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
:
73 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR
:
74 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR
:
76 struct bt_field_class_named_field_class_container
*container_fc
=
80 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
81 struct bt_named_field_class
*named_fc
=
82 container_fc
->named_fcs
->pdata
[i
];
83 struct bt_field_path_item item
= {
84 .type
= BT_FIELD_PATH_ITEM_TYPE_INDEX
,
88 bt_field_path_append_item(field_path
, &item
);
89 found
= find_field_class_recursive(named_fc
->fc
,
95 bt_field_path_remove_last_item(field_path
);
100 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
101 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
103 struct bt_field_class_array
*array_fc
= (void *) fc
;
104 struct bt_field_path_item item
= {
105 .type
= BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
,
106 .index
= UINT64_C(-1),
109 bt_field_path_append_item(field_path
, &item
);
110 found
= find_field_class_recursive(array_fc
->element_fc
,
116 bt_field_path_remove_last_item(field_path
);
128 int find_field_class(struct bt_field_class
*root_fc
,
129 enum bt_field_path_scope root_scope
, struct bt_field_class
*tgt_fc
,
130 struct bt_field_path
**ret_field_path
)
133 struct bt_field_path
*field_path
= NULL
;
139 field_path
= bt_field_path_create();
145 field_path
->root
= root_scope
;
146 if (!find_field_class_recursive(root_fc
, tgt_fc
, field_path
)) {
148 BT_OBJECT_PUT_REF_AND_RESET(field_path
);
152 *ret_field_path
= field_path
;
157 struct bt_field_path
*find_field_class_in_ctx(struct bt_field_class
*fc
,
158 struct bt_resolve_field_path_context
*ctx
)
160 struct bt_field_path
*field_path
= NULL
;
163 ret
= find_field_class(ctx
->packet_context
, BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
,
165 if (ret
|| field_path
) {
169 ret
= find_field_class(ctx
->event_common_context
,
170 BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
, fc
, &field_path
);
171 if (ret
|| field_path
) {
175 ret
= find_field_class(ctx
->event_specific_context
,
176 BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
, fc
, &field_path
);
177 if (ret
|| field_path
) {
181 ret
= find_field_class(ctx
->event_payload
, BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
,
183 if (ret
|| field_path
) {
191 BT_ASSERT_PRE_DEV_FUNC
193 bool target_is_before_source(struct bt_field_path
*src_field_path
,
194 struct bt_field_path
*tgt_field_path
)
196 bool is_valid
= true;
197 uint64_t src_i
= 0, tgt_i
= 0;
199 if (tgt_field_path
->root
< src_field_path
->root
) {
203 if (tgt_field_path
->root
> src_field_path
->root
) {
208 BT_ASSERT(tgt_field_path
->root
== src_field_path
->root
);
210 for (src_i
= 0, tgt_i
= 0; src_i
< src_field_path
->items
->len
&&
211 tgt_i
< tgt_field_path
->items
->len
; src_i
++, tgt_i
++) {
212 struct bt_field_path_item
*src_fp_item
=
213 bt_field_path_borrow_item_by_index_inline(
214 src_field_path
, src_i
);
215 struct bt_field_path_item
*tgt_fp_item
=
216 bt_field_path_borrow_item_by_index_inline(
217 tgt_field_path
, tgt_i
);
219 if (src_fp_item
->type
== BT_FIELD_PATH_ITEM_TYPE_INDEX
&&
220 tgt_fp_item
->type
== BT_FIELD_PATH_ITEM_TYPE_INDEX
) {
221 if (tgt_fp_item
->index
> src_fp_item
->index
) {
235 BT_ASSERT_PRE_DEV_FUNC
237 struct bt_field_class
*borrow_root_field_class(
238 struct bt_resolve_field_path_context
*ctx
, enum bt_field_path_scope scope
)
241 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
:
242 return ctx
->packet_context
;
243 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
:
244 return ctx
->event_common_context
;
245 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
:
246 return ctx
->event_specific_context
;
247 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
:
248 return ctx
->event_payload
;
256 BT_ASSERT_PRE_DEV_FUNC
258 struct bt_field_class
*borrow_child_field_class(
259 struct bt_field_class
*parent_fc
,
260 struct bt_field_path_item
*fp_item
)
262 struct bt_field_class
*child_fc
= NULL
;
264 switch (parent_fc
->type
) {
265 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR
:
266 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR
:
267 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR
:
268 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR
:
270 struct bt_field_class_option
*opt_fc
= (void *) parent_fc
;
272 BT_ASSERT(fp_item
->type
==
273 BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT
);
274 child_fc
= opt_fc
->content_fc
;
277 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
278 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
:
279 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR
:
280 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR
:
282 struct bt_field_class_named_field_class_container
*container_fc
=
284 struct bt_named_field_class
*named_fc
;
286 BT_ASSERT(fp_item
->type
== BT_FIELD_PATH_ITEM_TYPE_INDEX
);
287 named_fc
= container_fc
->named_fcs
->pdata
[fp_item
->index
];
288 child_fc
= named_fc
->fc
;
291 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
292 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
294 struct bt_field_class_array
*array_fc
= (void *) parent_fc
;
296 BT_ASSERT(fp_item
->type
==
297 BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
);
298 child_fc
= array_fc
->element_fc
;
308 BT_ASSERT_PRE_DEV_FUNC
310 bool target_field_path_in_different_scope_has_struct_fc_only(
311 struct bt_field_path
*src_field_path
,
312 struct bt_field_path
*tgt_field_path
,
313 struct bt_resolve_field_path_context
*ctx
)
315 bool is_valid
= true;
317 struct bt_field_class
*fc
;
319 if (src_field_path
->root
== tgt_field_path
->root
) {
323 fc
= borrow_root_field_class(ctx
, tgt_field_path
->root
);
325 for (i
= 0; i
< tgt_field_path
->items
->len
; i
++) {
326 struct bt_field_path_item
*fp_item
=
327 bt_field_path_borrow_item_by_index_inline(
330 if (fc
->type
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
||
331 fc
->type
== BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
||
332 fc
->type
== BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR
||
333 fc
->type
== BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR
||
334 fc
->type
== BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR
||
335 fc
->type
== BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR
||
336 fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
||
337 fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR
||
338 fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR
) {
343 BT_ASSERT(fp_item
->type
== BT_FIELD_PATH_ITEM_TYPE_INDEX
);
344 fc
= borrow_child_field_class(fc
, fp_item
);
351 BT_ASSERT_PRE_DEV_FUNC
353 bool lca_is_structure_field_class(struct bt_field_path
*src_field_path
,
354 struct bt_field_path
*tgt_field_path
,
355 struct bt_resolve_field_path_context
*ctx
)
357 bool is_valid
= true;
358 struct bt_field_class
*src_fc
;
359 struct bt_field_class
*tgt_fc
;
360 struct bt_field_class
*prev_fc
= NULL
;
361 uint64_t src_i
= 0, tgt_i
= 0;
363 if (src_field_path
->root
!= tgt_field_path
->root
) {
367 src_fc
= borrow_root_field_class(ctx
, src_field_path
->root
);
368 tgt_fc
= borrow_root_field_class(ctx
, tgt_field_path
->root
);
372 for (src_i
= 0, tgt_i
= 0; src_i
< src_field_path
->items
->len
&&
373 tgt_i
< tgt_field_path
->items
->len
; src_i
++, tgt_i
++) {
374 struct bt_field_path_item
*src_fp_item
=
375 bt_field_path_borrow_item_by_index_inline(
376 src_field_path
, src_i
);
377 struct bt_field_path_item
*tgt_fp_item
=
378 bt_field_path_borrow_item_by_index_inline(
379 tgt_field_path
, tgt_i
);
381 if (src_fc
!= tgt_fc
) {
384 * This is correct: the LCA is the root
385 * scope field class, which must be a
386 * structure field class.
391 if (prev_fc
->type
!= BT_FIELD_CLASS_TYPE_STRUCTURE
) {
399 src_fc
= borrow_child_field_class(src_fc
, src_fp_item
);
400 tgt_fc
= borrow_child_field_class(tgt_fc
, tgt_fp_item
);
407 BT_ASSERT_PRE_DEV_FUNC
409 bool lca_to_target_has_struct_fc_only(struct bt_field_path
*src_field_path
,
410 struct bt_field_path
*tgt_field_path
,
411 struct bt_resolve_field_path_context
*ctx
)
413 bool is_valid
= true;
414 struct bt_field_class
*src_fc
;
415 struct bt_field_class
*tgt_fc
;
416 uint64_t src_i
= 0, tgt_i
= 0;
418 if (src_field_path
->root
!= tgt_field_path
->root
) {
422 src_fc
= borrow_root_field_class(ctx
, src_field_path
->root
);
423 tgt_fc
= borrow_root_field_class(ctx
, tgt_field_path
->root
);
426 BT_ASSERT(src_fc
== tgt_fc
);
429 for (src_i
= 0, tgt_i
= 0; src_i
< src_field_path
->items
->len
&&
430 tgt_i
< tgt_field_path
->items
->len
; src_i
++, tgt_i
++) {
431 struct bt_field_path_item
*src_fp_item
=
432 bt_field_path_borrow_item_by_index_inline(
433 src_field_path
, src_i
);
434 struct bt_field_path_item
*tgt_fp_item
=
435 bt_field_path_borrow_item_by_index_inline(
436 tgt_field_path
, tgt_i
);
438 if (src_i
!= tgt_i
) {
439 /* Next field class is different: LCA is `tgt_fc` */
443 src_fc
= borrow_child_field_class(src_fc
, src_fp_item
);
444 tgt_fc
= borrow_child_field_class(tgt_fc
, tgt_fp_item
);
447 /* Only structure field classes to the target */
448 for (; tgt_i
< tgt_field_path
->items
->len
; tgt_i
++) {
449 struct bt_field_path_item
*tgt_fp_item
=
450 bt_field_path_borrow_item_by_index_inline(
451 tgt_field_path
, tgt_i
);
453 if (tgt_fc
->type
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
||
454 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
||
455 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR
||
456 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR
||
457 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR
||
458 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR
||
459 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
||
460 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR
||
461 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR
) {
466 tgt_fc
= borrow_child_field_class(tgt_fc
, tgt_fp_item
);
473 BT_ASSERT_PRE_DEV_FUNC
475 bool field_path_is_valid(struct bt_field_class
*src_fc
,
476 struct bt_field_class
*tgt_fc
,
477 struct bt_resolve_field_path_context
*ctx
)
479 bool is_valid
= true;
480 struct bt_field_path
*src_field_path
= find_field_class_in_ctx(
482 struct bt_field_path
*tgt_field_path
= find_field_class_in_ctx(
485 if (!src_field_path
) {
486 BT_ASSERT_PRE_DEV_MSG("Cannot find requesting field class in "
487 "resolving context: %!+F", src_fc
);
492 if (!tgt_field_path
) {
493 BT_ASSERT_PRE_DEV_MSG("Cannot find target field class in "
494 "resolving context: %!+F", tgt_fc
);
499 /* Target must be before source */
500 if (!target_is_before_source(src_field_path
, tgt_field_path
)) {
501 BT_ASSERT_PRE_DEV_MSG("Target field class is located after "
502 "requesting field class: %![req-fc-]+F, %![tgt-fc-]+F",
509 * If target is in a different scope than source, there are no
510 * array or variant field classes on the way to the target.
512 if (!target_field_path_in_different_scope_has_struct_fc_only(
513 src_field_path
, tgt_field_path
, ctx
)) {
514 BT_ASSERT_PRE_DEV_MSG("Target field class is located in a "
515 "different scope than requesting field class, "
516 "but within an array or a variant field class: "
517 "%![req-fc-]+F, %![tgt-fc-]+F",
523 /* Same scope: LCA must be a structure field class */
524 if (!lca_is_structure_field_class(src_field_path
, tgt_field_path
, ctx
)) {
525 BT_ASSERT_PRE_DEV_MSG("Lowest common ancestor of target and "
526 "requesting field classes is not a structure field class: "
527 "%![req-fc-]+F, %![tgt-fc-]+F",
533 /* Same scope: path from LCA to target has no array/variant FTs */
534 if (!lca_to_target_has_struct_fc_only(src_field_path
, tgt_field_path
,
536 BT_ASSERT_PRE_DEV_MSG("Path from lowest common ancestor of target "
537 "and requesting field classes to target field class "
538 "contains an array or a variant field class: "
539 "%![req-fc-]+F, %![tgt-fc-]+F", src_fc
, tgt_fc
);
545 bt_object_put_ref(src_field_path
);
546 bt_object_put_ref(tgt_field_path
);
551 struct bt_field_path
*resolve_field_path(struct bt_field_class
*src_fc
,
552 struct bt_field_class
*tgt_fc
,
553 struct bt_resolve_field_path_context
*ctx
)
555 BT_ASSERT_PRE_DEV(field_path_is_valid(src_fc
, tgt_fc
, ctx
),
556 "Invalid target field class: %![req-fc-]+F, %![tgt-fc-]+F",
558 return find_field_class_in_ctx(tgt_fc
, ctx
);
562 int bt_resolve_field_paths(struct bt_field_class
*fc
,
563 struct bt_resolve_field_path_context
*ctx
)
569 /* Resolving part for dynamic array and variant field classes */
571 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR
:
572 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR
:
573 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR
:
575 struct bt_field_class_option_with_selector
*opt_fc
= (void *) fc
;
577 if (opt_fc
->selector_fc
) {
578 BT_ASSERT(!opt_fc
->selector_field_path
);
579 opt_fc
->selector_field_path
= resolve_field_path(
580 fc
, opt_fc
->selector_fc
, ctx
);
581 if (!opt_fc
->selector_field_path
) {
589 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
591 struct bt_field_class_array_dynamic
*dyn_array_fc
= (void *) fc
;
593 if (dyn_array_fc
->length_fc
) {
594 BT_ASSERT(!dyn_array_fc
->length_field_path
);
595 dyn_array_fc
->length_field_path
= resolve_field_path(
596 fc
, dyn_array_fc
->length_fc
, ctx
);
597 if (!dyn_array_fc
->length_field_path
) {
605 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR
:
606 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR
:
608 struct bt_field_class_variant_with_selector
*var_fc
=
611 if (var_fc
->selector_fc
) {
612 BT_ASSERT(!var_fc
->selector_field_path
);
613 var_fc
->selector_field_path
=
614 resolve_field_path(fc
,
615 (void *) var_fc
->selector_fc
, ctx
);
616 if (!var_fc
->selector_field_path
) {
628 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR
:
629 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR
:
630 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR
:
631 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR
:
633 struct bt_field_class_option
*opt_fc
= (void *) fc
;
635 ret
= bt_resolve_field_paths(opt_fc
->content_fc
, ctx
);
638 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
639 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
:
640 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR
:
641 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR
:
643 struct bt_field_class_named_field_class_container
*container_fc
=
647 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
648 struct bt_named_field_class
*named_fc
=
649 container_fc
->named_fcs
->pdata
[i
];
651 ret
= bt_resolve_field_paths(named_fc
->fc
, ctx
);
659 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
660 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
662 struct bt_field_class_array
*array_fc
= (void *) fc
;
664 ret
= bt_resolve_field_paths(array_fc
->element_fc
, ctx
);