Fix typos
[babeltrace.git] / src / plugins / lttng-utils / debug-info / trace-ir-data-copy.c
CommitLineData
ca9f27f3 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
ca9f27f3
FD
3 *
4 * Copyright (c) 2015-2019 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
6 *
0235b0db 7 * Babeltrace - Trace IR data object copy
ca9f27f3
FD
8 */
9
91bc8451 10#define BT_COMP_LOG_SELF_COMP self_comp
3a3d15f3 11#define BT_LOG_OUTPUT_LEVEL log_level
350ad6c1 12#define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-DATA-COPY"
d9c39b0a 13#include "logging/comp-logging.h"
ca9f27f3
FD
14
15#include <inttypes.h>
16#include <stdint.h>
17
578e048b 18#include "common/assert.h"
498e7994 19#include "common/common.h"
ca9f27f3
FD
20
21#include "trace-ir-data-copy.h"
22
3b34b490
FD
23enum debug_info_trace_ir_mapping_status copy_trace_content(
24 const bt_trace *in_trace, bt_trace *out_trace,
91bc8451 25 bt_logging_level log_level, bt_self_component *self_comp)
ca9f27f3 26{
3b34b490 27 enum debug_info_trace_ir_mapping_status status;
ca9f27f3 28 const char *trace_name;
335a2da5 29 uint64_t i, env_field_count;
ca9f27f3 30
91bc8451 31 BT_COMP_LOGD("Copying content of trace: in-t-addr=%p, out-t-addr=%p",
ca9f27f3 32 in_trace, out_trace);
3b34b490 33
ca9f27f3 34 trace_name = bt_trace_get_name(in_trace);
3b34b490 35
ca9f27f3
FD
36 /* Copy the trace name. */
37 if (trace_name) {
3b34b490
FD
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;
ca9f27f3
FD
45 goto end;
46 }
47 }
48
ce45f74a
PP
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
335a2da5 56 /*
3b34b490 57 * Do not copy the trace UUID as the trace may be modified and should
335a2da5
PP
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
e7401568 74 BT_COMP_LOGD("Copying trace environment entry: "
3b34b490 75 "index=%" PRId64 ", value-addr=%p, value-name=\"%s\"",
335a2da5
PP
76 i, value, value_name);
77
78 BT_ASSERT(value_name);
79 BT_ASSERT(value);
80
81 if (bt_value_is_signed_integer(value)) {
bc463d34
FD
82 set_env_status = bt_trace_set_environment_entry_integer(
83 out_trace, value_name,
84 bt_value_integer_signed_get( value));
335a2da5 85 } else if (bt_value_is_string(value)) {
bc463d34
FD
86 set_env_status = bt_trace_set_environment_entry_string(
87 out_trace, value_name,
88 bt_value_string_get(value));
335a2da5 89 } else {
498e7994 90 bt_common_abort();
335a2da5
PP
91 }
92
bc463d34 93 if (set_env_status != BT_TRACE_SET_ENVIRONMENT_ENTRY_STATUS_OK) {
3b34b490
FD
94 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot copy trace's environment: "
95 "out-t-addr=%p, name=\"%s\"",
335a2da5 96 out_trace, trace_name);
3b34b490 97 status = (int) set_env_status;
335a2da5
PP
98 goto end;
99 }
100 }
101
91bc8451 102 BT_COMP_LOGD("Copied content of trace: in-t-addr=%p, out-t-addr=%p",
3b34b490
FD
103 in_trace, out_trace);
104
105 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
ca9f27f3 106end:
3b34b490 107 return status;
ca9f27f3
FD
108}
109
3b34b490
FD
110enum debug_info_trace_ir_mapping_status copy_stream_content(
111 const bt_stream *in_stream, bt_stream *out_stream,
91bc8451 112 bt_logging_level log_level, bt_self_component *self_comp)
ca9f27f3 113{
3b34b490 114 enum debug_info_trace_ir_mapping_status status;
ca9f27f3 115 const char *stream_name;
ca9f27f3 116
91bc8451 117 BT_COMP_LOGD("Copying content of stream: in-s-addr=%p, out-s-addr=%p",
ca9f27f3
FD
118 in_stream, out_stream);
119
ca9f27f3 120 stream_name = bt_stream_get_name(in_stream);
3b34b490 121
ca9f27f3 122 if (stream_name) {
3b34b490
FD
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;
ca9f27f3
FD
130 goto end;
131 }
132 }
133
ce45f74a
PP
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));
3b34b490 140
91bc8451 141 BT_COMP_LOGD("Copied content of stream: in-s-addr=%p, out-s-addr=%p",
3b34b490
FD
142 in_stream, out_stream);
143 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
ca9f27f3 144end:
3b34b490 145 return status;
ca9f27f3
FD
146}
147
3b34b490
FD
148enum debug_info_trace_ir_mapping_status copy_packet_content(
149 const bt_packet *in_packet, bt_packet *out_packet,
91bc8451 150 bt_logging_level log_level, bt_self_component *self_comp)
ca9f27f3 151{
3b34b490 152 enum debug_info_trace_ir_mapping_status status;
ca9f27f3
FD
153 const bt_field *in_context_field;
154 bt_field *out_context_field;
155
91bc8451 156 BT_COMP_LOGD("Copying content of packet: in-p-addr=%p, out-p-addr=%p",
ca9f27f3
FD
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);
3b34b490 164 status = copy_field_content(in_context_field, out_context_field,
91bc8451 165 log_level, self_comp);
3b34b490
FD
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 }
ca9f27f3
FD
172 }
173
91bc8451 174 BT_COMP_LOGD("Copied content of packet: in-p-addr=%p, out-p-addr=%p",
ca9f27f3 175 in_packet, out_packet);
3b34b490
FD
176 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
177end:
178 return status;
ca9f27f3
FD
179}
180
3b34b490
FD
181enum debug_info_trace_ir_mapping_status copy_event_content(
182 const bt_event *in_event, bt_event *out_event,
91bc8451 183 bt_logging_level log_level, bt_self_component *self_comp)
ca9f27f3 184{
3b34b490 185 enum debug_info_trace_ir_mapping_status status;
ca9f27f3
FD
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
91bc8451 191 BT_COMP_LOGD("Copying content of event: in-e-addr=%p, out-e-addr=%p",
ca9f27f3
FD
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);
98b15851 198 BT_ASSERT_DBG(out_common_ctx_field);
3b34b490
FD
199
200 status = copy_field_content(in_common_ctx_field,
91bc8451 201 out_common_ctx_field, log_level, self_comp);
3b34b490
FD
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 }
ca9f27f3
FD
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);
98b15851 215 BT_ASSERT_DBG(out_specific_ctx_field);
3b34b490
FD
216
217 status = copy_field_content(in_specific_ctx_field,
91bc8451 218 out_specific_ctx_field, log_level, self_comp);
3b34b490
FD
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 }
ca9f27f3
FD
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);
98b15851 230 BT_ASSERT_DBG(out_payload_field);
3b34b490
FD
231
232 status = copy_field_content(in_payload_field,
91bc8451 233 out_payload_field, log_level, self_comp);
3b34b490
FD
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 }
ca9f27f3
FD
240 }
241
91bc8451 242 BT_COMP_LOGD("Copied content of event: in-e-addr=%p, out-e-addr=%p",
ca9f27f3 243 in_event, out_event);
3b34b490
FD
244 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
245end:
246 return status;
ca9f27f3
FD
247}
248
3b34b490
FD
249enum debug_info_trace_ir_mapping_status copy_field_content(
250 const bt_field *in_field, bt_field *out_field,
91bc8451 251 bt_logging_level log_level, bt_self_component *self_comp)
ca9f27f3 252{
3b34b490 253 enum debug_info_trace_ir_mapping_status status;
ca9f27f3
FD
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);
98b15851 258 BT_ASSERT_DBG(in_fc_type == out_fc_type);
ca9f27f3 259
ef267d12 260 BT_COMP_LOGT("Copying content of field: in-f-addr=%p, out-f-addr=%p",
ca9f27f3 261 in_field, out_field);
ebdb6693
PP
262
263 if (in_fc_type == BT_FIELD_CLASS_TYPE_BOOL) {
f7cfbcc3
PP
264 bt_field_bool_set_value(out_field,
265 bt_field_bool_get_value(in_field));
ebdb6693 266 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_BIT_ARRAY) {
dc7ac074
PP
267 bt_field_bit_array_set_value_as_integer(out_field,
268 bt_field_bit_array_get_value_as_integer(in_field));
ebdb6693
PP
269 } else if (bt_field_class_type_is(in_fc_type,
270 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) {
9c08c816 271 bt_field_integer_unsigned_set_value(out_field,
3b34b490 272 bt_field_integer_unsigned_get_value(in_field));
ebdb6693
PP
273 } else if (bt_field_class_type_is(in_fc_type,
274 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER)) {
9c08c816 275 bt_field_integer_signed_set_value(out_field,
3b34b490 276 bt_field_integer_signed_get_value(in_field));
ebdb6693 277 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) {
fe4df857 278 bt_field_real_single_precision_set_value(out_field,
3b34b490 279 bt_field_real_single_precision_get_value(in_field));
ebdb6693 280 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL) {
fe4df857 281 bt_field_real_double_precision_set_value(out_field,
3b34b490 282 bt_field_real_double_precision_get_value(in_field));
ebdb6693 283 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_STRING) {
ca9f27f3 284 const char *str = bt_field_string_get_value(in_field);
3b34b490 285 bt_field_string_set_value_status set_value_status =
d24d5663 286 bt_field_string_set_value(out_field, str);
3b34b490
FD
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,
ca9f27f3 290 out_field, str);
3b34b490
FD
291 status = (int) set_value_status;
292 goto end;
ca9f27f3 293 }
ebdb6693 294 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) {
ca9f27f3
FD
295 uint64_t i, nb_member_struct;
296 const bt_field *in_member_field;
297 bt_field *out_member_field;
1e6fd1d7 298 const bt_field_class *in_field_class;
ca9f27f3
FD
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(
3b34b490 303 in_field_class);
ca9f27f3
FD
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++) {
1e6fd1d7
PP
311 const bt_field_class_structure_member *member =
312 bt_field_class_structure_borrow_member_by_index_const(
313 in_field_class, i);
ca9f27f3 314
1e6fd1d7
PP
315 in_member_name =
316 bt_field_class_structure_member_get_name(
317 member);
ca9f27f3
FD
318 in_member_field =
319 bt_field_structure_borrow_member_field_by_name_const(
bc463d34 320 in_field, in_member_name);
ca9f27f3
FD
321 out_member_field =
322 bt_field_structure_borrow_member_field_by_name(
bc463d34 323 out_field, in_member_name);
ca9f27f3 324
3b34b490 325 status = copy_field_content(in_member_field,
91bc8451 326 out_member_field, log_level, self_comp);
3b34b490
FD
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 }
ca9f27f3 337 }
ebdb6693
PP
338 } else if (bt_field_class_type_is(in_fc_type,
339 BT_FIELD_CLASS_TYPE_ARRAY)) {
ca9f27f3
FD
340 const bt_field *in_element_field;
341 bt_field *out_element_field;
342 uint64_t i, array_len;
9c08c816 343 bt_field_array_dynamic_set_length_status set_len_status;
ca9f27f3
FD
344
345 array_len = bt_field_array_get_length(in_field);
346
ebdb6693
PP
347 if (bt_field_class_type_is(in_fc_type,
348 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY)) {
9c08c816 349 set_len_status = bt_field_array_dynamic_set_length(
d24d5663
PP
350 out_field, array_len);
351 if (set_len_status !=
352 BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_OK) {
3b34b490
FD
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;
ca9f27f3
FD
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(
d24d5663 365 in_field, i);
ca9f27f3
FD
366 out_element_field =
367 bt_field_array_borrow_element_field_by_index(
d24d5663 368 out_field, i);
3b34b490
FD
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 }
ca9f27f3 378 }
ebdb6693
PP
379 } else if (bt_field_class_type_is(in_fc_type,
380 BT_FIELD_CLASS_TYPE_OPTION)) {
f29ef814
PP
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);
98b15851 390 BT_ASSERT_DBG(out_option_field);
3b34b490 391 status = copy_field_content(in_option_field, out_option_field,
f29ef814 392 log_level, self_comp);
3b34b490
FD
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 }
f29ef814
PP
400 } else {
401 bt_field_option_set_has_field(out_field, BT_FALSE);
402 }
ebdb6693
PP
403 } else if (bt_field_class_type_is(in_fc_type,
404 BT_FIELD_CLASS_TYPE_VARIANT)) {
7b4311c1 405 bt_field_variant_select_option_by_index_status sel_opt_status;
ca9f27f3
FD
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 =
7b4311c1 411 bt_field_variant_get_selected_option_index(
bc463d34 412 in_field);
7b4311c1 413 sel_opt_status = bt_field_variant_select_option_by_index(out_field,
bc463d34 414 in_selected_option_idx);
d24d5663 415 if (sel_opt_status !=
7b4311c1 416 BT_FIELD_VARIANT_SELECT_OPTION_STATUS_OK) {
3b34b490
FD
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,
ca9f27f3 420 out_field, in_selected_option_idx);
3b34b490
FD
421 status = (int) sel_opt_status;
422 goto end;
ca9f27f3
FD
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
3b34b490 428 status = copy_field_content(in_option_field, out_option_field,
91bc8451 429 log_level, self_comp);
3b34b490
FD
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 }
ebdb6693 437 } else {
498e7994 438 bt_common_abort();
ca9f27f3 439 }
ebdb6693 440
ef267d12 441 BT_COMP_LOGT("Copied content of field: in-f-addr=%p, out-f-addr=%p",
ca9f27f3 442 in_field, out_field);
3b34b490
FD
443
444 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
445end:
446 return status;
ca9f27f3 447}
This page took 0.097102 seconds and 4 git commands to generate.