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/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_STRUCTURE
:
51 case BT_FIELD_CLASS_TYPE_VARIANT
:
53 struct bt_field_class_named_field_class_container
*container_fc
=
57 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
58 struct bt_named_field_class
*named_fc
=
59 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(
61 struct bt_field_path_item item
= {
62 .type
= BT_FIELD_PATH_ITEM_TYPE_INDEX
,
66 bt_field_path_append_item(field_path
, &item
);
67 found
= find_field_class_recursive(named_fc
->fc
,
73 bt_field_path_remove_last_item(field_path
);
78 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
79 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
81 struct bt_field_class_array
*array_fc
= (void *) fc
;
82 struct bt_field_path_item item
= {
83 .type
= BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
,
84 .index
= UINT64_C(-1),
87 bt_field_path_append_item(field_path
, &item
);
88 found
= find_field_class_recursive(array_fc
->element_fc
,
94 bt_field_path_remove_last_item(field_path
);
106 int find_field_class(struct bt_field_class
*root_fc
,
107 enum bt_scope root_scope
, struct bt_field_class
*tgt_fc
,
108 struct bt_field_path
**ret_field_path
)
111 struct bt_field_path
*field_path
= NULL
;
117 field_path
= bt_field_path_create();
123 field_path
->root
= root_scope
;
124 if (!find_field_class_recursive(root_fc
, tgt_fc
, field_path
)) {
126 BT_OBJECT_PUT_REF_AND_RESET(field_path
);
130 *ret_field_path
= field_path
;
135 struct bt_field_path
*find_field_class_in_ctx(struct bt_field_class
*fc
,
136 struct bt_resolve_field_path_context
*ctx
)
138 struct bt_field_path
*field_path
= NULL
;
141 ret
= find_field_class(ctx
->packet_context
, BT_SCOPE_PACKET_CONTEXT
,
143 if (ret
|| field_path
) {
147 ret
= find_field_class(ctx
->event_common_context
,
148 BT_SCOPE_EVENT_COMMON_CONTEXT
, fc
, &field_path
);
149 if (ret
|| field_path
) {
153 ret
= find_field_class(ctx
->event_specific_context
,
154 BT_SCOPE_EVENT_SPECIFIC_CONTEXT
, fc
, &field_path
);
155 if (ret
|| field_path
) {
159 ret
= find_field_class(ctx
->event_payload
, BT_SCOPE_EVENT_PAYLOAD
,
161 if (ret
|| field_path
) {
171 bool target_is_before_source(struct bt_field_path
*src_field_path
,
172 struct bt_field_path
*tgt_field_path
)
174 bool is_valid
= true;
175 uint64_t src_i
= 0, tgt_i
= 0;
177 if (tgt_field_path
->root
< src_field_path
->root
) {
181 if (tgt_field_path
->root
> src_field_path
->root
) {
186 BT_ASSERT(tgt_field_path
->root
== src_field_path
->root
);
188 for (src_i
= 0, tgt_i
= 0; src_i
< src_field_path
->items
->len
&&
189 tgt_i
< tgt_field_path
->items
->len
; src_i
++, tgt_i
++) {
190 struct bt_field_path_item
*src_fp_item
=
191 bt_field_path_borrow_item_by_index_inline(
192 src_field_path
, src_i
);
193 struct bt_field_path_item
*tgt_fp_item
=
194 bt_field_path_borrow_item_by_index_inline(
195 tgt_field_path
, tgt_i
);
197 if (src_fp_item
->type
== BT_FIELD_PATH_ITEM_TYPE_INDEX
&&
198 tgt_fp_item
->type
== BT_FIELD_PATH_ITEM_TYPE_INDEX
) {
199 if (tgt_fp_item
->index
> src_fp_item
->index
) {
215 struct bt_field_class
*borrow_root_field_class(
216 struct bt_resolve_field_path_context
*ctx
, enum bt_scope scope
)
219 case BT_SCOPE_PACKET_CONTEXT
:
220 return ctx
->packet_context
;
221 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
222 return ctx
->event_common_context
;
223 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
224 return ctx
->event_specific_context
;
225 case BT_SCOPE_EVENT_PAYLOAD
:
226 return ctx
->event_payload
;
236 struct bt_field_class
*borrow_child_field_class(
237 struct bt_field_class
*parent_fc
,
238 struct bt_field_path_item
*fp_item
)
240 struct bt_field_class
*child_fc
= NULL
;
242 switch (parent_fc
->type
) {
243 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
244 case BT_FIELD_CLASS_TYPE_VARIANT
:
246 struct bt_named_field_class
*named_fc
;
248 BT_ASSERT(fp_item
->type
== BT_FIELD_PATH_ITEM_TYPE_INDEX
);
249 named_fc
= BT_FIELD_CLASS_NAMED_FC_AT_INDEX(parent_fc
,
251 child_fc
= named_fc
->fc
;
254 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
255 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
257 struct bt_field_class_array
*array_fc
= (void *) parent_fc
;
259 BT_ASSERT(fp_item
->type
==
260 BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
);
261 child_fc
= array_fc
->element_fc
;
273 bool target_field_path_in_different_scope_has_struct_fc_only(
274 struct bt_field_path
*src_field_path
,
275 struct bt_field_path
*tgt_field_path
,
276 struct bt_resolve_field_path_context
*ctx
)
278 bool is_valid
= true;
280 struct bt_field_class
*fc
;
282 if (src_field_path
->root
== tgt_field_path
->root
) {
286 fc
= borrow_root_field_class(ctx
, tgt_field_path
->root
);
288 for (i
= 0; i
< tgt_field_path
->items
->len
; i
++) {
289 struct bt_field_path_item
*fp_item
=
290 bt_field_path_borrow_item_by_index_inline(
293 if (fc
->type
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
||
294 fc
->type
== BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
||
295 fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT
) {
300 BT_ASSERT(fp_item
->type
== BT_FIELD_PATH_ITEM_TYPE_INDEX
);
301 fc
= borrow_child_field_class(fc
, fp_item
);
310 bool lca_is_structure_field_class(struct bt_field_path
*src_field_path
,
311 struct bt_field_path
*tgt_field_path
,
312 struct bt_resolve_field_path_context
*ctx
)
314 bool is_valid
= true;
315 struct bt_field_class
*src_fc
;
316 struct bt_field_class
*tgt_fc
;
317 struct bt_field_class
*prev_fc
= NULL
;
318 uint64_t src_i
= 0, tgt_i
= 0;
320 if (src_field_path
->root
!= tgt_field_path
->root
) {
324 src_fc
= borrow_root_field_class(ctx
, src_field_path
->root
);
325 tgt_fc
= borrow_root_field_class(ctx
, tgt_field_path
->root
);
329 for (src_i
= 0, tgt_i
= 0; src_i
< src_field_path
->items
->len
&&
330 tgt_i
< tgt_field_path
->items
->len
; src_i
++, tgt_i
++) {
331 struct bt_field_path_item
*src_fp_item
=
332 bt_field_path_borrow_item_by_index_inline(
333 src_field_path
, src_i
);
334 struct bt_field_path_item
*tgt_fp_item
=
335 bt_field_path_borrow_item_by_index_inline(
336 tgt_field_path
, tgt_i
);
338 if (src_fc
!= tgt_fc
) {
341 * This is correct: the LCA is the root
342 * scope field class, which must be a
343 * structure field class.
348 if (prev_fc
->type
!= BT_FIELD_CLASS_TYPE_STRUCTURE
) {
356 src_fc
= borrow_child_field_class(src_fc
, src_fp_item
);
357 tgt_fc
= borrow_child_field_class(tgt_fc
, tgt_fp_item
);
366 bool lca_to_target_has_struct_fc_only(struct bt_field_path
*src_field_path
,
367 struct bt_field_path
*tgt_field_path
,
368 struct bt_resolve_field_path_context
*ctx
)
370 bool is_valid
= true;
371 struct bt_field_class
*src_fc
;
372 struct bt_field_class
*tgt_fc
;
373 uint64_t src_i
= 0, tgt_i
= 0;
375 if (src_field_path
->root
!= tgt_field_path
->root
) {
379 src_fc
= borrow_root_field_class(ctx
, src_field_path
->root
);
380 tgt_fc
= borrow_root_field_class(ctx
, tgt_field_path
->root
);
383 BT_ASSERT(src_fc
== tgt_fc
);
386 for (src_i
= 0, tgt_i
= 0; src_i
< src_field_path
->items
->len
&&
387 tgt_i
< tgt_field_path
->items
->len
; src_i
++, tgt_i
++) {
388 struct bt_field_path_item
*src_fp_item
=
389 bt_field_path_borrow_item_by_index_inline(
390 src_field_path
, src_i
);
391 struct bt_field_path_item
*tgt_fp_item
=
392 bt_field_path_borrow_item_by_index_inline(
393 tgt_field_path
, tgt_i
);
395 if (src_i
!= tgt_i
) {
396 /* Next field class is different: LCA is `tgt_fc` */
400 src_fc
= borrow_child_field_class(src_fc
, src_fp_item
);
401 tgt_fc
= borrow_child_field_class(tgt_fc
, tgt_fp_item
);
404 /* Only structure field classes to the target */
405 for (; tgt_i
< tgt_field_path
->items
->len
; tgt_i
++) {
406 struct bt_field_path_item
*tgt_fp_item
=
407 bt_field_path_borrow_item_by_index_inline(
408 tgt_field_path
, tgt_i
);
410 if (tgt_fc
->type
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
||
411 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
||
412 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT
) {
417 tgt_fc
= borrow_child_field_class(tgt_fc
, tgt_fp_item
);
426 bool field_path_is_valid(struct bt_field_class
*src_fc
,
427 struct bt_field_class
*tgt_fc
,
428 struct bt_resolve_field_path_context
*ctx
)
430 bool is_valid
= true;
431 struct bt_field_path
*src_field_path
= find_field_class_in_ctx(
433 struct bt_field_path
*tgt_field_path
= find_field_class_in_ctx(
436 if (!src_field_path
) {
437 BT_ASSERT_PRE_MSG("Cannot find requesting field class in "
438 "resolving context: %!+F", src_fc
);
443 if (!tgt_field_path
) {
444 BT_ASSERT_PRE_MSG("Cannot find target field class in "
445 "resolving context: %!+F", tgt_fc
);
450 /* Target must be before source */
451 if (!target_is_before_source(src_field_path
, tgt_field_path
)) {
452 BT_ASSERT_PRE_MSG("Target field class is located after "
453 "requesting field class: %![req-fc-]+F, %![tgt-fc-]+F",
460 * If target is in a different scope than source, there are no
461 * array or variant field classes on the way to the target.
463 if (!target_field_path_in_different_scope_has_struct_fc_only(
464 src_field_path
, tgt_field_path
, ctx
)) {
465 BT_ASSERT_PRE_MSG("Target field class is located in a "
466 "different scope than requesting field class, "
467 "but within an array or a variant field class: "
468 "%![req-fc-]+F, %![tgt-fc-]+F",
474 /* Same scope: LCA must be a structure field class */
475 if (!lca_is_structure_field_class(src_field_path
, tgt_field_path
, ctx
)) {
476 BT_ASSERT_PRE_MSG("Lowest common ancestor of target and "
477 "requesting field classes is not a structure field class: "
478 "%![req-fc-]+F, %![tgt-fc-]+F",
484 /* Same scope: path from LCA to target has no array/variant FTs */
485 if (!lca_to_target_has_struct_fc_only(src_field_path
, tgt_field_path
,
487 BT_ASSERT_PRE_MSG("Path from lowest common ancestor of target "
488 "and requesting field classes to target field class "
489 "contains an array or a variant field class: "
490 "%![req-fc-]+F, %![tgt-fc-]+F", src_fc
, tgt_fc
);
496 bt_object_put_ref(src_field_path
);
497 bt_object_put_ref(tgt_field_path
);
502 struct bt_field_path
*resolve_field_path(struct bt_field_class
*src_fc
,
503 struct bt_field_class
*tgt_fc
,
504 struct bt_resolve_field_path_context
*ctx
)
506 BT_ASSERT_PRE(field_path_is_valid(src_fc
, tgt_fc
, ctx
),
507 "Invalid target field class: %![req-fc-]+F, %![tgt-fc-]+F",
509 return find_field_class_in_ctx(tgt_fc
, ctx
);
513 int bt_resolve_field_paths(struct bt_field_class
*fc
,
514 struct bt_resolve_field_path_context
*ctx
)
520 /* Resolving part for dynamic array and variant field classes */
522 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
524 struct bt_field_class_dynamic_array
*dyn_array_fc
= (void *) fc
;
526 if (dyn_array_fc
->length_fc
) {
527 BT_ASSERT(!dyn_array_fc
->length_field_path
);
528 dyn_array_fc
->length_field_path
= resolve_field_path(
529 fc
, dyn_array_fc
->length_fc
, ctx
);
530 if (!dyn_array_fc
->length_field_path
) {
538 case BT_FIELD_CLASS_TYPE_VARIANT
:
540 struct bt_field_class_variant
*var_fc
= (void *) fc
;
542 if (var_fc
->selector_fc
) {
543 BT_ASSERT(!var_fc
->selector_field_path
);
544 var_fc
->selector_field_path
=
545 resolve_field_path(fc
,
546 var_fc
->selector_fc
, ctx
);
547 if (!var_fc
->selector_field_path
) {
559 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
560 case BT_FIELD_CLASS_TYPE_VARIANT
:
562 struct bt_field_class_named_field_class_container
*container_fc
=
566 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
567 struct bt_named_field_class
*named_fc
=
568 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(
571 ret
= bt_resolve_field_paths(named_fc
->fc
, ctx
);
579 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
580 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
582 struct bt_field_class_array
*array_fc
= (void *) fc
;
584 ret
= bt_resolve_field_paths(array_fc
->element_fc
, ctx
);