| 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_LOG_TAG "PLUGIN-LTTNG-UTILS-DEBUG-INFO-TRACE-IR-DATA-COPY" |
| 27 | #include "logging.h" |
| 28 | |
| 29 | #include <inttypes.h> |
| 30 | #include <stdint.h> |
| 31 | |
| 32 | #include "common/assert.h" |
| 33 | |
| 34 | #include "trace-ir-data-copy.h" |
| 35 | |
| 36 | BT_HIDDEN |
| 37 | void copy_trace_content(const bt_trace *in_trace, bt_trace *out_trace) |
| 38 | { |
| 39 | bt_trace_status status; |
| 40 | const char *trace_name; |
| 41 | |
| 42 | BT_LOGD("Copying content of trace: in-t-addr=%p, out-t-addr=%p", |
| 43 | in_trace, out_trace); |
| 44 | |
| 45 | trace_name = bt_trace_get_name(in_trace); |
| 46 | /* Copy the trace name. */ |
| 47 | if (trace_name) { |
| 48 | status = bt_trace_set_name(out_trace, trace_name); |
| 49 | if (status != BT_TRACE_STATUS_OK) { |
| 50 | BT_LOGE("Cannot set trace's name: trace-addr=%p, name=\"%s\"", |
| 51 | out_trace, trace_name); |
| 52 | goto end; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | BT_LOGD("Copied content of trace: in-t-addr=%p, out-t-addr=%p", |
| 57 | in_trace, out_trace); |
| 58 | end: |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | BT_HIDDEN |
| 63 | void copy_stream_content(const bt_stream *in_stream, bt_stream *out_stream) |
| 64 | { |
| 65 | const char *stream_name; |
| 66 | bt_stream_status status; |
| 67 | |
| 68 | BT_LOGD("Copying content of stream: in-s-addr=%p, out-s-addr=%p", |
| 69 | in_stream, out_stream); |
| 70 | |
| 71 | stream_name = bt_stream_get_name(in_stream); |
| 72 | if (stream_name) { |
| 73 | status = bt_stream_set_name(out_stream, stream_name); |
| 74 | if (status != BT_STREAM_STATUS_OK) { |
| 75 | BT_LOGE("Cannot set stream's name: stream-addr=%p, " |
| 76 | "name=%s", out_stream, stream_name); |
| 77 | goto end; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | BT_LOGD("Copied content of stream: in-s-addr=%p, out-s-addr=%p", |
| 82 | in_stream, out_stream); |
| 83 | end: |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | BT_HIDDEN |
| 88 | void copy_packet_content(const bt_packet *in_packet, bt_packet *out_packet) |
| 89 | { |
| 90 | const bt_field *in_context_field; |
| 91 | bt_field *out_context_field; |
| 92 | |
| 93 | BT_LOGD("Copying content of packet: in-p-addr=%p, out-p-addr=%p", |
| 94 | in_packet, out_packet); |
| 95 | |
| 96 | /* Copy context field. */ |
| 97 | in_context_field = bt_packet_borrow_context_field_const(in_packet); |
| 98 | if (in_context_field) { |
| 99 | out_context_field = bt_packet_borrow_context_field(out_packet); |
| 100 | BT_ASSERT(out_context_field); |
| 101 | copy_field_content(in_context_field, out_context_field); |
| 102 | } |
| 103 | |
| 104 | BT_LOGD("Copied content of packet: in-p-addr=%p, out-p-addr=%p", |
| 105 | in_packet, out_packet); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | BT_HIDDEN |
| 110 | void copy_event_content(const bt_event *in_event, bt_event *out_event) |
| 111 | { |
| 112 | const bt_field *in_common_ctx_field, *in_specific_ctx_field, |
| 113 | *in_payload_field; |
| 114 | bt_field *out_common_ctx_field, *out_specific_ctx_field, |
| 115 | *out_payload_field; |
| 116 | |
| 117 | BT_LOGD("Copying content of event: in-e-addr=%p, out-e-addr=%p", |
| 118 | in_event, out_event); |
| 119 | in_common_ctx_field = |
| 120 | bt_event_borrow_common_context_field_const(in_event); |
| 121 | if (in_common_ctx_field) { |
| 122 | out_common_ctx_field = |
| 123 | bt_event_borrow_common_context_field(out_event); |
| 124 | BT_ASSERT(out_common_ctx_field); |
| 125 | copy_field_content(in_common_ctx_field, |
| 126 | out_common_ctx_field); |
| 127 | } |
| 128 | |
| 129 | in_specific_ctx_field = |
| 130 | bt_event_borrow_specific_context_field_const(in_event); |
| 131 | if (in_specific_ctx_field) { |
| 132 | out_specific_ctx_field = |
| 133 | bt_event_borrow_specific_context_field(out_event); |
| 134 | BT_ASSERT(out_specific_ctx_field); |
| 135 | copy_field_content(in_specific_ctx_field, |
| 136 | out_specific_ctx_field); |
| 137 | } |
| 138 | |
| 139 | in_payload_field = bt_event_borrow_payload_field_const(in_event); |
| 140 | if (in_payload_field) { |
| 141 | out_payload_field = bt_event_borrow_payload_field(out_event); |
| 142 | BT_ASSERT(out_payload_field); |
| 143 | copy_field_content(in_payload_field, |
| 144 | out_payload_field); |
| 145 | } |
| 146 | |
| 147 | BT_LOGD("Copied content of event: in-e-addr=%p, out-e-addr=%p", |
| 148 | in_event, out_event); |
| 149 | } |
| 150 | |
| 151 | BT_HIDDEN |
| 152 | void copy_field_content(const bt_field *in_field, bt_field *out_field) |
| 153 | { |
| 154 | bt_field_class_type in_fc_type, out_fc_type; |
| 155 | |
| 156 | in_fc_type = bt_field_get_class_type(in_field); |
| 157 | out_fc_type = bt_field_get_class_type(out_field); |
| 158 | BT_ASSERT(in_fc_type == out_fc_type); |
| 159 | |
| 160 | BT_LOGV("Copying content of field: in-f-addr=%p, out-f-addr=%p", |
| 161 | in_field, out_field); |
| 162 | switch (in_fc_type) { |
| 163 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: |
| 164 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: |
| 165 | bt_field_unsigned_integer_set_value(out_field, |
| 166 | bt_field_unsigned_integer_get_value(in_field)); |
| 167 | break; |
| 168 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: |
| 169 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: |
| 170 | bt_field_signed_integer_set_value(out_field, |
| 171 | bt_field_signed_integer_get_value(in_field)); |
| 172 | break; |
| 173 | case BT_FIELD_CLASS_TYPE_REAL: |
| 174 | bt_field_real_set_value(out_field, |
| 175 | bt_field_real_get_value(in_field)); |
| 176 | break; |
| 177 | case BT_FIELD_CLASS_TYPE_STRING: |
| 178 | { |
| 179 | const char *str = bt_field_string_get_value(in_field); |
| 180 | bt_field_status status = bt_field_string_set_value(out_field, str); |
| 181 | if (status != BT_FIELD_STATUS_OK) { |
| 182 | BT_LOGE("Cannot set string field's value: " |
| 183 | "str-field-addr=%p, str=%s" PRId64, |
| 184 | out_field, str); |
| 185 | } |
| 186 | break; |
| 187 | } |
| 188 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 189 | { |
| 190 | uint64_t i, nb_member_struct; |
| 191 | const bt_field *in_member_field; |
| 192 | bt_field *out_member_field; |
| 193 | const bt_field_class *in_field_class; |
| 194 | const char *in_member_name; |
| 195 | |
| 196 | in_field_class = bt_field_borrow_class_const(in_field); |
| 197 | nb_member_struct = bt_field_class_structure_get_member_count( |
| 198 | in_field_class); |
| 199 | |
| 200 | /* |
| 201 | * Iterate over the fields by names in the input field to avoid |
| 202 | * problem if the struct fields are not in the same order after |
| 203 | * the debug-info was added. |
| 204 | */ |
| 205 | for (i = 0; i < nb_member_struct; i++) { |
| 206 | const bt_field_class_structure_member *member = |
| 207 | bt_field_class_structure_borrow_member_by_index_const( |
| 208 | in_field_class, i); |
| 209 | |
| 210 | in_member_name = |
| 211 | bt_field_class_structure_member_get_name( |
| 212 | member); |
| 213 | in_member_field = |
| 214 | bt_field_structure_borrow_member_field_by_name_const( |
| 215 | in_field, in_member_name); |
| 216 | out_member_field = |
| 217 | bt_field_structure_borrow_member_field_by_name( |
| 218 | out_field, in_member_name); |
| 219 | |
| 220 | copy_field_content(in_member_field, |
| 221 | out_member_field); |
| 222 | } |
| 223 | break; |
| 224 | } |
| 225 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 226 | /* fall through */ |
| 227 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 228 | { |
| 229 | const bt_field *in_element_field; |
| 230 | bt_field *out_element_field; |
| 231 | uint64_t i, array_len; |
| 232 | bt_field_status status; |
| 233 | |
| 234 | array_len = bt_field_array_get_length(in_field); |
| 235 | |
| 236 | if (in_fc_type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY) { |
| 237 | status = bt_field_dynamic_array_set_length(out_field, |
| 238 | array_len); |
| 239 | if (status != BT_FIELD_STATUS_OK) { |
| 240 | BT_LOGE("Cannot set dynamic array field's " |
| 241 | "length field: field-addr=%p, " |
| 242 | "length=%" PRIu64, out_field, array_len); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | for (i = 0; i < array_len; i++) { |
| 247 | in_element_field = |
| 248 | bt_field_array_borrow_element_field_by_index_const( |
| 249 | in_field, i); |
| 250 | out_element_field = |
| 251 | bt_field_array_borrow_element_field_by_index( |
| 252 | out_field, i); |
| 253 | copy_field_content(in_element_field, out_element_field); |
| 254 | } |
| 255 | break; |
| 256 | } |
| 257 | case BT_FIELD_CLASS_TYPE_VARIANT: |
| 258 | { |
| 259 | bt_field_status status; |
| 260 | uint64_t in_selected_option_idx; |
| 261 | const bt_field *in_option_field; |
| 262 | bt_field *out_option_field; |
| 263 | |
| 264 | in_selected_option_idx = |
| 265 | bt_field_variant_get_selected_option_field_index( |
| 266 | in_field); |
| 267 | status = bt_field_variant_select_option_field(out_field, |
| 268 | in_selected_option_idx); |
| 269 | if (status != BT_FIELD_STATUS_OK) { |
| 270 | BT_LOGE("Cannot select variant field's option field: " |
| 271 | "var-field-addr=%p, opt-index=%" PRId64, |
| 272 | out_field, in_selected_option_idx); |
| 273 | } |
| 274 | |
| 275 | in_option_field = bt_field_variant_borrow_selected_option_field_const(in_field); |
| 276 | out_option_field = bt_field_variant_borrow_selected_option_field(out_field); |
| 277 | |
| 278 | copy_field_content(in_option_field, out_option_field); |
| 279 | |
| 280 | break; |
| 281 | } |
| 282 | default: |
| 283 | abort(); |
| 284 | } |
| 285 | BT_LOGV("Copied content of field: in-f-addr=%p, out-f-addr=%p", |
| 286 | in_field, out_field); |
| 287 | } |