| 1 | /* |
| 2 | * Babeltrace - Trace IR field copy |
| 3 | * |
| 4 | * Copyright (c) 2015-2019 EfficiOS Inc. and Linux Foundation |
| 5 | * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com> |
| 6 | * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #define BT_COMP_LOG_SELF_COMP (md_maps->self_comp) |
| 28 | #define BT_LOG_OUTPUT_LEVEL (md_maps->log_level) |
| 29 | #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-META-FC-COPY" |
| 30 | #include "plugins/comp-logging.h" |
| 31 | |
| 32 | #include "common/assert.h" |
| 33 | #include "common/common.h" |
| 34 | #include "compat/compiler.h" |
| 35 | #include <babeltrace2/babeltrace.h> |
| 36 | |
| 37 | #include "trace-ir-metadata-copy.h" |
| 38 | #include "trace-ir-metadata-field-class-copy.h" |
| 39 | |
| 40 | /* |
| 41 | * This fonction walks througth the nested structures field class to resolve a |
| 42 | * field path object. A field path is made of indexes inside possibly nested |
| 43 | * structures ultimately leading to a field class. |
| 44 | */ |
| 45 | static |
| 46 | const bt_field_class *walk_field_path(struct trace_ir_metadata_maps *md_maps, |
| 47 | const bt_field_path *fp, const bt_field_class *fc) |
| 48 | { |
| 49 | uint64_t i, fp_item_count; |
| 50 | const bt_field_class *curr_fc; |
| 51 | |
| 52 | BT_ASSERT(bt_field_class_get_type(fc) == BT_FIELD_CLASS_TYPE_STRUCTURE); |
| 53 | BT_COMP_LOGD("Walking field path on field class: fp-addr=%p, fc-addr=%p", |
| 54 | fp, fc); |
| 55 | |
| 56 | fp_item_count = bt_field_path_get_item_count(fp); |
| 57 | curr_fc = fc; |
| 58 | for (i = 0; i < fp_item_count; i++) { |
| 59 | bt_field_class_type fc_type = bt_field_class_get_type(curr_fc); |
| 60 | const bt_field_path_item *fp_item = |
| 61 | bt_field_path_borrow_item_by_index_const(fp, i); |
| 62 | |
| 63 | switch (fc_type) { |
| 64 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 65 | { |
| 66 | const bt_field_class_structure_member *member; |
| 67 | |
| 68 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 69 | BT_FIELD_PATH_ITEM_TYPE_INDEX); |
| 70 | member = bt_field_class_structure_borrow_member_by_index_const( |
| 71 | curr_fc, |
| 72 | bt_field_path_item_index_get_index(fp_item)); |
| 73 | curr_fc = bt_field_class_structure_member_borrow_field_class_const( |
| 74 | member); |
| 75 | break; |
| 76 | } |
| 77 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
| 78 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: |
| 79 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: |
| 80 | { |
| 81 | const bt_field_class_variant_option *option; |
| 82 | |
| 83 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 84 | BT_FIELD_PATH_ITEM_TYPE_INDEX); |
| 85 | option = bt_field_class_variant_borrow_option_by_index_const( |
| 86 | curr_fc, |
| 87 | bt_field_path_item_index_get_index(fp_item)); |
| 88 | curr_fc = bt_field_class_variant_option_borrow_field_class_const( |
| 89 | option); |
| 90 | break; |
| 91 | } |
| 92 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 93 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 94 | { |
| 95 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
| 96 | BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT); |
| 97 | curr_fc = bt_field_class_array_borrow_element_field_class_const( |
| 98 | curr_fc); |
| 99 | break; |
| 100 | } |
| 101 | default: |
| 102 | abort(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return curr_fc; |
| 107 | } |
| 108 | |
| 109 | static |
| 110 | const bt_field_class *resolve_field_path_to_field_class(const bt_field_path *fp, |
| 111 | struct trace_ir_metadata_maps *md_maps) |
| 112 | { |
| 113 | struct field_class_resolving_context *fc_resolving_ctx; |
| 114 | const bt_field_class *fc; |
| 115 | bt_field_path_scope fp_scope; |
| 116 | |
| 117 | BT_COMP_LOGD("Resolving field path: fp-addr=%p", fp); |
| 118 | |
| 119 | fc_resolving_ctx = md_maps->fc_resolving_ctx; |
| 120 | fp_scope = bt_field_path_get_root_scope(fp); |
| 121 | |
| 122 | switch (fp_scope) { |
| 123 | case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT: |
| 124 | fc = walk_field_path(md_maps, fp, |
| 125 | fc_resolving_ctx->packet_context); |
| 126 | break; |
| 127 | case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT: |
| 128 | fc = walk_field_path(md_maps, fp, |
| 129 | fc_resolving_ctx->event_common_context); |
| 130 | break; |
| 131 | case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT: |
| 132 | fc = walk_field_path(md_maps, fp, |
| 133 | fc_resolving_ctx->event_specific_context); |
| 134 | break; |
| 135 | case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD: |
| 136 | fc = walk_field_path(md_maps, fp, |
| 137 | fc_resolving_ctx->event_payload); |
| 138 | break; |
| 139 | default: |
| 140 | abort(); |
| 141 | } |
| 142 | |
| 143 | return fc; |
| 144 | } |
| 145 | |
| 146 | static inline |
| 147 | void field_class_integer_set_props(const bt_field_class *input_fc, |
| 148 | bt_field_class *output_fc) |
| 149 | { |
| 150 | bt_field_class_integer_set_preferred_display_base(output_fc, |
| 151 | bt_field_class_integer_get_preferred_display_base(input_fc)); |
| 152 | bt_field_class_integer_set_field_value_range(output_fc, |
| 153 | bt_field_class_integer_get_field_value_range(input_fc)); |
| 154 | } |
| 155 | |
| 156 | static inline |
| 157 | int field_class_unsigned_integer_copy( |
| 158 | struct trace_ir_metadata_maps *md_maps, |
| 159 | const bt_field_class *in_field_class, |
| 160 | bt_field_class *out_field_class) |
| 161 | { |
| 162 | BT_COMP_LOGD("Copying content of unsigned integer field class: " |
| 163 | "in-fc-addr=%p, out-fc-addr=%p", |
| 164 | in_field_class, out_field_class); |
| 165 | |
| 166 | field_class_integer_set_props(in_field_class, out_field_class); |
| 167 | |
| 168 | BT_COMP_LOGD("Copied content of unsigned integer field class: " |
| 169 | "in-fc-addr=%p, out-fc-addr=%p", |
| 170 | in_field_class, out_field_class); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static inline |
| 175 | int field_class_signed_integer_copy( |
| 176 | struct trace_ir_metadata_maps *md_maps, |
| 177 | const bt_field_class *in_field_class, |
| 178 | bt_field_class *out_field_class) |
| 179 | { |
| 180 | BT_COMP_LOGD("Copying content of signed integer field class: " |
| 181 | "in-fc-addr=%p, out-fc-addr=%p", |
| 182 | in_field_class, out_field_class); |
| 183 | |
| 184 | field_class_integer_set_props(in_field_class, out_field_class); |
| 185 | |
| 186 | BT_COMP_LOGD("Copied content of signed integer field class: " |
| 187 | "in-fc-addr=%p, out-fc-addr=%p", |
| 188 | in_field_class, out_field_class); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | BT_HIDDEN |
| 193 | int field_class_unsigned_enumeration_copy( |
| 194 | struct trace_ir_metadata_maps *md_maps, |
| 195 | const bt_field_class *in_field_class, |
| 196 | bt_field_class *out_field_class) |
| 197 | { |
| 198 | uint64_t i, enum_mapping_count; |
| 199 | int ret = 0; |
| 200 | |
| 201 | BT_COMP_LOGD("Copying content of unsigned enumeration field class: " |
| 202 | "in-fc-addr=%p, out-fc-addr=%p", |
| 203 | in_field_class, out_field_class); |
| 204 | |
| 205 | /* Copy properties of the inner integer. */ |
| 206 | field_class_integer_set_props(in_field_class, out_field_class); |
| 207 | |
| 208 | /* Copy all enumeration entries. */ |
| 209 | enum_mapping_count = bt_field_class_enumeration_get_mapping_count(in_field_class); |
| 210 | for (i = 0; i < enum_mapping_count; i++) { |
| 211 | const char *label; |
| 212 | const bt_integer_range_set_unsigned *range_set; |
| 213 | const bt_field_class_enumeration_unsigned_mapping *u_mapping; |
| 214 | const bt_field_class_enumeration_mapping *mapping; |
| 215 | |
| 216 | u_mapping = bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const( |
| 217 | in_field_class, i); |
| 218 | mapping = bt_field_class_enumeration_unsigned_mapping_as_mapping_const( |
| 219 | u_mapping); |
| 220 | label = bt_field_class_enumeration_mapping_get_label(mapping); |
| 221 | range_set = bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const( |
| 222 | u_mapping); |
| 223 | ret = bt_field_class_enumeration_unsigned_add_mapping( |
| 224 | out_field_class, label, range_set); |
| 225 | if (ret) { |
| 226 | goto error; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | BT_COMP_LOGD("Copied content of unsigned enumeration field class: " |
| 231 | "in-fc-addr=%p, out-fc-addr=%p", |
| 232 | in_field_class, out_field_class); |
| 233 | |
| 234 | error: |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | static inline |
| 239 | int field_class_signed_enumeration_copy( |
| 240 | struct trace_ir_metadata_maps *md_maps, |
| 241 | const bt_field_class *in_field_class, |
| 242 | bt_field_class *out_field_class) |
| 243 | { |
| 244 | uint64_t i, enum_mapping_count; |
| 245 | int ret = 0; |
| 246 | |
| 247 | BT_COMP_LOGD("Copying content of signed enumeration field class: " |
| 248 | "in-fc-addr=%p, out-fc-addr=%p", |
| 249 | in_field_class, out_field_class); |
| 250 | |
| 251 | /* Copy properties of the inner integer. */ |
| 252 | field_class_integer_set_props(in_field_class, out_field_class); |
| 253 | |
| 254 | /* Copy all enumeration entries. */ |
| 255 | enum_mapping_count = |
| 256 | bt_field_class_enumeration_get_mapping_count(in_field_class); |
| 257 | for (i = 0; i < enum_mapping_count; i++) { |
| 258 | const char *label; |
| 259 | const bt_integer_range_set_signed *range_set; |
| 260 | const bt_field_class_enumeration_signed_mapping *s_mapping; |
| 261 | const bt_field_class_enumeration_mapping *mapping; |
| 262 | |
| 263 | s_mapping = bt_field_class_enumeration_signed_borrow_mapping_by_index_const( |
| 264 | in_field_class, i); |
| 265 | mapping = bt_field_class_enumeration_signed_mapping_as_mapping_const( |
| 266 | s_mapping); |
| 267 | label = bt_field_class_enumeration_mapping_get_label(mapping); |
| 268 | range_set = bt_field_class_enumeration_signed_mapping_borrow_ranges_const( |
| 269 | s_mapping); |
| 270 | ret = bt_field_class_enumeration_signed_add_mapping( |
| 271 | out_field_class, label, range_set); |
| 272 | if (ret) { |
| 273 | goto error; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | BT_COMP_LOGD("Copied content of signed enumeration field class: " |
| 278 | "in-fc-addr=%p, out-fc-addr=%p", |
| 279 | in_field_class, out_field_class); |
| 280 | |
| 281 | error: |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | static inline |
| 286 | int field_class_real_copy( |
| 287 | struct trace_ir_metadata_maps *md_maps, |
| 288 | const bt_field_class *in_field_class, |
| 289 | bt_field_class *out_field_class) |
| 290 | { |
| 291 | BT_COMP_LOGD("Copying content of real field class: " |
| 292 | "in-fc-addr=%p, out-fc-addr=%p", |
| 293 | in_field_class, out_field_class); |
| 294 | |
| 295 | bt_field_class_real_set_is_single_precision(out_field_class, |
| 296 | bt_field_class_real_is_single_precision(in_field_class)); |
| 297 | |
| 298 | BT_COMP_LOGD("Copied content real field class: in-fc-addr=%p, " |
| 299 | "out-fc-addr=%p", in_field_class, out_field_class); |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static inline |
| 305 | int field_class_structure_copy( |
| 306 | struct trace_ir_metadata_maps *md_maps, |
| 307 | const bt_field_class *in_field_class, |
| 308 | bt_field_class *out_field_class) |
| 309 | { |
| 310 | uint64_t i, struct_member_count; |
| 311 | int ret = 0; |
| 312 | |
| 313 | BT_COMP_LOGD("Copying content of structure field class: " |
| 314 | "in-fc-addr=%p, out-fc-addr=%p", |
| 315 | in_field_class, out_field_class); |
| 316 | /* Get the number of member in that struct. */ |
| 317 | struct_member_count = |
| 318 | bt_field_class_structure_get_member_count(in_field_class); |
| 319 | |
| 320 | /* Iterate over all the members of the struct. */ |
| 321 | for (i = 0; i < struct_member_count; i++) { |
| 322 | const bt_field_class_structure_member *member; |
| 323 | const char *member_name; |
| 324 | const bt_field_class *member_fc; |
| 325 | bt_field_class *out_member_field_class; |
| 326 | |
| 327 | member = bt_field_class_structure_borrow_member_by_index_const( |
| 328 | in_field_class, i); |
| 329 | member_fc = bt_field_class_structure_member_borrow_field_class_const( |
| 330 | member); |
| 331 | member_name = bt_field_class_structure_member_get_name(member); |
| 332 | BT_COMP_LOGD("Copying structure field class's field: " |
| 333 | "index=%" PRId64 ", " |
| 334 | "member-fc-addr=%p, field-name=\"%s\"", |
| 335 | i, member_fc, member_name); |
| 336 | |
| 337 | out_member_field_class = create_field_class_copy(md_maps, |
| 338 | member_fc); |
| 339 | if (!out_member_field_class) { |
| 340 | BT_COMP_LOGE("Cannot copy structure field class's field: " |
| 341 | "index=%" PRId64 ", " |
| 342 | "field-fc-addr=%p, field-name=\"%s\"", |
| 343 | i, member_fc, member_name); |
| 344 | ret = -1; |
| 345 | goto error; |
| 346 | } |
| 347 | ret = copy_field_class_content(md_maps, member_fc, |
| 348 | out_member_field_class); |
| 349 | if (ret) { |
| 350 | goto error; |
| 351 | } |
| 352 | |
| 353 | if (bt_field_class_structure_append_member(out_field_class, |
| 354 | member_name, out_member_field_class) != |
| 355 | BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK) { |
| 356 | BT_COMP_LOGE("Cannot append structure field class's field: " |
| 357 | "index=%" PRId64 ", " |
| 358 | "field-fc-addr=%p, field-name=\"%s\"", |
| 359 | i, member_fc, member_name); |
| 360 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_member_field_class); |
| 361 | ret = -1; |
| 362 | goto error; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | BT_COMP_LOGD("Copied structure field class: original-fc-addr=%p, copy-fc-addr=%p", |
| 367 | in_field_class, out_field_class); |
| 368 | |
| 369 | error: |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | static inline |
| 374 | int field_class_variant_copy( |
| 375 | struct trace_ir_metadata_maps *md_maps, |
| 376 | const bt_field_class *in_field_class, |
| 377 | bt_field_class *out_field_class) |
| 378 | { |
| 379 | bt_field_class *out_tag_field_class = NULL; |
| 380 | uint64_t i, variant_option_count; |
| 381 | bt_field_class_type fc_type = bt_field_class_get_type(in_field_class); |
| 382 | int ret = 0; |
| 383 | |
| 384 | BT_COMP_LOGD("Copying content of variant field class: " |
| 385 | "in-fc-addr=%p, out-fc-addr=%p", |
| 386 | in_field_class, out_field_class); |
| 387 | variant_option_count = |
| 388 | bt_field_class_variant_get_option_count(in_field_class); |
| 389 | for (i = 0; i < variant_option_count; i++) { |
| 390 | const bt_field_class *option_fc; |
| 391 | const char *option_name; |
| 392 | bt_field_class *out_option_field_class; |
| 393 | const bt_field_class_variant_option *option; |
| 394 | |
| 395 | option = bt_field_class_variant_borrow_option_by_index_const( |
| 396 | in_field_class, i); |
| 397 | option_fc = bt_field_class_variant_option_borrow_field_class_const( |
| 398 | option); |
| 399 | option_name = bt_field_class_variant_option_get_name(option); |
| 400 | out_option_field_class = create_field_class_copy_internal( |
| 401 | md_maps, option_fc); |
| 402 | if (!out_option_field_class) { |
| 403 | BT_COMP_LOGE_STR("Cannot copy field class."); |
| 404 | ret = -1; |
| 405 | goto error; |
| 406 | } |
| 407 | ret = copy_field_class_content_internal(md_maps, option_fc, |
| 408 | out_option_field_class); |
| 409 | if (ret) { |
| 410 | BT_COMP_LOGE_STR("Error copying content of option variant " |
| 411 | "field class'"); |
| 412 | goto error; |
| 413 | } |
| 414 | |
| 415 | if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR) { |
| 416 | const bt_field_class_variant_with_selector_unsigned_option *spec_opt = |
| 417 | bt_field_class_variant_with_selector_unsigned_borrow_option_by_index_const( |
| 418 | in_field_class, i); |
| 419 | const bt_integer_range_set_unsigned *ranges = |
| 420 | bt_field_class_variant_with_selector_unsigned_option_borrow_ranges_const( |
| 421 | spec_opt); |
| 422 | |
| 423 | if (bt_field_class_variant_with_selector_unsigned_append_option( |
| 424 | out_field_class, option_name, |
| 425 | out_option_field_class, ranges) != |
| 426 | BT_FIELD_CLASS_VARIANT_WITH_SELECTOR_APPEND_OPTION_STATUS_OK) { |
| 427 | BT_COMP_LOGE_STR("Cannot append option to variant field class with unsigned selector'"); |
| 428 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_tag_field_class); |
| 429 | ret = -1; |
| 430 | goto error; |
| 431 | } |
| 432 | } else if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR) { |
| 433 | const bt_field_class_variant_with_selector_signed_option *spec_opt = |
| 434 | bt_field_class_variant_with_selector_signed_borrow_option_by_index_const( |
| 435 | in_field_class, i); |
| 436 | const bt_integer_range_set_signed *ranges = |
| 437 | bt_field_class_variant_with_selector_signed_option_borrow_ranges_const( |
| 438 | spec_opt); |
| 439 | |
| 440 | if (bt_field_class_variant_with_selector_signed_append_option( |
| 441 | out_field_class, option_name, |
| 442 | out_option_field_class, ranges) != |
| 443 | BT_FIELD_CLASS_VARIANT_WITH_SELECTOR_APPEND_OPTION_STATUS_OK) { |
| 444 | BT_COMP_LOGE_STR("Cannot append option to variant field class with signed selector'"); |
| 445 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_tag_field_class); |
| 446 | ret = -1; |
| 447 | goto error; |
| 448 | } |
| 449 | } else { |
| 450 | BT_ASSERT(fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR); |
| 451 | |
| 452 | if (bt_field_class_variant_without_selector_append_option( |
| 453 | out_field_class, option_name, |
| 454 | out_option_field_class) != |
| 455 | BT_FIELD_CLASS_VARIANT_WITHOUT_SELECTOR_APPEND_OPTION_STATUS_OK) { |
| 456 | BT_COMP_LOGE_STR("Cannot append option to variant field class'"); |
| 457 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_tag_field_class); |
| 458 | ret = -1; |
| 459 | goto error; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | BT_COMP_LOGD("Copied content of variant field class: in-fc-addr=%p, " |
| 465 | "out-fc-addr=%p", in_field_class, out_field_class); |
| 466 | |
| 467 | error: |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | static inline |
| 472 | int field_class_static_array_copy( |
| 473 | struct trace_ir_metadata_maps *md_maps, |
| 474 | const bt_field_class *in_field_class, |
| 475 | bt_field_class *out_field_class) |
| 476 | { |
| 477 | BT_COMP_LOGD("Copying content of static array field class: in-fc-addr=%p, " |
| 478 | "out-fc-addr=%p", in_field_class, out_field_class); |
| 479 | /* |
| 480 | * There is no content to copy. Keep this function call anyway for |
| 481 | * logging purposes. |
| 482 | */ |
| 483 | BT_COMP_LOGD("Copied content of static array field class: in-fc-addr=%p, " |
| 484 | "out-fc-addr=%p", in_field_class, out_field_class); |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static inline |
| 490 | int field_class_dynamic_array_copy( |
| 491 | struct trace_ir_metadata_maps *md_maps, |
| 492 | const bt_field_class *in_field_class, |
| 493 | bt_field_class *out_field_class) |
| 494 | { |
| 495 | BT_COMP_LOGD("Copying content of dynamic array field class: " |
| 496 | "in-fc-addr=%p, out-fc-addr=%p", |
| 497 | in_field_class, out_field_class); |
| 498 | |
| 499 | /* |
| 500 | * There is no content to copy. Keep this function call anyway for |
| 501 | * logging purposes. |
| 502 | */ |
| 503 | BT_COMP_LOGD("Copied dynamic array field class: in-fc-addr=%p, " |
| 504 | "out-fc-addr=%p", in_field_class, out_field_class); |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static inline |
| 510 | int field_class_string_copy(struct trace_ir_metadata_maps *md_maps, |
| 511 | const bt_field_class *in_field_class, |
| 512 | bt_field_class *out_field_class) |
| 513 | { |
| 514 | BT_COMP_LOGD("Copying content of string field class: in-fc-addr=%p, " |
| 515 | "out-fc-addr=%p", in_field_class, out_field_class); |
| 516 | /* |
| 517 | * There is no content to copy. Keep this function call anyway for |
| 518 | * logging purposes. |
| 519 | */ |
| 520 | BT_COMP_LOGD("Copied content of string field class: in-fc-addr=%p, " |
| 521 | "out-fc-addr=%p", in_field_class, out_field_class); |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static |
| 527 | bt_field_class *copy_field_class_array_element(struct trace_ir_metadata_maps *md_maps, |
| 528 | const bt_field_class *in_elem_fc) |
| 529 | { |
| 530 | int ret; |
| 531 | bt_field_class *out_elem_fc = |
| 532 | create_field_class_copy_internal(md_maps, in_elem_fc); |
| 533 | if (!out_elem_fc) { |
| 534 | BT_COMP_LOGE("Error creating output elem field class " |
| 535 | "from input elem field class for static array: " |
| 536 | "in-fc-addr=%p", in_elem_fc); |
| 537 | goto error; |
| 538 | } |
| 539 | |
| 540 | ret = copy_field_class_content_internal(md_maps, in_elem_fc, out_elem_fc); |
| 541 | if (ret) { |
| 542 | BT_COMP_LOGE("Error creating output elem field class " |
| 543 | "from input elem field class for static array: " |
| 544 | "in-fc-addr=%p", in_elem_fc); |
| 545 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_elem_fc); |
| 546 | goto error; |
| 547 | } |
| 548 | |
| 549 | error: |
| 550 | return out_elem_fc; |
| 551 | } |
| 552 | |
| 553 | BT_HIDDEN |
| 554 | bt_field_class *create_field_class_copy_internal(struct trace_ir_metadata_maps *md_maps, |
| 555 | const bt_field_class *in_field_class) |
| 556 | { |
| 557 | bt_field_class *out_field_class = NULL; |
| 558 | bt_field_class_type fc_type = bt_field_class_get_type(in_field_class); |
| 559 | |
| 560 | BT_COMP_LOGD("Creating bare field class based on field class: in-fc-addr=%p", |
| 561 | in_field_class); |
| 562 | |
| 563 | switch (fc_type) { |
| 564 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: |
| 565 | out_field_class = bt_field_class_integer_unsigned_create( |
| 566 | md_maps->output_trace_class); |
| 567 | break; |
| 568 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: |
| 569 | out_field_class = bt_field_class_integer_signed_create( |
| 570 | md_maps->output_trace_class); |
| 571 | break; |
| 572 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: |
| 573 | out_field_class = bt_field_class_enumeration_unsigned_create( |
| 574 | md_maps->output_trace_class); |
| 575 | break; |
| 576 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: |
| 577 | out_field_class = bt_field_class_enumeration_signed_create( |
| 578 | md_maps->output_trace_class); |
| 579 | break; |
| 580 | case BT_FIELD_CLASS_TYPE_REAL: |
| 581 | out_field_class = bt_field_class_real_create( |
| 582 | md_maps->output_trace_class); |
| 583 | break; |
| 584 | case BT_FIELD_CLASS_TYPE_STRING: |
| 585 | out_field_class = bt_field_class_string_create( |
| 586 | md_maps->output_trace_class); |
| 587 | break; |
| 588 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 589 | out_field_class = bt_field_class_structure_create( |
| 590 | md_maps->output_trace_class); |
| 591 | break; |
| 592 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 593 | { |
| 594 | const bt_field_class *in_elem_fc = |
| 595 | bt_field_class_array_borrow_element_field_class_const( |
| 596 | in_field_class); |
| 597 | uint64_t array_len = |
| 598 | bt_field_class_array_static_get_length(in_field_class); |
| 599 | |
| 600 | bt_field_class *out_elem_fc = copy_field_class_array_element( |
| 601 | md_maps, in_elem_fc); |
| 602 | if (!out_elem_fc) { |
| 603 | out_field_class = NULL; |
| 604 | goto error; |
| 605 | } |
| 606 | |
| 607 | out_field_class = bt_field_class_array_static_create( |
| 608 | md_maps->output_trace_class, |
| 609 | out_elem_fc, array_len); |
| 610 | break; |
| 611 | } |
| 612 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 613 | { |
| 614 | const bt_field_class *in_elem_fc = |
| 615 | bt_field_class_array_borrow_element_field_class_const( |
| 616 | in_field_class); |
| 617 | const bt_field_path *length_fp = |
| 618 | bt_field_class_array_dynamic_borrow_length_field_path_const( |
| 619 | in_field_class); |
| 620 | bt_field_class *out_length_fc = NULL; |
| 621 | |
| 622 | bt_field_class *out_elem_fc = copy_field_class_array_element( |
| 623 | md_maps, in_elem_fc); |
| 624 | if (!out_elem_fc) { |
| 625 | out_field_class = NULL; |
| 626 | goto error; |
| 627 | } |
| 628 | |
| 629 | if (length_fp) { |
| 630 | const bt_field_class *in_length_fc = |
| 631 | resolve_field_path_to_field_class(length_fp, |
| 632 | md_maps); |
| 633 | |
| 634 | BT_ASSERT(in_length_fc); |
| 635 | out_length_fc = g_hash_table_lookup(md_maps->field_class_map, |
| 636 | in_length_fc); |
| 637 | BT_ASSERT(out_length_fc); |
| 638 | } |
| 639 | |
| 640 | out_field_class = bt_field_class_array_dynamic_create( |
| 641 | md_maps->output_trace_class, |
| 642 | out_elem_fc, out_length_fc); |
| 643 | break; |
| 644 | } |
| 645 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
| 646 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: |
| 647 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: |
| 648 | { |
| 649 | bt_field_class *out_sel_fc = NULL; |
| 650 | |
| 651 | if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR || |
| 652 | fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR) { |
| 653 | const bt_field_class *in_sel_fc; |
| 654 | const bt_field_path *sel_fp = |
| 655 | bt_field_class_variant_with_selector_borrow_selector_field_path_const( |
| 656 | in_field_class); |
| 657 | |
| 658 | BT_ASSERT(sel_fp); |
| 659 | in_sel_fc = resolve_field_path_to_field_class(sel_fp, |
| 660 | md_maps); |
| 661 | BT_ASSERT(in_sel_fc); |
| 662 | out_sel_fc = g_hash_table_lookup( |
| 663 | md_maps->field_class_map, in_sel_fc); |
| 664 | BT_ASSERT(out_sel_fc); |
| 665 | } |
| 666 | |
| 667 | out_field_class = bt_field_class_variant_create( |
| 668 | md_maps->output_trace_class, out_sel_fc); |
| 669 | break; |
| 670 | } |
| 671 | default: |
| 672 | abort(); |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * Add mapping from in_field_class to out_field_class. This simplifies |
| 677 | * the resolution of field paths in variant and dynamic array field |
| 678 | * classes. |
| 679 | */ |
| 680 | g_hash_table_insert(md_maps->field_class_map, |
| 681 | (gpointer) in_field_class, out_field_class); |
| 682 | |
| 683 | error: |
| 684 | if(out_field_class){ |
| 685 | BT_COMP_LOGD("Created bare field class based on field class: in-fc-addr=%p, " |
| 686 | "out-fc-addr=%p", in_field_class, out_field_class); |
| 687 | } else { |
| 688 | BT_COMP_LOGE("Error creating output field class from input field " |
| 689 | "class: in-fc-addr=%p", in_field_class); |
| 690 | } |
| 691 | |
| 692 | return out_field_class; |
| 693 | } |
| 694 | |
| 695 | BT_HIDDEN |
| 696 | int copy_field_class_content_internal( |
| 697 | struct trace_ir_metadata_maps *md_maps, |
| 698 | const bt_field_class *in_field_class, |
| 699 | bt_field_class *out_field_class) |
| 700 | { |
| 701 | int ret = 0; |
| 702 | switch(bt_field_class_get_type(in_field_class)) { |
| 703 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: |
| 704 | ret = field_class_unsigned_integer_copy(md_maps, |
| 705 | in_field_class, out_field_class); |
| 706 | break; |
| 707 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: |
| 708 | ret = field_class_signed_integer_copy(md_maps, |
| 709 | in_field_class, out_field_class); |
| 710 | break; |
| 711 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: |
| 712 | ret = field_class_unsigned_enumeration_copy(md_maps, |
| 713 | in_field_class, out_field_class); |
| 714 | break; |
| 715 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: |
| 716 | ret = field_class_signed_enumeration_copy(md_maps, |
| 717 | in_field_class, out_field_class); |
| 718 | break; |
| 719 | case BT_FIELD_CLASS_TYPE_REAL: |
| 720 | ret = field_class_real_copy(md_maps, |
| 721 | in_field_class, out_field_class); |
| 722 | break; |
| 723 | case BT_FIELD_CLASS_TYPE_STRING: |
| 724 | ret = field_class_string_copy(md_maps, |
| 725 | in_field_class, out_field_class); |
| 726 | break; |
| 727 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 728 | ret = field_class_structure_copy(md_maps, |
| 729 | in_field_class, out_field_class); |
| 730 | break; |
| 731 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 732 | ret = field_class_static_array_copy(md_maps, |
| 733 | in_field_class, out_field_class); |
| 734 | break; |
| 735 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 736 | ret = field_class_dynamic_array_copy(md_maps, |
| 737 | in_field_class, out_field_class); |
| 738 | break; |
| 739 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
| 740 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: |
| 741 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: |
| 742 | ret = field_class_variant_copy(md_maps, |
| 743 | in_field_class, out_field_class); |
| 744 | break; |
| 745 | default: |
| 746 | abort(); |
| 747 | } |
| 748 | |
| 749 | return ret; |
| 750 | } |