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 "RESOLVE-FIELD-PATH"
24 #include <babeltrace/lib-logging-internal.h>
26 #include <babeltrace/assert-pre-internal.h>
27 #include <babeltrace/assert-internal.h>
28 #include <babeltrace/trace-ir/field-class-internal.h>
29 #include <babeltrace/trace-ir/field-path-internal.h>
30 #include <babeltrace/trace-ir/field-path-const.h>
31 #include <babeltrace/trace-ir/resolve-field-path-internal.h>
38 bool find_field_class_recursive(struct bt_field_class
*fc
,
39 struct bt_field_class
*tgt_fc
, struct bt_field_path
*field_path
)
49 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
50 case BT_FIELD_CLASS_TYPE_VARIANT
:
52 struct bt_field_class_named_field_class_container
*container_fc
=
56 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
57 struct bt_named_field_class
*named_fc
=
58 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(
61 g_array_append_val(field_path
->indexes
, i
);
62 found
= find_field_class_recursive(named_fc
->fc
,
68 g_array_set_size(field_path
->indexes
,
69 field_path
->indexes
->len
- 1);
74 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
75 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
77 struct bt_field_class_array
*array_fc
= (void *) fc
;
79 found
= find_field_class_recursive(array_fc
->element_fc
,
92 int find_field_class(struct bt_field_class
*root_fc
,
93 enum bt_scope root_scope
, struct bt_field_class
*tgt_fc
,
94 struct bt_field_path
**ret_field_path
)
97 struct bt_field_path
*field_path
= NULL
;
103 field_path
= bt_field_path_create();
109 field_path
->root
= root_scope
;
110 if (!find_field_class_recursive(root_fc
, tgt_fc
, field_path
)) {
112 BT_OBJECT_PUT_REF_AND_RESET(field_path
);
116 *ret_field_path
= field_path
;
121 struct bt_field_path
*find_field_class_in_ctx(struct bt_field_class
*fc
,
122 struct bt_resolve_field_path_context
*ctx
)
124 struct bt_field_path
*field_path
= NULL
;
127 ret
= find_field_class(ctx
->packet_context
, BT_SCOPE_PACKET_CONTEXT
,
129 if (ret
|| field_path
) {
133 ret
= find_field_class(ctx
->event_common_context
,
134 BT_SCOPE_EVENT_COMMON_CONTEXT
, fc
, &field_path
);
135 if (ret
|| field_path
) {
139 ret
= find_field_class(ctx
->event_specific_context
,
140 BT_SCOPE_EVENT_SPECIFIC_CONTEXT
, fc
, &field_path
);
141 if (ret
|| field_path
) {
145 ret
= find_field_class(ctx
->event_payload
, BT_SCOPE_EVENT_PAYLOAD
,
147 if (ret
|| field_path
) {
157 bool target_is_before_source(struct bt_field_path
*src_field_path
,
158 struct bt_field_path
*tgt_field_path
)
160 bool is_valid
= true;
161 uint64_t src_i
= 0, tgt_i
= 0;
163 if (tgt_field_path
->root
< src_field_path
->root
) {
167 if (tgt_field_path
->root
> src_field_path
->root
) {
172 BT_ASSERT(tgt_field_path
->root
== src_field_path
->root
);
174 while (src_i
< src_field_path
->indexes
->len
&&
175 tgt_i
< tgt_field_path
->indexes
->len
) {
176 uint64_t src_index
= bt_field_path_get_index_by_index_inline(
177 src_field_path
, src_i
);
178 uint64_t tgt_index
= bt_field_path_get_index_by_index_inline(
179 tgt_field_path
, tgt_i
);
181 if (tgt_index
> src_index
) {
196 struct bt_field_class
*borrow_root_field_class(
197 struct bt_resolve_field_path_context
*ctx
, enum bt_scope scope
)
200 case BT_SCOPE_PACKET_CONTEXT
:
201 return ctx
->packet_context
;
202 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
203 return ctx
->event_common_context
;
204 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
205 return ctx
->event_specific_context
;
206 case BT_SCOPE_EVENT_PAYLOAD
:
207 return ctx
->event_payload
;
217 struct bt_field_class
*borrow_child_field_class(struct bt_field_class
*parent_fc
,
218 uint64_t index
, bool *advance
)
220 struct bt_field_class
*child_fc
= NULL
;
222 switch (parent_fc
->type
) {
223 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
224 case BT_FIELD_CLASS_TYPE_VARIANT
:
226 struct bt_named_field_class
*named_fc
=
227 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(parent_fc
, index
);
229 child_fc
= named_fc
->fc
;
233 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
234 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
236 struct bt_field_class_array
*array_fc
= (void *) parent_fc
;
238 child_fc
= array_fc
->element_fc
;
251 bool target_field_path_in_different_scope_has_struct_fc_only(
252 struct bt_field_path
*src_field_path
,
253 struct bt_field_path
*tgt_field_path
,
254 struct bt_resolve_field_path_context
*ctx
)
256 bool is_valid
= true;
258 struct bt_field_class
*fc
;
260 if (src_field_path
->root
== tgt_field_path
->root
) {
264 fc
= borrow_root_field_class(ctx
, tgt_field_path
->root
);
266 while (i
< tgt_field_path
->indexes
->len
) {
267 uint64_t index
= bt_field_path_get_index_by_index_inline(
271 if (fc
->type
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
||
272 fc
->type
== BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
||
273 fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT
) {
278 fc
= borrow_child_field_class(fc
, index
, &advance
);
291 bool lca_is_structure_field_class(struct bt_field_path
*src_field_path
,
292 struct bt_field_path
*tgt_field_path
,
293 struct bt_resolve_field_path_context
*ctx
)
295 bool is_valid
= true;
296 struct bt_field_class
*src_fc
;
297 struct bt_field_class
*tgt_fc
;
298 struct bt_field_class
*prev_fc
= NULL
;
299 uint64_t src_i
= 0, tgt_i
= 0;
301 if (src_field_path
->root
!= tgt_field_path
->root
) {
305 src_fc
= borrow_root_field_class(ctx
, src_field_path
->root
);
306 tgt_fc
= borrow_root_field_class(ctx
, tgt_field_path
->root
);
310 while (src_i
< src_field_path
->indexes
->len
&&
311 tgt_i
< tgt_field_path
->indexes
->len
) {
313 uint64_t src_index
= bt_field_path_get_index_by_index_inline(
314 src_field_path
, src_i
);
315 uint64_t tgt_index
= bt_field_path_get_index_by_index_inline(
316 tgt_field_path
, tgt_i
);
318 if (src_fc
!= tgt_fc
) {
321 * This is correct: the LCA is the root
322 * scope field class, which must be a
323 * structure field class.
328 if (prev_fc
->type
!= BT_FIELD_CLASS_TYPE_STRUCTURE
) {
336 src_fc
= borrow_child_field_class(src_fc
, src_index
, &advance
);
342 tgt_fc
= borrow_child_field_class(tgt_fc
, tgt_index
, &advance
);
355 bool lca_to_target_has_struct_fc_only(struct bt_field_path
*src_field_path
,
356 struct bt_field_path
*tgt_field_path
,
357 struct bt_resolve_field_path_context
*ctx
)
359 bool is_valid
= true;
360 struct bt_field_class
*src_fc
;
361 struct bt_field_class
*tgt_fc
;
362 uint64_t src_i
= 0, tgt_i
= 0;
364 if (src_field_path
->root
!= tgt_field_path
->root
) {
368 src_fc
= borrow_root_field_class(ctx
, src_field_path
->root
);
369 tgt_fc
= borrow_root_field_class(ctx
, tgt_field_path
->root
);
372 BT_ASSERT(src_fc
== tgt_fc
);
375 while (src_i
< src_field_path
->indexes
->len
&&
376 tgt_i
< tgt_field_path
->indexes
->len
) {
378 uint64_t src_index
= bt_field_path_get_index_by_index_inline(
379 src_field_path
, src_i
);
380 uint64_t tgt_index
= bt_field_path_get_index_by_index_inline(
381 tgt_field_path
, tgt_i
);
383 if (src_i
!= tgt_i
) {
384 /* Next field class is different: LCA is `tgt_fc` */
388 src_fc
= borrow_child_field_class(src_fc
, src_index
, &advance
);
394 tgt_fc
= borrow_child_field_class(tgt_fc
, tgt_index
, &advance
);
401 /* Only structure field classes to the target */
402 while (tgt_i
< tgt_field_path
->indexes
->len
) {
404 uint64_t tgt_index
= bt_field_path_get_index_by_index_inline(
405 tgt_field_path
, tgt_i
);
407 if (tgt_fc
->type
== BT_FIELD_CLASS_TYPE_STATIC_ARRAY
||
408 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
||
409 tgt_fc
->type
== BT_FIELD_CLASS_TYPE_VARIANT
) {
414 tgt_fc
= borrow_child_field_class(tgt_fc
, tgt_index
, &advance
);
427 bool field_path_is_valid(struct bt_field_class
*src_fc
,
428 struct bt_field_class
*tgt_fc
,
429 struct bt_resolve_field_path_context
*ctx
)
431 bool is_valid
= true;
432 struct bt_field_path
*src_field_path
= find_field_class_in_ctx(
434 struct bt_field_path
*tgt_field_path
= find_field_class_in_ctx(
437 if (!src_field_path
) {
438 BT_ASSERT_PRE_MSG("Cannot find requesting field class in "
439 "resolving context: %!+F", src_fc
);
444 if (!tgt_field_path
) {
445 BT_ASSERT_PRE_MSG("Cannot find target field class in "
446 "resolving context: %!+F", tgt_fc
);
451 /* Target must be before source */
452 if (!target_is_before_source(src_field_path
, tgt_field_path
)) {
453 BT_ASSERT_PRE_MSG("Target field class is located after "
454 "requesting field class: %![req-fc-]+F, %![tgt-fc-]+F",
461 * If target is in a different scope than source, there are no
462 * array or variant field classes on the way to the target.
464 if (!target_field_path_in_different_scope_has_struct_fc_only(
465 src_field_path
, tgt_field_path
, ctx
)) {
466 BT_ASSERT_PRE_MSG("Target field class is located in a "
467 "different scope than requesting field class, "
468 "but within an array or a variant field class: "
469 "%![req-fc-]+F, %![tgt-fc-]+F",
475 /* Same scope: LCA must be a structure field class */
476 if (!lca_is_structure_field_class(src_field_path
, tgt_field_path
, ctx
)) {
477 BT_ASSERT_PRE_MSG("Lowest common ancestor of target and "
478 "requesting field classes is not a structure field class: "
479 "%![req-fc-]+F, %![tgt-fc-]+F",
485 /* Same scope: path from LCA to target has no array/variant FTs */
486 if (!lca_to_target_has_struct_fc_only(src_field_path
, tgt_field_path
,
488 BT_ASSERT_PRE_MSG("Path from lowest common ancestor of target "
489 "and requesting field classes to target field class "
490 "contains an array or a variant field class: "
491 "%![req-fc-]+F, %![tgt-fc-]+F", src_fc
, tgt_fc
);
497 bt_object_put_ref(src_field_path
);
498 bt_object_put_ref(tgt_field_path
);
503 struct bt_field_path
*resolve_field_path(struct bt_field_class
*src_fc
,
504 struct bt_field_class
*tgt_fc
,
505 struct bt_resolve_field_path_context
*ctx
)
507 BT_ASSERT_PRE(field_path_is_valid(src_fc
, tgt_fc
, ctx
),
508 "Invalid target field class: %![req-fc-]+F, %![tgt-fc-]+F",
510 return find_field_class_in_ctx(tgt_fc
, ctx
);
514 int bt_resolve_field_paths(struct bt_field_class
*fc
,
515 struct bt_resolve_field_path_context
*ctx
)
521 /* Resolving part for dynamic array and variant field classes */
523 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
525 struct bt_field_class_dynamic_array
*dyn_array_fc
= (void *) fc
;
527 if (dyn_array_fc
->length_fc
) {
528 BT_ASSERT(!dyn_array_fc
->length_field_path
);
529 dyn_array_fc
->length_field_path
= resolve_field_path(
530 fc
, dyn_array_fc
->length_fc
, ctx
);
531 if (!dyn_array_fc
->length_field_path
) {
539 case BT_FIELD_CLASS_TYPE_VARIANT
:
541 struct bt_field_class_variant
*var_fc
= (void *) fc
;
543 if (var_fc
->selector_fc
) {
544 BT_ASSERT(!var_fc
->selector_field_path
);
545 var_fc
->selector_field_path
=
546 resolve_field_path(fc
,
547 var_fc
->selector_fc
, ctx
);
548 if (!var_fc
->selector_field_path
) {
560 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
561 case BT_FIELD_CLASS_TYPE_VARIANT
:
563 struct bt_field_class_named_field_class_container
*container_fc
=
567 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
568 struct bt_named_field_class
*named_fc
=
569 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(
572 ret
= bt_resolve_field_paths(named_fc
->fc
, ctx
);
580 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
581 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
583 struct bt_field_class_array
*array_fc
= (void *) fc
;
585 ret
= bt_resolve_field_paths(array_fc
->element_fc
, ctx
);