Commit | Line | Data |
---|---|---|
58f6d595 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 | ||
475e740e | 26 | #define BT_COMP_LOG_SELF_COMP self_comp |
a07aba4f | 27 | #define BT_LOG_OUTPUT_LEVEL log_level |
b03487ab | 28 | #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-DATA-COPY" |
475e740e | 29 | #include "plugins/comp-logging.h" |
58f6d595 FD |
30 | |
31 | #include <inttypes.h> | |
32 | #include <stdint.h> | |
33 | ||
57952005 | 34 | #include "common/assert.h" |
58f6d595 FD |
35 | |
36 | #include "trace-ir-data-copy.h" | |
37 | ||
38 | BT_HIDDEN | |
a07aba4f | 39 | void copy_trace_content(const bt_trace *in_trace, bt_trace *out_trace, |
475e740e | 40 | bt_logging_level log_level, bt_self_component *self_comp) |
58f6d595 | 41 | { |
fb25b9e3 | 42 | bt_trace_set_name_status status; |
58f6d595 | 43 | const char *trace_name; |
cd03c43c | 44 | uint64_t i, env_field_count; |
58f6d595 | 45 | |
475e740e | 46 | BT_COMP_LOGD("Copying content of trace: in-t-addr=%p, out-t-addr=%p", |
58f6d595 | 47 | in_trace, out_trace); |
58f6d595 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); | |
fb25b9e3 | 52 | if (status != BT_TRACE_SET_NAME_STATUS_OK) { |
475e740e | 53 | BT_COMP_LOGE("Cannot set trace's name: trace-addr=%p, name=\"%s\"", |
fb25b9e3 | 54 | out_trace, trace_name); |
0fb60f5d | 55 | bt_current_thread_clear_error(); |
58f6d595 FD |
56 | goto end; |
57 | } | |
58 | } | |
59 | ||
cd03c43c PP |
60 | /* |
61 | * Do not copy the trace UUID as it may be modified and should | |
62 | * no longer have the same UUID. | |
63 | */ | |
64 | ||
65 | /* | |
66 | * Go over all the entries in the environment section of the | |
67 | * trace and copy the content to the new trace. | |
68 | */ | |
69 | env_field_count = bt_trace_get_environment_entry_count(in_trace); | |
70 | for (i = 0; i < env_field_count; i++) { | |
71 | const char *value_name; | |
72 | const bt_value *value = NULL; | |
73 | bt_trace_set_environment_entry_status set_env_status; | |
74 | ||
75 | bt_trace_borrow_environment_entry_by_index_const( | |
76 | in_trace, i, &value_name, &value); | |
77 | ||
78 | BT_COMP_LOGD("Copying trace environnement entry: " | |
79 | "index=%" PRId64 ", value-addr=%p, value-name=%s", | |
80 | i, value, value_name); | |
81 | ||
82 | BT_ASSERT(value_name); | |
83 | BT_ASSERT(value); | |
84 | ||
85 | if (bt_value_is_signed_integer(value)) { | |
86 | set_env_status = | |
87 | bt_trace_set_environment_entry_integer( | |
88 | out_trace, value_name, | |
89 | bt_value_signed_integer_get( | |
90 | value)); | |
91 | } else if (bt_value_is_string(value)) { | |
92 | set_env_status = | |
93 | bt_trace_set_environment_entry_string( | |
94 | out_trace, value_name, | |
95 | bt_value_string_get(value)); | |
96 | } else { | |
97 | abort(); | |
98 | } | |
99 | ||
100 | if (set_env_status != | |
101 | BT_TRACE_SET_ENVIRONMENT_ENTRY_STATUS_OK) { | |
102 | BT_COMP_LOGE("Cannot copy trace's environment: " | |
103 | "trace-addr=%p, name=\"%s\"", | |
104 | out_trace, trace_name); | |
105 | bt_current_thread_clear_error(); | |
106 | goto end; | |
107 | } | |
108 | } | |
109 | ||
475e740e | 110 | BT_COMP_LOGD("Copied content of trace: in-t-addr=%p, out-t-addr=%p", |
58f6d595 FD |
111 | in_trace, out_trace); |
112 | end: | |
113 | return; | |
114 | } | |
115 | ||
116 | BT_HIDDEN | |
a07aba4f | 117 | void copy_stream_content(const bt_stream *in_stream, bt_stream *out_stream, |
475e740e | 118 | bt_logging_level log_level, bt_self_component *self_comp) |
58f6d595 | 119 | { |
58f6d595 | 120 | const char *stream_name; |
fb25b9e3 | 121 | bt_stream_set_name_status status; |
58f6d595 | 122 | |
475e740e | 123 | BT_COMP_LOGD("Copying content of stream: in-s-addr=%p, out-s-addr=%p", |
58f6d595 FD |
124 | in_stream, out_stream); |
125 | ||
58f6d595 FD |
126 | stream_name = bt_stream_get_name(in_stream); |
127 | if (stream_name) { | |
128 | status = bt_stream_set_name(out_stream, stream_name); | |
fb25b9e3 | 129 | if (status != BT_STREAM_SET_NAME_STATUS_OK) { |
475e740e | 130 | BT_COMP_LOGE("Cannot set stream's name: stream-addr=%p, " |
58f6d595 FD |
131 | "name=%s", out_stream, stream_name); |
132 | goto end; | |
133 | } | |
134 | } | |
135 | ||
475e740e | 136 | BT_COMP_LOGD("Copied content of stream: in-s-addr=%p, out-s-addr=%p", |
58f6d595 FD |
137 | in_stream, out_stream); |
138 | end: | |
139 | return; | |
140 | } | |
141 | ||
142 | BT_HIDDEN | |
a07aba4f | 143 | void copy_packet_content(const bt_packet *in_packet, bt_packet *out_packet, |
475e740e | 144 | bt_logging_level log_level, bt_self_component *self_comp) |
58f6d595 FD |
145 | { |
146 | const bt_field *in_context_field; | |
147 | bt_field *out_context_field; | |
148 | ||
475e740e | 149 | BT_COMP_LOGD("Copying content of packet: in-p-addr=%p, out-p-addr=%p", |
58f6d595 FD |
150 | in_packet, out_packet); |
151 | ||
152 | /* Copy context field. */ | |
153 | in_context_field = bt_packet_borrow_context_field_const(in_packet); | |
154 | if (in_context_field) { | |
155 | out_context_field = bt_packet_borrow_context_field(out_packet); | |
156 | BT_ASSERT(out_context_field); | |
a07aba4f | 157 | copy_field_content(in_context_field, out_context_field, |
475e740e | 158 | log_level, self_comp); |
58f6d595 FD |
159 | } |
160 | ||
475e740e | 161 | BT_COMP_LOGD("Copied content of packet: in-p-addr=%p, out-p-addr=%p", |
58f6d595 FD |
162 | in_packet, out_packet); |
163 | return; | |
164 | } | |
165 | ||
166 | BT_HIDDEN | |
a07aba4f | 167 | void copy_event_content(const bt_event *in_event, bt_event *out_event, |
475e740e | 168 | bt_logging_level log_level, bt_self_component *self_comp) |
58f6d595 FD |
169 | { |
170 | const bt_field *in_common_ctx_field, *in_specific_ctx_field, | |
171 | *in_payload_field; | |
172 | bt_field *out_common_ctx_field, *out_specific_ctx_field, | |
173 | *out_payload_field; | |
174 | ||
475e740e | 175 | BT_COMP_LOGD("Copying content of event: in-e-addr=%p, out-e-addr=%p", |
58f6d595 FD |
176 | in_event, out_event); |
177 | in_common_ctx_field = | |
178 | bt_event_borrow_common_context_field_const(in_event); | |
179 | if (in_common_ctx_field) { | |
180 | out_common_ctx_field = | |
181 | bt_event_borrow_common_context_field(out_event); | |
182 | BT_ASSERT(out_common_ctx_field); | |
183 | copy_field_content(in_common_ctx_field, | |
475e740e | 184 | out_common_ctx_field, log_level, self_comp); |
58f6d595 FD |
185 | } |
186 | ||
187 | in_specific_ctx_field = | |
188 | bt_event_borrow_specific_context_field_const(in_event); | |
189 | if (in_specific_ctx_field) { | |
190 | out_specific_ctx_field = | |
191 | bt_event_borrow_specific_context_field(out_event); | |
192 | BT_ASSERT(out_specific_ctx_field); | |
193 | copy_field_content(in_specific_ctx_field, | |
475e740e | 194 | out_specific_ctx_field, log_level, self_comp); |
58f6d595 FD |
195 | } |
196 | ||
197 | in_payload_field = bt_event_borrow_payload_field_const(in_event); | |
198 | if (in_payload_field) { | |
199 | out_payload_field = bt_event_borrow_payload_field(out_event); | |
200 | BT_ASSERT(out_payload_field); | |
201 | copy_field_content(in_payload_field, | |
475e740e | 202 | out_payload_field, log_level, self_comp); |
58f6d595 FD |
203 | } |
204 | ||
475e740e | 205 | BT_COMP_LOGD("Copied content of event: in-e-addr=%p, out-e-addr=%p", |
58f6d595 FD |
206 | in_event, out_event); |
207 | } | |
208 | ||
209 | BT_HIDDEN | |
a07aba4f | 210 | void copy_field_content(const bt_field *in_field, bt_field *out_field, |
475e740e | 211 | bt_logging_level log_level, bt_self_component *self_comp) |
58f6d595 FD |
212 | { |
213 | bt_field_class_type in_fc_type, out_fc_type; | |
214 | ||
215 | in_fc_type = bt_field_get_class_type(in_field); | |
216 | out_fc_type = bt_field_get_class_type(out_field); | |
217 | BT_ASSERT(in_fc_type == out_fc_type); | |
218 | ||
c9ecaa78 | 219 | BT_COMP_LOGT("Copying content of field: in-f-addr=%p, out-f-addr=%p", |
58f6d595 FD |
220 | in_field, out_field); |
221 | switch (in_fc_type) { | |
222 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: | |
223 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
224 | bt_field_unsigned_integer_set_value(out_field, | |
225 | bt_field_unsigned_integer_get_value(in_field)); | |
226 | break; | |
227 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
228 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
229 | bt_field_signed_integer_set_value(out_field, | |
230 | bt_field_signed_integer_get_value(in_field)); | |
231 | break; | |
232 | case BT_FIELD_CLASS_TYPE_REAL: | |
233 | bt_field_real_set_value(out_field, | |
234 | bt_field_real_get_value(in_field)); | |
235 | break; | |
236 | case BT_FIELD_CLASS_TYPE_STRING: | |
237 | { | |
238 | const char *str = bt_field_string_get_value(in_field); | |
fb25b9e3 PP |
239 | bt_field_string_set_value_status status = |
240 | bt_field_string_set_value(out_field, str); | |
241 | if (status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) { | |
475e740e | 242 | BT_COMP_LOGE("Cannot set string field's value: " |
58f6d595 FD |
243 | "str-field-addr=%p, str=%s" PRId64, |
244 | out_field, str); | |
0fb60f5d PP |
245 | bt_current_thread_clear_error(); |
246 | ||
58f6d595 FD |
247 | } |
248 | break; | |
249 | } | |
250 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
251 | { | |
252 | uint64_t i, nb_member_struct; | |
253 | const bt_field *in_member_field; | |
254 | bt_field *out_member_field; | |
e24f271a | 255 | const bt_field_class *in_field_class; |
58f6d595 FD |
256 | const char *in_member_name; |
257 | ||
258 | in_field_class = bt_field_borrow_class_const(in_field); | |
259 | nb_member_struct = bt_field_class_structure_get_member_count( | |
260 | in_field_class); | |
261 | ||
262 | /* | |
263 | * Iterate over the fields by names in the input field to avoid | |
264 | * problem if the struct fields are not in the same order after | |
265 | * the debug-info was added. | |
266 | */ | |
267 | for (i = 0; i < nb_member_struct; i++) { | |
e24f271a PP |
268 | const bt_field_class_structure_member *member = |
269 | bt_field_class_structure_borrow_member_by_index_const( | |
270 | in_field_class, i); | |
58f6d595 | 271 | |
e24f271a PP |
272 | in_member_name = |
273 | bt_field_class_structure_member_get_name( | |
274 | member); | |
58f6d595 FD |
275 | in_member_field = |
276 | bt_field_structure_borrow_member_field_by_name_const( | |
277 | in_field, in_member_name); | |
278 | out_member_field = | |
279 | bt_field_structure_borrow_member_field_by_name( | |
280 | out_field, in_member_name); | |
281 | ||
282 | copy_field_content(in_member_field, | |
475e740e | 283 | out_member_field, log_level, self_comp); |
58f6d595 FD |
284 | } |
285 | break; | |
286 | } | |
287 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
288 | /* fall through */ | |
289 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
290 | { | |
291 | const bt_field *in_element_field; | |
292 | bt_field *out_element_field; | |
293 | uint64_t i, array_len; | |
fb25b9e3 | 294 | bt_field_dynamic_array_set_length_status set_len_status; |
58f6d595 FD |
295 | |
296 | array_len = bt_field_array_get_length(in_field); | |
297 | ||
298 | if (in_fc_type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY) { | |
fb25b9e3 PP |
299 | set_len_status = bt_field_dynamic_array_set_length( |
300 | out_field, array_len); | |
301 | if (set_len_status != | |
302 | BT_FIELD_DYNAMIC_ARRAY_SET_LENGTH_STATUS_OK) { | |
475e740e | 303 | BT_COMP_LOGE("Cannot set dynamic array field's " |
58f6d595 FD |
304 | "length field: field-addr=%p, " |
305 | "length=%" PRIu64, out_field, array_len); | |
0fb60f5d | 306 | bt_current_thread_clear_error(); |
58f6d595 FD |
307 | } |
308 | } | |
309 | ||
310 | for (i = 0; i < array_len; i++) { | |
311 | in_element_field = | |
312 | bt_field_array_borrow_element_field_by_index_const( | |
fb25b9e3 | 313 | in_field, i); |
58f6d595 FD |
314 | out_element_field = |
315 | bt_field_array_borrow_element_field_by_index( | |
fb25b9e3 | 316 | out_field, i); |
a07aba4f | 317 | copy_field_content(in_element_field, out_element_field, |
475e740e | 318 | log_level, self_comp); |
58f6d595 FD |
319 | } |
320 | break; | |
321 | } | |
322 | case BT_FIELD_CLASS_TYPE_VARIANT: | |
323 | { | |
fb25b9e3 | 324 | bt_field_variant_select_option_field_status sel_opt_status; |
58f6d595 FD |
325 | uint64_t in_selected_option_idx; |
326 | const bt_field *in_option_field; | |
327 | bt_field *out_option_field; | |
328 | ||
329 | in_selected_option_idx = | |
330 | bt_field_variant_get_selected_option_field_index( | |
331 | in_field); | |
fb25b9e3 | 332 | sel_opt_status = bt_field_variant_select_option_field(out_field, |
58f6d595 | 333 | in_selected_option_idx); |
fb25b9e3 PP |
334 | if (sel_opt_status != |
335 | BT_FIELD_VARIANT_SELECT_OPTION_FIELD_STATUS_OK) { | |
475e740e | 336 | BT_COMP_LOGE("Cannot select variant field's option field: " |
58f6d595 FD |
337 | "var-field-addr=%p, opt-index=%" PRId64, |
338 | out_field, in_selected_option_idx); | |
0fb60f5d | 339 | bt_current_thread_clear_error(); |
58f6d595 FD |
340 | } |
341 | ||
342 | in_option_field = bt_field_variant_borrow_selected_option_field_const(in_field); | |
343 | out_option_field = bt_field_variant_borrow_selected_option_field(out_field); | |
344 | ||
a07aba4f | 345 | copy_field_content(in_option_field, out_option_field, |
475e740e | 346 | log_level, self_comp); |
58f6d595 FD |
347 | |
348 | break; | |
349 | } | |
350 | default: | |
351 | abort(); | |
352 | } | |
c9ecaa78 | 353 | BT_COMP_LOGT("Copied content of field: in-f-addr=%p, out-f-addr=%p", |
58f6d595 FD |
354 | in_field, out_field); |
355 | } |