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