2 * SPDX-License-Identifier: MIT
4 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 #define BT_LOG_TAG "CTF-WRITER/FIELDS"
15 #include <babeltrace2-ctf-writer/object.h>
17 #include "common/align.h"
18 #include "common/assert.h"
19 #include "compat/compiler.h"
20 #include "compat/endian.h"
21 #include "compat/fcntl.h"
22 #include "ctfser/ctfser.h"
24 #include "assert-pre.h"
26 #include "field-types.h"
29 #define BT_CTF_ASSERT_PRE_CTF_FIELD_IS_INT_OR_ENUM(_field, _name) \
30 BT_CTF_ASSERT_PRE((_field)->type->id == BT_CTF_FIELD_TYPE_ID_INTEGER || \
31 (_field)->type->id == BT_CTF_FIELD_TYPE_ID_ENUM, \
32 _name " is not an integer or an enumeration field: " \
33 "field-addr=%p", (_field))
36 struct bt_ctf_field_common
*bt_ctf_field_common_copy(struct bt_ctf_field_common
*field
)
38 struct bt_ctf_field_common
*copy
= NULL
;
40 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Field");
41 BT_ASSERT_DBG(field_type_common_has_known_id(field
->type
));
42 BT_ASSERT_DBG(field
->methods
->copy
);
43 copy
= field
->methods
->copy(field
);
45 BT_LOGW("Cannot create field: ft-addr=%p", field
->type
);
49 bt_ctf_field_common_set(copy
, field
->payload_set
);
56 int bt_ctf_field_common_structure_initialize(struct bt_ctf_field_common
*field
,
57 struct bt_ctf_field_type_common
*type
,
58 bool is_shared
, bt_ctf_object_release_func release_func
,
59 struct bt_ctf_field_common_methods
*methods
,
60 bt_ctf_field_common_create_func field_create_func
,
61 GDestroyNotify field_release_func
)
64 struct bt_ctf_field_type_common_structure
*structure_type
=
65 BT_CTF_FROM_COMMON(type
);
66 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
69 BT_LOGD("Initializing common structure field object: ft-addr=%p", type
);
70 bt_ctf_field_common_initialize(field
, type
, is_shared
,
71 release_func
, methods
);
72 structure
->fields
= g_ptr_array_new_with_free_func(field_release_func
);
73 g_ptr_array_set_size(structure
->fields
, structure_type
->fields
->len
);
75 /* Create all fields contained in the structure field. */
76 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
77 struct bt_ctf_field_common
*member_field
;
78 struct bt_ctf_field_type_common_structure_field
*struct_field
=
79 BT_CTF_FIELD_TYPE_COMMON_STRUCTURE_FIELD_AT_INDEX(
81 member_field
= field_create_func(struct_field
->type
);
83 BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu",
84 g_quark_to_string(struct_field
->name
), i
);
89 g_ptr_array_index(structure
->fields
, i
) = member_field
;
92 BT_LOGD("Initialized common structure field object: addr=%p, ft-addr=%p",
100 int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common
*field
,
101 struct bt_ctf_field_type_common
*type
,
102 bool is_shared
, bt_ctf_object_release_func release_func
,
103 struct bt_ctf_field_common_methods
*methods
,
104 bt_ctf_field_common_create_func field_create_func
,
105 GDestroyNotify field_release_func
)
108 struct bt_ctf_field_type_common_variant
*variant_type
=
109 BT_CTF_FROM_COMMON(type
);
110 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
113 BT_LOGD("Initializing common variant field object: ft-addr=%p", type
);
114 bt_ctf_field_common_initialize(field
, type
, is_shared
,
115 release_func
, methods
);
116 ret
= bt_ctf_field_type_common_variant_update_choices(type
);
118 BT_LOGE("Cannot update common variant field type choices: "
123 variant
->fields
= g_ptr_array_new_with_free_func(field_release_func
);
124 g_ptr_array_set_size(variant
->fields
, variant_type
->choices
->len
);
126 /* Create all fields contained in the variant field. */
127 for (i
= 0; i
< variant_type
->choices
->len
; i
++) {
128 struct bt_ctf_field_common
*member_field
;
129 struct bt_ctf_field_type_common_variant_choice
*var_choice
=
130 BT_CTF_FIELD_TYPE_COMMON_VARIANT_CHOICE_AT_INDEX(
133 member_field
= field_create_func(var_choice
->type
);
135 BT_LOGE("Failed to create variant field's member: name=\"%s\", index=%zu",
136 g_quark_to_string(var_choice
->name
), i
);
141 g_ptr_array_index(variant
->fields
, i
) = member_field
;
144 BT_LOGD("Initialized common variant field object: addr=%p, ft-addr=%p",
152 int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common
*field
,
153 struct bt_ctf_field_type_common
*type
,
154 bool is_shared
, bt_ctf_object_release_func release_func
,
155 struct bt_ctf_field_common_methods
*methods
)
158 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
160 BT_LOGD("Initializing common string field object: ft-addr=%p", type
);
161 bt_ctf_field_common_initialize(field
, type
, is_shared
,
162 release_func
, methods
);
163 string
->buf
= g_array_sized_new(FALSE
, FALSE
, sizeof(char), 1);
169 g_array_index(string
->buf
, char, 0) = '\0';
170 BT_LOGD("Initialized common string field object: addr=%p, ft-addr=%p",
178 int bt_ctf_field_common_array_initialize(struct bt_ctf_field_common
*field
,
179 struct bt_ctf_field_type_common
*type
,
180 bool is_shared
, bt_ctf_object_release_func release_func
,
181 struct bt_ctf_field_common_methods
*methods
,
182 bt_ctf_field_common_create_func field_create_func
,
183 GDestroyNotify field_destroy_func
)
185 struct bt_ctf_field_type_common_array
*array_type
= BT_CTF_FROM_COMMON(type
);
186 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
187 unsigned int array_length
;
191 BT_LOGD("Initializing common array field object: ft-addr=%p", type
);
193 bt_ctf_field_common_initialize(field
, type
, is_shared
,
194 release_func
, methods
);
195 array_length
= array_type
->length
;
196 array
->elements
= g_ptr_array_sized_new(array_length
);
197 if (!array
->elements
) {
202 g_ptr_array_set_free_func(array
->elements
, field_destroy_func
);
203 g_ptr_array_set_size(array
->elements
, array_length
);
205 for (i
= 0; i
< array_length
; i
++) {
206 array
->elements
->pdata
[i
] = field_create_func(
207 array_type
->element_ft
);
208 if (!array
->elements
->pdata
[i
]) {
214 BT_LOGD("Initialized common array field object: addr=%p, ft-addr=%p",
222 int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common
*field
,
223 struct bt_ctf_field_type_common
*type
,
224 bool is_shared
, bt_ctf_object_release_func release_func
,
225 struct bt_ctf_field_common_methods
*methods
,
226 GDestroyNotify field_destroy_func
)
228 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
231 BT_LOGD("Initializing common sequence field object: ft-addr=%p", type
);
233 bt_ctf_field_common_initialize(field
, type
, is_shared
,
234 release_func
, methods
);
235 sequence
->elements
= g_ptr_array_new();
236 if (!sequence
->elements
) {
241 g_ptr_array_set_free_func(sequence
->elements
, field_destroy_func
);
242 BT_LOGD("Initialized common sequence field object: addr=%p, ft-addr=%p",
250 int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common
*field
)
252 return (field
&& field
->payload_set
) ? 0 : -1;
256 int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common
*field
)
260 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
262 BT_ASSERT_DBG(field
);
264 for (i
= 0; i
< structure
->fields
->len
; i
++) {
265 ret
= bt_ctf_field_common_validate_recursive(
266 (void *) structure
->fields
->pdata
[i
]);
272 this_ret
= bt_ctf_field_type_common_structure_borrow_field_by_index(
273 field
->type
, &name
, NULL
, i
);
274 BT_ASSERT_DBG(this_ret
== 0);
275 BT_CTF_ASSERT_PRE_MSG("Invalid structure field's field: "
276 "struct-field-addr=%p, field-name=\"%s\", "
277 "index=%" PRId64
", field-addr=%p",
278 field
, name
, i
, structure
->fields
->pdata
[i
]);
288 int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common
*field
)
291 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
293 BT_ASSERT_DBG(field
);
295 if (!variant
->current_field
) {
300 ret
= bt_ctf_field_common_validate_recursive(variant
->current_field
);
307 int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common
*field
)
311 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
313 BT_ASSERT_DBG(field
);
315 for (i
= 0; i
< array
->elements
->len
; i
++) {
316 ret
= bt_ctf_field_common_validate_recursive((void *) array
->elements
->pdata
[i
]);
318 BT_CTF_ASSERT_PRE_MSG("Invalid array field's element field: "
319 "array-field-addr=%p, %" PRId64
", "
320 "elem-field-addr=%p",
321 field
, i
, array
->elements
->pdata
[i
]);
331 int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common
*field
)
335 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
337 BT_ASSERT_DBG(field
);
339 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
340 ret
= bt_ctf_field_common_validate_recursive(
341 (void *) sequence
->elements
->pdata
[i
]);
343 BT_CTF_ASSERT_PRE_MSG("Invalid sequence field's element field: "
344 "seq-field-addr=%p, %" PRId64
", "
345 "elem-field-addr=%p",
346 field
, i
, sequence
->elements
->pdata
[i
]);
355 void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common
*field
)
357 BT_ASSERT_DBG(field
);
358 field
->payload_set
= false;
362 void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common
*field
)
365 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
367 BT_ASSERT_DBG(field
);
369 for (i
= 0; i
< structure
->fields
->len
; i
++) {
370 struct bt_ctf_field_common
*member
= structure
->fields
->pdata
[i
];
374 * Structure members are lazily initialized;
375 * skip if this member has not been allocated
381 bt_ctf_field_common_reset_recursive(member
);
386 void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common
*field
)
388 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
390 BT_ASSERT_DBG(field
);
391 variant
->current_field
= NULL
;
395 void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common
*field
)
398 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
400 BT_ASSERT_DBG(field
);
402 for (i
= 0; i
< array
->elements
->len
; i
++) {
403 struct bt_ctf_field_common
*member
= array
->elements
->pdata
[i
];
407 * Array elements are lazily initialized; skip
408 * if this member has not been allocated yet.
413 bt_ctf_field_common_reset_recursive(member
);
418 void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common
*field
)
420 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
423 BT_ASSERT_DBG(field
);
425 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
426 if (sequence
->elements
->pdata
[i
]) {
427 bt_ctf_field_common_reset_recursive(
428 sequence
->elements
->pdata
[i
]);
432 sequence
->length
= 0;
436 void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common
*field
,
439 field
->frozen
= is_frozen
;
443 void bt_ctf_field_common_structure_set_is_frozen_recursive(
444 struct bt_ctf_field_common
*field
, bool is_frozen
)
447 struct bt_ctf_field_common_structure
*structure_field
=
448 BT_CTF_FROM_COMMON(field
);
450 BT_LOGD("Freezing structure field object: addr=%p", field
);
452 for (i
= 0; i
< structure_field
->fields
->len
; i
++) {
453 struct bt_ctf_field_common
*struct_field
=
454 g_ptr_array_index(structure_field
->fields
, i
);
456 BT_LOGD("Freezing structure field's field: field-addr=%p, index=%" PRId64
,
458 bt_ctf_field_common_set_is_frozen_recursive(struct_field
,
462 bt_ctf_field_common_generic_set_is_frozen(field
, is_frozen
);
466 void bt_ctf_field_common_variant_set_is_frozen_recursive(
467 struct bt_ctf_field_common
*field
, bool is_frozen
)
470 struct bt_ctf_field_common_variant
*variant_field
= BT_CTF_FROM_COMMON(field
);
472 BT_LOGD("Freezing variant field object: addr=%p", field
);
474 for (i
= 0; i
< variant_field
->fields
->len
; i
++) {
475 struct bt_ctf_field_common
*var_field
=
476 g_ptr_array_index(variant_field
->fields
, i
);
478 BT_LOGD("Freezing variant field's field: field-addr=%p, index=%" PRId64
,
480 bt_ctf_field_common_set_is_frozen_recursive(var_field
, is_frozen
);
483 bt_ctf_field_common_generic_set_is_frozen(field
, is_frozen
);
487 void bt_ctf_field_common_array_set_is_frozen_recursive(
488 struct bt_ctf_field_common
*field
, bool is_frozen
)
491 struct bt_ctf_field_common_array
*array_field
= BT_CTF_FROM_COMMON(field
);
493 BT_LOGD("Freezing array field object: addr=%p", field
);
495 for (i
= 0; i
< array_field
->elements
->len
; i
++) {
496 struct bt_ctf_field_common
*elem_field
=
497 g_ptr_array_index(array_field
->elements
, i
);
499 BT_LOGD("Freezing array field object's element field: "
500 "element-field-addr=%p, index=%" PRId64
,
502 bt_ctf_field_common_set_is_frozen_recursive(elem_field
, is_frozen
);
505 bt_ctf_field_common_generic_set_is_frozen(field
, is_frozen
);
509 void bt_ctf_field_common_sequence_set_is_frozen_recursive(
510 struct bt_ctf_field_common
*field
, bool is_frozen
)
513 struct bt_ctf_field_common_sequence
*sequence_field
=
514 BT_CTF_FROM_COMMON(field
);
516 BT_LOGD("Freezing sequence field object: addr=%p", field
);
518 for (i
= 0; i
< sequence_field
->length
; i
++) {
519 struct bt_ctf_field_common
*elem_field
=
520 g_ptr_array_index(sequence_field
->elements
, i
);
522 BT_LOGD("Freezing sequence field object's element field: "
523 "element-field-addr=%p, index=%" PRId64
,
525 bt_ctf_field_common_set_is_frozen_recursive(elem_field
, is_frozen
);
528 bt_ctf_field_common_generic_set_is_frozen(field
, is_frozen
);
532 void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common
*field
,
539 BT_LOGD("Setting field object's frozen state: addr=%p, is-frozen=%d",
541 BT_ASSERT_DBG(field_type_common_has_known_id(field
->type
));
542 BT_ASSERT_DBG(field
->methods
->set_is_frozen
);
543 field
->methods
->set_is_frozen(field
, is_frozen
);
550 bt_ctf_bool
bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common
*field
)
552 return field
&& field
->payload_set
;
556 bt_ctf_bool
bt_ctf_field_common_structure_is_set_recursive(
557 struct bt_ctf_field_common
*field
)
559 bt_ctf_bool is_set
= BT_CTF_FALSE
;
561 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
563 BT_ASSERT_DBG(field
);
565 for (i
= 0; i
< structure
->fields
->len
; i
++) {
566 is_set
= bt_ctf_field_common_is_set_recursive(
567 structure
->fields
->pdata
[i
]);
578 bt_ctf_bool
bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common
*field
)
580 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
581 bt_ctf_bool is_set
= BT_CTF_FALSE
;
583 BT_ASSERT_DBG(field
);
585 if (variant
->current_field
) {
586 is_set
= bt_ctf_field_common_is_set_recursive(
587 variant
->current_field
);
594 bt_ctf_bool
bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common
*field
)
597 bt_ctf_bool is_set
= BT_CTF_FALSE
;
598 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
600 BT_ASSERT_DBG(field
);
602 for (i
= 0; i
< array
->elements
->len
; i
++) {
603 is_set
= bt_ctf_field_common_is_set_recursive(array
->elements
->pdata
[i
]);
614 bt_ctf_bool
bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common
*field
)
617 bt_ctf_bool is_set
= BT_CTF_FALSE
;
618 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
620 BT_ASSERT_DBG(field
);
622 if (!sequence
->elements
) {
626 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
627 is_set
= bt_ctf_field_common_is_set_recursive(
628 sequence
->elements
->pdata
[i
]);
638 static struct bt_ctf_field_common_methods bt_ctf_field_integer_methods
= {
639 .set_is_frozen
= bt_ctf_field_common_generic_set_is_frozen
,
640 .validate
= bt_ctf_field_common_generic_validate
,
642 .is_set
= bt_ctf_field_common_generic_is_set
,
643 .reset
= bt_ctf_field_common_generic_reset
,
646 static struct bt_ctf_field_common_methods bt_ctf_field_floating_point_methods
= {
647 .set_is_frozen
= bt_ctf_field_common_generic_set_is_frozen
,
648 .validate
= bt_ctf_field_common_generic_validate
,
650 .is_set
= bt_ctf_field_common_generic_is_set
,
651 .reset
= bt_ctf_field_common_generic_reset
,
655 void bt_ctf_field_enumeration_set_is_frozen_recursive(
656 struct bt_ctf_field_common
*field
, bool is_frozen
);
659 int bt_ctf_field_enumeration_validate_recursive(struct bt_ctf_field_common
*field
);
662 bt_ctf_bool
bt_ctf_field_enumeration_is_set_recursive(
663 struct bt_ctf_field_common
*field
);
666 void bt_ctf_field_enumeration_reset_recursive(struct bt_ctf_field_common
*field
);
668 static struct bt_ctf_field_common_methods bt_ctf_field_enumeration_methods
= {
669 .set_is_frozen
= bt_ctf_field_enumeration_set_is_frozen_recursive
,
670 .validate
= bt_ctf_field_enumeration_validate_recursive
,
672 .is_set
= bt_ctf_field_enumeration_is_set_recursive
,
673 .reset
= bt_ctf_field_enumeration_reset_recursive
,
676 static struct bt_ctf_field_common_methods bt_ctf_field_string_methods
= {
677 .set_is_frozen
= bt_ctf_field_common_generic_set_is_frozen
,
678 .validate
= bt_ctf_field_common_generic_validate
,
680 .is_set
= bt_ctf_field_common_generic_is_set
,
681 .reset
= bt_ctf_field_common_generic_reset
,
684 static struct bt_ctf_field_common_methods bt_ctf_field_structure_methods
= {
685 .set_is_frozen
= bt_ctf_field_common_structure_set_is_frozen_recursive
,
686 .validate
= bt_ctf_field_common_structure_validate_recursive
,
688 .is_set
= bt_ctf_field_common_structure_is_set_recursive
,
689 .reset
= bt_ctf_field_common_structure_reset_recursive
,
692 static struct bt_ctf_field_common_methods bt_ctf_field_sequence_methods
= {
693 .set_is_frozen
= bt_ctf_field_common_sequence_set_is_frozen_recursive
,
694 .validate
= bt_ctf_field_common_sequence_validate_recursive
,
696 .is_set
= bt_ctf_field_common_sequence_is_set_recursive
,
697 .reset
= bt_ctf_field_common_sequence_reset_recursive
,
700 static struct bt_ctf_field_common_methods bt_ctf_field_array_methods
= {
701 .set_is_frozen
= bt_ctf_field_common_array_set_is_frozen_recursive
,
702 .validate
= bt_ctf_field_common_array_validate_recursive
,
704 .is_set
= bt_ctf_field_common_array_is_set_recursive
,
705 .reset
= bt_ctf_field_common_array_reset_recursive
,
709 void bt_ctf_field_variant_set_is_frozen_recursive(struct bt_ctf_field_common
*field
,
713 int bt_ctf_field_variant_validate_recursive(struct bt_ctf_field_common
*field
);
716 bt_ctf_bool
bt_ctf_field_variant_is_set_recursive(struct bt_ctf_field_common
*field
);
719 void bt_ctf_field_variant_reset_recursive(struct bt_ctf_field_common
*field
);
721 static struct bt_ctf_field_common_methods bt_ctf_field_variant_methods
= {
722 .set_is_frozen
= bt_ctf_field_variant_set_is_frozen_recursive
,
723 .validate
= bt_ctf_field_variant_validate_recursive
,
725 .is_set
= bt_ctf_field_variant_is_set_recursive
,
726 .reset
= bt_ctf_field_variant_reset_recursive
,
730 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
733 struct bt_ctf_field
*bt_ctf_field_enumeration_create(struct bt_ctf_field_type
*);
736 struct bt_ctf_field
*bt_ctf_field_floating_point_create(struct bt_ctf_field_type
*);
739 struct bt_ctf_field
*bt_ctf_field_structure_create(struct bt_ctf_field_type
*);
742 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*);
745 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*);
748 struct bt_ctf_field
*bt_ctf_field_sequence_create(struct bt_ctf_field_type
*);
751 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
754 struct bt_ctf_field
*(* const field_create_funcs
[])(struct bt_ctf_field_type
*) = {
755 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_integer_create
,
756 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_create
,
757 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = bt_ctf_field_floating_point_create
,
758 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_structure_create
,
759 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_variant_create
,
760 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_array_create
,
761 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_create
,
762 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_string_create
,
765 typedef int (*bt_ctf_field_serialize_recursive_func
)(
766 struct bt_ctf_field_common
*, struct bt_ctfser
*,
767 enum bt_ctf_byte_order
);
770 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
772 BT_LOGD("Destroying CTF writer integer field object: addr=%p", field
);
773 bt_ctf_field_common_integer_finalize((void *) field
);
778 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
780 BT_LOGD("Destroying CTF writer floating point field object: addr=%p",
782 bt_ctf_field_common_floating_point_finalize((void *) field
);
787 void bt_ctf_field_enumeration_destroy_recursive(struct bt_ctf_field
*field
)
789 struct bt_ctf_field_enumeration
*enumeration
= BT_CTF_FROM_COMMON(field
);
791 BT_LOGD("Destroying CTF writer enumeration field object: addr=%p",
793 BT_LOGD_STR("Putting container field.");
794 bt_ctf_object_put_ref(enumeration
->container
);
795 bt_ctf_field_common_finalize((void *) field
);
800 void bt_ctf_field_structure_destroy_recursive(struct bt_ctf_field
*field
)
802 BT_LOGD("Destroying CTF writer structure field object: addr=%p", field
);
803 bt_ctf_field_common_structure_finalize_recursive((void *) field
);
808 void bt_ctf_field_variant_destroy_recursive(struct bt_ctf_field
*field
)
810 struct bt_ctf_field_variant
*variant
= BT_CTF_FROM_COMMON(field
);
812 BT_LOGD("Destroying CTF writer variant field object: addr=%p", field
);
813 BT_LOGD_STR("Putting tag field.");
814 bt_ctf_object_put_ref(variant
->tag
);
815 bt_ctf_field_common_variant_finalize_recursive((void *) field
);
820 void bt_ctf_field_array_destroy_recursive(struct bt_ctf_field
*field
)
822 BT_LOGD("Destroying CTF writer array field object: addr=%p", field
);
823 bt_ctf_field_common_array_finalize_recursive((void *) field
);
828 void bt_ctf_field_sequence_destroy_recursive(struct bt_ctf_field
*field
)
830 BT_LOGD("Destroying CTF writer sequence field object: addr=%p", field
);
831 bt_ctf_field_common_sequence_finalize_recursive((void *) field
);
836 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
838 BT_LOGD("Destroying CTF writer string field object: addr=%p", field
);
839 bt_ctf_field_common_string_finalize((void *) field
);
844 int bt_ctf_field_serialize_recursive(struct bt_ctf_field
*field
,
845 struct bt_ctfser
*ctfser
,
846 enum bt_ctf_byte_order native_byte_order
)
848 struct bt_ctf_field_common
*field_common
= (void *) field
;
849 bt_ctf_field_serialize_recursive_func serialize_func
;
851 BT_ASSERT_DBG(ctfser
);
852 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Field");
853 BT_ASSERT_DBG(field_common
->spec
.writer
.serialize_func
);
854 serialize_func
= field_common
->spec
.writer
.serialize_func
;
855 return serialize_func(field_common
, ctfser
,
860 int bt_ctf_field_integer_serialize(struct bt_ctf_field_common
*field
,
861 struct bt_ctfser
*ctfser
,
862 enum bt_ctf_byte_order native_byte_order
)
865 struct bt_ctf_field_type_common_integer
*int_type
=
866 BT_CTF_FROM_COMMON(field
->type
);
867 struct bt_ctf_field_common_integer
*int_field
=
868 BT_CTF_FROM_COMMON(field
);
869 enum bt_ctf_byte_order byte_order
;
871 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "Integer field");
872 BT_LOGT("Serializing CTF writer integer field: addr=%p, native-bo=%s",
874 bt_ctf_byte_order_string(native_byte_order
));
875 byte_order
= int_type
->user_byte_order
;
876 if (byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
877 byte_order
= native_byte_order
;
880 if (int_type
->is_signed
) {
881 ret
= bt_ctfser_write_signed_int(ctfser
,
882 int_field
->payload
.signd
, int_type
->common
.alignment
,
884 byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
?
885 LITTLE_ENDIAN
: BIG_ENDIAN
);
887 ret
= bt_ctfser_write_unsigned_int(ctfser
,
888 int_field
->payload
.unsignd
, int_type
->common
.alignment
,
890 byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
?
891 LITTLE_ENDIAN
: BIG_ENDIAN
);
894 if (G_UNLIKELY(ret
)) {
895 BT_LOGE("Cannot serialize integer field: ret=%d", ret
);
904 int bt_ctf_field_enumeration_serialize_recursive(
905 struct bt_ctf_field_common
*field
, struct bt_ctfser
*ctfser
,
906 enum bt_ctf_byte_order native_byte_order
)
908 struct bt_ctf_field_enumeration
*enumeration
= (void *) field
;
910 BT_LOGT("Serializing enumeration field: addr=%p, native-bo=%s",
911 field
, bt_ctf_byte_order_string(native_byte_order
));
912 BT_LOGT_STR("Serializing enumeration field's payload field.");
913 return bt_ctf_field_serialize_recursive(
914 (void *) enumeration
->container
, ctfser
, native_byte_order
);
918 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field_common
*field
,
919 struct bt_ctfser
*ctfser
,
920 enum bt_ctf_byte_order native_byte_order
)
923 struct bt_ctf_field_type_common_floating_point
*flt_type
=
924 BT_CTF_FROM_COMMON(field
->type
);
925 struct bt_ctf_field_common_floating_point
*flt_field
= BT_CTF_FROM_COMMON(field
);
926 enum bt_ctf_byte_order byte_order
;
928 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "Floating point number field");
929 BT_LOGT("Serializing floating point number field: "
930 "addr=%p, native-bo=%s", field
,
931 bt_ctf_byte_order_string(native_byte_order
));
933 byte_order
= flt_type
->user_byte_order
;
934 if (byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
935 byte_order
= native_byte_order
;
938 if (flt_type
->mant_dig
== FLT_MANT_DIG
) {
939 ret
= bt_ctfser_write_float32(ctfser
, flt_field
->payload
,
940 flt_type
->common
.alignment
,
941 byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
?
942 LITTLE_ENDIAN
: BIG_ENDIAN
);
943 } else if (flt_type
->mant_dig
== DBL_MANT_DIG
) {
944 ret
= bt_ctfser_write_float64(ctfser
, flt_field
->payload
,
945 flt_type
->common
.alignment
,
946 byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
?
947 LITTLE_ENDIAN
: BIG_ENDIAN
);
952 if (G_UNLIKELY(ret
)) {
953 BT_LOGE("Cannot serialize floating point number field: "
963 int bt_ctf_field_structure_serialize_recursive(struct bt_ctf_field_common
*field
,
964 struct bt_ctfser
*ctfser
,
965 enum bt_ctf_byte_order native_byte_order
)
969 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
971 BT_LOGT("Serializing structure field: addr=%p, native-bo=%s",
972 field
, bt_ctf_byte_order_string(native_byte_order
));
973 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
,
974 field
->type
->alignment
);
975 if (G_UNLIKELY(ret
)) {
976 BT_LOGE("Cannot align offset before serializing structure field: "
981 for (i
= 0; i
< structure
->fields
->len
; i
++) {
982 struct bt_ctf_field_common
*member
= g_ptr_array_index(
983 structure
->fields
, i
);
984 const char *field_name
= NULL
;
986 BT_LOGT("Serializing structure field's field: ser-offset=%" PRIu64
", "
987 "field-addr=%p, index=%" PRIu64
,
988 bt_ctfser_get_offset_in_current_packet_bits(ctfser
),
991 if (G_UNLIKELY(!member
)) {
992 ret
= bt_ctf_field_type_common_structure_borrow_field_by_index(
993 field
->type
, &field_name
, NULL
, i
);
994 BT_ASSERT_DBG(ret
== 0);
995 BT_LOGW("Cannot serialize structure field's field: field is not set: "
996 "struct-field-addr=%p, "
997 "field-name=\"%s\", index=%" PRId64
,
998 field
, field_name
, i
);
1003 ret
= bt_ctf_field_serialize_recursive((void *) member
, ctfser
,
1005 if (G_UNLIKELY(ret
)) {
1006 ret
= bt_ctf_field_type_common_structure_borrow_field_by_index(
1007 field
->type
, &field_name
, NULL
, i
);
1008 BT_ASSERT_DBG(ret
== 0);
1009 BT_LOGW("Cannot serialize structure field's field: "
1010 "struct-field-addr=%p, field-addr=%p, "
1011 "field-name=\"%s\", index=%" PRId64
,
1012 field
->type
, member
, field_name
, i
);
1022 int bt_ctf_field_variant_serialize_recursive(struct bt_ctf_field_common
*field
,
1023 struct bt_ctfser
*ctfser
,
1024 enum bt_ctf_byte_order native_byte_order
)
1026 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
1028 BT_LOGT("Serializing variant field: addr=%p, native-bo=%s",
1029 field
, bt_ctf_byte_order_string(native_byte_order
));
1030 BT_LOGT_STR("Serializing variant field's payload field.");
1031 return bt_ctf_field_serialize_recursive(
1032 (void *) variant
->current_field
, ctfser
, native_byte_order
);
1036 int bt_ctf_field_array_serialize_recursive(struct bt_ctf_field_common
*field
,
1037 struct bt_ctfser
*ctfser
,
1038 enum bt_ctf_byte_order native_byte_order
)
1042 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
1044 BT_LOGT("Serializing array field: addr=%p, native-bo=%s",
1045 field
, bt_ctf_byte_order_string(native_byte_order
));
1047 for (i
= 0; i
< array
->elements
->len
; i
++) {
1048 struct bt_ctf_field_common
*elem_field
=
1049 g_ptr_array_index(array
->elements
, i
);
1051 BT_LOGT("Serializing array field's element field: "
1052 "ser-offset=%" PRIu64
", field-addr=%p, index=%" PRId64
,
1053 bt_ctfser_get_offset_in_current_packet_bits(ctfser
),
1055 ret
= bt_ctf_field_serialize_recursive(
1056 (void *) elem_field
, ctfser
, native_byte_order
);
1057 if (G_UNLIKELY(ret
)) {
1058 BT_LOGW("Cannot serialize array field's element field: "
1059 "array-field-addr=%p, field-addr=%p, "
1060 "index=%" PRId64
, field
, elem_field
, i
);
1070 int bt_ctf_field_sequence_serialize_recursive(struct bt_ctf_field_common
*field
,
1071 struct bt_ctfser
*ctfser
,
1072 enum bt_ctf_byte_order native_byte_order
)
1076 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
1078 BT_LOGT("Serializing sequence field: addr=%p, native-bo=%s",
1079 field
, bt_ctf_byte_order_string(native_byte_order
));
1081 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1082 struct bt_ctf_field_common
*elem_field
=
1083 g_ptr_array_index(sequence
->elements
, i
);
1085 BT_LOGT("Serializing sequence field's element field: "
1086 "ser-offset=%" PRIu64
", field-addr=%p, index=%" PRId64
,
1087 bt_ctfser_get_offset_in_current_packet_bits(ctfser
),
1089 ret
= bt_ctf_field_serialize_recursive(
1090 (void *) elem_field
, ctfser
, native_byte_order
);
1091 if (G_UNLIKELY(ret
)) {
1092 BT_LOGW("Cannot serialize sequence field's element field: "
1093 "sequence-field-addr=%p, field-addr=%p, "
1094 "index=%" PRId64
, field
, elem_field
, i
);
1104 int bt_ctf_field_string_serialize(struct bt_ctf_field_common
*field
,
1105 struct bt_ctfser
*ctfser
,
1106 enum bt_ctf_byte_order native_byte_order
)
1109 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
1111 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "String field");
1112 BT_LOGT("Serializing string field: addr=%p, native-bo=%s",
1113 field
, bt_ctf_byte_order_string((int) native_byte_order
));
1114 ret
= bt_ctfser_write_string(ctfser
, (const char *) string
->buf
->data
);
1115 if (G_UNLIKELY(ret
)) {
1116 BT_LOGE("Cannot serialize string field: ret=%d", ret
);
1124 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
1126 struct bt_ctf_field
*field
= NULL
;
1127 enum bt_ctf_field_type_id type_id
;
1129 BT_CTF_ASSERT_PRE_NON_NULL(type
, "Field type");
1130 BT_ASSERT_DBG(field_type_common_has_known_id((void *) type
));
1131 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_validate((void *) type
) == 0,
1132 "Field type is invalid: ft-addr=%p", type
);
1133 type_id
= bt_ctf_field_type_get_type_id(type
);
1134 field
= field_create_funcs
[type_id
](type
);
1139 bt_ctf_field_type_common_freeze((void *) type
);
1145 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
1147 return bt_ctf_object_get_ref(bt_ctf_field_common_borrow_type((void *) field
));
1150 enum bt_ctf_field_type_id
bt_ctf_field_get_type_id(struct bt_ctf_field
*field
)
1152 struct bt_ctf_field_common
*field_common
= (void *) field
;
1154 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Field");
1155 return (int) field_common
->type
->id
;
1158 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
1159 struct bt_ctf_field
*length_field
)
1162 struct bt_ctf_field_common
*common_length_field
= (void *) length_field
;
1165 BT_CTF_ASSERT_PRE_NON_NULL(length_field
, "Length field");
1166 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET((void *) length_field
, "Length field");
1167 BT_CTF_ASSERT_PRE(common_length_field
->type
->id
== BT_CTF_FIELD_TYPE_ID_INTEGER
||
1168 common_length_field
->type
->id
== BT_CTF_FIELD_TYPE_ID_ENUM
,
1169 "Length field must be an integer or enumeration field: field-addr=%p",
1172 if (common_length_field
->type
->id
== BT_CTF_FIELD_TYPE_ID_ENUM
) {
1173 struct bt_ctf_field_enumeration
*enumeration
= (void *)
1176 length_field
= (void *) enumeration
->container
;
1179 ret
= bt_ctf_field_integer_unsigned_get_value(length_field
, &length
);
1180 BT_ASSERT_DBG(ret
== 0);
1181 return bt_ctf_field_common_sequence_set_length((void *) field
,
1182 length
, (bt_ctf_field_common_create_func
) bt_ctf_field_create
);
1185 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
1186 struct bt_ctf_field
*field
, uint64_t index
)
1188 return bt_ctf_object_get_ref(bt_ctf_field_common_structure_borrow_field_by_index(
1189 (void *) field
, index
));
1192 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_name(
1193 struct bt_ctf_field
*field
, const char *name
)
1195 return bt_ctf_object_get_ref(bt_ctf_field_common_structure_borrow_field_by_name(
1196 (void *) field
, name
));
1199 struct bt_ctf_field
*bt_ctf_field_array_get_field(
1200 struct bt_ctf_field
*field
, uint64_t index
)
1202 return bt_ctf_object_get_ref(
1203 bt_ctf_field_common_array_borrow_field((void *) field
, index
));
1206 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(
1207 struct bt_ctf_field
*field
, uint64_t index
)
1209 return bt_ctf_object_get_ref(
1210 bt_ctf_field_common_sequence_borrow_field((void *) field
, index
));
1213 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
1214 struct bt_ctf_field
*tag_field
)
1216 struct bt_ctf_field_variant
*variant_field
= (void *) field
;
1217 struct bt_ctf_field_enumeration
*enum_field
= (void *) tag_field
;
1218 struct bt_ctf_field_type_common_variant
*variant_ft
;
1219 struct bt_ctf_field_type_common_enumeration
*tag_ft
;
1220 struct bt_ctf_field
*current_field
= NULL
;
1221 bt_ctf_bool is_signed
;
1225 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Variant field");
1226 BT_CTF_ASSERT_PRE_NON_NULL(tag_field
, "Tag field");
1227 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET((void *) tag_field
, "Tag field");
1228 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(
1229 (struct bt_ctf_field_common
*) tag_field
,
1230 BT_CTF_FIELD_TYPE_ID_ENUM
, "Tag field");
1231 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(
1232 (struct bt_ctf_field_common
*) field
,
1233 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
1235 bt_ctf_field_common_validate_recursive((void *) tag_field
) == 0,
1236 "Tag field is invalid: field-addr=%p", tag_field
);
1237 variant_ft
= BT_CTF_FROM_COMMON(variant_field
->common
.common
.type
);
1238 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare(
1239 BT_CTF_TO_COMMON(variant_ft
->tag_ft
), enum_field
->common
.type
) == 0,
1240 "Unexpected tag field's type: expected-ft-addr=%p, "
1241 "tag-ft-addr=%p", variant_ft
->tag_ft
,
1242 enum_field
->common
.type
);
1243 tag_ft
= BT_CTF_FROM_COMMON(enum_field
->common
.type
);
1244 is_signed
= tag_ft
->container_ft
->is_signed
;
1249 ret
= bt_ctf_field_integer_signed_get_value(
1250 (void *) enum_field
->container
, &tag_ival
);
1251 tag_uval
= (uint64_t) tag_ival
;
1253 ret
= bt_ctf_field_integer_unsigned_get_value(
1254 (void *) enum_field
->container
, &tag_uval
);
1257 BT_ASSERT_DBG(ret
== 0);
1258 ret
= bt_ctf_field_common_variant_set_tag((void *) field
, tag_uval
,
1264 bt_ctf_object_put_ref(variant_field
->tag
);
1265 variant_field
->tag
= bt_ctf_object_get_ref(tag_field
);
1266 current_field
= bt_ctf_field_variant_get_current_field(field
);
1267 BT_ASSERT_DBG(current_field
);
1270 return current_field
;
1273 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
1274 struct bt_ctf_field
*variant_field
)
1276 return bt_ctf_object_get_ref(bt_ctf_field_common_variant_borrow_current_field(
1277 (void *) variant_field
));
1281 struct bt_ctf_field
*bt_ctf_field_enumeration_borrow_container(
1282 struct bt_ctf_field
*field
)
1284 struct bt_ctf_field_enumeration
*enumeration
= (void *) field
;
1286 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Enumeration field");
1287 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID((struct bt_ctf_field_common
*) field
,
1288 BT_CTF_FIELD_TYPE_ID_ENUM
, "Field");
1289 BT_ASSERT_DBG(enumeration
->container
);
1290 return (void *) enumeration
->container
;
1293 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
1294 struct bt_ctf_field
*field
)
1296 return bt_ctf_object_get_ref(bt_ctf_field_enumeration_borrow_container(field
));
1299 int bt_ctf_field_integer_signed_get_value(struct bt_ctf_field
*field
,
1302 struct bt_ctf_field_common_integer
*integer
= (void *) field
;
1304 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Integer field");
1305 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
1306 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(BT_CTF_TO_COMMON(integer
), "Integer field");
1307 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer
),
1308 BT_CTF_FIELD_TYPE_ID_INTEGER
, "Field");
1309 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_integer_is_signed(
1310 integer
->common
.type
),
1311 "Field's type is unsigned: field-addr=%p", field
);
1312 *value
= integer
->payload
.signd
;
1316 int bt_ctf_field_integer_signed_set_value(struct bt_ctf_field
*field
,
1320 struct bt_ctf_field_common_integer
*integer
= (void *) field
;
1321 struct bt_ctf_field_type_common_integer
*integer_type
;
1323 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Integer field");
1324 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(BT_CTF_TO_COMMON(integer
), "Integer field");
1325 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer
),
1326 BT_CTF_FIELD_TYPE_ID_INTEGER
, "Field");
1327 integer_type
= BT_CTF_FROM_COMMON(integer
->common
.type
);
1329 bt_ctf_field_type_common_integer_is_signed(integer
->common
.type
),
1330 "Field's type is unsigned: field-addr=%p", field
);
1331 BT_CTF_ASSERT_PRE(value_is_in_range_signed(integer_type
->size
, value
),
1332 "Value is out of bounds: value=%" PRId64
", field-addr=%p",
1334 integer
->payload
.signd
= value
;
1335 bt_ctf_field_common_set(BT_CTF_TO_COMMON(integer
), true);
1339 int bt_ctf_field_integer_unsigned_get_value(struct bt_ctf_field
*field
,
1342 struct bt_ctf_field_common_integer
*integer
= (void *) field
;
1344 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Integer field");
1345 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
1346 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(BT_CTF_TO_COMMON(integer
), "Integer field");
1347 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer
),
1348 BT_CTF_FIELD_TYPE_ID_INTEGER
, "Field");
1350 !bt_ctf_field_type_common_integer_is_signed(integer
->common
.type
),
1351 "Field's type is signed: field-addr=%p", field
);
1352 *value
= integer
->payload
.unsignd
;
1356 int bt_ctf_field_integer_unsigned_set_value(struct bt_ctf_field
*field
,
1359 struct bt_ctf_field_common_integer
*integer
= (void *) field
;
1360 struct bt_ctf_field_type_common_integer
*integer_type
;
1362 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Integer field");
1363 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(BT_CTF_TO_COMMON(integer
), "Integer field");
1364 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer
),
1365 BT_CTF_FIELD_TYPE_ID_INTEGER
, "Field");
1366 integer_type
= BT_CTF_FROM_COMMON(integer
->common
.type
);
1368 !bt_ctf_field_type_common_integer_is_signed(integer
->common
.type
),
1369 "Field's type is signed: field-addr=%p", field
);
1370 BT_CTF_ASSERT_PRE(value_is_in_range_unsigned(integer_type
->size
, value
),
1371 "Value is out of bounds: value=%" PRIu64
", field-addr=%p",
1373 integer
->payload
.unsignd
= value
;
1374 bt_ctf_field_common_set(BT_CTF_TO_COMMON(integer
), true);
1378 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
1381 return bt_ctf_field_common_floating_point_get_value((void *) field
, value
);
1384 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
1387 return bt_ctf_field_common_floating_point_set_value((void *) field
, value
);
1390 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
1392 return bt_ctf_field_common_string_get_value((void *) field
);
1395 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
, const char *value
)
1397 return bt_ctf_field_common_string_set_value((void *) field
, value
);
1400 int bt_ctf_field_string_append(struct bt_ctf_field
*field
, const char *value
)
1402 return bt_ctf_field_common_string_append((void *) field
, value
);
1405 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1406 const char *value
, unsigned int length
)
1408 return bt_ctf_field_common_string_append_len((void *) field
, value
, length
);
1411 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1413 return (void *) bt_ctf_field_common_copy((void *) field
);
1417 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1419 struct bt_ctf_field_common_integer
*integer
=
1420 g_new0(struct bt_ctf_field_common_integer
, 1);
1422 BT_LOGD("Creating CTF writer integer field object: ft-addr=%p", type
);
1425 bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(integer
), (void *) type
,
1427 (bt_ctf_object_release_func
) bt_ctf_field_integer_destroy
,
1428 &bt_ctf_field_integer_methods
);
1429 integer
->common
.spec
.writer
.serialize_func
=
1430 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_integer_serialize
;
1431 BT_LOGD("Created CTF writer integer field object: addr=%p, ft-addr=%p",
1434 BT_LOGE_STR("Failed to allocate one integer field.");
1437 return (void *) integer
;
1441 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1442 struct bt_ctf_field_type
*type
)
1444 struct bt_ctf_field_type_common_enumeration
*enum_ft
= (void *) type
;
1445 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1446 struct bt_ctf_field_enumeration
, 1);
1448 BT_LOGD("Creating CTF writer enumeration field object: ft-addr=%p", type
);
1451 BT_LOGE_STR("Failed to allocate one enumeration field.");
1455 bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(enumeration
),
1457 true, (bt_ctf_object_release_func
)
1458 bt_ctf_field_enumeration_destroy_recursive
,
1459 &bt_ctf_field_enumeration_methods
);
1460 enumeration
->container
= (void *) bt_ctf_field_create(
1461 BT_CTF_FROM_COMMON(enum_ft
->container_ft
));
1462 if (!enumeration
->container
) {
1463 BT_CTF_OBJECT_PUT_REF_AND_RESET(enumeration
);
1467 enumeration
->common
.spec
.writer
.serialize_func
=
1468 (bt_ctf_field_serialize_recursive_func
)
1469 bt_ctf_field_enumeration_serialize_recursive
;
1470 BT_LOGD("Created CTF writer enumeration field object: addr=%p, ft-addr=%p",
1474 return (void *) enumeration
;
1478 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1479 struct bt_ctf_field_type
*type
)
1481 struct bt_ctf_field_common_floating_point
*floating_point
;
1483 BT_LOGD("Creating CTF writer floating point number field object: ft-addr=%p", type
);
1484 floating_point
= g_new0(struct bt_ctf_field_common_floating_point
, 1);
1486 if (floating_point
) {
1487 bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(floating_point
),
1489 true, (bt_ctf_object_release_func
)
1490 bt_ctf_field_floating_point_destroy
,
1491 &bt_ctf_field_floating_point_methods
);
1492 floating_point
->common
.spec
.writer
.serialize_func
=
1493 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_floating_point_serialize
;
1494 BT_LOGD("Created CTF writer floating point number field object: addr=%p, ft-addr=%p",
1495 floating_point
, type
);
1497 BT_LOGE_STR("Failed to allocate one floating point number field.");
1500 return (void *) floating_point
;
1504 struct bt_ctf_field
*bt_ctf_field_structure_create(
1505 struct bt_ctf_field_type
*type
)
1507 struct bt_ctf_field_common_structure
*structure
= g_new0(
1508 struct bt_ctf_field_common_structure
, 1);
1511 BT_LOGD("Creating CTF writer structure field object: ft-addr=%p", type
);
1514 BT_LOGE_STR("Failed to allocate one structure field.");
1518 iret
= bt_ctf_field_common_structure_initialize(BT_CTF_TO_COMMON(structure
),
1520 true, (bt_ctf_object_release_func
)
1521 bt_ctf_field_structure_destroy_recursive
,
1522 &bt_ctf_field_structure_methods
,
1523 (bt_ctf_field_common_create_func
) bt_ctf_field_create
,
1524 (GDestroyNotify
) bt_ctf_object_put_ref
);
1525 structure
->common
.spec
.writer
.serialize_func
=
1526 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_structure_serialize_recursive
;
1528 BT_CTF_OBJECT_PUT_REF_AND_RESET(structure
);
1532 BT_LOGD("Created CTF writer structure field object: addr=%p, ft-addr=%p",
1536 return (void *) structure
;
1540 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1542 struct bt_ctf_field_type_common_variant
*var_ft
= (void *) type
;
1543 struct bt_ctf_field_variant
*variant
= g_new0(
1544 struct bt_ctf_field_variant
, 1);
1546 BT_LOGD("Creating CTF writer variant field object: ft-addr=%p", type
);
1549 BT_LOGE_STR("Failed to allocate one variant field.");
1553 bt_ctf_field_common_variant_initialize(BT_CTF_TO_COMMON(BT_CTF_TO_COMMON(variant
)),
1555 true, (bt_ctf_object_release_func
)
1556 bt_ctf_field_variant_destroy_recursive
,
1557 &bt_ctf_field_variant_methods
,
1558 (bt_ctf_field_common_create_func
) bt_ctf_field_create
,
1559 (GDestroyNotify
) bt_ctf_object_put_ref
);
1560 variant
->tag
= (void *) bt_ctf_field_create(
1561 BT_CTF_FROM_COMMON(var_ft
->tag_ft
));
1562 variant
->common
.common
.spec
.writer
.serialize_func
=
1563 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_variant_serialize_recursive
;
1564 BT_LOGD("Created CTF writer variant field object: addr=%p, ft-addr=%p",
1568 return (void *) variant
;
1572 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1574 struct bt_ctf_field_common_array
*array
=
1575 g_new0(struct bt_ctf_field_common_array
, 1);
1578 BT_LOGD("Creating CTF writer array field object: ft-addr=%p", type
);
1579 BT_ASSERT_DBG(type
);
1582 BT_LOGE_STR("Failed to allocate one array field.");
1586 ret
= bt_ctf_field_common_array_initialize(BT_CTF_TO_COMMON(array
),
1588 true, (bt_ctf_object_release_func
)
1589 bt_ctf_field_array_destroy_recursive
,
1590 &bt_ctf_field_array_methods
,
1591 (bt_ctf_field_common_create_func
) bt_ctf_field_create
,
1592 (GDestroyNotify
) bt_ctf_object_put_ref
);
1593 array
->common
.spec
.writer
.serialize_func
=
1594 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_array_serialize_recursive
;
1596 BT_CTF_OBJECT_PUT_REF_AND_RESET(array
);
1600 BT_LOGD("Created CTF writer array field object: addr=%p, ft-addr=%p",
1604 return (void *) array
;
1608 struct bt_ctf_field
*bt_ctf_field_sequence_create(struct bt_ctf_field_type
*type
)
1610 struct bt_ctf_field_common_sequence
*sequence
= g_new0(
1611 struct bt_ctf_field_common_sequence
, 1);
1613 BT_LOGD("Creating CTF writer sequence field object: ft-addr=%p", type
);
1616 bt_ctf_field_common_sequence_initialize(BT_CTF_TO_COMMON(sequence
),
1618 true, (bt_ctf_object_release_func
)
1619 bt_ctf_field_sequence_destroy_recursive
,
1620 &bt_ctf_field_sequence_methods
,
1621 (GDestroyNotify
) bt_ctf_object_put_ref
);
1622 sequence
->common
.spec
.writer
.serialize_func
=
1623 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_sequence_serialize_recursive
;
1624 BT_LOGD("Created CTF writer sequence field object: addr=%p, ft-addr=%p",
1627 BT_LOGE_STR("Failed to allocate one sequence field.");
1630 return (void *) sequence
;
1634 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1636 struct bt_ctf_field_common_string
*string
= g_new0(
1637 struct bt_ctf_field_common_string
, 1);
1639 BT_LOGD("Creating CTF writer string field object: ft-addr=%p", type
);
1642 bt_ctf_field_common_string_initialize(BT_CTF_TO_COMMON(string
),
1644 true, (bt_ctf_object_release_func
)
1645 bt_ctf_field_string_destroy
,
1646 &bt_ctf_field_string_methods
);
1647 string
->common
.spec
.writer
.serialize_func
=
1648 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_string_serialize
;
1649 BT_LOGD("Created CTF writer string field object: addr=%p, ft-addr=%p",
1652 BT_LOGE_STR("Failed to allocate one string field.");
1655 return (void *) string
;
1659 void bt_ctf_field_enumeration_set_is_frozen_recursive(
1660 struct bt_ctf_field_common
*field
, bool is_frozen
)
1662 struct bt_ctf_field_enumeration
*enumeration
= (void *) field
;
1664 if (enumeration
->container
) {
1665 bt_ctf_field_common_set_is_frozen_recursive(
1666 (void *) enumeration
->container
, is_frozen
);
1669 bt_ctf_field_common_generic_set_is_frozen((void *) field
, is_frozen
);
1673 int bt_ctf_field_enumeration_validate_recursive(struct bt_ctf_field_common
*field
)
1676 struct bt_ctf_field_enumeration
*enumeration
= (void *) field
;
1678 if (enumeration
->container
) {
1679 ret
= bt_ctf_field_common_validate_recursive(
1680 (void *) enumeration
->container
);
1687 bt_ctf_bool
bt_ctf_field_enumeration_is_set_recursive(struct bt_ctf_field_common
*field
)
1689 bt_ctf_bool is_set
= BT_CTF_FALSE
;
1690 struct bt_ctf_field_enumeration
*enumeration
= (void *) field
;
1692 if (enumeration
->container
) {
1693 is_set
= bt_ctf_field_common_is_set_recursive(
1694 (void *) enumeration
->container
);
1701 void bt_ctf_field_enumeration_reset_recursive(struct bt_ctf_field_common
*field
)
1703 struct bt_ctf_field_enumeration
*enumeration
= (void *) field
;
1705 if (enumeration
->container
) {
1706 bt_ctf_field_common_reset_recursive(
1707 (void *) enumeration
->container
);
1710 bt_ctf_field_common_generic_reset((void *) field
);
1714 void bt_ctf_field_variant_set_is_frozen_recursive(
1715 struct bt_ctf_field_common
*field
, bool is_frozen
)
1717 struct bt_ctf_field_variant
*variant
= (void *) field
;
1720 bt_ctf_field_common_set_is_frozen_recursive(
1721 (void *) variant
->tag
, is_frozen
);
1724 bt_ctf_field_common_variant_set_is_frozen_recursive((void *) field
,
1729 int bt_ctf_field_variant_validate_recursive(struct bt_ctf_field_common
*field
)
1732 struct bt_ctf_field_variant
*variant
= (void *) field
;
1735 ret
= bt_ctf_field_common_validate_recursive(
1736 (void *) variant
->tag
);
1742 ret
= bt_ctf_field_common_variant_validate_recursive((void *) field
);
1749 bt_ctf_bool
bt_ctf_field_variant_is_set_recursive(struct bt_ctf_field_common
*field
)
1752 struct bt_ctf_field_variant
*variant
= (void *) field
;
1755 is_set
= bt_ctf_field_common_is_set_recursive(
1756 (void *) variant
->tag
);
1762 is_set
= bt_ctf_field_common_variant_is_set_recursive((void *) field
);
1769 void bt_ctf_field_variant_reset_recursive(struct bt_ctf_field_common
*field
)
1771 struct bt_ctf_field_variant
*variant
= (void *) field
;
1774 bt_ctf_field_common_reset_recursive(
1775 (void *) variant
->tag
);
1778 bt_ctf_field_common_variant_reset_recursive((void *) field
);
1781 BT_CTF_ASSERT_PRE_FUNC
1782 static inline bool field_to_set_has_expected_type(
1783 struct bt_ctf_field_common
*struct_field
,
1784 const char *name
, struct bt_ctf_field_common
*value
)
1787 struct bt_ctf_field_type_common
*expected_field_type
= NULL
;
1789 expected_field_type
=
1790 bt_ctf_field_type_common_structure_borrow_field_type_by_name(
1791 struct_field
->type
, name
);
1793 if (bt_ctf_field_type_common_compare(expected_field_type
, value
->type
)) {
1794 BT_CTF_ASSERT_PRE_MSG("Value field's type is different from the expected field type: "
1795 "value-ft-addr=%p, expected-ft-addr=%p", value
->type
,
1796 expected_field_type
);
1806 int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field
*field
,
1807 const char *name
, struct bt_ctf_field
*value
)
1811 struct bt_ctf_field_common
*common_field
= (void *) field
;
1812 struct bt_ctf_field_common_structure
*structure
=
1813 BT_CTF_FROM_COMMON(common_field
);
1814 struct bt_ctf_field_common
*common_value
= (void *) value
;
1816 GHashTable
*field_name_to_index
;
1817 struct bt_ctf_field_type_common_structure
*structure_ft
;
1819 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Parent field");
1820 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Field name");
1821 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value field");
1822 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(common_field
,
1823 BT_CTF_FIELD_TYPE_ID_STRUCT
, "Parent field");
1824 BT_CTF_ASSERT_PRE(field_to_set_has_expected_type(common_field
,
1825 name
, common_value
),
1826 "Value field's type is different from the expected field type.");
1827 field_quark
= g_quark_from_string(name
);
1828 structure_ft
= BT_CTF_FROM_COMMON(common_field
->type
);
1829 field_name_to_index
= structure_ft
->field_name_to_index
;
1830 if (!g_hash_table_lookup_extended(field_name_to_index
,
1831 GUINT_TO_POINTER(field_quark
), NULL
,
1832 (gpointer
*) &index
)) {
1833 BT_LOGT("Invalid parameter: no such field in structure field's type: "
1834 "struct-field-addr=%p, struct-ft-addr=%p, "
1835 "field-ft-addr=%p, name=\"%s\"",
1836 field
, common_field
->type
, common_value
->type
, name
);
1840 bt_ctf_object_get_ref(value
);
1841 BT_CTF_OBJECT_MOVE_REF(structure
->fields
->pdata
[index
], value
);