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