| 1 | /* |
| 2 | * Babeltrace - Trace IR data object copy |
| 3 | * |
| 4 | * Copyright (c) 2015-2019 EfficiOS Inc. and Linux Foundation |
| 5 | * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #define BT_COMP_LOG_SELF_COMP self_comp |
| 27 | #define BT_LOG_OUTPUT_LEVEL log_level |
| 28 | #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-DATA-COPY" |
| 29 | #include "logging/comp-logging.h" |
| 30 | |
| 31 | #include <inttypes.h> |
| 32 | #include <stdint.h> |
| 33 | |
| 34 | #include "common/assert.h" |
| 35 | |
| 36 | #include "trace-ir-data-copy.h" |
| 37 | |
| 38 | BT_HIDDEN |
| 39 | void copy_trace_content(const bt_trace *in_trace, bt_trace *out_trace, |
| 40 | bt_logging_level log_level, bt_self_component *self_comp) |
| 41 | { |
| 42 | bt_trace_set_name_status status; |
| 43 | const char *trace_name; |
| 44 | uint64_t i, env_field_count; |
| 45 | |
| 46 | BT_COMP_LOGD("Copying content of trace: in-t-addr=%p, out-t-addr=%p", |
| 47 | in_trace, out_trace); |
| 48 | trace_name = bt_trace_get_name(in_trace); |
| 49 | /* Copy the trace name. */ |
| 50 | if (trace_name) { |
| 51 | status = bt_trace_set_name(out_trace, trace_name); |
| 52 | if (status != BT_TRACE_SET_NAME_STATUS_OK) { |
| 53 | BT_COMP_LOGE("Cannot set trace's name: trace-addr=%p, name=\"%s\"", |
| 54 | out_trace, trace_name); |
| 55 | bt_current_thread_clear_error(); |
| 56 | goto end; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Safe to use the same value object because it's frozen at this |
| 62 | * point. |
| 63 | */ |
| 64 | bt_trace_set_user_attributes(out_trace, |
| 65 | bt_trace_borrow_user_attributes_const(in_trace)); |
| 66 | |
| 67 | /* |
| 68 | * Do not copy the trace UUID as it may be modified and should |
| 69 | * no longer have the same UUID. |
| 70 | */ |
| 71 | |
| 72 | /* |
| 73 | * Go over all the entries in the environment section of the |
| 74 | * trace and copy the content to the new trace. |
| 75 | */ |
| 76 | env_field_count = bt_trace_get_environment_entry_count(in_trace); |
| 77 | for (i = 0; i < env_field_count; i++) { |
| 78 | const char *value_name; |
| 79 | const bt_value *value = NULL; |
| 80 | bt_trace_set_environment_entry_status set_env_status; |
| 81 | |
| 82 | bt_trace_borrow_environment_entry_by_index_const( |
| 83 | in_trace, i, &value_name, &value); |
| 84 | |
| 85 | BT_COMP_LOGD("Copying trace environnement entry: " |
| 86 | "index=%" PRId64 ", value-addr=%p, value-name=%s", |
| 87 | i, value, value_name); |
| 88 | |
| 89 | BT_ASSERT(value_name); |
| 90 | BT_ASSERT(value); |
| 91 | |
| 92 | if (bt_value_is_signed_integer(value)) { |
| 93 | set_env_status = |
| 94 | bt_trace_set_environment_entry_integer( |
| 95 | out_trace, value_name, |
| 96 | bt_value_integer_signed_get( |
| 97 | value)); |
| 98 | } else if (bt_value_is_string(value)) { |
| 99 | set_env_status = |
| 100 | bt_trace_set_environment_entry_string( |
| 101 | out_trace, value_name, |
| 102 | bt_value_string_get(value)); |
| 103 | } else { |
| 104 | abort(); |
| 105 | } |
| 106 | |
| 107 | if (set_env_status != |
| 108 | BT_TRACE_SET_ENVIRONMENT_ENTRY_STATUS_OK) { |
| 109 | BT_COMP_LOGE("Cannot copy trace's environment: " |
| 110 | "trace-addr=%p, name=\"%s\"", |
| 111 | out_trace, trace_name); |
| 112 | bt_current_thread_clear_error(); |
| 113 | goto end; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | BT_COMP_LOGD("Copied content of trace: in-t-addr=%p, out-t-addr=%p", |
| 118 | in_trace, out_trace); |
| 119 | end: |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | BT_HIDDEN |
| 124 | void copy_stream_content(const bt_stream *in_stream, bt_stream *out_stream, |
| 125 | bt_logging_level log_level, bt_self_component *self_comp) |
| 126 | { |
| 127 | const char *stream_name; |
| 128 | bt_stream_set_name_status status; |
| 129 | |
| 130 | BT_COMP_LOGD("Copying content of stream: in-s-addr=%p, out-s-addr=%p", |
| 131 | in_stream, out_stream); |
| 132 | |
| 133 | stream_name = bt_stream_get_name(in_stream); |
| 134 | if (stream_name) { |
| 135 | status = bt_stream_set_name(out_stream, stream_name); |
| 136 | if (status != BT_STREAM_SET_NAME_STATUS_OK) { |
| 137 | BT_COMP_LOGE("Cannot set stream's name: stream-addr=%p, " |
| 138 | "name=%s", out_stream, stream_name); |
| 139 | goto end; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Safe to use the same value object because it's frozen at this |
| 145 | * point. |
| 146 | */ |
| 147 | bt_stream_set_user_attributes(out_stream, |
| 148 | bt_stream_borrow_user_attributes_const(in_stream)); |
| 149 | BT_COMP_LOGD("Copied content of stream: in-s-addr=%p, out-s-addr=%p", |
| 150 | in_stream, out_stream); |
| 151 | end: |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | BT_HIDDEN |
| 156 | void copy_packet_content(const bt_packet *in_packet, bt_packet *out_packet, |
| 157 | bt_logging_level log_level, bt_self_component *self_comp) |
| 158 | { |
| 159 | const bt_field *in_context_field; |
| 160 | bt_field *out_context_field; |
| 161 | |
| 162 | BT_COMP_LOGD("Copying content of packet: in-p-addr=%p, out-p-addr=%p", |
| 163 | in_packet, out_packet); |
| 164 | |
| 165 | /* Copy context field. */ |
| 166 | in_context_field = bt_packet_borrow_context_field_const(in_packet); |
| 167 | if (in_context_field) { |
| 168 | out_context_field = bt_packet_borrow_context_field(out_packet); |
| 169 | BT_ASSERT(out_context_field); |
| 170 | copy_field_content(in_context_field, out_context_field, |
| 171 | log_level, self_comp); |
| 172 | } |
| 173 | |
| 174 | BT_COMP_LOGD("Copied content of packet: in-p-addr=%p, out-p-addr=%p", |
| 175 | in_packet, out_packet); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | BT_HIDDEN |
| 180 | void copy_event_content(const bt_event *in_event, bt_event *out_event, |
| 181 | bt_logging_level log_level, bt_self_component *self_comp) |
| 182 | { |
| 183 | const bt_field *in_common_ctx_field, *in_specific_ctx_field, |
| 184 | *in_payload_field; |
| 185 | bt_field *out_common_ctx_field, *out_specific_ctx_field, |
| 186 | *out_payload_field; |
| 187 | |
| 188 | BT_COMP_LOGD("Copying content of event: in-e-addr=%p, out-e-addr=%p", |
| 189 | in_event, out_event); |
| 190 | in_common_ctx_field = |
| 191 | bt_event_borrow_common_context_field_const(in_event); |
| 192 | if (in_common_ctx_field) { |
| 193 | out_common_ctx_field = |
| 194 | bt_event_borrow_common_context_field(out_event); |
| 195 | BT_ASSERT(out_common_ctx_field); |
| 196 | copy_field_content(in_common_ctx_field, |
| 197 | out_common_ctx_field, log_level, self_comp); |
| 198 | } |
| 199 | |
| 200 | in_specific_ctx_field = |
| 201 | bt_event_borrow_specific_context_field_const(in_event); |
| 202 | if (in_specific_ctx_field) { |
| 203 | out_specific_ctx_field = |
| 204 | bt_event_borrow_specific_context_field(out_event); |
| 205 | BT_ASSERT(out_specific_ctx_field); |
| 206 | copy_field_content(in_specific_ctx_field, |
| 207 | out_specific_ctx_field, log_level, self_comp); |
| 208 | } |
| 209 | |
| 210 | in_payload_field = bt_event_borrow_payload_field_const(in_event); |
| 211 | if (in_payload_field) { |
| 212 | out_payload_field = bt_event_borrow_payload_field(out_event); |
| 213 | BT_ASSERT(out_payload_field); |
| 214 | copy_field_content(in_payload_field, |
| 215 | out_payload_field, log_level, self_comp); |
| 216 | } |
| 217 | |
| 218 | BT_COMP_LOGD("Copied content of event: in-e-addr=%p, out-e-addr=%p", |
| 219 | in_event, out_event); |
| 220 | } |
| 221 | |
| 222 | BT_HIDDEN |
| 223 | void copy_field_content(const bt_field *in_field, bt_field *out_field, |
| 224 | bt_logging_level log_level, bt_self_component *self_comp) |
| 225 | { |
| 226 | bt_field_class_type in_fc_type, out_fc_type; |
| 227 | |
| 228 | in_fc_type = bt_field_get_class_type(in_field); |
| 229 | out_fc_type = bt_field_get_class_type(out_field); |
| 230 | BT_ASSERT(in_fc_type == out_fc_type); |
| 231 | |
| 232 | BT_COMP_LOGT("Copying content of field: in-f-addr=%p, out-f-addr=%p", |
| 233 | in_field, out_field); |
| 234 | switch (in_fc_type) { |
| 235 | case BT_FIELD_CLASS_TYPE_BOOL: |
| 236 | bt_field_bool_set_value(out_field, |
| 237 | bt_field_bool_get_value(in_field)); |
| 238 | break; |
| 239 | case BT_FIELD_CLASS_TYPE_BIT_ARRAY: |
| 240 | bt_field_bit_array_set_value_as_integer(out_field, |
| 241 | bt_field_bit_array_get_value_as_integer(in_field)); |
| 242 | break; |
| 243 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: |
| 244 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: |
| 245 | bt_field_integer_unsigned_set_value(out_field, |
| 246 | bt_field_integer_unsigned_get_value(in_field)); |
| 247 | break; |
| 248 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: |
| 249 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: |
| 250 | bt_field_integer_signed_set_value(out_field, |
| 251 | bt_field_integer_signed_get_value(in_field)); |
| 252 | break; |
| 253 | case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: |
| 254 | bt_field_real_single_precision_set_value(out_field, |
| 255 | bt_field_real_single_precision_get_value(in_field)); |
| 256 | break; |
| 257 | case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: |
| 258 | bt_field_real_double_precision_set_value(out_field, |
| 259 | bt_field_real_double_precision_get_value(in_field)); |
| 260 | break; |
| 261 | case BT_FIELD_CLASS_TYPE_STRING: |
| 262 | { |
| 263 | const char *str = bt_field_string_get_value(in_field); |
| 264 | bt_field_string_set_value_status status = |
| 265 | bt_field_string_set_value(out_field, str); |
| 266 | if (status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) { |
| 267 | BT_COMP_LOGE("Cannot set string field's value: " |
| 268 | "str-field-addr=%p, str=%s" PRId64, |
| 269 | out_field, str); |
| 270 | bt_current_thread_clear_error(); |
| 271 | |
| 272 | } |
| 273 | break; |
| 274 | } |
| 275 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 276 | { |
| 277 | uint64_t i, nb_member_struct; |
| 278 | const bt_field *in_member_field; |
| 279 | bt_field *out_member_field; |
| 280 | const bt_field_class *in_field_class; |
| 281 | const char *in_member_name; |
| 282 | |
| 283 | in_field_class = bt_field_borrow_class_const(in_field); |
| 284 | nb_member_struct = bt_field_class_structure_get_member_count( |
| 285 | in_field_class); |
| 286 | |
| 287 | /* |
| 288 | * Iterate over the fields by names in the input field to avoid |
| 289 | * problem if the struct fields are not in the same order after |
| 290 | * the debug-info was added. |
| 291 | */ |
| 292 | for (i = 0; i < nb_member_struct; i++) { |
| 293 | const bt_field_class_structure_member *member = |
| 294 | bt_field_class_structure_borrow_member_by_index_const( |
| 295 | in_field_class, i); |
| 296 | |
| 297 | in_member_name = |
| 298 | bt_field_class_structure_member_get_name( |
| 299 | member); |
| 300 | in_member_field = |
| 301 | bt_field_structure_borrow_member_field_by_name_const( |
| 302 | in_field, in_member_name); |
| 303 | out_member_field = |
| 304 | bt_field_structure_borrow_member_field_by_name( |
| 305 | out_field, in_member_name); |
| 306 | |
| 307 | copy_field_content(in_member_field, |
| 308 | out_member_field, log_level, self_comp); |
| 309 | } |
| 310 | break; |
| 311 | } |
| 312 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 313 | /* fall through */ |
| 314 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 315 | { |
| 316 | const bt_field *in_element_field; |
| 317 | bt_field *out_element_field; |
| 318 | uint64_t i, array_len; |
| 319 | bt_field_array_dynamic_set_length_status set_len_status; |
| 320 | |
| 321 | array_len = bt_field_array_get_length(in_field); |
| 322 | |
| 323 | if (in_fc_type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY) { |
| 324 | set_len_status = bt_field_array_dynamic_set_length( |
| 325 | out_field, array_len); |
| 326 | if (set_len_status != |
| 327 | BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_OK) { |
| 328 | BT_COMP_LOGE("Cannot set dynamic array field's " |
| 329 | "length field: field-addr=%p, " |
| 330 | "length=%" PRIu64, out_field, array_len); |
| 331 | bt_current_thread_clear_error(); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | for (i = 0; i < array_len; i++) { |
| 336 | in_element_field = |
| 337 | bt_field_array_borrow_element_field_by_index_const( |
| 338 | in_field, i); |
| 339 | out_element_field = |
| 340 | bt_field_array_borrow_element_field_by_index( |
| 341 | out_field, i); |
| 342 | copy_field_content(in_element_field, out_element_field, |
| 343 | log_level, self_comp); |
| 344 | } |
| 345 | break; |
| 346 | } |
| 347 | case BT_FIELD_CLASS_TYPE_OPTION: |
| 348 | { |
| 349 | const bt_field *in_option_field; |
| 350 | bt_field *out_option_field; |
| 351 | |
| 352 | in_option_field = bt_field_option_borrow_field_const(in_field); |
| 353 | |
| 354 | if (in_option_field) { |
| 355 | bt_field_option_set_has_field(out_field, BT_TRUE); |
| 356 | out_option_field = bt_field_option_borrow_field( |
| 357 | out_field); |
| 358 | BT_ASSERT(out_option_field); |
| 359 | copy_field_content(in_option_field, out_option_field, |
| 360 | log_level, self_comp); |
| 361 | } else { |
| 362 | bt_field_option_set_has_field(out_field, BT_FALSE); |
| 363 | } |
| 364 | |
| 365 | break; |
| 366 | } |
| 367 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
| 368 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: |
| 369 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: |
| 370 | { |
| 371 | bt_field_variant_select_option_field_by_index_status sel_opt_status; |
| 372 | uint64_t in_selected_option_idx; |
| 373 | const bt_field *in_option_field; |
| 374 | bt_field *out_option_field; |
| 375 | |
| 376 | in_selected_option_idx = |
| 377 | bt_field_variant_get_selected_option_field_index( |
| 378 | in_field); |
| 379 | sel_opt_status = bt_field_variant_select_option_field_by_index(out_field, |
| 380 | in_selected_option_idx); |
| 381 | if (sel_opt_status != |
| 382 | BT_FIELD_VARIANT_SELECT_OPTION_FIELD_STATUS_OK) { |
| 383 | BT_COMP_LOGE("Cannot select variant field's option field: " |
| 384 | "var-field-addr=%p, opt-index=%" PRId64, |
| 385 | out_field, in_selected_option_idx); |
| 386 | bt_current_thread_clear_error(); |
| 387 | } |
| 388 | |
| 389 | in_option_field = bt_field_variant_borrow_selected_option_field_const(in_field); |
| 390 | out_option_field = bt_field_variant_borrow_selected_option_field(out_field); |
| 391 | |
| 392 | copy_field_content(in_option_field, out_option_field, |
| 393 | log_level, self_comp); |
| 394 | |
| 395 | break; |
| 396 | } |
| 397 | default: |
| 398 | abort(); |
| 399 | } |
| 400 | BT_COMP_LOGT("Copied content of field: in-f-addr=%p, out-f-addr=%p", |
| 401 | in_field, out_field); |
| 402 | } |