2 * Babeltrace - Trace IR data object copy
4 * Copyright (c) 2015-2019 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 #define BT_COMP_LOG_SELF_COMP self_comp
27 #define BT_LOG_OUTPUT_LEVEL log_level
28 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-DATA-COPY"
29 #include "logging/comp-logging.h"
34 #include "common/assert.h"
36 #include "trace-ir-data-copy.h"
39 enum debug_info_trace_ir_mapping_status
copy_trace_content(
40 const bt_trace
*in_trace
, bt_trace
*out_trace
,
41 bt_logging_level log_level
, bt_self_component
*self_comp
)
43 enum debug_info_trace_ir_mapping_status status
;
44 const char *trace_name
;
45 uint64_t i
, env_field_count
;
47 BT_COMP_LOGD("Copying content of trace: in-t-addr=%p, out-t-addr=%p",
50 trace_name
= bt_trace_get_name(in_trace
);
52 /* Copy the trace name. */
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
,
60 status
= (int) set_name_status
;
66 * Safe to use the same value object because it's frozen at this
69 bt_trace_set_user_attributes(out_trace
,
70 bt_trace_borrow_user_attributes_const(in_trace
));
73 * Do not copy the trace UUID as the trace may be modified and should
74 * no longer have the same UUID.
78 * Go over all the entries in the environment section of the
79 * trace and copy the content to the new trace.
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
;
87 bt_trace_borrow_environment_entry_by_index_const(
88 in_trace
, i
, &value_name
, &value
);
90 BT_COMP_LOGD("Copying trace environnement entry: "
91 "index=%" PRId64
", value-addr=%p, value-name=\"%s\"",
92 i
, value
, value_name
);
94 BT_ASSERT(value_name
);
97 if (bt_value_is_signed_integer(value
)) {
98 set_env_status
= bt_trace_set_environment_entry_integer(
99 out_trace
, value_name
,
100 bt_value_integer_signed_get( value
));
101 } else if (bt_value_is_string(value
)) {
102 set_env_status
= bt_trace_set_environment_entry_string(
103 out_trace
, value_name
,
104 bt_value_string_get(value
));
109 if (set_env_status
!= BT_TRACE_SET_ENVIRONMENT_ENTRY_STATUS_OK
) {
110 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Cannot copy trace's environment: "
111 "out-t-addr=%p, name=\"%s\"",
112 out_trace
, trace_name
);
113 status
= (int) set_env_status
;
118 BT_COMP_LOGD("Copied content of trace: in-t-addr=%p, out-t-addr=%p",
119 in_trace
, out_trace
);
121 status
= DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK
;
127 enum debug_info_trace_ir_mapping_status
copy_stream_content(
128 const bt_stream
*in_stream
, bt_stream
*out_stream
,
129 bt_logging_level log_level
, bt_self_component
*self_comp
)
131 enum debug_info_trace_ir_mapping_status status
;
132 const char *stream_name
;
134 BT_COMP_LOGD("Copying content of stream: in-s-addr=%p, out-s-addr=%p",
135 in_stream
, out_stream
);
137 stream_name
= bt_stream_get_name(in_stream
);
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
,
146 status
= (int) set_name_status
;
152 * Safe to use the same value object because it's frozen at this
155 bt_stream_set_user_attributes(out_stream
,
156 bt_stream_borrow_user_attributes_const(in_stream
));
158 BT_COMP_LOGD("Copied content of stream: in-s-addr=%p, out-s-addr=%p",
159 in_stream
, out_stream
);
160 status
= DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK
;
166 enum debug_info_trace_ir_mapping_status
copy_packet_content(
167 const bt_packet
*in_packet
, bt_packet
*out_packet
,
168 bt_logging_level log_level
, bt_self_component
*self_comp
)
170 enum debug_info_trace_ir_mapping_status status
;
171 const bt_field
*in_context_field
;
172 bt_field
*out_context_field
;
174 BT_COMP_LOGD("Copying content of packet: in-p-addr=%p, out-p-addr=%p",
175 in_packet
, out_packet
);
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
);
182 status
= copy_field_content(in_context_field
, out_context_field
,
183 log_level
, self_comp
);
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
);
192 BT_COMP_LOGD("Copied content of packet: in-p-addr=%p, out-p-addr=%p",
193 in_packet
, out_packet
);
194 status
= DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK
;
200 enum debug_info_trace_ir_mapping_status
copy_event_content(
201 const bt_event
*in_event
, bt_event
*out_event
,
202 bt_logging_level log_level
, bt_self_component
*self_comp
)
204 enum debug_info_trace_ir_mapping_status status
;
205 const bt_field
*in_common_ctx_field
, *in_specific_ctx_field
,
207 bt_field
*out_common_ctx_field
, *out_specific_ctx_field
,
210 BT_COMP_LOGD("Copying content of event: in-e-addr=%p, out-e-addr=%p",
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
);
217 BT_ASSERT_DBG(out_common_ctx_field
);
219 status
= copy_field_content(in_common_ctx_field
,
220 out_common_ctx_field
, log_level
, self_comp
);
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
);
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
);
234 BT_ASSERT_DBG(out_specific_ctx_field
);
236 status
= copy_field_content(in_specific_ctx_field
,
237 out_specific_ctx_field
, log_level
, self_comp
);
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
);
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
);
249 BT_ASSERT_DBG(out_payload_field
);
251 status
= copy_field_content(in_payload_field
,
252 out_payload_field
, log_level
, self_comp
);
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
);
261 BT_COMP_LOGD("Copied content of event: in-e-addr=%p, out-e-addr=%p",
262 in_event
, out_event
);
263 status
= DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK
;
269 enum debug_info_trace_ir_mapping_status
copy_field_content(
270 const bt_field
*in_field
, bt_field
*out_field
,
271 bt_logging_level log_level
, bt_self_component
*self_comp
)
273 enum debug_info_trace_ir_mapping_status status
;
274 bt_field_class_type in_fc_type
, out_fc_type
;
276 in_fc_type
= bt_field_get_class_type(in_field
);
277 out_fc_type
= bt_field_get_class_type(out_field
);
278 BT_ASSERT_DBG(in_fc_type
== out_fc_type
);
280 BT_COMP_LOGT("Copying content of field: in-f-addr=%p, out-f-addr=%p",
281 in_field
, out_field
);
283 if (in_fc_type
== BT_FIELD_CLASS_TYPE_BOOL
) {
284 bt_field_bool_set_value(out_field
,
285 bt_field_bool_get_value(in_field
));
286 } else if (in_fc_type
== BT_FIELD_CLASS_TYPE_BIT_ARRAY
) {
287 bt_field_bit_array_set_value_as_integer(out_field
,
288 bt_field_bit_array_get_value_as_integer(in_field
));
289 } else if (bt_field_class_type_is(in_fc_type
,
290 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
)) {
291 bt_field_integer_unsigned_set_value(out_field
,
292 bt_field_integer_unsigned_get_value(in_field
));
293 } else if (bt_field_class_type_is(in_fc_type
,
294 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
)) {
295 bt_field_integer_signed_set_value(out_field
,
296 bt_field_integer_signed_get_value(in_field
));
297 } else if (in_fc_type
== BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
) {
298 bt_field_real_single_precision_set_value(out_field
,
299 bt_field_real_single_precision_get_value(in_field
));
300 } else if (in_fc_type
== BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
) {
301 bt_field_real_double_precision_set_value(out_field
,
302 bt_field_real_double_precision_get_value(in_field
));
303 } else if (in_fc_type
== BT_FIELD_CLASS_TYPE_STRING
) {
304 const char *str
= bt_field_string_get_value(in_field
);
305 bt_field_string_set_value_status set_value_status
=
306 bt_field_string_set_value(out_field
, str
);
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
,
311 status
= (int) set_value_status
;
314 } else if (in_fc_type
== BT_FIELD_CLASS_TYPE_STRUCTURE
) {
315 uint64_t i
, nb_member_struct
;
316 const bt_field
*in_member_field
;
317 bt_field
*out_member_field
;
318 const bt_field_class
*in_field_class
;
319 const char *in_member_name
;
321 in_field_class
= bt_field_borrow_class_const(in_field
);
322 nb_member_struct
= bt_field_class_structure_get_member_count(
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.
330 for (i
= 0; i
< nb_member_struct
; i
++) {
331 const bt_field_class_structure_member
*member
=
332 bt_field_class_structure_borrow_member_by_index_const(
336 bt_field_class_structure_member_get_name(
339 bt_field_structure_borrow_member_field_by_name_const(
340 in_field
, in_member_name
);
342 bt_field_structure_borrow_member_field_by_name(
343 out_field
, in_member_name
);
345 status
= copy_field_content(in_member_field
,
346 out_member_field
, log_level
, self_comp
);
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
,
358 } else if (bt_field_class_type_is(in_fc_type
,
359 BT_FIELD_CLASS_TYPE_ARRAY
)) {
360 const bt_field
*in_element_field
;
361 bt_field
*out_element_field
;
362 uint64_t i
, array_len
;
363 bt_field_array_dynamic_set_length_status set_len_status
;
365 array_len
= bt_field_array_get_length(in_field
);
367 if (bt_field_class_type_is(in_fc_type
,
368 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
)) {
369 set_len_status
= bt_field_array_dynamic_set_length(
370 out_field
, array_len
);
371 if (set_len_status
!=
372 BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_OK
) {
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
;
382 for (i
= 0; i
< array_len
; i
++) {
384 bt_field_array_borrow_element_field_by_index_const(
387 bt_field_array_borrow_element_field_by_index(
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
);
399 } else if (bt_field_class_type_is(in_fc_type
,
400 BT_FIELD_CLASS_TYPE_OPTION
)) {
401 const bt_field
*in_option_field
;
402 bt_field
*out_option_field
;
404 in_option_field
= bt_field_option_borrow_field_const(in_field
);
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(
410 BT_ASSERT_DBG(out_option_field
);
411 status
= copy_field_content(in_option_field
, out_option_field
,
412 log_level
, self_comp
);
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
);
421 bt_field_option_set_has_field(out_field
, BT_FALSE
);
423 } else if (bt_field_class_type_is(in_fc_type
,
424 BT_FIELD_CLASS_TYPE_VARIANT
)) {
425 bt_field_variant_select_option_field_by_index_status sel_opt_status
;
426 uint64_t in_selected_option_idx
;
427 const bt_field
*in_option_field
;
428 bt_field
*out_option_field
;
430 in_selected_option_idx
=
431 bt_field_variant_get_selected_option_field_index(
433 sel_opt_status
= bt_field_variant_select_option_field_by_index(out_field
,
434 in_selected_option_idx
);
435 if (sel_opt_status
!=
436 BT_FIELD_VARIANT_SELECT_OPTION_FIELD_STATUS_OK
) {
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
,
440 out_field
, in_selected_option_idx
);
441 status
= (int) sel_opt_status
;
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
);
448 status
= copy_field_content(in_option_field
, out_option_field
,
449 log_level
, self_comp
);
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
);
461 BT_COMP_LOGT("Copied content of field: in-f-addr=%p, out-f-addr=%p",
462 in_field
, out_field
);
464 status
= DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK
;