lib: add option field classes with integer selectors
[babeltrace.git] / src / plugins / lttng-utils / debug-info / trace-ir-metadata-field-class-copy.c
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_COMP_LOG_SELF_COMP (md_maps->self_comp)
28 #define BT_LOG_OUTPUT_LEVEL (md_maps->log_level)
29 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-META-FC-COPY"
30 #include "logging/comp-logging.h"
31
32 #include "common/assert.h"
33 #include "common/common.h"
34 #include "compat/compiler.h"
35 #include <babeltrace2/babeltrace.h>
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
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)
48 {
49 uint64_t i, fp_item_count;
50 const bt_field_class *curr_fc;
51
52 BT_ASSERT(bt_field_class_get_type(fc) == BT_FIELD_CLASS_TYPE_STRUCTURE);
53 BT_COMP_LOGD("Walking field path on field class: fp-addr=%p, fc-addr=%p",
54 fp, fc);
55
56 fp_item_count = bt_field_path_get_item_count(fp);
57 curr_fc = fc;
58 for (i = 0; i < fp_item_count; i++) {
59 bt_field_class_type fc_type = bt_field_class_get_type(curr_fc);
60 const bt_field_path_item *fp_item =
61 bt_field_path_borrow_item_by_index_const(fp, i);
62
63 switch (fc_type) {
64 case BT_FIELD_CLASS_TYPE_STRUCTURE:
65 {
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));
73 curr_fc = bt_field_class_structure_member_borrow_field_class_const(
74 member);
75 break;
76 }
77 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR:
78 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR:
79 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR:
80 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR:
81 {
82 BT_ASSERT(bt_field_path_item_get_type(fp_item) ==
83 BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT);
84 curr_fc = bt_field_class_option_borrow_field_class_const(
85 curr_fc);
86 break;
87 }
88 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
89 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
90 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
91 {
92 const bt_field_class_variant_option *option;
93
94 BT_ASSERT(bt_field_path_item_get_type(fp_item) ==
95 BT_FIELD_PATH_ITEM_TYPE_INDEX);
96 option = bt_field_class_variant_borrow_option_by_index_const(
97 curr_fc,
98 bt_field_path_item_index_get_index(fp_item));
99 curr_fc = bt_field_class_variant_option_borrow_field_class_const(
100 option);
101 break;
102 }
103 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
104 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
105 {
106 BT_ASSERT(bt_field_path_item_get_type(fp_item) ==
107 BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT);
108 curr_fc = bt_field_class_array_borrow_element_field_class_const(
109 curr_fc);
110 break;
111 }
112 default:
113 abort();
114 }
115 }
116
117 return curr_fc;
118 }
119
120 static
121 const bt_field_class *resolve_field_path_to_field_class(const bt_field_path *fp,
122 struct trace_ir_metadata_maps *md_maps)
123 {
124 struct field_class_resolving_context *fc_resolving_ctx;
125 const bt_field_class *fc;
126 bt_field_path_scope fp_scope;
127
128 BT_COMP_LOGD("Resolving field path: fp-addr=%p", fp);
129
130 fc_resolving_ctx = md_maps->fc_resolving_ctx;
131 fp_scope = bt_field_path_get_root_scope(fp);
132
133 switch (fp_scope) {
134 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT:
135 fc = walk_field_path(md_maps, fp,
136 fc_resolving_ctx->packet_context);
137 break;
138 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT:
139 fc = walk_field_path(md_maps, fp,
140 fc_resolving_ctx->event_common_context);
141 break;
142 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT:
143 fc = walk_field_path(md_maps, fp,
144 fc_resolving_ctx->event_specific_context);
145 break;
146 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD:
147 fc = walk_field_path(md_maps, fp,
148 fc_resolving_ctx->event_payload);
149 break;
150 default:
151 abort();
152 }
153
154 return fc;
155 }
156
157 static inline
158 void field_class_integer_set_props(const bt_field_class *input_fc,
159 bt_field_class *output_fc)
160 {
161 bt_field_class_integer_set_preferred_display_base(output_fc,
162 bt_field_class_integer_get_preferred_display_base(input_fc));
163 bt_field_class_integer_set_field_value_range(output_fc,
164 bt_field_class_integer_get_field_value_range(input_fc));
165 }
166
167 static inline
168 int field_class_bool_copy(
169 struct trace_ir_metadata_maps *md_maps,
170 const bt_field_class *in_field_class,
171 bt_field_class *out_field_class)
172 {
173 BT_COMP_LOGD("Copying content of boolean field class: "
174 "in-fc-addr=%p, out-fc-addr=%p",
175 in_field_class, out_field_class);
176 BT_COMP_LOGD("Copied content of boolean field class: "
177 "in-fc-addr=%p, out-fc-addr=%p",
178 in_field_class, out_field_class);
179 return 0;
180 }
181
182 static inline
183 int field_class_bit_array_copy(
184 struct trace_ir_metadata_maps *md_maps,
185 const bt_field_class *in_field_class,
186 bt_field_class *out_field_class)
187 {
188 BT_COMP_LOGD("Copying content of bit array field class: "
189 "in-fc-addr=%p, out-fc-addr=%p",
190 in_field_class, out_field_class);
191 BT_COMP_LOGD("Copied content of bit array field class: "
192 "in-fc-addr=%p, out-fc-addr=%p",
193 in_field_class, out_field_class);
194 return 0;
195 }
196
197 static inline
198 int field_class_unsigned_integer_copy(
199 struct trace_ir_metadata_maps *md_maps,
200 const bt_field_class *in_field_class,
201 bt_field_class *out_field_class)
202 {
203 BT_COMP_LOGD("Copying content of unsigned integer field class: "
204 "in-fc-addr=%p, out-fc-addr=%p",
205 in_field_class, out_field_class);
206
207 field_class_integer_set_props(in_field_class, out_field_class);
208
209 BT_COMP_LOGD("Copied content of unsigned integer field class: "
210 "in-fc-addr=%p, out-fc-addr=%p",
211 in_field_class, out_field_class);
212 return 0;
213 }
214
215 static inline
216 int field_class_signed_integer_copy(
217 struct trace_ir_metadata_maps *md_maps,
218 const bt_field_class *in_field_class,
219 bt_field_class *out_field_class)
220 {
221 BT_COMP_LOGD("Copying content of signed integer field class: "
222 "in-fc-addr=%p, out-fc-addr=%p",
223 in_field_class, out_field_class);
224
225 field_class_integer_set_props(in_field_class, out_field_class);
226
227 BT_COMP_LOGD("Copied content of signed integer field class: "
228 "in-fc-addr=%p, out-fc-addr=%p",
229 in_field_class, out_field_class);
230 return 0;
231 }
232
233 BT_HIDDEN
234 int field_class_unsigned_enumeration_copy(
235 struct trace_ir_metadata_maps *md_maps,
236 const bt_field_class *in_field_class,
237 bt_field_class *out_field_class)
238 {
239 uint64_t i, enum_mapping_count;
240 int ret = 0;
241
242 BT_COMP_LOGD("Copying content of unsigned enumeration field class: "
243 "in-fc-addr=%p, out-fc-addr=%p",
244 in_field_class, out_field_class);
245
246 /* Copy properties of the inner integer. */
247 field_class_integer_set_props(in_field_class, out_field_class);
248
249 /* Copy all enumeration entries. */
250 enum_mapping_count = bt_field_class_enumeration_get_mapping_count(in_field_class);
251 for (i = 0; i < enum_mapping_count; i++) {
252 const char *label;
253 const bt_integer_range_set_unsigned *range_set;
254 const bt_field_class_enumeration_unsigned_mapping *u_mapping;
255 const bt_field_class_enumeration_mapping *mapping;
256
257 u_mapping = bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
258 in_field_class, i);
259 mapping = bt_field_class_enumeration_unsigned_mapping_as_mapping_const(
260 u_mapping);
261 label = bt_field_class_enumeration_mapping_get_label(mapping);
262 range_set = bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
263 u_mapping);
264 ret = bt_field_class_enumeration_unsigned_add_mapping(
265 out_field_class, label, range_set);
266 if (ret) {
267 goto error;
268 }
269 }
270
271 BT_COMP_LOGD("Copied content of unsigned enumeration field class: "
272 "in-fc-addr=%p, out-fc-addr=%p",
273 in_field_class, out_field_class);
274
275 error:
276 return ret;
277 }
278
279 static inline
280 int field_class_signed_enumeration_copy(
281 struct trace_ir_metadata_maps *md_maps,
282 const bt_field_class *in_field_class,
283 bt_field_class *out_field_class)
284 {
285 uint64_t i, enum_mapping_count;
286 int ret = 0;
287
288 BT_COMP_LOGD("Copying content of signed enumeration field class: "
289 "in-fc-addr=%p, out-fc-addr=%p",
290 in_field_class, out_field_class);
291
292 /* Copy properties of the inner integer. */
293 field_class_integer_set_props(in_field_class, out_field_class);
294
295 /* Copy all enumeration entries. */
296 enum_mapping_count =
297 bt_field_class_enumeration_get_mapping_count(in_field_class);
298 for (i = 0; i < enum_mapping_count; i++) {
299 const char *label;
300 const bt_integer_range_set_signed *range_set;
301 const bt_field_class_enumeration_signed_mapping *s_mapping;
302 const bt_field_class_enumeration_mapping *mapping;
303
304 s_mapping = bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
305 in_field_class, i);
306 mapping = bt_field_class_enumeration_signed_mapping_as_mapping_const(
307 s_mapping);
308 label = bt_field_class_enumeration_mapping_get_label(mapping);
309 range_set = bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
310 s_mapping);
311 ret = bt_field_class_enumeration_signed_add_mapping(
312 out_field_class, label, range_set);
313 if (ret) {
314 goto error;
315 }
316 }
317
318 BT_COMP_LOGD("Copied content of signed enumeration field class: "
319 "in-fc-addr=%p, out-fc-addr=%p",
320 in_field_class, out_field_class);
321
322 error:
323 return ret;
324 }
325
326 static inline
327 int field_class_single_precision_real_copy(
328 struct trace_ir_metadata_maps *md_maps,
329 const bt_field_class *in_field_class,
330 bt_field_class *out_field_class)
331 {
332 BT_COMP_LOGD("Copying content of single-precision real field class: "
333 "in-fc-addr=%p, out-fc-addr=%p", in_field_class, out_field_class);
334
335 BT_COMP_LOGD("Copied content single-precision real field class:"
336 "in-fc-addr=%p, out-fc-addr=%p", in_field_class, out_field_class);
337
338 return 0;
339 }
340
341 static inline
342 int field_class_double_precision_real_copy(
343 struct trace_ir_metadata_maps *md_maps,
344 const bt_field_class *in_field_class,
345 bt_field_class *out_field_class)
346 {
347 BT_COMP_LOGD("Copying content of double-precision real field class: "
348 "in-fc-addr=%p, out-fc-addr=%p", in_field_class, out_field_class);
349
350 BT_COMP_LOGD("Copied content double-precision real field class:"
351 "in-fc-addr=%p, out-fc-addr=%p", in_field_class, out_field_class);
352
353 return 0;
354 }
355
356 static inline
357 int field_class_structure_copy(
358 struct trace_ir_metadata_maps *md_maps,
359 const bt_field_class *in_field_class,
360 bt_field_class *out_field_class)
361 {
362 uint64_t i, struct_member_count;
363 int ret = 0;
364
365 BT_COMP_LOGD("Copying content of structure field class: "
366 "in-fc-addr=%p, out-fc-addr=%p",
367 in_field_class, out_field_class);
368 /* Get the number of member in that struct. */
369 struct_member_count =
370 bt_field_class_structure_get_member_count(in_field_class);
371
372 /* Iterate over all the members of the struct. */
373 for (i = 0; i < struct_member_count; i++) {
374 const bt_field_class_structure_member *in_member;
375 bt_field_class_structure_member *out_member;
376 const char *member_name;
377 const bt_field_class *in_member_fc;
378 bt_field_class *out_member_field_class;
379
380 in_member = bt_field_class_structure_borrow_member_by_index_const(
381 in_field_class, i);
382 in_member_fc = bt_field_class_structure_member_borrow_field_class_const(
383 in_member);
384 member_name = bt_field_class_structure_member_get_name(in_member);
385 BT_COMP_LOGD("Copying structure field class's field: "
386 "index=%" PRId64 ", "
387 "member-fc-addr=%p, field-name=\"%s\"",
388 i, in_member_fc, member_name);
389
390 out_member_field_class = create_field_class_copy(md_maps,
391 in_member_fc);
392 if (!out_member_field_class) {
393 BT_COMP_LOGE("Cannot copy structure field class's field: "
394 "index=%" PRId64 ", "
395 "field-fc-addr=%p, field-name=\"%s\"",
396 i, in_member_fc, member_name);
397 ret = -1;
398 goto error;
399 }
400 ret = copy_field_class_content(md_maps, in_member_fc,
401 out_member_field_class);
402 if (ret) {
403 goto error;
404 }
405
406 if (bt_field_class_structure_append_member(out_field_class,
407 member_name, out_member_field_class) !=
408 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK) {
409 BT_COMP_LOGE("Cannot append structure field class's field: "
410 "index=%" PRId64 ", "
411 "field-fc-addr=%p, field-name=\"%s\"",
412 i, in_member_fc, member_name);
413 BT_FIELD_CLASS_PUT_REF_AND_RESET(out_member_field_class);
414 ret = -1;
415 goto error;
416 }
417
418 out_member = bt_field_class_structure_borrow_member_by_index(
419 out_field_class, i);
420 BT_ASSERT(out_member);
421
422 /*
423 * Safe to use the same value object because it's frozen
424 * at this point.
425 */
426 bt_field_class_structure_member_set_user_attributes(
427 out_member,
428 bt_field_class_structure_member_borrow_user_attributes_const(
429 in_member));
430 }
431
432 BT_COMP_LOGD("Copied structure field class: original-fc-addr=%p, copy-fc-addr=%p",
433 in_field_class, out_field_class);
434
435 error:
436 return ret;
437 }
438
439 static inline
440 int field_class_variant_copy(
441 struct trace_ir_metadata_maps *md_maps,
442 const bt_field_class *in_field_class,
443 bt_field_class *out_field_class)
444 {
445 bt_field_class *out_tag_field_class = NULL;
446 uint64_t i, variant_option_count;
447 bt_field_class_type fc_type = bt_field_class_get_type(in_field_class);
448 int ret = 0;
449
450 BT_COMP_LOGD("Copying content of variant field class: "
451 "in-fc-addr=%p, out-fc-addr=%p",
452 in_field_class, out_field_class);
453 variant_option_count =
454 bt_field_class_variant_get_option_count(in_field_class);
455 for (i = 0; i < variant_option_count; i++) {
456 const bt_field_class *in_option_fc;
457 const char *option_name;
458 bt_field_class *out_option_field_class;
459 const bt_field_class_variant_option *in_option;
460 bt_field_class_variant_option *out_option;
461
462 in_option = bt_field_class_variant_borrow_option_by_index_const(
463 in_field_class, i);
464 in_option_fc = bt_field_class_variant_option_borrow_field_class_const(
465 in_option);
466 option_name = bt_field_class_variant_option_get_name(in_option);
467 out_option_field_class = create_field_class_copy_internal(
468 md_maps, in_option_fc);
469 if (!out_option_field_class) {
470 BT_COMP_LOGE_STR("Cannot copy field class.");
471 ret = -1;
472 goto error;
473 }
474 ret = copy_field_class_content_internal(md_maps, in_option_fc,
475 out_option_field_class);
476 if (ret) {
477 BT_COMP_LOGE_STR("Error copying content of option variant "
478 "field class'");
479 goto error;
480 }
481
482 if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR) {
483 const bt_field_class_variant_with_selector_unsigned_option *spec_opt =
484 bt_field_class_variant_with_selector_unsigned_borrow_option_by_index_const(
485 in_field_class, i);
486 const bt_integer_range_set_unsigned *ranges =
487 bt_field_class_variant_with_selector_unsigned_option_borrow_ranges_const(
488 spec_opt);
489
490 if (bt_field_class_variant_with_selector_unsigned_append_option(
491 out_field_class, option_name,
492 out_option_field_class, ranges) !=
493 BT_FIELD_CLASS_VARIANT_WITH_SELECTOR_APPEND_OPTION_STATUS_OK) {
494 BT_COMP_LOGE_STR("Cannot append option to variant field class with unsigned selector'");
495 BT_FIELD_CLASS_PUT_REF_AND_RESET(out_tag_field_class);
496 ret = -1;
497 goto error;
498 }
499 } else if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR) {
500 const bt_field_class_variant_with_selector_signed_option *spec_opt =
501 bt_field_class_variant_with_selector_signed_borrow_option_by_index_const(
502 in_field_class, i);
503 const bt_integer_range_set_signed *ranges =
504 bt_field_class_variant_with_selector_signed_option_borrow_ranges_const(
505 spec_opt);
506
507 if (bt_field_class_variant_with_selector_signed_append_option(
508 out_field_class, option_name,
509 out_option_field_class, ranges) !=
510 BT_FIELD_CLASS_VARIANT_WITH_SELECTOR_APPEND_OPTION_STATUS_OK) {
511 BT_COMP_LOGE_STR("Cannot append option to variant field class with signed selector'");
512 BT_FIELD_CLASS_PUT_REF_AND_RESET(out_tag_field_class);
513 ret = -1;
514 goto error;
515 }
516 } else {
517 BT_ASSERT(fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR);
518
519 if (bt_field_class_variant_without_selector_append_option(
520 out_field_class, option_name,
521 out_option_field_class) !=
522 BT_FIELD_CLASS_VARIANT_WITHOUT_SELECTOR_APPEND_OPTION_STATUS_OK) {
523 BT_COMP_LOGE_STR("Cannot append option to variant field class'");
524 BT_FIELD_CLASS_PUT_REF_AND_RESET(out_tag_field_class);
525 ret = -1;
526 goto error;
527 }
528 }
529
530 out_option = bt_field_class_variant_borrow_option_by_index(
531 out_field_class, i);
532 BT_ASSERT(out_option);
533
534 /*
535 * Safe to use the same value object because it's frozen
536 * at this point.
537 */
538 bt_field_class_variant_option_set_user_attributes(
539 out_option,
540 bt_field_class_variant_option_borrow_user_attributes_const(
541 in_option));
542 }
543
544 BT_COMP_LOGD("Copied content of variant field class: in-fc-addr=%p, "
545 "out-fc-addr=%p", in_field_class, out_field_class);
546
547 error:
548 return ret;
549 }
550
551 static inline
552 int field_class_static_array_copy(
553 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_COMP_LOGD("Copying content of static array 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_COMP_LOGD("Copied content of static array 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 inline
570 int field_class_dynamic_array_copy(
571 struct trace_ir_metadata_maps *md_maps,
572 const bt_field_class *in_field_class,
573 bt_field_class *out_field_class)
574 {
575 BT_COMP_LOGD("Copying content of dynamic array field class: "
576 "in-fc-addr=%p, out-fc-addr=%p",
577 in_field_class, out_field_class);
578
579 /*
580 * There is no content to copy. Keep this function call anyway for
581 * logging purposes.
582 */
583 BT_COMP_LOGD("Copied dynamic array field class: in-fc-addr=%p, "
584 "out-fc-addr=%p", in_field_class, out_field_class);
585
586 return 0;
587 }
588
589 static inline
590 int field_class_option_copy(
591 struct trace_ir_metadata_maps *md_maps,
592 const bt_field_class *in_field_class,
593 bt_field_class *out_field_class)
594 {
595 BT_COMP_LOGD("Copying content of option field class: "
596 "in-fc-addr=%p, out-fc-addr=%p",
597 in_field_class, out_field_class);
598
599 if (bt_field_class_get_type(out_field_class) ==
600 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR) {
601 bt_field_class_option_with_selector_bool_set_selector_is_reversed(
602 out_field_class,
603 bt_field_class_option_with_selector_bool_selector_is_reversed(
604 in_field_class));
605 }
606
607 BT_COMP_LOGD("Copied option field class: in-fc-addr=%p, "
608 "out-fc-addr=%p", in_field_class, out_field_class);
609
610 return 0;
611 }
612
613 static inline
614 int field_class_string_copy(struct trace_ir_metadata_maps *md_maps,
615 const bt_field_class *in_field_class,
616 bt_field_class *out_field_class)
617 {
618 BT_COMP_LOGD("Copying content of string field class: in-fc-addr=%p, "
619 "out-fc-addr=%p", in_field_class, out_field_class);
620 /*
621 * There is no content to copy. Keep this function call anyway for
622 * logging purposes.
623 */
624 BT_COMP_LOGD("Copied content of string field class: in-fc-addr=%p, "
625 "out-fc-addr=%p", in_field_class, out_field_class);
626
627 return 0;
628 }
629
630 static
631 bt_field_class *copy_field_class_array_element(struct trace_ir_metadata_maps *md_maps,
632 const bt_field_class *in_elem_fc)
633 {
634 int ret;
635 bt_field_class *out_elem_fc =
636 create_field_class_copy_internal(md_maps, in_elem_fc);
637 if (!out_elem_fc) {
638 BT_COMP_LOGE("Error creating output elem field class "
639 "from input elem field class for static array: "
640 "in-fc-addr=%p", in_elem_fc);
641 goto error;
642 }
643
644 ret = copy_field_class_content_internal(md_maps, in_elem_fc, out_elem_fc);
645 if (ret) {
646 BT_COMP_LOGE("Error creating output elem field class "
647 "from input elem field class for static array: "
648 "in-fc-addr=%p", in_elem_fc);
649 BT_FIELD_CLASS_PUT_REF_AND_RESET(out_elem_fc);
650 goto error;
651 }
652
653 error:
654 return out_elem_fc;
655 }
656
657 BT_HIDDEN
658 bt_field_class *create_field_class_copy_internal(struct trace_ir_metadata_maps *md_maps,
659 const bt_field_class *in_field_class)
660 {
661 bt_field_class *out_field_class = NULL;
662 bt_field_class_type fc_type = bt_field_class_get_type(in_field_class);
663
664 BT_COMP_LOGD("Creating bare field class based on field class: in-fc-addr=%p",
665 in_field_class);
666
667 switch (fc_type) {
668 case BT_FIELD_CLASS_TYPE_BOOL:
669 out_field_class = bt_field_class_bool_create(
670 md_maps->output_trace_class);
671 break;
672 case BT_FIELD_CLASS_TYPE_BIT_ARRAY:
673 out_field_class = bt_field_class_bit_array_create(
674 md_maps->output_trace_class,
675 bt_field_class_bit_array_get_length(
676 in_field_class));
677 break;
678 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
679 out_field_class = bt_field_class_integer_unsigned_create(
680 md_maps->output_trace_class);
681 break;
682 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
683 out_field_class = bt_field_class_integer_signed_create(
684 md_maps->output_trace_class);
685 break;
686 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
687 out_field_class = bt_field_class_enumeration_unsigned_create(
688 md_maps->output_trace_class);
689 break;
690 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
691 out_field_class = bt_field_class_enumeration_signed_create(
692 md_maps->output_trace_class);
693 break;
694 case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL:
695 out_field_class = bt_field_class_real_single_precision_create(
696 md_maps->output_trace_class);
697 break;
698 case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL:
699 out_field_class = bt_field_class_real_double_precision_create(
700 md_maps->output_trace_class);
701 break;
702 case BT_FIELD_CLASS_TYPE_STRING:
703 out_field_class = bt_field_class_string_create(
704 md_maps->output_trace_class);
705 break;
706 case BT_FIELD_CLASS_TYPE_STRUCTURE:
707 out_field_class = bt_field_class_structure_create(
708 md_maps->output_trace_class);
709 break;
710 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
711 {
712 const bt_field_class *in_elem_fc =
713 bt_field_class_array_borrow_element_field_class_const(
714 in_field_class);
715 uint64_t array_len =
716 bt_field_class_array_static_get_length(in_field_class);
717
718 bt_field_class *out_elem_fc = copy_field_class_array_element(
719 md_maps, in_elem_fc);
720 if (!out_elem_fc) {
721 out_field_class = NULL;
722 goto error;
723 }
724
725 out_field_class = bt_field_class_array_static_create(
726 md_maps->output_trace_class,
727 out_elem_fc, array_len);
728 break;
729 }
730 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
731 {
732 const bt_field_class *in_elem_fc =
733 bt_field_class_array_borrow_element_field_class_const(
734 in_field_class);
735 const bt_field_path *length_fp =
736 bt_field_class_array_dynamic_borrow_length_field_path_const(
737 in_field_class);
738 bt_field_class *out_length_fc = NULL;
739
740 bt_field_class *out_elem_fc = copy_field_class_array_element(
741 md_maps, in_elem_fc);
742 if (!out_elem_fc) {
743 out_field_class = NULL;
744 goto error;
745 }
746
747 if (length_fp) {
748 const bt_field_class *in_length_fc =
749 resolve_field_path_to_field_class(length_fp,
750 md_maps);
751
752 BT_ASSERT(in_length_fc);
753 out_length_fc = g_hash_table_lookup(md_maps->field_class_map,
754 in_length_fc);
755 BT_ASSERT(out_length_fc);
756 }
757
758 out_field_class = bt_field_class_array_dynamic_create(
759 md_maps->output_trace_class,
760 out_elem_fc, out_length_fc);
761 break;
762 }
763 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR:
764 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR:
765 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR:
766 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR:
767 {
768 const bt_field_class *in_content_fc =
769 bt_field_class_option_borrow_field_class_const(
770 in_field_class);
771 bt_field_class *out_selector_fc = NULL;
772 bt_field_class *out_content_fc;
773 int ret;
774
775 out_content_fc = create_field_class_copy_internal(
776 md_maps, in_content_fc);
777 if (!out_content_fc) {
778 BT_COMP_LOGE_STR("Cannot copy option's content field class.");
779 goto error;
780 }
781
782 ret = copy_field_class_content_internal(md_maps,
783 in_content_fc, out_content_fc);
784 if (ret) {
785 BT_COMP_LOGE_STR("Error copying content of option's "
786 "content field class");
787 goto error;
788 }
789
790 if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR) {
791 out_field_class =
792 bt_field_class_option_without_selector_create(
793 md_maps->output_trace_class,
794 out_content_fc);
795 } else {
796 const bt_field_path *in_selector_fp =
797 bt_field_class_option_with_selector_borrow_selector_field_path_const(
798 in_field_class);
799 const bt_field_class *in_selector_fc;
800
801 BT_ASSERT(in_selector_fp);
802 in_selector_fc = resolve_field_path_to_field_class(
803 in_selector_fp, md_maps);
804 BT_ASSERT(in_selector_fc);
805 out_selector_fc = g_hash_table_lookup(
806 md_maps->field_class_map, in_selector_fc);
807 BT_ASSERT(out_selector_fc);
808
809 if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR) {
810 out_field_class =
811 bt_field_class_option_with_selector_bool_create(
812 md_maps->output_trace_class,
813 out_content_fc, out_selector_fc);
814 } else if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR) {
815 const bt_integer_range_set_unsigned *ranges =
816 bt_field_class_option_with_selector_integer_unsigned_borrow_selector_ranges_const(
817 in_field_class);
818
819 BT_ASSERT(ranges);
820 out_field_class =
821 bt_field_class_option_with_selector_integer_unsigned_create(
822 md_maps->output_trace_class,
823 out_content_fc, out_selector_fc,
824 ranges);
825 } else if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR) {
826 const bt_integer_range_set_signed *ranges =
827 bt_field_class_option_with_selector_integer_signed_borrow_selector_ranges_const(
828 in_field_class);
829
830 BT_ASSERT(ranges);
831 out_field_class =
832 bt_field_class_option_with_selector_integer_signed_create(
833 md_maps->output_trace_class,
834 out_content_fc, out_selector_fc,
835 ranges);
836 }
837 }
838
839 BT_ASSERT(out_field_class);
840 break;
841 }
842 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
843 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
844 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
845 {
846 bt_field_class *out_sel_fc = NULL;
847
848 if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR ||
849 fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR) {
850 const bt_field_class *in_sel_fc;
851 const bt_field_path *sel_fp =
852 bt_field_class_variant_with_selector_borrow_selector_field_path_const(
853 in_field_class);
854
855 BT_ASSERT(sel_fp);
856 in_sel_fc = resolve_field_path_to_field_class(sel_fp,
857 md_maps);
858 BT_ASSERT(in_sel_fc);
859 out_sel_fc = g_hash_table_lookup(
860 md_maps->field_class_map, in_sel_fc);
861 BT_ASSERT(out_sel_fc);
862 }
863
864 out_field_class = bt_field_class_variant_create(
865 md_maps->output_trace_class, out_sel_fc);
866 break;
867 }
868 default:
869 abort();
870 }
871
872 /*
873 * Add mapping from in_field_class to out_field_class. This simplifies
874 * the resolution of field paths in variant and dynamic array field
875 * classes.
876 */
877 g_hash_table_insert(md_maps->field_class_map,
878 (gpointer) in_field_class, out_field_class);
879
880 error:
881 if(out_field_class){
882 BT_COMP_LOGD("Created bare field class based on field class: in-fc-addr=%p, "
883 "out-fc-addr=%p", in_field_class, out_field_class);
884 } else {
885 BT_COMP_LOGE("Error creating output field class from input field "
886 "class: in-fc-addr=%p", in_field_class);
887 }
888
889 return out_field_class;
890 }
891
892 BT_HIDDEN
893 int copy_field_class_content_internal(
894 struct trace_ir_metadata_maps *md_maps,
895 const bt_field_class *in_field_class,
896 bt_field_class *out_field_class)
897 {
898 int ret = 0;
899
900 /*
901 * Safe to use the same value object because it's frozen at this
902 * point.
903 */
904 bt_field_class_set_user_attributes(out_field_class,
905 bt_field_class_borrow_user_attributes_const(in_field_class));
906
907 switch(bt_field_class_get_type(in_field_class)) {
908 case BT_FIELD_CLASS_TYPE_BOOL:
909 ret = field_class_bool_copy(md_maps,
910 in_field_class, out_field_class);
911 break;
912 case BT_FIELD_CLASS_TYPE_BIT_ARRAY:
913 ret = field_class_bit_array_copy(md_maps,
914 in_field_class, out_field_class);
915 break;
916 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
917 ret = field_class_unsigned_integer_copy(md_maps,
918 in_field_class, out_field_class);
919 break;
920 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
921 ret = field_class_signed_integer_copy(md_maps,
922 in_field_class, out_field_class);
923 break;
924 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
925 ret = field_class_unsigned_enumeration_copy(md_maps,
926 in_field_class, out_field_class);
927 break;
928 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
929 ret = field_class_signed_enumeration_copy(md_maps,
930 in_field_class, out_field_class);
931 break;
932 case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL:
933 ret = field_class_single_precision_real_copy(md_maps,
934 in_field_class, out_field_class);
935 break;
936 case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL:
937 ret = field_class_double_precision_real_copy(md_maps,
938 in_field_class, out_field_class);
939 break;
940 case BT_FIELD_CLASS_TYPE_STRING:
941 ret = field_class_string_copy(md_maps,
942 in_field_class, out_field_class);
943 break;
944 case BT_FIELD_CLASS_TYPE_STRUCTURE:
945 ret = field_class_structure_copy(md_maps,
946 in_field_class, out_field_class);
947 break;
948 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
949 ret = field_class_static_array_copy(md_maps,
950 in_field_class, out_field_class);
951 break;
952 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
953 ret = field_class_dynamic_array_copy(md_maps,
954 in_field_class, out_field_class);
955 break;
956 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR:
957 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR:
958 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR:
959 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR:
960 ret = field_class_option_copy(md_maps,
961 in_field_class, out_field_class);
962 break;
963 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
964 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
965 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
966 ret = field_class_variant_copy(md_maps,
967 in_field_class, out_field_class);
968 break;
969 default:
970 abort();
971 }
972
973 return ret;
974 }
This page took 0.102202 seconds and 4 git commands to generate.