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