lib: strictly type function return status enumerations
[babeltrace.git] / src / plugins / lttng-utils / debug-info / trace-ir-data-copy.c
CommitLineData
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"
91bc8451 29#include "plugins/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
38BT_HIDDEN
3a3d15f3 39void 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
FD
43 const char *trace_name;
44
91bc8451 45 BT_COMP_LOGD("Copying content of trace: in-t-addr=%p, out-t-addr=%p",
ca9f27f3 46 in_trace, out_trace);
ca9f27f3
FD
47 trace_name = bt_trace_get_name(in_trace);
48 /* Copy the trace name. */
49 if (trace_name) {
50 status = bt_trace_set_name(out_trace, trace_name);
d24d5663 51 if (status != BT_TRACE_SET_NAME_STATUS_OK) {
91bc8451 52 BT_COMP_LOGE("Cannot set trace's name: trace-addr=%p, name=\"%s\"",
d24d5663 53 out_trace, trace_name);
ca9f27f3
FD
54 goto end;
55 }
56 }
57
91bc8451 58 BT_COMP_LOGD("Copied content of trace: in-t-addr=%p, out-t-addr=%p",
ca9f27f3
FD
59 in_trace, out_trace);
60end:
61 return;
62}
63
64BT_HIDDEN
3a3d15f3 65void copy_stream_content(const bt_stream *in_stream, bt_stream *out_stream,
91bc8451 66 bt_logging_level log_level, bt_self_component *self_comp)
ca9f27f3 67{
ca9f27f3 68 const char *stream_name;
d24d5663 69 bt_stream_set_name_status status;
ca9f27f3 70
91bc8451 71 BT_COMP_LOGD("Copying content of stream: in-s-addr=%p, out-s-addr=%p",
ca9f27f3
FD
72 in_stream, out_stream);
73
ca9f27f3
FD
74 stream_name = bt_stream_get_name(in_stream);
75 if (stream_name) {
76 status = bt_stream_set_name(out_stream, stream_name);
d24d5663 77 if (status != BT_STREAM_SET_NAME_STATUS_OK) {
91bc8451 78 BT_COMP_LOGE("Cannot set stream's name: stream-addr=%p, "
ca9f27f3
FD
79 "name=%s", out_stream, stream_name);
80 goto end;
81 }
82 }
83
91bc8451 84 BT_COMP_LOGD("Copied content of stream: in-s-addr=%p, out-s-addr=%p",
ca9f27f3
FD
85 in_stream, out_stream);
86end:
87 return;
88}
89
90BT_HIDDEN
3a3d15f3 91void copy_packet_content(const bt_packet *in_packet, bt_packet *out_packet,
91bc8451 92 bt_logging_level log_level, bt_self_component *self_comp)
ca9f27f3
FD
93{
94 const bt_field *in_context_field;
95 bt_field *out_context_field;
96
91bc8451 97 BT_COMP_LOGD("Copying content of packet: in-p-addr=%p, out-p-addr=%p",
ca9f27f3
FD
98 in_packet, out_packet);
99
100 /* Copy context field. */
101 in_context_field = bt_packet_borrow_context_field_const(in_packet);
102 if (in_context_field) {
103 out_context_field = bt_packet_borrow_context_field(out_packet);
104 BT_ASSERT(out_context_field);
3a3d15f3 105 copy_field_content(in_context_field, out_context_field,
91bc8451 106 log_level, self_comp);
ca9f27f3
FD
107 }
108
91bc8451 109 BT_COMP_LOGD("Copied content of packet: in-p-addr=%p, out-p-addr=%p",
ca9f27f3
FD
110 in_packet, out_packet);
111 return;
112}
113
114BT_HIDDEN
3a3d15f3 115void copy_event_content(const bt_event *in_event, bt_event *out_event,
91bc8451 116 bt_logging_level log_level, bt_self_component *self_comp)
ca9f27f3
FD
117{
118 const bt_field *in_common_ctx_field, *in_specific_ctx_field,
119 *in_payload_field;
120 bt_field *out_common_ctx_field, *out_specific_ctx_field,
121 *out_payload_field;
122
91bc8451 123 BT_COMP_LOGD("Copying content of event: in-e-addr=%p, out-e-addr=%p",
ca9f27f3
FD
124 in_event, out_event);
125 in_common_ctx_field =
126 bt_event_borrow_common_context_field_const(in_event);
127 if (in_common_ctx_field) {
128 out_common_ctx_field =
129 bt_event_borrow_common_context_field(out_event);
130 BT_ASSERT(out_common_ctx_field);
131 copy_field_content(in_common_ctx_field,
91bc8451 132 out_common_ctx_field, log_level, self_comp);
ca9f27f3
FD
133 }
134
135 in_specific_ctx_field =
136 bt_event_borrow_specific_context_field_const(in_event);
137 if (in_specific_ctx_field) {
138 out_specific_ctx_field =
139 bt_event_borrow_specific_context_field(out_event);
140 BT_ASSERT(out_specific_ctx_field);
141 copy_field_content(in_specific_ctx_field,
91bc8451 142 out_specific_ctx_field, log_level, self_comp);
ca9f27f3
FD
143 }
144
145 in_payload_field = bt_event_borrow_payload_field_const(in_event);
146 if (in_payload_field) {
147 out_payload_field = bt_event_borrow_payload_field(out_event);
148 BT_ASSERT(out_payload_field);
149 copy_field_content(in_payload_field,
91bc8451 150 out_payload_field, log_level, self_comp);
ca9f27f3
FD
151 }
152
91bc8451 153 BT_COMP_LOGD("Copied content of event: in-e-addr=%p, out-e-addr=%p",
ca9f27f3
FD
154 in_event, out_event);
155}
156
157BT_HIDDEN
3a3d15f3 158void copy_field_content(const bt_field *in_field, bt_field *out_field,
91bc8451 159 bt_logging_level log_level, bt_self_component *self_comp)
ca9f27f3
FD
160{
161 bt_field_class_type in_fc_type, out_fc_type;
162
163 in_fc_type = bt_field_get_class_type(in_field);
164 out_fc_type = bt_field_get_class_type(out_field);
165 BT_ASSERT(in_fc_type == out_fc_type);
166
ef267d12 167 BT_COMP_LOGT("Copying content of field: in-f-addr=%p, out-f-addr=%p",
ca9f27f3
FD
168 in_field, out_field);
169 switch (in_fc_type) {
170 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
171 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
172 bt_field_unsigned_integer_set_value(out_field,
173 bt_field_unsigned_integer_get_value(in_field));
174 break;
175 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
176 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
177 bt_field_signed_integer_set_value(out_field,
178 bt_field_signed_integer_get_value(in_field));
179 break;
180 case BT_FIELD_CLASS_TYPE_REAL:
181 bt_field_real_set_value(out_field,
182 bt_field_real_get_value(in_field));
183 break;
184 case BT_FIELD_CLASS_TYPE_STRING:
185 {
186 const char *str = bt_field_string_get_value(in_field);
d24d5663
PP
187 bt_field_string_set_value_status status =
188 bt_field_string_set_value(out_field, str);
189 if (status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
91bc8451 190 BT_COMP_LOGE("Cannot set string field's value: "
ca9f27f3
FD
191 "str-field-addr=%p, str=%s" PRId64,
192 out_field, str);
193 }
194 break;
195 }
196 case BT_FIELD_CLASS_TYPE_STRUCTURE:
197 {
198 uint64_t i, nb_member_struct;
199 const bt_field *in_member_field;
200 bt_field *out_member_field;
1e6fd1d7 201 const bt_field_class *in_field_class;
ca9f27f3
FD
202 const char *in_member_name;
203
204 in_field_class = bt_field_borrow_class_const(in_field);
205 nb_member_struct = bt_field_class_structure_get_member_count(
206 in_field_class);
207
208 /*
209 * Iterate over the fields by names in the input field to avoid
210 * problem if the struct fields are not in the same order after
211 * the debug-info was added.
212 */
213 for (i = 0; i < nb_member_struct; i++) {
1e6fd1d7
PP
214 const bt_field_class_structure_member *member =
215 bt_field_class_structure_borrow_member_by_index_const(
216 in_field_class, i);
ca9f27f3 217
1e6fd1d7
PP
218 in_member_name =
219 bt_field_class_structure_member_get_name(
220 member);
ca9f27f3
FD
221 in_member_field =
222 bt_field_structure_borrow_member_field_by_name_const(
223 in_field, in_member_name);
224 out_member_field =
225 bt_field_structure_borrow_member_field_by_name(
226 out_field, in_member_name);
227
228 copy_field_content(in_member_field,
91bc8451 229 out_member_field, log_level, self_comp);
ca9f27f3
FD
230 }
231 break;
232 }
233 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
234 /* fall through */
235 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
236 {
237 const bt_field *in_element_field;
238 bt_field *out_element_field;
239 uint64_t i, array_len;
d24d5663 240 bt_field_dynamic_array_set_length_status set_len_status;
ca9f27f3
FD
241
242 array_len = bt_field_array_get_length(in_field);
243
244 if (in_fc_type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY) {
d24d5663
PP
245 set_len_status = bt_field_dynamic_array_set_length(
246 out_field, array_len);
247 if (set_len_status !=
248 BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_OK) {
91bc8451 249 BT_COMP_LOGE("Cannot set dynamic array field's "
ca9f27f3
FD
250 "length field: field-addr=%p, "
251 "length=%" PRIu64, out_field, array_len);
252 }
253 }
254
255 for (i = 0; i < array_len; i++) {
256 in_element_field =
257 bt_field_array_borrow_element_field_by_index_const(
d24d5663 258 in_field, i);
ca9f27f3
FD
259 out_element_field =
260 bt_field_array_borrow_element_field_by_index(
d24d5663 261 out_field, i);
3a3d15f3 262 copy_field_content(in_element_field, out_element_field,
91bc8451 263 log_level, self_comp);
ca9f27f3
FD
264 }
265 break;
266 }
267 case BT_FIELD_CLASS_TYPE_VARIANT:
268 {
d24d5663 269 bt_field_variant_select_option_field_status sel_opt_status;
ca9f27f3
FD
270 uint64_t in_selected_option_idx;
271 const bt_field *in_option_field;
272 bt_field *out_option_field;
273
274 in_selected_option_idx =
275 bt_field_variant_get_selected_option_field_index(
276 in_field);
d24d5663 277 sel_opt_status = bt_field_variant_select_option_field(out_field,
ca9f27f3 278 in_selected_option_idx);
d24d5663
PP
279 if (sel_opt_status !=
280 BT_FIELD_VARIANT_SELECT_OPTION_FIELD_STATUS_OK) {
91bc8451 281 BT_COMP_LOGE("Cannot select variant field's option field: "
ca9f27f3
FD
282 "var-field-addr=%p, opt-index=%" PRId64,
283 out_field, in_selected_option_idx);
284 }
285
286 in_option_field = bt_field_variant_borrow_selected_option_field_const(in_field);
287 out_option_field = bt_field_variant_borrow_selected_option_field(out_field);
288
3a3d15f3 289 copy_field_content(in_option_field, out_option_field,
91bc8451 290 log_level, self_comp);
ca9f27f3
FD
291
292 break;
293 }
294 default:
295 abort();
296 }
ef267d12 297 BT_COMP_LOGT("Copied content of field: in-f-addr=%p, out-f-addr=%p",
ca9f27f3
FD
298 in_field, out_field);
299}
This page took 0.042699 seconds and 4 git commands to generate.