lib: standardize variant field option function names
[babeltrace.git] / src / plugins / lttng-utils / debug-info / trace-ir-data-copy.c
CommitLineData
58f6d595
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
475e740e 26#define BT_COMP_LOG_SELF_COMP self_comp
a07aba4f 27#define BT_LOG_OUTPUT_LEVEL log_level
b03487ab 28#define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-DATA-COPY"
3fa1b6a3 29#include "logging/comp-logging.h"
58f6d595
FD
30
31#include <inttypes.h>
32#include <stdint.h>
33
57952005 34#include "common/assert.h"
24847fc7 35#include "common/common.h"
58f6d595
FD
36
37#include "trace-ir-data-copy.h"
38
39BT_HIDDEN
bc79143c
FD
40enum debug_info_trace_ir_mapping_status copy_trace_content(
41 const bt_trace *in_trace, bt_trace *out_trace,
475e740e 42 bt_logging_level log_level, bt_self_component *self_comp)
58f6d595 43{
bc79143c 44 enum debug_info_trace_ir_mapping_status status;
58f6d595 45 const char *trace_name;
cd03c43c 46 uint64_t i, env_field_count;
58f6d595 47
475e740e 48 BT_COMP_LOGD("Copying content of trace: in-t-addr=%p, out-t-addr=%p",
58f6d595 49 in_trace, out_trace);
bc79143c 50
58f6d595 51 trace_name = bt_trace_get_name(in_trace);
bc79143c 52
58f6d595
FD
53 /* Copy the trace name. */
54 if (trace_name) {
bc79143c
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;
58f6d595
FD
62 goto end;
63 }
64 }
65
84ff66df
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
cd03c43c 73 /*
bc79143c 74 * Do not copy the trace UUID as the trace may be modified and should
cd03c43c
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: "
bc79143c 92 "index=%" PRId64 ", value-addr=%p, value-name=\"%s\"",
cd03c43c
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)) {
9a51bfb2
FD
99 set_env_status = bt_trace_set_environment_entry_integer(
100 out_trace, value_name,
101 bt_value_integer_signed_get( value));
cd03c43c 102 } else if (bt_value_is_string(value)) {
9a51bfb2
FD
103 set_env_status = bt_trace_set_environment_entry_string(
104 out_trace, value_name,
105 bt_value_string_get(value));
cd03c43c 106 } else {
24847fc7 107 bt_common_abort();
cd03c43c
PP
108 }
109
9a51bfb2 110 if (set_env_status != BT_TRACE_SET_ENVIRONMENT_ENTRY_STATUS_OK) {
bc79143c
FD
111 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot copy trace's environment: "
112 "out-t-addr=%p, name=\"%s\"",
cd03c43c 113 out_trace, trace_name);
bc79143c 114 status = (int) set_env_status;
cd03c43c
PP
115 goto end;
116 }
117 }
118
475e740e 119 BT_COMP_LOGD("Copied content of trace: in-t-addr=%p, out-t-addr=%p",
bc79143c
FD
120 in_trace, out_trace);
121
122 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
58f6d595 123end:
bc79143c 124 return status;
58f6d595
FD
125}
126
127BT_HIDDEN
bc79143c
FD
128enum debug_info_trace_ir_mapping_status copy_stream_content(
129 const bt_stream *in_stream, bt_stream *out_stream,
475e740e 130 bt_logging_level log_level, bt_self_component *self_comp)
58f6d595 131{
bc79143c 132 enum debug_info_trace_ir_mapping_status status;
58f6d595 133 const char *stream_name;
58f6d595 134
475e740e 135 BT_COMP_LOGD("Copying content of stream: in-s-addr=%p, out-s-addr=%p",
58f6d595
FD
136 in_stream, out_stream);
137
58f6d595 138 stream_name = bt_stream_get_name(in_stream);
bc79143c 139
58f6d595 140 if (stream_name) {
bc79143c
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;
58f6d595
FD
148 goto end;
149 }
150 }
151
84ff66df
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));
bc79143c 158
475e740e 159 BT_COMP_LOGD("Copied content of stream: in-s-addr=%p, out-s-addr=%p",
bc79143c
FD
160 in_stream, out_stream);
161 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
58f6d595 162end:
bc79143c 163 return status;
58f6d595
FD
164}
165
166BT_HIDDEN
bc79143c
FD
167enum debug_info_trace_ir_mapping_status copy_packet_content(
168 const bt_packet *in_packet, bt_packet *out_packet,
475e740e 169 bt_logging_level log_level, bt_self_component *self_comp)
58f6d595 170{
bc79143c 171 enum debug_info_trace_ir_mapping_status status;
58f6d595
FD
172 const bt_field *in_context_field;
173 bt_field *out_context_field;
174
475e740e 175 BT_COMP_LOGD("Copying content of packet: in-p-addr=%p, out-p-addr=%p",
58f6d595
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);
bc79143c 183 status = copy_field_content(in_context_field, out_context_field,
475e740e 184 log_level, self_comp);
bc79143c
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 }
58f6d595
FD
191 }
192
475e740e 193 BT_COMP_LOGD("Copied content of packet: in-p-addr=%p, out-p-addr=%p",
58f6d595 194 in_packet, out_packet);
bc79143c
FD
195 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
196end:
197 return status;
58f6d595
FD
198}
199
200BT_HIDDEN
bc79143c
FD
201enum debug_info_trace_ir_mapping_status copy_event_content(
202 const bt_event *in_event, bt_event *out_event,
475e740e 203 bt_logging_level log_level, bt_self_component *self_comp)
58f6d595 204{
bc79143c 205 enum debug_info_trace_ir_mapping_status status;
58f6d595
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
475e740e 211 BT_COMP_LOGD("Copying content of event: in-e-addr=%p, out-e-addr=%p",
58f6d595
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);
ec4a3354 218 BT_ASSERT_DBG(out_common_ctx_field);
bc79143c
FD
219
220 status = copy_field_content(in_common_ctx_field,
475e740e 221 out_common_ctx_field, log_level, self_comp);
bc79143c
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 }
58f6d595
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);
ec4a3354 235 BT_ASSERT_DBG(out_specific_ctx_field);
bc79143c
FD
236
237 status = copy_field_content(in_specific_ctx_field,
475e740e 238 out_specific_ctx_field, log_level, self_comp);
bc79143c
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 }
58f6d595
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);
ec4a3354 250 BT_ASSERT_DBG(out_payload_field);
bc79143c
FD
251
252 status = copy_field_content(in_payload_field,
475e740e 253 out_payload_field, log_level, self_comp);
bc79143c
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 }
58f6d595
FD
260 }
261
475e740e 262 BT_COMP_LOGD("Copied content of event: in-e-addr=%p, out-e-addr=%p",
58f6d595 263 in_event, out_event);
bc79143c
FD
264 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
265end:
266 return status;
58f6d595
FD
267}
268
269BT_HIDDEN
bc79143c
FD
270enum debug_info_trace_ir_mapping_status copy_field_content(
271 const bt_field *in_field, bt_field *out_field,
475e740e 272 bt_logging_level log_level, bt_self_component *self_comp)
58f6d595 273{
bc79143c 274 enum debug_info_trace_ir_mapping_status status;
58f6d595
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);
ec4a3354 279 BT_ASSERT_DBG(in_fc_type == out_fc_type);
58f6d595 280
c9ecaa78 281 BT_COMP_LOGT("Copying content of field: in-f-addr=%p, out-f-addr=%p",
58f6d595 282 in_field, out_field);
94847783
PP
283
284 if (in_fc_type == BT_FIELD_CLASS_TYPE_BOOL) {
41bdd7c1
PP
285 bt_field_bool_set_value(out_field,
286 bt_field_bool_get_value(in_field));
94847783 287 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_BIT_ARRAY) {
f5f2c882
PP
288 bt_field_bit_array_set_value_as_integer(out_field,
289 bt_field_bit_array_get_value_as_integer(in_field));
94847783
PP
290 } else if (bt_field_class_type_is(in_fc_type,
291 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) {
60bbfc7c 292 bt_field_integer_unsigned_set_value(out_field,
bc79143c 293 bt_field_integer_unsigned_get_value(in_field));
94847783
PP
294 } else if (bt_field_class_type_is(in_fc_type,
295 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER)) {
60bbfc7c 296 bt_field_integer_signed_set_value(out_field,
bc79143c 297 bt_field_integer_signed_get_value(in_field));
94847783 298 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) {
76276a81 299 bt_field_real_single_precision_set_value(out_field,
bc79143c 300 bt_field_real_single_precision_get_value(in_field));
94847783 301 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL) {
76276a81 302 bt_field_real_double_precision_set_value(out_field,
bc79143c 303 bt_field_real_double_precision_get_value(in_field));
94847783 304 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_STRING) {
58f6d595 305 const char *str = bt_field_string_get_value(in_field);
bc79143c 306 bt_field_string_set_value_status set_value_status =
fb25b9e3 307 bt_field_string_set_value(out_field, str);
bc79143c
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,
58f6d595 311 out_field, str);
bc79143c
FD
312 status = (int) set_value_status;
313 goto end;
58f6d595 314 }
94847783 315 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) {
58f6d595
FD
316 uint64_t i, nb_member_struct;
317 const bt_field *in_member_field;
318 bt_field *out_member_field;
e24f271a 319 const bt_field_class *in_field_class;
58f6d595
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(
bc79143c 324 in_field_class);
58f6d595
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++) {
e24f271a
PP
332 const bt_field_class_structure_member *member =
333 bt_field_class_structure_borrow_member_by_index_const(
334 in_field_class, i);
58f6d595 335
e24f271a
PP
336 in_member_name =
337 bt_field_class_structure_member_get_name(
338 member);
58f6d595
FD
339 in_member_field =
340 bt_field_structure_borrow_member_field_by_name_const(
9a51bfb2 341 in_field, in_member_name);
58f6d595
FD
342 out_member_field =
343 bt_field_structure_borrow_member_field_by_name(
9a51bfb2 344 out_field, in_member_name);
58f6d595 345
bc79143c 346 status = copy_field_content(in_member_field,
475e740e 347 out_member_field, log_level, self_comp);
bc79143c
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 }
58f6d595 358 }
94847783
PP
359 } else if (bt_field_class_type_is(in_fc_type,
360 BT_FIELD_CLASS_TYPE_ARRAY)) {
58f6d595
FD
361 const bt_field *in_element_field;
362 bt_field *out_element_field;
363 uint64_t i, array_len;
60bbfc7c 364 bt_field_array_dynamic_set_length_status set_len_status;
58f6d595
FD
365
366 array_len = bt_field_array_get_length(in_field);
367
94847783
PP
368 if (bt_field_class_type_is(in_fc_type,
369 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY)) {
60bbfc7c 370 set_len_status = bt_field_array_dynamic_set_length(
fb25b9e3
PP
371 out_field, array_len);
372 if (set_len_status !=
373 BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_OK) {
bc79143c
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;
58f6d595
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(
fb25b9e3 386 in_field, i);
58f6d595
FD
387 out_element_field =
388 bt_field_array_borrow_element_field_by_index(
fb25b9e3 389 out_field, i);
bc79143c
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 }
58f6d595 399 }
94847783
PP
400 } else if (bt_field_class_type_is(in_fc_type,
401 BT_FIELD_CLASS_TYPE_OPTION)) {
978237e9
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);
ec4a3354 411 BT_ASSERT_DBG(out_option_field);
bc79143c 412 status = copy_field_content(in_option_field, out_option_field,
978237e9 413 log_level, self_comp);
bc79143c
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 }
978237e9
PP
421 } else {
422 bt_field_option_set_has_field(out_field, BT_FALSE);
423 }
94847783
PP
424 } else if (bt_field_class_type_is(in_fc_type,
425 BT_FIELD_CLASS_TYPE_VARIANT)) {
7e0b3e01 426 bt_field_variant_select_option_by_index_status sel_opt_status;
58f6d595
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 =
7e0b3e01 432 bt_field_variant_get_selected_option_index(
9a51bfb2 433 in_field);
7e0b3e01 434 sel_opt_status = bt_field_variant_select_option_by_index(out_field,
9a51bfb2 435 in_selected_option_idx);
fb25b9e3 436 if (sel_opt_status !=
7e0b3e01 437 BT_FIELD_VARIANT_SELECT_OPTION_STATUS_OK) {
bc79143c
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,
58f6d595 441 out_field, in_selected_option_idx);
bc79143c
FD
442 status = (int) sel_opt_status;
443 goto end;
58f6d595
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
bc79143c 449 status = copy_field_content(in_option_field, out_option_field,
475e740e 450 log_level, self_comp);
bc79143c
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 }
94847783 458 } else {
24847fc7 459 bt_common_abort();
58f6d595 460 }
94847783 461
c9ecaa78 462 BT_COMP_LOGT("Copied content of field: in-f-addr=%p, out-f-addr=%p",
58f6d595 463 in_field, out_field);
bc79143c
FD
464
465 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
466end:
467 return status;
58f6d595 468}
This page took 0.069211 seconds and 4 git commands to generate.