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 "plugins/comp-logging.h"
34 #include "common/assert.h"
36 #include "trace-ir-data-copy.h"
39 void copy_trace_content(const bt_trace
*in_trace
, bt_trace
*out_trace
,
40 bt_logging_level log_level
, bt_self_component
*self_comp
)
42 bt_trace_status status
;
43 const char *trace_name
;
45 BT_COMP_LOGD("Copying content of trace: in-t-addr=%p, out-t-addr=%p",
48 trace_name
= bt_trace_get_name(in_trace
);
49 /* Copy the trace name. */
51 status
= bt_trace_set_name(out_trace
, trace_name
);
52 if (status
!= BT_TRACE_STATUS_OK
) {
53 BT_COMP_LOGE("Cannot set trace's name: trace-addr=%p, name=\"%s\"",
54 out_trace
, trace_name
);
59 BT_COMP_LOGD("Copied content of trace: in-t-addr=%p, out-t-addr=%p",
66 void copy_stream_content(const bt_stream
*in_stream
, bt_stream
*out_stream
,
67 bt_logging_level log_level
, bt_self_component
*self_comp
)
69 const char *stream_name
;
70 bt_stream_status status
;
72 BT_COMP_LOGD("Copying content of stream: in-s-addr=%p, out-s-addr=%p",
73 in_stream
, out_stream
);
75 stream_name
= bt_stream_get_name(in_stream
);
77 status
= bt_stream_set_name(out_stream
, stream_name
);
78 if (status
!= BT_STREAM_STATUS_OK
) {
79 BT_COMP_LOGE("Cannot set stream's name: stream-addr=%p, "
80 "name=%s", out_stream
, stream_name
);
85 BT_COMP_LOGD("Copied content of stream: in-s-addr=%p, out-s-addr=%p",
86 in_stream
, out_stream
);
92 void copy_packet_content(const bt_packet
*in_packet
, bt_packet
*out_packet
,
93 bt_logging_level log_level
, bt_self_component
*self_comp
)
95 const bt_field
*in_context_field
;
96 bt_field
*out_context_field
;
98 BT_COMP_LOGD("Copying content of packet: in-p-addr=%p, out-p-addr=%p",
99 in_packet
, out_packet
);
101 /* Copy context field. */
102 in_context_field
= bt_packet_borrow_context_field_const(in_packet
);
103 if (in_context_field
) {
104 out_context_field
= bt_packet_borrow_context_field(out_packet
);
105 BT_ASSERT(out_context_field
);
106 copy_field_content(in_context_field
, out_context_field
,
107 log_level
, self_comp
);
110 BT_COMP_LOGD("Copied content of packet: in-p-addr=%p, out-p-addr=%p",
111 in_packet
, out_packet
);
116 void copy_event_content(const bt_event
*in_event
, bt_event
*out_event
,
117 bt_logging_level log_level
, bt_self_component
*self_comp
)
119 const bt_field
*in_common_ctx_field
, *in_specific_ctx_field
,
121 bt_field
*out_common_ctx_field
, *out_specific_ctx_field
,
124 BT_COMP_LOGD("Copying content of event: in-e-addr=%p, out-e-addr=%p",
125 in_event
, out_event
);
126 in_common_ctx_field
=
127 bt_event_borrow_common_context_field_const(in_event
);
128 if (in_common_ctx_field
) {
129 out_common_ctx_field
=
130 bt_event_borrow_common_context_field(out_event
);
131 BT_ASSERT(out_common_ctx_field
);
132 copy_field_content(in_common_ctx_field
,
133 out_common_ctx_field
, log_level
, self_comp
);
136 in_specific_ctx_field
=
137 bt_event_borrow_specific_context_field_const(in_event
);
138 if (in_specific_ctx_field
) {
139 out_specific_ctx_field
=
140 bt_event_borrow_specific_context_field(out_event
);
141 BT_ASSERT(out_specific_ctx_field
);
142 copy_field_content(in_specific_ctx_field
,
143 out_specific_ctx_field
, log_level
, self_comp
);
146 in_payload_field
= bt_event_borrow_payload_field_const(in_event
);
147 if (in_payload_field
) {
148 out_payload_field
= bt_event_borrow_payload_field(out_event
);
149 BT_ASSERT(out_payload_field
);
150 copy_field_content(in_payload_field
,
151 out_payload_field
, log_level
, self_comp
);
154 BT_COMP_LOGD("Copied content of event: in-e-addr=%p, out-e-addr=%p",
155 in_event
, out_event
);
159 void copy_field_content(const bt_field
*in_field
, bt_field
*out_field
,
160 bt_logging_level log_level
, bt_self_component
*self_comp
)
162 bt_field_class_type in_fc_type
, out_fc_type
;
164 in_fc_type
= bt_field_get_class_type(in_field
);
165 out_fc_type
= bt_field_get_class_type(out_field
);
166 BT_ASSERT(in_fc_type
== out_fc_type
);
168 BT_COMP_LOGV("Copying content of field: in-f-addr=%p, out-f-addr=%p",
169 in_field
, out_field
);
170 switch (in_fc_type
) {
171 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
172 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
173 bt_field_unsigned_integer_set_value(out_field
,
174 bt_field_unsigned_integer_get_value(in_field
));
176 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
177 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
178 bt_field_signed_integer_set_value(out_field
,
179 bt_field_signed_integer_get_value(in_field
));
181 case BT_FIELD_CLASS_TYPE_REAL
:
182 bt_field_real_set_value(out_field
,
183 bt_field_real_get_value(in_field
));
185 case BT_FIELD_CLASS_TYPE_STRING
:
187 const char *str
= bt_field_string_get_value(in_field
);
188 bt_field_status status
= bt_field_string_set_value(out_field
, str
);
189 if (status
!= BT_FIELD_STATUS_OK
) {
190 BT_COMP_LOGE("Cannot set string field's value: "
191 "str-field-addr=%p, str=%s" PRId64
,
196 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
198 uint64_t i
, nb_member_struct
;
199 const bt_field
*in_member_field
;
200 bt_field
*out_member_field
;
201 const bt_field_class
*in_field_class
;
202 const char *in_member_name
;
204 in_field_class
= bt_field_borrow_class_const(in_field
);
205 nb_member_struct
= bt_field_class_structure_get_member_count(
209 * Iterate over the fields by names in the input field to avoid
210 * problem if the struct fields are not in the same order after
211 * the debug-info was added.
213 for (i
= 0; i
< nb_member_struct
; i
++) {
214 const bt_field_class_structure_member
*member
=
215 bt_field_class_structure_borrow_member_by_index_const(
219 bt_field_class_structure_member_get_name(
222 bt_field_structure_borrow_member_field_by_name_const(
223 in_field
, in_member_name
);
225 bt_field_structure_borrow_member_field_by_name(
226 out_field
, in_member_name
);
228 copy_field_content(in_member_field
,
229 out_member_field
, log_level
, self_comp
);
233 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
235 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
237 const bt_field
*in_element_field
;
238 bt_field
*out_element_field
;
239 uint64_t i
, array_len
;
240 bt_field_status status
;
242 array_len
= bt_field_array_get_length(in_field
);
244 if (in_fc_type
== BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
) {
245 status
= bt_field_dynamic_array_set_length(out_field
,
247 if (status
!= BT_FIELD_STATUS_OK
) {
248 BT_COMP_LOGE("Cannot set dynamic array field's "
249 "length field: field-addr=%p, "
250 "length=%" PRIu64
, out_field
, array_len
);
254 for (i
= 0; i
< array_len
; i
++) {
256 bt_field_array_borrow_element_field_by_index_const(
259 bt_field_array_borrow_element_field_by_index(
261 copy_field_content(in_element_field
, out_element_field
,
262 log_level
, self_comp
);
266 case BT_FIELD_CLASS_TYPE_VARIANT
:
268 bt_field_status status
;
269 uint64_t in_selected_option_idx
;
270 const bt_field
*in_option_field
;
271 bt_field
*out_option_field
;
273 in_selected_option_idx
=
274 bt_field_variant_get_selected_option_field_index(
276 status
= bt_field_variant_select_option_field(out_field
,
277 in_selected_option_idx
);
278 if (status
!= BT_FIELD_STATUS_OK
) {
279 BT_COMP_LOGE("Cannot select variant field's option field: "
280 "var-field-addr=%p, opt-index=%" PRId64
,
281 out_field
, in_selected_option_idx
);
284 in_option_field
= bt_field_variant_borrow_selected_option_field_const(in_field
);
285 out_option_field
= bt_field_variant_borrow_selected_option_field(out_field
);
287 copy_field_content(in_option_field
, out_option_field
,
288 log_level
, self_comp
);
295 BT_COMP_LOGV("Copied content of field: in-f-addr=%p, out-f-addr=%p",
296 in_field
, out_field
);