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