| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (c) 2015-2019 EfficiOS Inc. and Linux Foundation |
| 5 | * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 6 | * |
| 7 | * Babeltrace - Trace IR data object copy |
| 8 | */ |
| 9 | |
| 10 | #define BT_COMP_LOG_SELF_COMP self_comp |
| 11 | #define BT_LOG_OUTPUT_LEVEL log_level |
| 12 | #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-DATA-COPY" |
| 13 | #include "logging/comp-logging.h" |
| 14 | |
| 15 | #include <inttypes.h> |
| 16 | #include <stdint.h> |
| 17 | |
| 18 | #include "common/assert.h" |
| 19 | #include "common/common.h" |
| 20 | |
| 21 | #include "trace-ir-data-copy.h" |
| 22 | |
| 23 | BT_HIDDEN |
| 24 | enum debug_info_trace_ir_mapping_status copy_trace_content( |
| 25 | const bt_trace *in_trace, bt_trace *out_trace, |
| 26 | bt_logging_level log_level, bt_self_component *self_comp) |
| 27 | { |
| 28 | enum debug_info_trace_ir_mapping_status status; |
| 29 | const char *trace_name; |
| 30 | uint64_t i, env_field_count; |
| 31 | |
| 32 | BT_COMP_LOGD("Copying content of trace: in-t-addr=%p, out-t-addr=%p", |
| 33 | in_trace, out_trace); |
| 34 | |
| 35 | trace_name = bt_trace_get_name(in_trace); |
| 36 | |
| 37 | /* Copy the trace name. */ |
| 38 | if (trace_name) { |
| 39 | bt_trace_set_name_status set_name_status = |
| 40 | bt_trace_set_name(out_trace, trace_name); |
| 41 | if (set_name_status != BT_TRACE_SET_NAME_STATUS_OK) { |
| 42 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot set trace's name: " |
| 43 | "out-t-addr=%p, name=\"%s\"", out_trace, |
| 44 | trace_name); |
| 45 | status = (int) set_name_status; |
| 46 | goto end; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Safe to use the same value object because it's frozen at this |
| 52 | * point. |
| 53 | */ |
| 54 | bt_trace_set_user_attributes(out_trace, |
| 55 | bt_trace_borrow_user_attributes_const(in_trace)); |
| 56 | |
| 57 | /* |
| 58 | * Do not copy the trace UUID as the trace may be modified and should |
| 59 | * no longer have the same UUID. |
| 60 | */ |
| 61 | |
| 62 | /* |
| 63 | * Go over all the entries in the environment section of the |
| 64 | * trace and copy the content to the new trace. |
| 65 | */ |
| 66 | env_field_count = bt_trace_get_environment_entry_count(in_trace); |
| 67 | for (i = 0; i < env_field_count; i++) { |
| 68 | const char *value_name; |
| 69 | const bt_value *value = NULL; |
| 70 | bt_trace_set_environment_entry_status set_env_status; |
| 71 | |
| 72 | bt_trace_borrow_environment_entry_by_index_const( |
| 73 | in_trace, i, &value_name, &value); |
| 74 | |
| 75 | BT_COMP_LOGD("Copying trace environnement entry: " |
| 76 | "index=%" PRId64 ", value-addr=%p, value-name=\"%s\"", |
| 77 | i, value, value_name); |
| 78 | |
| 79 | BT_ASSERT(value_name); |
| 80 | BT_ASSERT(value); |
| 81 | |
| 82 | if (bt_value_is_signed_integer(value)) { |
| 83 | set_env_status = bt_trace_set_environment_entry_integer( |
| 84 | out_trace, value_name, |
| 85 | bt_value_integer_signed_get( value)); |
| 86 | } else if (bt_value_is_string(value)) { |
| 87 | set_env_status = bt_trace_set_environment_entry_string( |
| 88 | out_trace, value_name, |
| 89 | bt_value_string_get(value)); |
| 90 | } else { |
| 91 | bt_common_abort(); |
| 92 | } |
| 93 | |
| 94 | if (set_env_status != BT_TRACE_SET_ENVIRONMENT_ENTRY_STATUS_OK) { |
| 95 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot copy trace's environment: " |
| 96 | "out-t-addr=%p, name=\"%s\"", |
| 97 | out_trace, trace_name); |
| 98 | status = (int) set_env_status; |
| 99 | goto end; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | BT_COMP_LOGD("Copied content of trace: in-t-addr=%p, out-t-addr=%p", |
| 104 | in_trace, out_trace); |
| 105 | |
| 106 | status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK; |
| 107 | end: |
| 108 | return status; |
| 109 | } |
| 110 | |
| 111 | BT_HIDDEN |
| 112 | enum debug_info_trace_ir_mapping_status copy_stream_content( |
| 113 | const bt_stream *in_stream, bt_stream *out_stream, |
| 114 | bt_logging_level log_level, bt_self_component *self_comp) |
| 115 | { |
| 116 | enum debug_info_trace_ir_mapping_status status; |
| 117 | const char *stream_name; |
| 118 | |
| 119 | BT_COMP_LOGD("Copying content of stream: in-s-addr=%p, out-s-addr=%p", |
| 120 | in_stream, out_stream); |
| 121 | |
| 122 | stream_name = bt_stream_get_name(in_stream); |
| 123 | |
| 124 | if (stream_name) { |
| 125 | bt_stream_set_name_status set_name_status = |
| 126 | bt_stream_set_name(out_stream, stream_name); |
| 127 | if (set_name_status != BT_STREAM_SET_NAME_STATUS_OK) { |
| 128 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot set stream's name: " |
| 129 | "stream-addr=%p, name=\"%s\"", out_stream, |
| 130 | stream_name); |
| 131 | status = (int) set_name_status; |
| 132 | goto end; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Safe to use the same value object because it's frozen at this |
| 138 | * point. |
| 139 | */ |
| 140 | bt_stream_set_user_attributes(out_stream, |
| 141 | bt_stream_borrow_user_attributes_const(in_stream)); |
| 142 | |
| 143 | BT_COMP_LOGD("Copied content of stream: in-s-addr=%p, out-s-addr=%p", |
| 144 | in_stream, out_stream); |
| 145 | status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK; |
| 146 | end: |
| 147 | return status; |
| 148 | } |
| 149 | |
| 150 | BT_HIDDEN |
| 151 | enum debug_info_trace_ir_mapping_status copy_packet_content( |
| 152 | const bt_packet *in_packet, bt_packet *out_packet, |
| 153 | bt_logging_level log_level, bt_self_component *self_comp) |
| 154 | { |
| 155 | enum debug_info_trace_ir_mapping_status status; |
| 156 | const bt_field *in_context_field; |
| 157 | bt_field *out_context_field; |
| 158 | |
| 159 | BT_COMP_LOGD("Copying content of packet: in-p-addr=%p, out-p-addr=%p", |
| 160 | in_packet, out_packet); |
| 161 | |
| 162 | /* Copy context field. */ |
| 163 | in_context_field = bt_packet_borrow_context_field_const(in_packet); |
| 164 | if (in_context_field) { |
| 165 | out_context_field = bt_packet_borrow_context_field(out_packet); |
| 166 | BT_ASSERT(out_context_field); |
| 167 | status = copy_field_content(in_context_field, out_context_field, |
| 168 | log_level, self_comp); |
| 169 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 170 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot copy context field: " |
| 171 | "in-ctx-f-addr=%p, out-ctx-f-addr=%p", |
| 172 | in_context_field, out_context_field); |
| 173 | goto end; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | BT_COMP_LOGD("Copied content of packet: in-p-addr=%p, out-p-addr=%p", |
| 178 | in_packet, out_packet); |
| 179 | status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK; |
| 180 | end: |
| 181 | return status; |
| 182 | } |
| 183 | |
| 184 | BT_HIDDEN |
| 185 | enum debug_info_trace_ir_mapping_status copy_event_content( |
| 186 | const bt_event *in_event, bt_event *out_event, |
| 187 | bt_logging_level log_level, bt_self_component *self_comp) |
| 188 | { |
| 189 | enum debug_info_trace_ir_mapping_status status; |
| 190 | const bt_field *in_common_ctx_field, *in_specific_ctx_field, |
| 191 | *in_payload_field; |
| 192 | bt_field *out_common_ctx_field, *out_specific_ctx_field, |
| 193 | *out_payload_field; |
| 194 | |
| 195 | BT_COMP_LOGD("Copying content of event: in-e-addr=%p, out-e-addr=%p", |
| 196 | in_event, out_event); |
| 197 | in_common_ctx_field = |
| 198 | bt_event_borrow_common_context_field_const(in_event); |
| 199 | if (in_common_ctx_field) { |
| 200 | out_common_ctx_field = |
| 201 | bt_event_borrow_common_context_field(out_event); |
| 202 | BT_ASSERT_DBG(out_common_ctx_field); |
| 203 | |
| 204 | status = copy_field_content(in_common_ctx_field, |
| 205 | out_common_ctx_field, log_level, self_comp); |
| 206 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 207 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot copy common context field: " |
| 208 | "in-comm-ctx-f-addr=%p, out-comm-ctx-f-addr=%p", |
| 209 | in_common_ctx_field, out_common_ctx_field); |
| 210 | goto end; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | in_specific_ctx_field = |
| 215 | bt_event_borrow_specific_context_field_const(in_event); |
| 216 | if (in_specific_ctx_field) { |
| 217 | out_specific_ctx_field = |
| 218 | bt_event_borrow_specific_context_field(out_event); |
| 219 | BT_ASSERT_DBG(out_specific_ctx_field); |
| 220 | |
| 221 | status = copy_field_content(in_specific_ctx_field, |
| 222 | out_specific_ctx_field, log_level, self_comp); |
| 223 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 224 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot copy specific context field: " |
| 225 | "in-spec-ctx-f-addr=%p, out-spec-ctx-f-addr=%p", |
| 226 | in_specific_ctx_field, out_specific_ctx_field); |
| 227 | goto end; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | in_payload_field = bt_event_borrow_payload_field_const(in_event); |
| 232 | if (in_payload_field) { |
| 233 | out_payload_field = bt_event_borrow_payload_field(out_event); |
| 234 | BT_ASSERT_DBG(out_payload_field); |
| 235 | |
| 236 | status = copy_field_content(in_payload_field, |
| 237 | out_payload_field, log_level, self_comp); |
| 238 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 239 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot copy payloat field: " |
| 240 | "in-payload-f-addr=%p, out-payload-f-addr=%p", |
| 241 | in_payload_field, out_payload_field); |
| 242 | goto end; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | BT_COMP_LOGD("Copied content of event: in-e-addr=%p, out-e-addr=%p", |
| 247 | in_event, out_event); |
| 248 | status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK; |
| 249 | end: |
| 250 | return status; |
| 251 | } |
| 252 | |
| 253 | BT_HIDDEN |
| 254 | enum debug_info_trace_ir_mapping_status copy_field_content( |
| 255 | const bt_field *in_field, bt_field *out_field, |
| 256 | bt_logging_level log_level, bt_self_component *self_comp) |
| 257 | { |
| 258 | enum debug_info_trace_ir_mapping_status status; |
| 259 | bt_field_class_type in_fc_type, out_fc_type; |
| 260 | |
| 261 | in_fc_type = bt_field_get_class_type(in_field); |
| 262 | out_fc_type = bt_field_get_class_type(out_field); |
| 263 | BT_ASSERT_DBG(in_fc_type == out_fc_type); |
| 264 | |
| 265 | BT_COMP_LOGT("Copying content of field: in-f-addr=%p, out-f-addr=%p", |
| 266 | in_field, out_field); |
| 267 | |
| 268 | if (in_fc_type == BT_FIELD_CLASS_TYPE_BOOL) { |
| 269 | bt_field_bool_set_value(out_field, |
| 270 | bt_field_bool_get_value(in_field)); |
| 271 | } else if (in_fc_type == BT_FIELD_CLASS_TYPE_BIT_ARRAY) { |
| 272 | bt_field_bit_array_set_value_as_integer(out_field, |
| 273 | bt_field_bit_array_get_value_as_integer(in_field)); |
| 274 | } else if (bt_field_class_type_is(in_fc_type, |
| 275 | BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) { |
| 276 | bt_field_integer_unsigned_set_value(out_field, |
| 277 | bt_field_integer_unsigned_get_value(in_field)); |
| 278 | } else if (bt_field_class_type_is(in_fc_type, |
| 279 | BT_FIELD_CLASS_TYPE_SIGNED_INTEGER)) { |
| 280 | bt_field_integer_signed_set_value(out_field, |
| 281 | bt_field_integer_signed_get_value(in_field)); |
| 282 | } else if (in_fc_type == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) { |
| 283 | bt_field_real_single_precision_set_value(out_field, |
| 284 | bt_field_real_single_precision_get_value(in_field)); |
| 285 | } else if (in_fc_type == BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL) { |
| 286 | bt_field_real_double_precision_set_value(out_field, |
| 287 | bt_field_real_double_precision_get_value(in_field)); |
| 288 | } else if (in_fc_type == BT_FIELD_CLASS_TYPE_STRING) { |
| 289 | const char *str = bt_field_string_get_value(in_field); |
| 290 | bt_field_string_set_value_status set_value_status = |
| 291 | bt_field_string_set_value(out_field, str); |
| 292 | if (set_value_status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) { |
| 293 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot set string field's value: " |
| 294 | "out-str-f-addr=%p, str=\"%s\"" PRId64, |
| 295 | out_field, str); |
| 296 | status = (int) set_value_status; |
| 297 | goto end; |
| 298 | } |
| 299 | } else if (in_fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) { |
| 300 | uint64_t i, nb_member_struct; |
| 301 | const bt_field *in_member_field; |
| 302 | bt_field *out_member_field; |
| 303 | const bt_field_class *in_field_class; |
| 304 | const char *in_member_name; |
| 305 | |
| 306 | in_field_class = bt_field_borrow_class_const(in_field); |
| 307 | nb_member_struct = bt_field_class_structure_get_member_count( |
| 308 | in_field_class); |
| 309 | |
| 310 | /* |
| 311 | * Iterate over the fields by names in the input field to avoid |
| 312 | * problem if the struct fields are not in the same order after |
| 313 | * the debug-info was added. |
| 314 | */ |
| 315 | for (i = 0; i < nb_member_struct; i++) { |
| 316 | const bt_field_class_structure_member *member = |
| 317 | bt_field_class_structure_borrow_member_by_index_const( |
| 318 | in_field_class, i); |
| 319 | |
| 320 | in_member_name = |
| 321 | bt_field_class_structure_member_get_name( |
| 322 | member); |
| 323 | in_member_field = |
| 324 | bt_field_structure_borrow_member_field_by_name_const( |
| 325 | in_field, in_member_name); |
| 326 | out_member_field = |
| 327 | bt_field_structure_borrow_member_field_by_name( |
| 328 | out_field, in_member_name); |
| 329 | |
| 330 | status = copy_field_content(in_member_field, |
| 331 | out_member_field, log_level, self_comp); |
| 332 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 333 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 334 | "Cannot copy struct member field: " |
| 335 | "out-struct-f-addr=%p, " |
| 336 | "out-struct-member-f-addr=%p, " |
| 337 | "member-name=\"%s\"", |
| 338 | out_field, out_member_field, |
| 339 | in_member_name); |
| 340 | goto end; |
| 341 | } |
| 342 | } |
| 343 | } else if (bt_field_class_type_is(in_fc_type, |
| 344 | BT_FIELD_CLASS_TYPE_ARRAY)) { |
| 345 | const bt_field *in_element_field; |
| 346 | bt_field *out_element_field; |
| 347 | uint64_t i, array_len; |
| 348 | bt_field_array_dynamic_set_length_status set_len_status; |
| 349 | |
| 350 | array_len = bt_field_array_get_length(in_field); |
| 351 | |
| 352 | if (bt_field_class_type_is(in_fc_type, |
| 353 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY)) { |
| 354 | set_len_status = bt_field_array_dynamic_set_length( |
| 355 | out_field, array_len); |
| 356 | if (set_len_status != |
| 357 | BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_OK) { |
| 358 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 359 | "Cannot set dynamic array field's length field: " |
| 360 | "out-arr-f-addr=%p, arr-length=%" PRIu64, |
| 361 | out_field, array_len); |
| 362 | status = (int) set_len_status; |
| 363 | goto end; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | for (i = 0; i < array_len; i++) { |
| 368 | in_element_field = |
| 369 | bt_field_array_borrow_element_field_by_index_const( |
| 370 | in_field, i); |
| 371 | out_element_field = |
| 372 | bt_field_array_borrow_element_field_by_index( |
| 373 | out_field, i); |
| 374 | status = copy_field_content(in_element_field, |
| 375 | out_element_field, log_level, self_comp); |
| 376 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 377 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 378 | "Cannot copy element field: " |
| 379 | "out-arr-f-addr=%p, out-arr-elem-f-addr=%p", |
| 380 | out_field, out_element_field); |
| 381 | goto end; |
| 382 | } |
| 383 | } |
| 384 | } else if (bt_field_class_type_is(in_fc_type, |
| 385 | BT_FIELD_CLASS_TYPE_OPTION)) { |
| 386 | const bt_field *in_option_field; |
| 387 | bt_field *out_option_field; |
| 388 | |
| 389 | in_option_field = bt_field_option_borrow_field_const(in_field); |
| 390 | |
| 391 | if (in_option_field) { |
| 392 | bt_field_option_set_has_field(out_field, BT_TRUE); |
| 393 | out_option_field = bt_field_option_borrow_field( |
| 394 | out_field); |
| 395 | BT_ASSERT_DBG(out_option_field); |
| 396 | status = copy_field_content(in_option_field, out_option_field, |
| 397 | log_level, self_comp); |
| 398 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 399 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 400 | "Cannot copy option field: " |
| 401 | "out-opt-f-addr=%p, out-opt-field-f-addr=%p", |
| 402 | out_field, out_option_field); |
| 403 | goto end; |
| 404 | } |
| 405 | } else { |
| 406 | bt_field_option_set_has_field(out_field, BT_FALSE); |
| 407 | } |
| 408 | } else if (bt_field_class_type_is(in_fc_type, |
| 409 | BT_FIELD_CLASS_TYPE_VARIANT)) { |
| 410 | bt_field_variant_select_option_by_index_status sel_opt_status; |
| 411 | uint64_t in_selected_option_idx; |
| 412 | const bt_field *in_option_field; |
| 413 | bt_field *out_option_field; |
| 414 | |
| 415 | in_selected_option_idx = |
| 416 | bt_field_variant_get_selected_option_index( |
| 417 | in_field); |
| 418 | sel_opt_status = bt_field_variant_select_option_by_index(out_field, |
| 419 | in_selected_option_idx); |
| 420 | if (sel_opt_status != |
| 421 | BT_FIELD_VARIANT_SELECT_OPTION_STATUS_OK) { |
| 422 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 423 | "Cannot select variant field's option field: " |
| 424 | "out-var-f-addr=%p, opt-index=%" PRId64, |
| 425 | out_field, in_selected_option_idx); |
| 426 | status = (int) sel_opt_status; |
| 427 | goto end; |
| 428 | } |
| 429 | |
| 430 | in_option_field = bt_field_variant_borrow_selected_option_field_const(in_field); |
| 431 | out_option_field = bt_field_variant_borrow_selected_option_field(out_field); |
| 432 | |
| 433 | status = copy_field_content(in_option_field, out_option_field, |
| 434 | log_level, self_comp); |
| 435 | if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) { |
| 436 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 437 | "Cannot copy element field: " |
| 438 | "out-var-f-addr=%p, out-opt-f-addr=%p", |
| 439 | out_field, out_option_field); |
| 440 | goto end; |
| 441 | } |
| 442 | } else { |
| 443 | bt_common_abort(); |
| 444 | } |
| 445 | |
| 446 | BT_COMP_LOGT("Copied content of field: in-f-addr=%p, out-f-addr=%p", |
| 447 | in_field, out_field); |
| 448 | |
| 449 | status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK; |
| 450 | end: |
| 451 | return status; |
| 452 | } |