Commit | Line | Data |
---|---|---|
ca9f27f3 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 | ||
91bc8451 | 26 | #define BT_COMP_LOG_SELF_COMP self_comp |
3a3d15f3 | 27 | #define BT_LOG_OUTPUT_LEVEL log_level |
350ad6c1 | 28 | #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-DATA-COPY" |
d9c39b0a | 29 | #include "logging/comp-logging.h" |
ca9f27f3 FD |
30 | |
31 | #include <inttypes.h> | |
32 | #include <stdint.h> | |
33 | ||
578e048b | 34 | #include "common/assert.h" |
ca9f27f3 FD |
35 | |
36 | #include "trace-ir-data-copy.h" | |
37 | ||
38 | BT_HIDDEN | |
3a3d15f3 | 39 | void copy_trace_content(const bt_trace *in_trace, bt_trace *out_trace, |
91bc8451 | 40 | bt_logging_level log_level, bt_self_component *self_comp) |
ca9f27f3 | 41 | { |
d24d5663 | 42 | bt_trace_set_name_status status; |
ca9f27f3 | 43 | const char *trace_name; |
335a2da5 | 44 | uint64_t i, env_field_count; |
ca9f27f3 | 45 | |
91bc8451 | 46 | BT_COMP_LOGD("Copying content of trace: in-t-addr=%p, out-t-addr=%p", |
ca9f27f3 | 47 | in_trace, out_trace); |
ca9f27f3 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); | |
d24d5663 | 52 | if (status != BT_TRACE_SET_NAME_STATUS_OK) { |
91bc8451 | 53 | BT_COMP_LOGE("Cannot set trace's name: trace-addr=%p, name=\"%s\"", |
d24d5663 | 54 | out_trace, trace_name); |
b80991f6 | 55 | bt_current_thread_clear_error(); |
ca9f27f3 FD |
56 | goto end; |
57 | } | |
58 | } | |
59 | ||
ce45f74a 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 | ||
335a2da5 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)) { | |
bc463d34 FD |
93 | set_env_status = bt_trace_set_environment_entry_integer( |
94 | out_trace, value_name, | |
95 | bt_value_integer_signed_get( value)); | |
335a2da5 | 96 | } else if (bt_value_is_string(value)) { |
bc463d34 FD |
97 | set_env_status = bt_trace_set_environment_entry_string( |
98 | out_trace, value_name, | |
99 | bt_value_string_get(value)); | |
335a2da5 PP |
100 | } else { |
101 | abort(); | |
102 | } | |
103 | ||
bc463d34 | 104 | if (set_env_status != BT_TRACE_SET_ENVIRONMENT_ENTRY_STATUS_OK) { |
335a2da5 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 | ||
91bc8451 | 113 | BT_COMP_LOGD("Copied content of trace: in-t-addr=%p, out-t-addr=%p", |
ca9f27f3 FD |
114 | in_trace, out_trace); |
115 | end: | |
116 | return; | |
117 | } | |
118 | ||
119 | BT_HIDDEN | |
3a3d15f3 | 120 | void copy_stream_content(const bt_stream *in_stream, bt_stream *out_stream, |
91bc8451 | 121 | bt_logging_level log_level, bt_self_component *self_comp) |
ca9f27f3 | 122 | { |
ca9f27f3 | 123 | const char *stream_name; |
d24d5663 | 124 | bt_stream_set_name_status status; |
ca9f27f3 | 125 | |
91bc8451 | 126 | BT_COMP_LOGD("Copying content of stream: in-s-addr=%p, out-s-addr=%p", |
ca9f27f3 FD |
127 | in_stream, out_stream); |
128 | ||
ca9f27f3 FD |
129 | stream_name = bt_stream_get_name(in_stream); |
130 | if (stream_name) { | |
131 | status = bt_stream_set_name(out_stream, stream_name); | |
d24d5663 | 132 | if (status != BT_STREAM_SET_NAME_STATUS_OK) { |
91bc8451 | 133 | BT_COMP_LOGE("Cannot set stream's name: stream-addr=%p, " |
ca9f27f3 FD |
134 | "name=%s", out_stream, stream_name); |
135 | goto end; | |
136 | } | |
137 | } | |
138 | ||
ce45f74a 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)); | |
91bc8451 | 145 | BT_COMP_LOGD("Copied content of stream: in-s-addr=%p, out-s-addr=%p", |
ca9f27f3 FD |
146 | in_stream, out_stream); |
147 | end: | |
148 | return; | |
149 | } | |
150 | ||
151 | BT_HIDDEN | |
3a3d15f3 | 152 | void copy_packet_content(const bt_packet *in_packet, bt_packet *out_packet, |
91bc8451 | 153 | bt_logging_level log_level, bt_self_component *self_comp) |
ca9f27f3 FD |
154 | { |
155 | const bt_field *in_context_field; | |
156 | bt_field *out_context_field; | |
157 | ||
91bc8451 | 158 | BT_COMP_LOGD("Copying content of packet: in-p-addr=%p, out-p-addr=%p", |
ca9f27f3 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); | |
3a3d15f3 | 166 | copy_field_content(in_context_field, out_context_field, |
91bc8451 | 167 | log_level, self_comp); |
ca9f27f3 FD |
168 | } |
169 | ||
91bc8451 | 170 | BT_COMP_LOGD("Copied content of packet: in-p-addr=%p, out-p-addr=%p", |
ca9f27f3 FD |
171 | in_packet, out_packet); |
172 | return; | |
173 | } | |
174 | ||
175 | BT_HIDDEN | |
3a3d15f3 | 176 | void copy_event_content(const bt_event *in_event, bt_event *out_event, |
91bc8451 | 177 | bt_logging_level log_level, bt_self_component *self_comp) |
ca9f27f3 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 | ||
91bc8451 | 184 | BT_COMP_LOGD("Copying content of event: in-e-addr=%p, out-e-addr=%p", |
ca9f27f3 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, | |
91bc8451 | 193 | out_common_ctx_field, log_level, self_comp); |
ca9f27f3 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, | |
91bc8451 | 203 | out_specific_ctx_field, log_level, self_comp); |
ca9f27f3 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, | |
91bc8451 | 211 | out_payload_field, log_level, self_comp); |
ca9f27f3 FD |
212 | } |
213 | ||
91bc8451 | 214 | BT_COMP_LOGD("Copied content of event: in-e-addr=%p, out-e-addr=%p", |
ca9f27f3 FD |
215 | in_event, out_event); |
216 | } | |
217 | ||
218 | BT_HIDDEN | |
3a3d15f3 | 219 | void copy_field_content(const bt_field *in_field, bt_field *out_field, |
91bc8451 | 220 | bt_logging_level log_level, bt_self_component *self_comp) |
ca9f27f3 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 | ||
ef267d12 | 228 | BT_COMP_LOGT("Copying content of field: in-f-addr=%p, out-f-addr=%p", |
ca9f27f3 FD |
229 | in_field, out_field); |
230 | switch (in_fc_type) { | |
f7cfbcc3 PP |
231 | case BT_FIELD_CLASS_TYPE_BOOL: |
232 | bt_field_bool_set_value(out_field, | |
233 | bt_field_bool_get_value(in_field)); | |
234 | break; | |
dc7ac074 PP |
235 | case BT_FIELD_CLASS_TYPE_BIT_ARRAY: |
236 | bt_field_bit_array_set_value_as_integer(out_field, | |
237 | bt_field_bit_array_get_value_as_integer(in_field)); | |
238 | break; | |
ca9f27f3 FD |
239 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: |
240 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
9c08c816 PP |
241 | bt_field_integer_unsigned_set_value(out_field, |
242 | bt_field_integer_unsigned_get_value(in_field)); | |
ca9f27f3 FD |
243 | break; |
244 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
245 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
9c08c816 PP |
246 | bt_field_integer_signed_set_value(out_field, |
247 | bt_field_integer_signed_get_value(in_field)); | |
ca9f27f3 | 248 | break; |
fe4df857 FD |
249 | case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: |
250 | bt_field_real_single_precision_set_value(out_field, | |
251 | bt_field_real_single_precision_get_value(in_field)); | |
252 | break; | |
253 | case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: | |
254 | bt_field_real_double_precision_set_value(out_field, | |
255 | bt_field_real_double_precision_get_value(in_field)); | |
ca9f27f3 FD |
256 | break; |
257 | case BT_FIELD_CLASS_TYPE_STRING: | |
258 | { | |
259 | const char *str = bt_field_string_get_value(in_field); | |
d24d5663 PP |
260 | bt_field_string_set_value_status status = |
261 | bt_field_string_set_value(out_field, str); | |
262 | if (status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) { | |
91bc8451 | 263 | BT_COMP_LOGE("Cannot set string field's value: " |
ca9f27f3 FD |
264 | "str-field-addr=%p, str=%s" PRId64, |
265 | out_field, str); | |
b80991f6 PP |
266 | bt_current_thread_clear_error(); |
267 | ||
ca9f27f3 FD |
268 | } |
269 | break; | |
270 | } | |
271 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
272 | { | |
273 | uint64_t i, nb_member_struct; | |
274 | const bt_field *in_member_field; | |
275 | bt_field *out_member_field; | |
1e6fd1d7 | 276 | const bt_field_class *in_field_class; |
ca9f27f3 FD |
277 | const char *in_member_name; |
278 | ||
279 | in_field_class = bt_field_borrow_class_const(in_field); | |
280 | nb_member_struct = bt_field_class_structure_get_member_count( | |
281 | in_field_class); | |
282 | ||
283 | /* | |
284 | * Iterate over the fields by names in the input field to avoid | |
285 | * problem if the struct fields are not in the same order after | |
286 | * the debug-info was added. | |
287 | */ | |
288 | for (i = 0; i < nb_member_struct; i++) { | |
1e6fd1d7 PP |
289 | const bt_field_class_structure_member *member = |
290 | bt_field_class_structure_borrow_member_by_index_const( | |
291 | in_field_class, i); | |
ca9f27f3 | 292 | |
1e6fd1d7 PP |
293 | in_member_name = |
294 | bt_field_class_structure_member_get_name( | |
295 | member); | |
ca9f27f3 FD |
296 | in_member_field = |
297 | bt_field_structure_borrow_member_field_by_name_const( | |
bc463d34 | 298 | in_field, in_member_name); |
ca9f27f3 FD |
299 | out_member_field = |
300 | bt_field_structure_borrow_member_field_by_name( | |
bc463d34 | 301 | out_field, in_member_name); |
ca9f27f3 FD |
302 | |
303 | copy_field_content(in_member_field, | |
91bc8451 | 304 | out_member_field, log_level, self_comp); |
ca9f27f3 FD |
305 | } |
306 | break; | |
307 | } | |
81b8fa44 PP |
308 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD: |
309 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD: | |
ca9f27f3 FD |
310 | /* fall through */ |
311 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
312 | { | |
313 | const bt_field *in_element_field; | |
314 | bt_field *out_element_field; | |
315 | uint64_t i, array_len; | |
9c08c816 | 316 | bt_field_array_dynamic_set_length_status set_len_status; |
ca9f27f3 FD |
317 | |
318 | array_len = bt_field_array_get_length(in_field); | |
319 | ||
81b8fa44 PP |
320 | if (in_fc_type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD || |
321 | in_fc_type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD) { | |
9c08c816 | 322 | set_len_status = bt_field_array_dynamic_set_length( |
d24d5663 PP |
323 | out_field, array_len); |
324 | if (set_len_status != | |
325 | BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_OK) { | |
91bc8451 | 326 | BT_COMP_LOGE("Cannot set dynamic array field's " |
ca9f27f3 FD |
327 | "length field: field-addr=%p, " |
328 | "length=%" PRIu64, out_field, array_len); | |
b80991f6 | 329 | bt_current_thread_clear_error(); |
ca9f27f3 FD |
330 | } |
331 | } | |
332 | ||
333 | for (i = 0; i < array_len; i++) { | |
334 | in_element_field = | |
335 | bt_field_array_borrow_element_field_by_index_const( | |
d24d5663 | 336 | in_field, i); |
ca9f27f3 FD |
337 | out_element_field = |
338 | bt_field_array_borrow_element_field_by_index( | |
d24d5663 | 339 | out_field, i); |
3a3d15f3 | 340 | copy_field_content(in_element_field, out_element_field, |
91bc8451 | 341 | log_level, self_comp); |
ca9f27f3 FD |
342 | } |
343 | break; | |
344 | } | |
de821fe5 PP |
345 | case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD: |
346 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD: | |
347 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: | |
348 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD: | |
f29ef814 PP |
349 | { |
350 | const bt_field *in_option_field; | |
351 | bt_field *out_option_field; | |
352 | ||
353 | in_option_field = bt_field_option_borrow_field_const(in_field); | |
354 | ||
355 | if (in_option_field) { | |
356 | bt_field_option_set_has_field(out_field, BT_TRUE); | |
357 | out_option_field = bt_field_option_borrow_field( | |
358 | out_field); | |
359 | BT_ASSERT(out_option_field); | |
360 | copy_field_content(in_option_field, out_option_field, | |
361 | log_level, self_comp); | |
362 | } else { | |
363 | bt_field_option_set_has_field(out_field, BT_FALSE); | |
364 | } | |
365 | ||
366 | break; | |
367 | } | |
de821fe5 PP |
368 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD: |
369 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: | |
370 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD: | |
ca9f27f3 | 371 | { |
45c51519 | 372 | bt_field_variant_select_option_field_by_index_status sel_opt_status; |
ca9f27f3 FD |
373 | uint64_t in_selected_option_idx; |
374 | const bt_field *in_option_field; | |
375 | bt_field *out_option_field; | |
376 | ||
377 | in_selected_option_idx = | |
378 | bt_field_variant_get_selected_option_field_index( | |
bc463d34 | 379 | in_field); |
45c51519 | 380 | sel_opt_status = bt_field_variant_select_option_field_by_index(out_field, |
bc463d34 | 381 | in_selected_option_idx); |
d24d5663 PP |
382 | if (sel_opt_status != |
383 | BT_FIELD_VARIANT_SELECT_OPTION_FIELD_STATUS_OK) { | |
91bc8451 | 384 | BT_COMP_LOGE("Cannot select variant field's option field: " |
ca9f27f3 FD |
385 | "var-field-addr=%p, opt-index=%" PRId64, |
386 | out_field, in_selected_option_idx); | |
b80991f6 | 387 | bt_current_thread_clear_error(); |
ca9f27f3 FD |
388 | } |
389 | ||
390 | in_option_field = bt_field_variant_borrow_selected_option_field_const(in_field); | |
391 | out_option_field = bt_field_variant_borrow_selected_option_field(out_field); | |
392 | ||
3a3d15f3 | 393 | copy_field_content(in_option_field, out_option_field, |
91bc8451 | 394 | log_level, self_comp); |
ca9f27f3 FD |
395 | |
396 | break; | |
397 | } | |
398 | default: | |
399 | abort(); | |
400 | } | |
ef267d12 | 401 | BT_COMP_LOGT("Copied content of field: in-f-addr=%p, out-f-addr=%p", |
ca9f27f3 FD |
402 | in_field, out_field); |
403 | } |