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