Commit | Line | Data |
---|---|---|
ca9f27f3 FD |
1 | /* |
2 | * Babeltrace - Trace IR field copy | |
3 | * | |
4 | * Copyright (c) 2015-2019 EfficiOS Inc. and Linux Foundation | |
5 | * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com> | |
6 | * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com> | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | * SOFTWARE. | |
25 | */ | |
26 | ||
91bc8451 | 27 | #define BT_COMP_LOG_SELF_COMP (md_maps->self_comp) |
3a3d15f3 | 28 | #define BT_LOG_OUTPUT_LEVEL (md_maps->log_level) |
350ad6c1 | 29 | #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-META-FC-COPY" |
91bc8451 | 30 | #include "plugins/comp-logging.h" |
ca9f27f3 | 31 | |
578e048b MJ |
32 | #include "common/assert.h" |
33 | #include "common/common.h" | |
34 | #include "compat/compiler.h" | |
3fadfbc0 | 35 | #include <babeltrace2/babeltrace.h> |
ca9f27f3 FD |
36 | |
37 | #include "trace-ir-metadata-copy.h" | |
38 | #include "trace-ir-metadata-field-class-copy.h" | |
39 | ||
40 | /* | |
41 | * This fonction walks througth the nested structures field class to resolve a | |
42 | * field path object. A field path is made of indexes inside possibly nested | |
43 | * structures ultimately leading to a field class. | |
44 | */ | |
45 | static | |
3a3d15f3 PP |
46 | const bt_field_class *walk_field_path(struct trace_ir_metadata_maps *md_maps, |
47 | const bt_field_path *fp, const bt_field_class *fc) | |
ca9f27f3 | 48 | { |
66ddcddf | 49 | uint64_t i, fp_item_count; |
ca9f27f3 FD |
50 | const bt_field_class *curr_fc; |
51 | ||
52 | BT_ASSERT(bt_field_class_get_type(fc) == BT_FIELD_CLASS_TYPE_STRUCTURE); | |
91bc8451 | 53 | BT_COMP_LOGD("Walking field path on field class: fp-addr=%p, fc-addr=%p", |
ca9f27f3 FD |
54 | fp, fc); |
55 | ||
66ddcddf | 56 | fp_item_count = bt_field_path_get_item_count(fp); |
ca9f27f3 | 57 | curr_fc = fc; |
66ddcddf | 58 | for (i = 0; i < fp_item_count; i++) { |
ca9f27f3 | 59 | bt_field_class_type fc_type = bt_field_class_get_type(curr_fc); |
66ddcddf PP |
60 | const bt_field_path_item *fp_item = |
61 | bt_field_path_borrow_item_by_index_const(fp, i); | |
ca9f27f3 FD |
62 | |
63 | switch (fc_type) { | |
64 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
1e6fd1d7 | 65 | { |
66ddcddf PP |
66 | const bt_field_class_structure_member *member; |
67 | ||
68 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == | |
69 | BT_FIELD_PATH_ITEM_TYPE_INDEX); | |
70 | member = bt_field_class_structure_borrow_member_by_index_const( | |
71 | curr_fc, | |
72 | bt_field_path_item_index_get_index(fp_item)); | |
1e6fd1d7 PP |
73 | curr_fc = bt_field_class_structure_member_borrow_field_class_const( |
74 | member); | |
ca9f27f3 | 75 | break; |
1e6fd1d7 | 76 | } |
ca9f27f3 | 77 | case BT_FIELD_CLASS_TYPE_VARIANT: |
1e6fd1d7 | 78 | { |
66ddcddf PP |
79 | const bt_field_class_variant_option *option; |
80 | ||
81 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == | |
82 | BT_FIELD_PATH_ITEM_TYPE_INDEX); | |
83 | option = bt_field_class_variant_borrow_option_by_index_const( | |
84 | curr_fc, | |
85 | bt_field_path_item_index_get_index(fp_item)); | |
1e6fd1d7 PP |
86 | curr_fc = bt_field_class_variant_option_borrow_field_class_const( |
87 | option); | |
ca9f27f3 | 88 | break; |
1e6fd1d7 | 89 | } |
66ddcddf PP |
90 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
91 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
92 | { | |
93 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == | |
94 | BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT); | |
95 | curr_fc = bt_field_class_array_borrow_element_field_class_const( | |
96 | curr_fc); | |
97 | break; | |
98 | } | |
ca9f27f3 FD |
99 | default: |
100 | abort(); | |
101 | } | |
102 | } | |
103 | ||
104 | return curr_fc; | |
105 | } | |
106 | ||
107 | static | |
108 | const bt_field_class *resolve_field_path_to_field_class(const bt_field_path *fp, | |
109 | struct trace_ir_metadata_maps *md_maps) | |
110 | { | |
111 | struct field_class_resolving_context *fc_resolving_ctx; | |
112 | const bt_field_class *fc; | |
113 | bt_scope fp_scope; | |
114 | ||
91bc8451 | 115 | BT_COMP_LOGD("Resolving field path: fp-addr=%p", fp); |
ca9f27f3 FD |
116 | |
117 | fc_resolving_ctx = md_maps->fc_resolving_ctx; | |
118 | fp_scope = bt_field_path_get_root_scope(fp); | |
119 | ||
120 | switch (fp_scope) { | |
121 | case BT_SCOPE_PACKET_CONTEXT: | |
3a3d15f3 PP |
122 | fc = walk_field_path(md_maps, fp, |
123 | fc_resolving_ctx->packet_context); | |
ca9f27f3 FD |
124 | break; |
125 | case BT_SCOPE_EVENT_COMMON_CONTEXT: | |
3a3d15f3 PP |
126 | fc = walk_field_path(md_maps, fp, |
127 | fc_resolving_ctx->event_common_context); | |
ca9f27f3 FD |
128 | break; |
129 | case BT_SCOPE_EVENT_SPECIFIC_CONTEXT: | |
3a3d15f3 PP |
130 | fc = walk_field_path(md_maps, fp, |
131 | fc_resolving_ctx->event_specific_context); | |
ca9f27f3 FD |
132 | break; |
133 | case BT_SCOPE_EVENT_PAYLOAD: | |
3a3d15f3 PP |
134 | fc = walk_field_path(md_maps, fp, |
135 | fc_resolving_ctx->event_payload); | |
ca9f27f3 FD |
136 | break; |
137 | default: | |
138 | abort(); | |
139 | } | |
140 | ||
141 | return fc; | |
142 | } | |
143 | ||
144 | static inline | |
145 | void field_class_integer_set_props(const bt_field_class *input_fc, | |
146 | bt_field_class *output_fc) | |
147 | { | |
148 | bt_field_class_integer_set_preferred_display_base(output_fc, | |
149 | bt_field_class_integer_get_preferred_display_base(input_fc)); | |
150 | bt_field_class_integer_set_field_value_range(output_fc, | |
151 | bt_field_class_integer_get_field_value_range(input_fc)); | |
152 | } | |
153 | ||
154 | static inline | |
155 | int field_class_unsigned_integer_copy( | |
156 | struct trace_ir_metadata_maps *md_maps, | |
157 | const bt_field_class *in_field_class, | |
158 | bt_field_class *out_field_class) | |
159 | { | |
91bc8451 | 160 | BT_COMP_LOGD("Copying content of unsigned integer field class: " |
ca9f27f3 FD |
161 | "in-fc-addr=%p, out-fc-addr=%p", |
162 | in_field_class, out_field_class); | |
163 | ||
164 | field_class_integer_set_props(in_field_class, out_field_class); | |
165 | ||
91bc8451 | 166 | BT_COMP_LOGD("Copied content of unsigned integer field class: " |
ca9f27f3 FD |
167 | "in-fc-addr=%p, out-fc-addr=%p", |
168 | in_field_class, out_field_class); | |
169 | return 0; | |
170 | } | |
171 | ||
172 | static inline | |
173 | int field_class_signed_integer_copy( | |
174 | struct trace_ir_metadata_maps *md_maps, | |
175 | const bt_field_class *in_field_class, | |
176 | bt_field_class *out_field_class) | |
177 | { | |
91bc8451 | 178 | BT_COMP_LOGD("Copying content of signed integer field class: " |
ca9f27f3 FD |
179 | "in-fc-addr=%p, out-fc-addr=%p", |
180 | in_field_class, out_field_class); | |
181 | ||
182 | field_class_integer_set_props(in_field_class, out_field_class); | |
183 | ||
91bc8451 | 184 | BT_COMP_LOGD("Copied content of signed integer field class: " |
ca9f27f3 FD |
185 | "in-fc-addr=%p, out-fc-addr=%p", |
186 | in_field_class, out_field_class); | |
187 | return 0; | |
188 | } | |
189 | ||
190 | BT_HIDDEN | |
191 | int field_class_unsigned_enumeration_copy( | |
192 | struct trace_ir_metadata_maps *md_maps, | |
193 | const bt_field_class *in_field_class, | |
194 | bt_field_class *out_field_class) | |
195 | { | |
196 | uint64_t i, enum_mapping_count; | |
197 | int ret = 0; | |
198 | ||
91bc8451 | 199 | BT_COMP_LOGD("Copying content of unsigned enumeration field class: " |
ca9f27f3 FD |
200 | "in-fc-addr=%p, out-fc-addr=%p", |
201 | in_field_class, out_field_class); | |
202 | ||
203 | /* Copy properties of the inner integer. */ | |
204 | field_class_integer_set_props(in_field_class, out_field_class); | |
205 | ||
206 | /* Copy all enumeration entries. */ | |
207 | enum_mapping_count = bt_field_class_enumeration_get_mapping_count(in_field_class); | |
208 | for (i = 0; i < enum_mapping_count; i++) { | |
209 | const char *label; | |
8f3ccfbc PP |
210 | const bt_field_class_unsigned_enumeration_mapping *u_mapping; |
211 | const bt_field_class_enumeration_mapping *mapping; | |
ca9f27f3 FD |
212 | uint64_t range_index, range_count; |
213 | ||
214 | /* Get the ranges and the range count. */ | |
8f3ccfbc PP |
215 | u_mapping = bt_field_class_unsigned_enumeration_borrow_mapping_by_index_const( |
216 | in_field_class, i); | |
217 | mapping = bt_field_class_unsigned_enumeration_mapping_as_mapping_const( | |
218 | u_mapping); | |
ca9f27f3 | 219 | range_count = |
8f3ccfbc PP |
220 | bt_field_class_enumeration_mapping_get_range_count( |
221 | mapping); | |
222 | label = bt_field_class_enumeration_mapping_get_label( | |
223 | mapping); | |
224 | ||
ca9f27f3 FD |
225 | /* |
226 | * Iterate over all the ranges to add them to copied field | |
227 | * class. | |
228 | */ | |
229 | for (range_index = 0; range_index < range_count; range_index++) { | |
230 | uint64_t lower, upper; | |
8f3ccfbc PP |
231 | bt_field_class_unsigned_enumeration_mapping_get_range_by_index( |
232 | u_mapping, range_index, &lower, &upper); | |
ca9f27f3 | 233 | |
91bc8451 | 234 | BT_COMP_LOGD("Copying range in enumeration field class: " |
ca9f27f3 FD |
235 | "label=%s, lower=%"PRId64", upper=%"PRId64, |
236 | label, lower, upper); | |
237 | ||
238 | /* Add the label and its range to the copy field class. */ | |
d24d5663 PP |
239 | if (bt_field_class_unsigned_enumeration_map_range( |
240 | out_field_class, label, lower, upper) != | |
241 | BT_FIELD_CLASS_ENUMERATION_MAP_RANGE_STATUS_OK) { | |
91bc8451 | 242 | BT_COMP_LOGE_STR("Failed to add range to unsigned " |
ca9f27f3 FD |
243 | "enumeration."); |
244 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_field_class); | |
245 | ret = -1; | |
246 | goto error; | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
91bc8451 | 251 | BT_COMP_LOGD("Copied content of unsigned enumeration field class: " |
ca9f27f3 FD |
252 | "in-fc-addr=%p, out-fc-addr=%p", |
253 | in_field_class, out_field_class); | |
254 | ||
255 | error: | |
256 | return ret; | |
257 | } | |
258 | ||
259 | static inline | |
260 | int field_class_signed_enumeration_copy( | |
261 | struct trace_ir_metadata_maps *md_maps, | |
262 | const bt_field_class *in_field_class, | |
263 | bt_field_class *out_field_class) | |
264 | { | |
265 | uint64_t i, enum_mapping_count; | |
266 | int ret = 0; | |
267 | ||
91bc8451 | 268 | BT_COMP_LOGD("Copying content of signed enumeration field class: " |
ca9f27f3 FD |
269 | "in-fc-addr=%p, out-fc-addr=%p", |
270 | in_field_class, out_field_class); | |
271 | ||
272 | /* Copy properties of the inner integer. */ | |
273 | field_class_integer_set_props(in_field_class, out_field_class); | |
274 | ||
275 | /* Copy all enumeration entries. */ | |
276 | enum_mapping_count = | |
277 | bt_field_class_enumeration_get_mapping_count(in_field_class); | |
278 | for (i = 0; i < enum_mapping_count; i++) { | |
279 | const char *label; | |
8f3ccfbc PP |
280 | const bt_field_class_signed_enumeration_mapping *i_mapping; |
281 | const bt_field_class_enumeration_mapping *mapping; | |
ca9f27f3 FD |
282 | uint64_t range_index, range_count; |
283 | ||
284 | /* Get the ranges and the range count. */ | |
8f3ccfbc PP |
285 | i_mapping = bt_field_class_signed_enumeration_borrow_mapping_by_index_const( |
286 | in_field_class, i); | |
287 | mapping = bt_field_class_signed_enumeration_mapping_as_mapping_const( | |
288 | i_mapping); | |
ca9f27f3 | 289 | range_count = |
8f3ccfbc PP |
290 | bt_field_class_enumeration_mapping_get_range_count( |
291 | mapping); | |
292 | label = bt_field_class_enumeration_mapping_get_label( | |
293 | mapping); | |
294 | ||
ca9f27f3 FD |
295 | /* |
296 | * Iterate over all the ranges to add them to copied field | |
297 | * class. | |
298 | */ | |
299 | for (range_index = 0; range_index < range_count; range_index++) { | |
300 | int64_t lower, upper; | |
8f3ccfbc PP |
301 | bt_field_class_signed_enumeration_mapping_get_range_by_index( |
302 | i_mapping, range_index, &lower, &upper); | |
ca9f27f3 | 303 | |
91bc8451 | 304 | BT_COMP_LOGD("Copying range in enumeration field class: " |
fca9fd7d | 305 | "label=%s, lower=%"PRId64", upper=%"PRId64, |
ca9f27f3 FD |
306 | label, lower, upper); |
307 | ||
308 | /* Add the label and its range to the copy field class. */ | |
d24d5663 PP |
309 | if (bt_field_class_signed_enumeration_map_range( |
310 | out_field_class, label, lower, upper) != | |
311 | BT_FIELD_CLASS_ENUMERATION_MAP_RANGE_STATUS_OK) { | |
91bc8451 | 312 | BT_COMP_LOGE_STR("Failed to add range to signed " |
ca9f27f3 FD |
313 | "enumeration."); |
314 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_field_class); | |
315 | ret = -1; | |
316 | goto error; | |
317 | } | |
318 | } | |
319 | } | |
320 | ||
91bc8451 | 321 | BT_COMP_LOGD("Copied content of signed enumeration field class: " |
ca9f27f3 FD |
322 | "in-fc-addr=%p, out-fc-addr=%p", |
323 | in_field_class, out_field_class); | |
324 | ||
325 | error: | |
326 | return ret; | |
327 | } | |
328 | ||
329 | static inline | |
330 | int field_class_real_copy( | |
331 | struct trace_ir_metadata_maps *md_maps, | |
332 | const bt_field_class *in_field_class, | |
333 | bt_field_class *out_field_class) | |
334 | { | |
91bc8451 | 335 | BT_COMP_LOGD("Copying content of real field class: " |
ca9f27f3 FD |
336 | "in-fc-addr=%p, out-fc-addr=%p", |
337 | in_field_class, out_field_class); | |
338 | ||
339 | bt_field_class_real_set_is_single_precision(out_field_class, | |
340 | bt_field_class_real_is_single_precision(in_field_class)); | |
341 | ||
91bc8451 | 342 | BT_COMP_LOGD("Copied content real field class: in-fc-addr=%p, " |
ca9f27f3 FD |
343 | "out-fc-addr=%p", in_field_class, out_field_class); |
344 | ||
345 | return 0; | |
346 | } | |
347 | ||
348 | static inline | |
349 | int field_class_structure_copy( | |
350 | struct trace_ir_metadata_maps *md_maps, | |
351 | const bt_field_class *in_field_class, | |
352 | bt_field_class *out_field_class) | |
353 | { | |
354 | uint64_t i, struct_member_count; | |
ca9f27f3 FD |
355 | int ret = 0; |
356 | ||
91bc8451 | 357 | BT_COMP_LOGD("Copying content of structure field class: " |
ca9f27f3 FD |
358 | "in-fc-addr=%p, out-fc-addr=%p", |
359 | in_field_class, out_field_class); | |
360 | /* Get the number of member in that struct. */ | |
361 | struct_member_count = | |
362 | bt_field_class_structure_get_member_count(in_field_class); | |
363 | ||
364 | /* Iterate over all the members of the struct. */ | |
365 | for (i = 0; i < struct_member_count; i++) { | |
1e6fd1d7 | 366 | const bt_field_class_structure_member *member; |
ca9f27f3 FD |
367 | const char *member_name; |
368 | const bt_field_class *member_fc; | |
369 | bt_field_class *out_member_field_class; | |
370 | ||
1e6fd1d7 PP |
371 | member = bt_field_class_structure_borrow_member_by_index_const( |
372 | in_field_class, i); | |
373 | member_fc = bt_field_class_structure_member_borrow_field_class_const( | |
374 | member); | |
375 | member_name = bt_field_class_structure_member_get_name(member); | |
91bc8451 | 376 | BT_COMP_LOGD("Copying structure field class's field: " |
ca9f27f3 FD |
377 | "index=%" PRId64 ", " |
378 | "member-fc-addr=%p, field-name=\"%s\"", | |
379 | i, member_fc, member_name); | |
380 | ||
381 | out_member_field_class = create_field_class_copy(md_maps, | |
382 | member_fc); | |
383 | if (!out_member_field_class) { | |
91bc8451 | 384 | BT_COMP_LOGE("Cannot copy structure field class's field: " |
ca9f27f3 FD |
385 | "index=%" PRId64 ", " |
386 | "field-fc-addr=%p, field-name=\"%s\"", | |
387 | i, member_fc, member_name); | |
388 | ret = -1; | |
389 | goto error; | |
390 | } | |
391 | ret = copy_field_class_content(md_maps, member_fc, | |
392 | out_member_field_class); | |
393 | if (ret) { | |
394 | goto error; | |
395 | } | |
396 | ||
d24d5663 PP |
397 | if (bt_field_class_structure_append_member(out_field_class, |
398 | member_name, out_member_field_class) != | |
399 | BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK) { | |
91bc8451 | 400 | BT_COMP_LOGE("Cannot append structure field class's field: " |
ca9f27f3 FD |
401 | "index=%" PRId64 ", " |
402 | "field-fc-addr=%p, field-name=\"%s\"", | |
403 | i, member_fc, member_name); | |
404 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_member_field_class); | |
405 | ret = -1; | |
406 | goto error; | |
407 | } | |
408 | } | |
409 | ||
91bc8451 | 410 | BT_COMP_LOGD("Copied structure field class: original-fc-addr=%p, copy-fc-addr=%p", |
ca9f27f3 FD |
411 | in_field_class, out_field_class); |
412 | ||
413 | error: | |
414 | return ret; | |
415 | } | |
416 | ||
417 | static inline | |
418 | int field_class_variant_copy( | |
419 | struct trace_ir_metadata_maps *md_maps, | |
420 | const bt_field_class *in_field_class, | |
421 | bt_field_class *out_field_class) | |
422 | { | |
1ec009b2 | 423 | bt_field_class *out_tag_field_class = NULL; |
ca9f27f3 FD |
424 | uint64_t i, variant_option_count; |
425 | const bt_field_path *tag_fp; | |
426 | const bt_field_class *tag_fc; | |
427 | int ret = 0; | |
428 | ||
91bc8451 | 429 | BT_COMP_LOGD("Copying content of variant field class: " |
ca9f27f3 FD |
430 | "in-fc-addr=%p, out-fc-addr=%p", |
431 | in_field_class, out_field_class); | |
432 | ||
433 | tag_fp = bt_field_class_variant_borrow_selector_field_path_const( | |
434 | in_field_class); | |
435 | if (tag_fp) { | |
436 | tag_fc = resolve_field_path_to_field_class(tag_fp, | |
437 | md_maps); | |
438 | ||
439 | out_tag_field_class = g_hash_table_lookup( | |
440 | md_maps->field_class_map, tag_fc); | |
441 | if (!out_tag_field_class) { | |
91bc8451 | 442 | BT_COMP_LOGE_STR("Cannot find the tag field class."); |
ca9f27f3 FD |
443 | ret = -1; |
444 | goto error; | |
445 | } | |
446 | bt_field_class_variant_set_selector_field_class(out_field_class, | |
447 | out_tag_field_class); | |
448 | } | |
449 | ||
450 | variant_option_count = | |
451 | bt_field_class_variant_get_option_count(in_field_class); | |
452 | for (i = 0; i < variant_option_count; i++) { | |
1e6fd1d7 | 453 | const bt_field_class *option_fc; |
ca9f27f3 FD |
454 | const char *option_name; |
455 | bt_field_class *out_option_field_class; | |
1e6fd1d7 | 456 | const bt_field_class_variant_option *option; |
ca9f27f3 | 457 | |
1e6fd1d7 PP |
458 | option = bt_field_class_variant_borrow_option_by_index_const( |
459 | in_field_class, i); | |
460 | option_fc = bt_field_class_variant_option_borrow_field_class_const( | |
461 | option); | |
462 | option_name = bt_field_class_variant_option_get_name(option); | |
ca9f27f3 | 463 | out_option_field_class = create_field_class_copy_internal( |
1e6fd1d7 | 464 | md_maps, option_fc); |
ca9f27f3 | 465 | if (!out_option_field_class) { |
91bc8451 | 466 | BT_COMP_LOGE_STR("Cannot copy field class."); |
ca9f27f3 FD |
467 | ret = -1; |
468 | goto error; | |
469 | } | |
1e6fd1d7 | 470 | ret = copy_field_class_content_internal(md_maps, option_fc, |
ca9f27f3 FD |
471 | out_option_field_class); |
472 | if (ret) { | |
91bc8451 | 473 | BT_COMP_LOGE_STR("Error copying content of option variant " |
ca9f27f3 FD |
474 | "field class'"); |
475 | goto error; | |
476 | } | |
477 | ||
d24d5663 | 478 | if (bt_field_class_variant_append_option( |
ca9f27f3 | 479 | out_field_class, option_name, |
d24d5663 PP |
480 | out_option_field_class) != |
481 | BT_FIELD_CLASS_VARIANT_APPEND_OPTION_STATUS_OK) { | |
91bc8451 | 482 | BT_COMP_LOGE_STR("Cannot append option to variant field class'"); |
ca9f27f3 FD |
483 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_tag_field_class); |
484 | ret = -1; | |
485 | goto error; | |
486 | } | |
487 | } | |
488 | ||
91bc8451 | 489 | BT_COMP_LOGD("Copied content of variant field class: in-fc-addr=%p, " |
ca9f27f3 FD |
490 | "out-fc-addr=%p", in_field_class, out_field_class); |
491 | ||
492 | error: | |
493 | return ret; | |
494 | } | |
495 | ||
496 | static inline | |
497 | int field_class_static_array_copy( | |
498 | struct trace_ir_metadata_maps *md_maps, | |
499 | const bt_field_class *in_field_class, | |
500 | bt_field_class *out_field_class) | |
501 | { | |
91bc8451 | 502 | BT_COMP_LOGD("Copying content of static array field class: in-fc-addr=%p, " |
ca9f27f3 FD |
503 | "out-fc-addr=%p", in_field_class, out_field_class); |
504 | /* | |
505 | * There is no content to copy. Keep this function call anyway for | |
506 | * logging purposes. | |
507 | */ | |
91bc8451 | 508 | BT_COMP_LOGD("Copied content of static array field class: in-fc-addr=%p, " |
ca9f27f3 FD |
509 | "out-fc-addr=%p", in_field_class, out_field_class); |
510 | ||
511 | return 0; | |
512 | } | |
513 | ||
514 | static inline | |
515 | int field_class_dynamic_array_copy( | |
516 | struct trace_ir_metadata_maps *md_maps, | |
517 | const bt_field_class *in_field_class, | |
518 | bt_field_class *out_field_class) | |
519 | { | |
3b40fbf9 | 520 | const bt_field_class *len_fc; |
ca9f27f3 | 521 | const bt_field_path *len_fp; |
ca9f27f3 FD |
522 | bt_field_class *out_len_field_class; |
523 | int ret = 0; | |
524 | ||
91bc8451 | 525 | BT_COMP_LOGD("Copying content of dynamic array field class: " |
ca9f27f3 FD |
526 | "in-fc-addr=%p, out-fc-addr=%p", |
527 | in_field_class, out_field_class); | |
528 | ||
ca9f27f3 FD |
529 | len_fp = bt_field_class_dynamic_array_borrow_length_field_path_const( |
530 | in_field_class); | |
531 | ||
532 | if (len_fp) { | |
91bc8451 | 533 | BT_COMP_LOGD("Copying dynamic array length field class using " |
ca9f27f3 FD |
534 | "field path: in-len-fp=%p", len_fp); |
535 | len_fc = resolve_field_path_to_field_class( | |
536 | len_fp, md_maps); | |
537 | out_len_field_class = g_hash_table_lookup( | |
538 | md_maps->field_class_map, len_fc); | |
539 | if (!out_len_field_class) { | |
91bc8451 | 540 | BT_COMP_LOGE_STR("Cannot find the output matching length" |
ca9f27f3 FD |
541 | "field class."); |
542 | ret = -1; | |
543 | goto error; | |
544 | } | |
545 | ||
d24d5663 PP |
546 | if (bt_field_class_dynamic_array_set_length_field_class( |
547 | out_field_class, out_len_field_class) != | |
548 | BT_FIELD_CLASS_DYNAMIC_ARRAY_SET_LENGTH_FIELD_CLASS_STATUS_OK) { | |
91bc8451 | 549 | BT_COMP_LOGE_STR("Cannot set dynamic array field class' " |
ca9f27f3 FD |
550 | "length field class."); |
551 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_len_field_class); | |
552 | ret = -1; | |
553 | goto error; | |
554 | } | |
555 | } | |
556 | ||
91bc8451 | 557 | BT_COMP_LOGD("Copied dynamic array field class: in-fc-addr=%p, " |
ca9f27f3 FD |
558 | "out-fc-addr=%p", in_field_class, out_field_class); |
559 | ||
560 | error: | |
561 | return ret; | |
562 | } | |
563 | ||
564 | static inline | |
565 | int field_class_string_copy(struct trace_ir_metadata_maps *md_maps, | |
566 | const bt_field_class *in_field_class, | |
567 | bt_field_class *out_field_class) | |
568 | { | |
91bc8451 | 569 | BT_COMP_LOGD("Copying content of string field class: in-fc-addr=%p, " |
ca9f27f3 FD |
570 | "out-fc-addr=%p", in_field_class, out_field_class); |
571 | /* | |
572 | * There is no content to copy. Keep this function call anyway for | |
573 | * logging purposes. | |
574 | */ | |
91bc8451 | 575 | BT_COMP_LOGD("Copied content of string field class: in-fc-addr=%p, " |
ca9f27f3 FD |
576 | "out-fc-addr=%p", in_field_class, out_field_class); |
577 | ||
578 | return 0; | |
579 | } | |
580 | ||
581 | static | |
582 | bt_field_class *copy_field_class_array_element(struct trace_ir_metadata_maps *md_maps, | |
583 | const bt_field_class *in_elem_fc) | |
584 | { | |
585 | int ret; | |
586 | bt_field_class *out_elem_fc = | |
587 | create_field_class_copy_internal(md_maps, in_elem_fc); | |
588 | if (!out_elem_fc) { | |
91bc8451 | 589 | BT_COMP_LOGE("Error creating output elem field class " |
ca9f27f3 FD |
590 | "from input elem field class for static array: " |
591 | "in-fc-addr=%p", in_elem_fc); | |
592 | goto error; | |
593 | } | |
594 | ||
595 | ret = copy_field_class_content_internal(md_maps, in_elem_fc, out_elem_fc); | |
596 | if (ret) { | |
91bc8451 | 597 | BT_COMP_LOGE("Error creating output elem field class " |
ca9f27f3 FD |
598 | "from input elem field class for static array: " |
599 | "in-fc-addr=%p", in_elem_fc); | |
600 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_elem_fc); | |
601 | goto error; | |
602 | } | |
603 | ||
604 | error: | |
605 | return out_elem_fc; | |
606 | } | |
607 | ||
608 | BT_HIDDEN | |
609 | bt_field_class *create_field_class_copy_internal(struct trace_ir_metadata_maps *md_maps, | |
610 | const bt_field_class *in_field_class) | |
611 | { | |
612 | bt_field_class *out_field_class = NULL; | |
613 | ||
91bc8451 | 614 | BT_COMP_LOGD("Creating bare field class based on field class: in-fc-addr=%p", |
ca9f27f3 FD |
615 | in_field_class); |
616 | ||
617 | switch(bt_field_class_get_type(in_field_class)) { | |
618 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: | |
619 | out_field_class = bt_field_class_unsigned_integer_create( | |
620 | md_maps->output_trace_class); | |
621 | break; | |
622 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
623 | out_field_class = bt_field_class_signed_integer_create( | |
624 | md_maps->output_trace_class); | |
625 | break; | |
626 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
627 | out_field_class = bt_field_class_unsigned_enumeration_create( | |
628 | md_maps->output_trace_class); | |
629 | break; | |
630 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
631 | out_field_class = bt_field_class_signed_enumeration_create( | |
632 | md_maps->output_trace_class); | |
633 | break; | |
634 | case BT_FIELD_CLASS_TYPE_REAL: | |
635 | out_field_class = bt_field_class_real_create( | |
636 | md_maps->output_trace_class); | |
637 | break; | |
638 | case BT_FIELD_CLASS_TYPE_STRING: | |
639 | out_field_class = bt_field_class_string_create( | |
640 | md_maps->output_trace_class); | |
641 | break; | |
642 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
643 | out_field_class = bt_field_class_structure_create( | |
644 | md_maps->output_trace_class); | |
645 | break; | |
646 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
647 | { | |
648 | const bt_field_class *in_elem_fc = | |
649 | bt_field_class_array_borrow_element_field_class_const( | |
650 | in_field_class); | |
651 | uint64_t array_len = | |
652 | bt_field_class_static_array_get_length(in_field_class); | |
653 | ||
654 | bt_field_class *out_elem_fc = copy_field_class_array_element( | |
655 | md_maps, in_elem_fc); | |
656 | if (!out_elem_fc) { | |
657 | out_field_class = NULL; | |
658 | goto error; | |
659 | } | |
660 | ||
661 | out_field_class = bt_field_class_static_array_create( | |
662 | md_maps->output_trace_class, | |
663 | out_elem_fc, array_len); | |
664 | break; | |
665 | } | |
666 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
667 | { | |
668 | const bt_field_class *in_elem_fc = | |
669 | bt_field_class_array_borrow_element_field_class_const( | |
670 | in_field_class); | |
671 | ||
672 | bt_field_class *out_elem_fc = copy_field_class_array_element( | |
673 | md_maps, in_elem_fc); | |
674 | if (!out_elem_fc) { | |
675 | out_field_class = NULL; | |
676 | goto error; | |
677 | } | |
678 | ||
679 | out_field_class = bt_field_class_dynamic_array_create( | |
680 | md_maps->output_trace_class, | |
681 | out_elem_fc); | |
682 | break; | |
683 | } | |
684 | case BT_FIELD_CLASS_TYPE_VARIANT: | |
685 | out_field_class = bt_field_class_variant_create( | |
686 | md_maps->output_trace_class); | |
687 | break; | |
688 | default: | |
689 | abort(); | |
690 | } | |
691 | ||
692 | /* | |
693 | * Add mapping from in_field_class to out_field_class. This simplifies | |
694 | * the resolution of field paths in variant and dynamic array field | |
695 | * classes. | |
696 | */ | |
697 | g_hash_table_insert(md_maps->field_class_map, | |
698 | (gpointer) in_field_class, out_field_class); | |
699 | ||
700 | error: | |
701 | if(out_field_class){ | |
91bc8451 | 702 | BT_COMP_LOGD("Created bare field class based on field class: in-fc-addr=%p, " |
ca9f27f3 FD |
703 | "out-fc-addr=%p", in_field_class, out_field_class); |
704 | } else { | |
91bc8451 | 705 | BT_COMP_LOGE("Error creating output field class from input field " |
ca9f27f3 FD |
706 | "class: in-fc-addr=%p", in_field_class); |
707 | } | |
708 | ||
709 | return out_field_class; | |
710 | } | |
711 | ||
712 | BT_HIDDEN | |
713 | int copy_field_class_content_internal( | |
714 | struct trace_ir_metadata_maps *md_maps, | |
715 | const bt_field_class *in_field_class, | |
716 | bt_field_class *out_field_class) | |
717 | { | |
718 | int ret = 0; | |
719 | switch(bt_field_class_get_type(in_field_class)) { | |
720 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: | |
721 | ret = field_class_unsigned_integer_copy(md_maps, | |
722 | in_field_class, out_field_class); | |
723 | break; | |
724 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
725 | ret = field_class_signed_integer_copy(md_maps, | |
726 | in_field_class, out_field_class); | |
727 | break; | |
728 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
729 | ret = field_class_unsigned_enumeration_copy(md_maps, | |
730 | in_field_class, out_field_class); | |
731 | break; | |
732 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
733 | ret = field_class_signed_enumeration_copy(md_maps, | |
734 | in_field_class, out_field_class); | |
735 | break; | |
736 | case BT_FIELD_CLASS_TYPE_REAL: | |
737 | ret = field_class_real_copy(md_maps, | |
738 | in_field_class, out_field_class); | |
739 | break; | |
740 | case BT_FIELD_CLASS_TYPE_STRING: | |
741 | ret = field_class_string_copy(md_maps, | |
742 | in_field_class, out_field_class); | |
743 | break; | |
744 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
745 | ret = field_class_structure_copy(md_maps, | |
746 | in_field_class, out_field_class); | |
747 | break; | |
748 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
749 | ret = field_class_static_array_copy(md_maps, | |
750 | in_field_class, out_field_class); | |
751 | break; | |
752 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
753 | ret = field_class_dynamic_array_copy(md_maps, | |
754 | in_field_class, out_field_class); | |
755 | break; | |
756 | case BT_FIELD_CLASS_TYPE_VARIANT: | |
757 | ret = field_class_variant_copy(md_maps, | |
758 | in_field_class, out_field_class); | |
759 | break; | |
760 | default: | |
761 | abort(); | |
762 | } | |
763 | ||
764 | return ret; | |
765 | } |