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