lib: standardize variant field option function names
[babeltrace.git] / src / plugins / lttng-utils / debug-info / trace-ir-data-copy.c
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
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"
30
31 #include <inttypes.h>
32 #include <stdint.h>
33
34 #include "common/assert.h"
35 #include "common/common.h"
36
37 #include "trace-ir-data-copy.h"
38
39 BT_HIDDEN
40 enum debug_info_trace_ir_mapping_status copy_trace_content(
41 const bt_trace *in_trace, bt_trace *out_trace,
42 bt_logging_level log_level, bt_self_component *self_comp)
43 {
44 enum debug_info_trace_ir_mapping_status status;
45 const char *trace_name;
46 uint64_t i, env_field_count;
47
48 BT_COMP_LOGD("Copying content of trace: in-t-addr=%p, out-t-addr=%p",
49 in_trace, out_trace);
50
51 trace_name = bt_trace_get_name(in_trace);
52
53 /* Copy the trace name. */
54 if (trace_name) {
55 bt_trace_set_name_status set_name_status =
56 bt_trace_set_name(out_trace, trace_name);
57 if (set_name_status != BT_TRACE_SET_NAME_STATUS_OK) {
58 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot set trace's name: "
59 "out-t-addr=%p, name=\"%s\"", out_trace,
60 trace_name);
61 status = (int) set_name_status;
62 goto end;
63 }
64 }
65
66 /*
67 * Safe to use the same value object because it's frozen at this
68 * point.
69 */
70 bt_trace_set_user_attributes(out_trace,
71 bt_trace_borrow_user_attributes_const(in_trace));
72
73 /*
74 * Do not copy the trace UUID as the trace may be modified and should
75 * no longer have the same UUID.
76 */
77
78 /*
79 * Go over all the entries in the environment section of the
80 * trace and copy the content to the new trace.
81 */
82 env_field_count = bt_trace_get_environment_entry_count(in_trace);
83 for (i = 0; i < env_field_count; i++) {
84 const char *value_name;
85 const bt_value *value = NULL;
86 bt_trace_set_environment_entry_status set_env_status;
87
88 bt_trace_borrow_environment_entry_by_index_const(
89 in_trace, i, &value_name, &value);
90
91 BT_COMP_LOGD("Copying trace environnement entry: "
92 "index=%" PRId64 ", value-addr=%p, value-name=\"%s\"",
93 i, value, value_name);
94
95 BT_ASSERT(value_name);
96 BT_ASSERT(value);
97
98 if (bt_value_is_signed_integer(value)) {
99 set_env_status = bt_trace_set_environment_entry_integer(
100 out_trace, value_name,
101 bt_value_integer_signed_get( value));
102 } else if (bt_value_is_string(value)) {
103 set_env_status = bt_trace_set_environment_entry_string(
104 out_trace, value_name,
105 bt_value_string_get(value));
106 } else {
107 bt_common_abort();
108 }
109
110 if (set_env_status != BT_TRACE_SET_ENVIRONMENT_ENTRY_STATUS_OK) {
111 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot copy trace's environment: "
112 "out-t-addr=%p, name=\"%s\"",
113 out_trace, trace_name);
114 status = (int) set_env_status;
115 goto end;
116 }
117 }
118
119 BT_COMP_LOGD("Copied content of trace: in-t-addr=%p, out-t-addr=%p",
120 in_trace, out_trace);
121
122 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
123 end:
124 return status;
125 }
126
127 BT_HIDDEN
128 enum debug_info_trace_ir_mapping_status copy_stream_content(
129 const bt_stream *in_stream, bt_stream *out_stream,
130 bt_logging_level log_level, bt_self_component *self_comp)
131 {
132 enum debug_info_trace_ir_mapping_status status;
133 const char *stream_name;
134
135 BT_COMP_LOGD("Copying content of stream: in-s-addr=%p, out-s-addr=%p",
136 in_stream, out_stream);
137
138 stream_name = bt_stream_get_name(in_stream);
139
140 if (stream_name) {
141 bt_stream_set_name_status set_name_status =
142 bt_stream_set_name(out_stream, stream_name);
143 if (set_name_status != BT_STREAM_SET_NAME_STATUS_OK) {
144 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot set stream's name: "
145 "stream-addr=%p, name=\"%s\"", out_stream,
146 stream_name);
147 status = (int) set_name_status;
148 goto end;
149 }
150 }
151
152 /*
153 * Safe to use the same value object because it's frozen at this
154 * point.
155 */
156 bt_stream_set_user_attributes(out_stream,
157 bt_stream_borrow_user_attributes_const(in_stream));
158
159 BT_COMP_LOGD("Copied content of stream: in-s-addr=%p, out-s-addr=%p",
160 in_stream, out_stream);
161 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
162 end:
163 return status;
164 }
165
166 BT_HIDDEN
167 enum debug_info_trace_ir_mapping_status copy_packet_content(
168 const bt_packet *in_packet, bt_packet *out_packet,
169 bt_logging_level log_level, bt_self_component *self_comp)
170 {
171 enum debug_info_trace_ir_mapping_status status;
172 const bt_field *in_context_field;
173 bt_field *out_context_field;
174
175 BT_COMP_LOGD("Copying content of packet: in-p-addr=%p, out-p-addr=%p",
176 in_packet, out_packet);
177
178 /* Copy context field. */
179 in_context_field = bt_packet_borrow_context_field_const(in_packet);
180 if (in_context_field) {
181 out_context_field = bt_packet_borrow_context_field(out_packet);
182 BT_ASSERT(out_context_field);
183 status = copy_field_content(in_context_field, out_context_field,
184 log_level, self_comp);
185 if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) {
186 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot copy context field: "
187 "in-ctx-f-addr=%p, out-ctx-f-addr=%p",
188 in_context_field, out_context_field);
189 goto end;
190 }
191 }
192
193 BT_COMP_LOGD("Copied content of packet: in-p-addr=%p, out-p-addr=%p",
194 in_packet, out_packet);
195 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
196 end:
197 return status;
198 }
199
200 BT_HIDDEN
201 enum debug_info_trace_ir_mapping_status copy_event_content(
202 const bt_event *in_event, bt_event *out_event,
203 bt_logging_level log_level, bt_self_component *self_comp)
204 {
205 enum debug_info_trace_ir_mapping_status status;
206 const bt_field *in_common_ctx_field, *in_specific_ctx_field,
207 *in_payload_field;
208 bt_field *out_common_ctx_field, *out_specific_ctx_field,
209 *out_payload_field;
210
211 BT_COMP_LOGD("Copying content of event: in-e-addr=%p, out-e-addr=%p",
212 in_event, out_event);
213 in_common_ctx_field =
214 bt_event_borrow_common_context_field_const(in_event);
215 if (in_common_ctx_field) {
216 out_common_ctx_field =
217 bt_event_borrow_common_context_field(out_event);
218 BT_ASSERT_DBG(out_common_ctx_field);
219
220 status = copy_field_content(in_common_ctx_field,
221 out_common_ctx_field, log_level, self_comp);
222 if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) {
223 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot copy common context field: "
224 "in-comm-ctx-f-addr=%p, out-comm-ctx-f-addr=%p",
225 in_common_ctx_field, out_common_ctx_field);
226 goto end;
227 }
228 }
229
230 in_specific_ctx_field =
231 bt_event_borrow_specific_context_field_const(in_event);
232 if (in_specific_ctx_field) {
233 out_specific_ctx_field =
234 bt_event_borrow_specific_context_field(out_event);
235 BT_ASSERT_DBG(out_specific_ctx_field);
236
237 status = copy_field_content(in_specific_ctx_field,
238 out_specific_ctx_field, log_level, self_comp);
239 if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) {
240 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot copy specific context field: "
241 "in-spec-ctx-f-addr=%p, out-spec-ctx-f-addr=%p",
242 in_specific_ctx_field, out_specific_ctx_field);
243 goto end;
244 }
245 }
246
247 in_payload_field = bt_event_borrow_payload_field_const(in_event);
248 if (in_payload_field) {
249 out_payload_field = bt_event_borrow_payload_field(out_event);
250 BT_ASSERT_DBG(out_payload_field);
251
252 status = copy_field_content(in_payload_field,
253 out_payload_field, log_level, self_comp);
254 if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) {
255 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot copy payloat field: "
256 "in-payload-f-addr=%p, out-payload-f-addr=%p",
257 in_payload_field, out_payload_field);
258 goto end;
259 }
260 }
261
262 BT_COMP_LOGD("Copied content of event: in-e-addr=%p, out-e-addr=%p",
263 in_event, out_event);
264 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
265 end:
266 return status;
267 }
268
269 BT_HIDDEN
270 enum debug_info_trace_ir_mapping_status copy_field_content(
271 const bt_field *in_field, bt_field *out_field,
272 bt_logging_level log_level, bt_self_component *self_comp)
273 {
274 enum debug_info_trace_ir_mapping_status status;
275 bt_field_class_type in_fc_type, out_fc_type;
276
277 in_fc_type = bt_field_get_class_type(in_field);
278 out_fc_type = bt_field_get_class_type(out_field);
279 BT_ASSERT_DBG(in_fc_type == out_fc_type);
280
281 BT_COMP_LOGT("Copying content of field: in-f-addr=%p, out-f-addr=%p",
282 in_field, out_field);
283
284 if (in_fc_type == BT_FIELD_CLASS_TYPE_BOOL) {
285 bt_field_bool_set_value(out_field,
286 bt_field_bool_get_value(in_field));
287 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_BIT_ARRAY) {
288 bt_field_bit_array_set_value_as_integer(out_field,
289 bt_field_bit_array_get_value_as_integer(in_field));
290 } else if (bt_field_class_type_is(in_fc_type,
291 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) {
292 bt_field_integer_unsigned_set_value(out_field,
293 bt_field_integer_unsigned_get_value(in_field));
294 } else if (bt_field_class_type_is(in_fc_type,
295 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER)) {
296 bt_field_integer_signed_set_value(out_field,
297 bt_field_integer_signed_get_value(in_field));
298 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) {
299 bt_field_real_single_precision_set_value(out_field,
300 bt_field_real_single_precision_get_value(in_field));
301 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL) {
302 bt_field_real_double_precision_set_value(out_field,
303 bt_field_real_double_precision_get_value(in_field));
304 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_STRING) {
305 const char *str = bt_field_string_get_value(in_field);
306 bt_field_string_set_value_status set_value_status =
307 bt_field_string_set_value(out_field, str);
308 if (set_value_status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
309 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Cannot set string field's value: "
310 "out-str-f-addr=%p, str=\"%s\"" PRId64,
311 out_field, str);
312 status = (int) set_value_status;
313 goto end;
314 }
315 } else if (in_fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) {
316 uint64_t i, nb_member_struct;
317 const bt_field *in_member_field;
318 bt_field *out_member_field;
319 const bt_field_class *in_field_class;
320 const char *in_member_name;
321
322 in_field_class = bt_field_borrow_class_const(in_field);
323 nb_member_struct = bt_field_class_structure_get_member_count(
324 in_field_class);
325
326 /*
327 * Iterate over the fields by names in the input field to avoid
328 * problem if the struct fields are not in the same order after
329 * the debug-info was added.
330 */
331 for (i = 0; i < nb_member_struct; i++) {
332 const bt_field_class_structure_member *member =
333 bt_field_class_structure_borrow_member_by_index_const(
334 in_field_class, i);
335
336 in_member_name =
337 bt_field_class_structure_member_get_name(
338 member);
339 in_member_field =
340 bt_field_structure_borrow_member_field_by_name_const(
341 in_field, in_member_name);
342 out_member_field =
343 bt_field_structure_borrow_member_field_by_name(
344 out_field, in_member_name);
345
346 status = copy_field_content(in_member_field,
347 out_member_field, log_level, self_comp);
348 if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) {
349 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
350 "Cannot copy struct member field: "
351 "out-struct-f-addr=%p, "
352 "out-struct-member-f-addr=%p, "
353 "member-name=\"%s\"",
354 out_field, out_member_field,
355 in_member_name);
356 goto end;
357 }
358 }
359 } else if (bt_field_class_type_is(in_fc_type,
360 BT_FIELD_CLASS_TYPE_ARRAY)) {
361 const bt_field *in_element_field;
362 bt_field *out_element_field;
363 uint64_t i, array_len;
364 bt_field_array_dynamic_set_length_status set_len_status;
365
366 array_len = bt_field_array_get_length(in_field);
367
368 if (bt_field_class_type_is(in_fc_type,
369 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY)) {
370 set_len_status = bt_field_array_dynamic_set_length(
371 out_field, array_len);
372 if (set_len_status !=
373 BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_OK) {
374 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
375 "Cannot set dynamic array field's length field: "
376 "out-arr-f-addr=%p, arr-length=%" PRIu64,
377 out_field, array_len);
378 status = (int) set_len_status;
379 goto end;
380 }
381 }
382
383 for (i = 0; i < array_len; i++) {
384 in_element_field =
385 bt_field_array_borrow_element_field_by_index_const(
386 in_field, i);
387 out_element_field =
388 bt_field_array_borrow_element_field_by_index(
389 out_field, i);
390 status = copy_field_content(in_element_field,
391 out_element_field, log_level, self_comp);
392 if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) {
393 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
394 "Cannot copy element field: "
395 "out-arr-f-addr=%p, out-arr-elem-f-addr=%p",
396 out_field, out_element_field);
397 goto end;
398 }
399 }
400 } else if (bt_field_class_type_is(in_fc_type,
401 BT_FIELD_CLASS_TYPE_OPTION)) {
402 const bt_field *in_option_field;
403 bt_field *out_option_field;
404
405 in_option_field = bt_field_option_borrow_field_const(in_field);
406
407 if (in_option_field) {
408 bt_field_option_set_has_field(out_field, BT_TRUE);
409 out_option_field = bt_field_option_borrow_field(
410 out_field);
411 BT_ASSERT_DBG(out_option_field);
412 status = copy_field_content(in_option_field, out_option_field,
413 log_level, self_comp);
414 if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) {
415 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
416 "Cannot copy option field: "
417 "out-opt-f-addr=%p, out-opt-field-f-addr=%p",
418 out_field, out_option_field);
419 goto end;
420 }
421 } else {
422 bt_field_option_set_has_field(out_field, BT_FALSE);
423 }
424 } else if (bt_field_class_type_is(in_fc_type,
425 BT_FIELD_CLASS_TYPE_VARIANT)) {
426 bt_field_variant_select_option_by_index_status sel_opt_status;
427 uint64_t in_selected_option_idx;
428 const bt_field *in_option_field;
429 bt_field *out_option_field;
430
431 in_selected_option_idx =
432 bt_field_variant_get_selected_option_index(
433 in_field);
434 sel_opt_status = bt_field_variant_select_option_by_index(out_field,
435 in_selected_option_idx);
436 if (sel_opt_status !=
437 BT_FIELD_VARIANT_SELECT_OPTION_STATUS_OK) {
438 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
439 "Cannot select variant field's option field: "
440 "out-var-f-addr=%p, opt-index=%" PRId64,
441 out_field, in_selected_option_idx);
442 status = (int) sel_opt_status;
443 goto end;
444 }
445
446 in_option_field = bt_field_variant_borrow_selected_option_field_const(in_field);
447 out_option_field = bt_field_variant_borrow_selected_option_field(out_field);
448
449 status = copy_field_content(in_option_field, out_option_field,
450 log_level, self_comp);
451 if (status != DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) {
452 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
453 "Cannot copy element field: "
454 "out-var-f-addr=%p, out-opt-f-addr=%p",
455 out_field, out_option_field);
456 goto end;
457 }
458 } else {
459 bt_common_abort();
460 }
461
462 BT_COMP_LOGT("Copied content of field: in-f-addr=%p, out-f-addr=%p",
463 in_field, out_field);
464
465 status = DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK;
466 end:
467 return status;
468 }
This page took 0.03931 seconds and 4 git commands to generate.