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