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