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_LOG_TAG "PLUGIN-LTTNG-UTILS-DEBUG-INFO-TRACE-IR-DATA-COPY"
32 #include <babeltrace/assert-internal.h>
34 #include "trace-ir-data-copy.h"
37 void copy_trace_content(const bt_trace
*in_trace
, bt_trace
*out_trace
)
39 bt_trace_status status
;
40 const char *trace_name
;
42 BT_LOGD("Copying content of trace: in-t-addr=%p, out-t-addr=%p",
45 trace_name
= bt_trace_get_name(in_trace
);
46 /* Copy the trace name. */
48 status
= bt_trace_set_name(out_trace
, trace_name
);
49 if (status
!= BT_TRACE_STATUS_OK
) {
50 BT_LOGE("Cannot set trace's name: trace-addr=%p, name=\"%s\"",
51 out_trace
, trace_name
);
56 BT_LOGD("Copied content of trace: in-t-addr=%p, out-t-addr=%p",
63 void copy_stream_content(const bt_stream
*in_stream
, bt_stream
*out_stream
)
65 const bt_trace
*in_trace
;
66 const bt_stream_class
*in_stream_class
;
67 const char *stream_name
;
68 bt_stream_status status
;
70 BT_LOGD("Copying content of stream: in-s-addr=%p, out-s-addr=%p",
71 in_stream
, out_stream
);
73 in_stream_class
= bt_stream_borrow_class_const(in_stream
);
74 in_trace
= bt_stream_borrow_trace_const(in_stream
);
76 stream_name
= bt_stream_get_name(in_stream
);
78 status
= bt_stream_set_name(out_stream
, stream_name
);
79 if (status
!= BT_STREAM_STATUS_OK
) {
80 BT_LOGE("Cannot set stream's name: stream-addr=%p, "
81 "name=%s", out_stream
, stream_name
);
86 BT_LOGD("Copied content of stream: in-s-addr=%p, out-s-addr=%p",
87 in_stream
, out_stream
);
93 void copy_packet_content(const bt_packet
*in_packet
, bt_packet
*out_packet
)
95 const bt_field
*in_context_field
;
96 bt_field
*out_context_field
;
98 BT_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
);
109 BT_LOGD("Copied content of packet: in-p-addr=%p, out-p-addr=%p",
110 in_packet
, out_packet
);
115 void copy_event_content(const bt_event
*in_event
, bt_event
*out_event
)
117 const bt_field
*in_common_ctx_field
, *in_specific_ctx_field
,
119 bt_field
*out_common_ctx_field
, *out_specific_ctx_field
,
122 BT_LOGD("Copying content of event: in-e-addr=%p, out-e-addr=%p",
123 in_event
, out_event
);
124 in_common_ctx_field
=
125 bt_event_borrow_common_context_field_const(in_event
);
126 if (in_common_ctx_field
) {
127 out_common_ctx_field
=
128 bt_event_borrow_common_context_field(out_event
);
129 BT_ASSERT(out_common_ctx_field
);
130 copy_field_content(in_common_ctx_field
,
131 out_common_ctx_field
);
134 in_specific_ctx_field
=
135 bt_event_borrow_specific_context_field_const(in_event
);
136 if (in_specific_ctx_field
) {
137 out_specific_ctx_field
=
138 bt_event_borrow_specific_context_field(out_event
);
139 BT_ASSERT(out_specific_ctx_field
);
140 copy_field_content(in_specific_ctx_field
,
141 out_specific_ctx_field
);
144 in_payload_field
= bt_event_borrow_payload_field_const(in_event
);
145 if (in_payload_field
) {
146 out_payload_field
= bt_event_borrow_payload_field(out_event
);
147 BT_ASSERT(out_payload_field
);
148 copy_field_content(in_payload_field
,
152 BT_LOGD("Copied content of event: in-e-addr=%p, out-e-addr=%p",
153 in_event
, out_event
);
157 void copy_field_content(const bt_field
*in_field
, bt_field
*out_field
)
159 bt_field_class_type in_fc_type
, out_fc_type
;
161 in_fc_type
= bt_field_get_class_type(in_field
);
162 out_fc_type
= bt_field_get_class_type(out_field
);
163 BT_ASSERT(in_fc_type
== out_fc_type
);
165 BT_LOGD("Copying content of field: in-f-addr=%p, out-f-addr=%p",
166 in_field
, out_field
);
167 switch (in_fc_type
) {
168 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
169 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
170 bt_field_unsigned_integer_set_value(out_field
,
171 bt_field_unsigned_integer_get_value(in_field
));
173 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
174 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
175 bt_field_signed_integer_set_value(out_field
,
176 bt_field_signed_integer_get_value(in_field
));
178 case BT_FIELD_CLASS_TYPE_REAL
:
179 bt_field_real_set_value(out_field
,
180 bt_field_real_get_value(in_field
));
182 case BT_FIELD_CLASS_TYPE_STRING
:
184 const char *str
= bt_field_string_get_value(in_field
);
185 bt_field_status status
= bt_field_string_set_value(out_field
, str
);
186 if (status
!= BT_FIELD_STATUS_OK
) {
187 BT_LOGE("Cannot set string field's value: "
188 "str-field-addr=%p, str=%s" PRId64
,
193 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
195 uint64_t i
, nb_member_struct
;
196 const bt_field
*in_member_field
;
197 bt_field
*out_member_field
;
198 const bt_field_class
*in_field_class
, *in_member_field_class
;
199 const char *in_member_name
;
201 in_field_class
= bt_field_borrow_class_const(in_field
);
202 nb_member_struct
= bt_field_class_structure_get_member_count(
206 * Iterate over the fields by names in the input field to avoid
207 * problem if the struct fields are not in the same order after
208 * the debug-info was added.
210 for (i
= 0; i
< nb_member_struct
; i
++) {
211 bt_field_class_structure_borrow_member_by_index_const(
212 in_field_class
, i
, &in_member_name
,
213 &in_member_field_class
);
216 bt_field_structure_borrow_member_field_by_name_const(
217 in_field
, in_member_name
);
219 bt_field_structure_borrow_member_field_by_name(
220 out_field
, in_member_name
);
222 copy_field_content(in_member_field
,
227 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
229 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
231 const bt_field
*in_element_field
;
232 bt_field
*out_element_field
;
233 uint64_t i
, array_len
;
234 bt_field_status status
;
236 array_len
= bt_field_array_get_length(in_field
);
238 if (in_fc_type
== BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
) {
239 status
= bt_field_dynamic_array_set_length(out_field
,
241 if (status
!= BT_FIELD_STATUS_OK
) {
242 BT_LOGE("Cannot set dynamic array field's "
243 "length field: field-addr=%p, "
244 "length=%" PRIu64
, out_field
, array_len
);
248 for (i
= 0; i
< array_len
; i
++) {
250 bt_field_array_borrow_element_field_by_index_const(
253 bt_field_array_borrow_element_field_by_index(
255 copy_field_content(in_element_field
, out_element_field
);
259 case BT_FIELD_CLASS_TYPE_VARIANT
:
261 bt_field_status status
;
262 uint64_t in_selected_option_idx
;
263 const bt_field
*in_option_field
;
264 bt_field
*out_option_field
;
266 in_selected_option_idx
=
267 bt_field_variant_get_selected_option_field_index(
269 status
= bt_field_variant_select_option_field(out_field
,
270 in_selected_option_idx
);
271 if (status
!= BT_FIELD_STATUS_OK
) {
272 BT_LOGE("Cannot select variant field's option field: "
273 "var-field-addr=%p, opt-index=%" PRId64
,
274 out_field
, in_selected_option_idx
);
277 in_option_field
= bt_field_variant_borrow_selected_option_field_const(in_field
);
278 out_option_field
= bt_field_variant_borrow_selected_option_field(out_field
);
280 copy_field_content(in_option_field
, out_option_field
);
287 BT_LOGD("Copied content of field: in-f-addr=%p, out-f-addr=%p",
288 in_field
, out_field
);