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