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))
35 struct bt_ctf_field_common
*bt_ctf_field_common_copy(struct bt_ctf_field_common
*field
)
37 struct bt_ctf_field_common
*copy
= NULL
;
39 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Field");
40 BT_ASSERT_DBG(field_type_common_has_known_id(field
->type
));
41 BT_ASSERT_DBG(field
->methods
->copy
);
42 copy
= field
->methods
->copy(field
);
44 BT_LOGW("Cannot create field: ft-addr=%p", field
->type
);
48 bt_ctf_field_common_set(copy
, field
->payload_set
);
54 int bt_ctf_field_common_structure_initialize(struct bt_ctf_field_common
*field
,
55 struct bt_ctf_field_type_common
*type
,
56 bool is_shared
, bt_ctf_object_release_func release_func
,
57 struct bt_ctf_field_common_methods
*methods
,
58 bt_ctf_field_common_create_func field_create_func
,
59 GDestroyNotify field_release_func
)
62 struct bt_ctf_field_type_common_structure
*structure_type
=
63 BT_CTF_FROM_COMMON(type
);
64 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
67 BT_LOGD("Initializing common structure field object: ft-addr=%p", type
);
68 bt_ctf_field_common_initialize(field
, type
, is_shared
,
69 release_func
, methods
);
70 structure
->fields
= g_ptr_array_new_with_free_func(field_release_func
);
71 g_ptr_array_set_size(structure
->fields
, structure_type
->fields
->len
);
73 /* Create all fields contained in the structure field. */
74 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
75 struct bt_ctf_field_common
*member_field
;
76 struct bt_ctf_field_type_common_structure_field
*struct_field
=
77 BT_CTF_FIELD_TYPE_COMMON_STRUCTURE_FIELD_AT_INDEX(
79 member_field
= field_create_func(struct_field
->type
);
81 BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu",
82 g_quark_to_string(struct_field
->name
), i
);
87 g_ptr_array_index(structure
->fields
, i
) = member_field
;
90 BT_LOGD("Initialized common structure field object: addr=%p, ft-addr=%p",
97 int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common
*field
,
98 struct bt_ctf_field_type_common
*type
,
99 bool is_shared
, bt_ctf_object_release_func release_func
,
100 struct bt_ctf_field_common_methods
*methods
,
101 bt_ctf_field_common_create_func field_create_func
,
102 GDestroyNotify field_release_func
)
105 struct bt_ctf_field_type_common_variant
*variant_type
=
106 BT_CTF_FROM_COMMON(type
);
107 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
110 BT_LOGD("Initializing common variant field object: ft-addr=%p", type
);
111 bt_ctf_field_common_initialize(field
, type
, is_shared
,
112 release_func
, methods
);
113 ret
= bt_ctf_field_type_common_variant_update_choices(type
);
115 BT_LOGE("Cannot update common variant field type choices: "
120 variant
->fields
= g_ptr_array_new_with_free_func(field_release_func
);
121 g_ptr_array_set_size(variant
->fields
, variant_type
->choices
->len
);
123 /* Create all fields contained in the variant field. */
124 for (i
= 0; i
< variant_type
->choices
->len
; i
++) {
125 struct bt_ctf_field_common
*member_field
;
126 struct bt_ctf_field_type_common_variant_choice
*var_choice
=
127 BT_CTF_FIELD_TYPE_COMMON_VARIANT_CHOICE_AT_INDEX(
130 member_field
= field_create_func(var_choice
->type
);
132 BT_LOGE("Failed to create variant field's member: name=\"%s\", index=%zu",
133 g_quark_to_string(var_choice
->name
), i
);
138 g_ptr_array_index(variant
->fields
, i
) = member_field
;
141 BT_LOGD("Initialized common variant field object: addr=%p, ft-addr=%p",
148 int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common
*field
,
149 struct bt_ctf_field_type_common
*type
,
150 bool is_shared
, bt_ctf_object_release_func release_func
,
151 struct bt_ctf_field_common_methods
*methods
)
154 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
156 BT_LOGD("Initializing common string field object: ft-addr=%p", type
);
157 bt_ctf_field_common_initialize(field
, type
, is_shared
,
158 release_func
, methods
);
159 string
->buf
= g_array_sized_new(FALSE
, FALSE
, sizeof(char), 1);
165 g_array_set_size(string
->buf
, 1);
166 bt_g_array_index(string
->buf
, char, 0) = '\0';
167 BT_LOGD("Initialized common string field object: addr=%p, ft-addr=%p",
174 int bt_ctf_field_common_array_initialize(struct bt_ctf_field_common
*field
,
175 struct bt_ctf_field_type_common
*type
,
176 bool is_shared
, bt_ctf_object_release_func release_func
,
177 struct bt_ctf_field_common_methods
*methods
,
178 bt_ctf_field_common_create_func field_create_func
,
179 GDestroyNotify field_destroy_func
)
181 struct bt_ctf_field_type_common_array
*array_type
= BT_CTF_FROM_COMMON(type
);
182 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
183 unsigned int array_length
;
187 BT_LOGD("Initializing common array field object: ft-addr=%p", type
);
189 bt_ctf_field_common_initialize(field
, type
, is_shared
,
190 release_func
, methods
);
191 array_length
= array_type
->length
;
192 array
->elements
= g_ptr_array_sized_new(array_length
);
193 if (!array
->elements
) {
198 g_ptr_array_set_free_func(array
->elements
, field_destroy_func
);
199 g_ptr_array_set_size(array
->elements
, array_length
);
201 for (i
= 0; i
< array_length
; i
++) {
202 array
->elements
->pdata
[i
] = field_create_func(
203 array_type
->element_ft
);
204 if (!array
->elements
->pdata
[i
]) {
210 BT_LOGD("Initialized common array field object: addr=%p, ft-addr=%p",
217 int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common
*field
,
218 struct bt_ctf_field_type_common
*type
,
219 bool is_shared
, bt_ctf_object_release_func release_func
,
220 struct bt_ctf_field_common_methods
*methods
,
221 GDestroyNotify field_destroy_func
)
223 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
226 BT_LOGD("Initializing common sequence field object: ft-addr=%p", type
);
228 bt_ctf_field_common_initialize(field
, type
, is_shared
,
229 release_func
, methods
);
230 sequence
->elements
= g_ptr_array_new();
231 if (!sequence
->elements
) {
236 g_ptr_array_set_free_func(sequence
->elements
, field_destroy_func
);
237 BT_LOGD("Initialized common sequence field object: addr=%p, ft-addr=%p",
244 int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common
*field
)
246 return (field
&& field
->payload_set
) ? 0 : -1;
249 int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common
*field
)
253 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
255 BT_ASSERT_DBG(field
);
257 for (i
= 0; i
< structure
->fields
->len
; i
++) {
258 ret
= bt_ctf_field_common_validate_recursive(
259 (void *) structure
->fields
->pdata
[i
]);
265 this_ret
= bt_ctf_field_type_common_structure_borrow_field_by_index(
266 field
->type
, &name
, NULL
, i
);
267 BT_ASSERT_DBG(this_ret
== 0);
268 BT_CTF_ASSERT_PRE_MSG("Invalid structure field's field: "
269 "struct-field-addr=%p, field-name=\"%s\", "
270 "index=%" PRId64
", field-addr=%p",
271 field
, name
, i
, structure
->fields
->pdata
[i
]);
280 int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common
*field
)
283 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
285 BT_ASSERT_DBG(field
);
287 if (!variant
->current_field
) {
292 ret
= bt_ctf_field_common_validate_recursive(variant
->current_field
);
298 int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common
*field
)
302 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
304 BT_ASSERT_DBG(field
);
306 for (i
= 0; i
< array
->elements
->len
; i
++) {
307 ret
= bt_ctf_field_common_validate_recursive((void *) array
->elements
->pdata
[i
]);
309 BT_CTF_ASSERT_PRE_MSG("Invalid array field's element field: "
310 "array-field-addr=%p, %" PRId64
", "
311 "elem-field-addr=%p",
312 field
, i
, array
->elements
->pdata
[i
]);
321 int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common
*field
)
325 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
327 BT_ASSERT_DBG(field
);
329 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
330 ret
= bt_ctf_field_common_validate_recursive(
331 (void *) sequence
->elements
->pdata
[i
]);
333 BT_CTF_ASSERT_PRE_MSG("Invalid sequence field's element field: "
334 "seq-field-addr=%p, %" PRId64
", "
335 "elem-field-addr=%p",
336 field
, i
, sequence
->elements
->pdata
[i
]);
344 void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common
*field
)
346 BT_ASSERT_DBG(field
);
347 field
->payload_set
= false;
350 void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common
*field
)
353 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
355 BT_ASSERT_DBG(field
);
357 for (i
= 0; i
< structure
->fields
->len
; i
++) {
358 struct bt_ctf_field_common
*member
= structure
->fields
->pdata
[i
];
362 * Structure members are lazily initialized;
363 * skip if this member has not been allocated
369 bt_ctf_field_common_reset_recursive(member
);
373 void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common
*field
)
375 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
377 BT_ASSERT_DBG(field
);
378 variant
->current_field
= NULL
;
381 void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common
*field
)
384 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
386 BT_ASSERT_DBG(field
);
388 for (i
= 0; i
< array
->elements
->len
; i
++) {
389 struct bt_ctf_field_common
*member
= array
->elements
->pdata
[i
];
393 * Array elements are lazily initialized; skip
394 * if this member has not been allocated yet.
399 bt_ctf_field_common_reset_recursive(member
);
403 void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common
*field
)
405 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
408 BT_ASSERT_DBG(field
);
410 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
411 if (sequence
->elements
->pdata
[i
]) {
412 bt_ctf_field_common_reset_recursive(
413 sequence
->elements
->pdata
[i
]);
417 sequence
->length
= 0;
420 void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common
*field
,
423 field
->frozen
= is_frozen
;
426 void bt_ctf_field_common_structure_set_is_frozen_recursive(
427 struct bt_ctf_field_common
*field
, bool is_frozen
)
430 struct bt_ctf_field_common_structure
*structure_field
=
431 BT_CTF_FROM_COMMON(field
);
433 BT_LOGD("Freezing structure field object: addr=%p", field
);
435 for (i
= 0; i
< structure_field
->fields
->len
; i
++) {
436 struct bt_ctf_field_common
*struct_field
=
437 g_ptr_array_index(structure_field
->fields
, i
);
439 BT_LOGD("Freezing structure field's field: field-addr=%p, index=%" PRId64
,
441 bt_ctf_field_common_set_is_frozen_recursive(struct_field
,
445 bt_ctf_field_common_generic_set_is_frozen(field
, is_frozen
);
448 void bt_ctf_field_common_variant_set_is_frozen_recursive(
449 struct bt_ctf_field_common
*field
, bool is_frozen
)
452 struct bt_ctf_field_common_variant
*variant_field
= BT_CTF_FROM_COMMON(field
);
454 BT_LOGD("Freezing variant field object: addr=%p", field
);
456 for (i
= 0; i
< variant_field
->fields
->len
; i
++) {
457 struct bt_ctf_field_common
*var_field
=
458 g_ptr_array_index(variant_field
->fields
, i
);
460 BT_LOGD("Freezing variant field's field: field-addr=%p, index=%" PRId64
,
462 bt_ctf_field_common_set_is_frozen_recursive(var_field
, is_frozen
);
465 bt_ctf_field_common_generic_set_is_frozen(field
, is_frozen
);
468 void bt_ctf_field_common_array_set_is_frozen_recursive(
469 struct bt_ctf_field_common
*field
, bool is_frozen
)
472 struct bt_ctf_field_common_array
*array_field
= BT_CTF_FROM_COMMON(field
);
474 BT_LOGD("Freezing array field object: addr=%p", field
);
476 for (i
= 0; i
< array_field
->elements
->len
; i
++) {
477 struct bt_ctf_field_common
*elem_field
=
478 g_ptr_array_index(array_field
->elements
, i
);
480 BT_LOGD("Freezing array field object's element field: "
481 "element-field-addr=%p, index=%" PRId64
,
483 bt_ctf_field_common_set_is_frozen_recursive(elem_field
, is_frozen
);
486 bt_ctf_field_common_generic_set_is_frozen(field
, is_frozen
);
489 void bt_ctf_field_common_sequence_set_is_frozen_recursive(
490 struct bt_ctf_field_common
*field
, bool is_frozen
)
493 struct bt_ctf_field_common_sequence
*sequence_field
=
494 BT_CTF_FROM_COMMON(field
);
496 BT_LOGD("Freezing sequence field object: addr=%p", field
);
498 for (i
= 0; i
< sequence_field
->length
; i
++) {
499 struct bt_ctf_field_common
*elem_field
=
500 g_ptr_array_index(sequence_field
->elements
, i
);
502 BT_LOGD("Freezing sequence field object's element field: "
503 "element-field-addr=%p, index=%" PRId64
,
505 bt_ctf_field_common_set_is_frozen_recursive(elem_field
, is_frozen
);
508 bt_ctf_field_common_generic_set_is_frozen(field
, is_frozen
);
511 void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common
*field
,
518 BT_LOGD("Setting field object's frozen state: addr=%p, is-frozen=%d",
520 BT_ASSERT_DBG(field_type_common_has_known_id(field
->type
));
521 BT_ASSERT_DBG(field
->methods
->set_is_frozen
);
522 field
->methods
->set_is_frozen(field
, is_frozen
);
528 bt_ctf_bool
bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common
*field
)
530 return field
&& field
->payload_set
;
533 bt_ctf_bool
bt_ctf_field_common_structure_is_set_recursive(
534 struct bt_ctf_field_common
*field
)
536 bt_ctf_bool is_set
= BT_CTF_FALSE
;
538 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
540 BT_ASSERT_DBG(field
);
542 for (i
= 0; i
< structure
->fields
->len
; i
++) {
543 is_set
= bt_ctf_field_common_is_set_recursive(
544 structure
->fields
->pdata
[i
]);
554 bt_ctf_bool
bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common
*field
)
556 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
557 bt_ctf_bool is_set
= BT_CTF_FALSE
;
559 BT_ASSERT_DBG(field
);
561 if (variant
->current_field
) {
562 is_set
= bt_ctf_field_common_is_set_recursive(
563 variant
->current_field
);
569 bt_ctf_bool
bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common
*field
)
572 bt_ctf_bool is_set
= BT_CTF_FALSE
;
573 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
575 BT_ASSERT_DBG(field
);
577 for (i
= 0; i
< array
->elements
->len
; i
++) {
578 is_set
= bt_ctf_field_common_is_set_recursive(array
->elements
->pdata
[i
]);
588 bt_ctf_bool
bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common
*field
)
591 bt_ctf_bool is_set
= BT_CTF_FALSE
;
592 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
594 BT_ASSERT_DBG(field
);
596 if (!sequence
->elements
) {
600 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
601 is_set
= bt_ctf_field_common_is_set_recursive(
602 sequence
->elements
->pdata
[i
]);
612 static struct bt_ctf_field_common_methods bt_ctf_field_integer_methods
= {
613 .set_is_frozen
= bt_ctf_field_common_generic_set_is_frozen
,
614 .validate
= bt_ctf_field_common_generic_validate
,
616 .is_set
= bt_ctf_field_common_generic_is_set
,
617 .reset
= bt_ctf_field_common_generic_reset
,
620 static struct bt_ctf_field_common_methods bt_ctf_field_floating_point_methods
= {
621 .set_is_frozen
= bt_ctf_field_common_generic_set_is_frozen
,
622 .validate
= bt_ctf_field_common_generic_validate
,
624 .is_set
= bt_ctf_field_common_generic_is_set
,
625 .reset
= bt_ctf_field_common_generic_reset
,
629 void bt_ctf_field_enumeration_set_is_frozen_recursive(
630 struct bt_ctf_field_common
*field
, bool is_frozen
);
633 int bt_ctf_field_enumeration_validate_recursive(struct bt_ctf_field_common
*field
);
636 bt_ctf_bool
bt_ctf_field_enumeration_is_set_recursive(
637 struct bt_ctf_field_common
*field
);
640 void bt_ctf_field_enumeration_reset_recursive(struct bt_ctf_field_common
*field
);
642 static struct bt_ctf_field_common_methods bt_ctf_field_enumeration_methods
= {
643 .set_is_frozen
= bt_ctf_field_enumeration_set_is_frozen_recursive
,
644 .validate
= bt_ctf_field_enumeration_validate_recursive
,
646 .is_set
= bt_ctf_field_enumeration_is_set_recursive
,
647 .reset
= bt_ctf_field_enumeration_reset_recursive
,
650 static struct bt_ctf_field_common_methods bt_ctf_field_string_methods
= {
651 .set_is_frozen
= bt_ctf_field_common_generic_set_is_frozen
,
652 .validate
= bt_ctf_field_common_generic_validate
,
654 .is_set
= bt_ctf_field_common_generic_is_set
,
655 .reset
= bt_ctf_field_common_generic_reset
,
658 static struct bt_ctf_field_common_methods bt_ctf_field_structure_methods
= {
659 .set_is_frozen
= bt_ctf_field_common_structure_set_is_frozen_recursive
,
660 .validate
= bt_ctf_field_common_structure_validate_recursive
,
662 .is_set
= bt_ctf_field_common_structure_is_set_recursive
,
663 .reset
= bt_ctf_field_common_structure_reset_recursive
,
666 static struct bt_ctf_field_common_methods bt_ctf_field_sequence_methods
= {
667 .set_is_frozen
= bt_ctf_field_common_sequence_set_is_frozen_recursive
,
668 .validate
= bt_ctf_field_common_sequence_validate_recursive
,
670 .is_set
= bt_ctf_field_common_sequence_is_set_recursive
,
671 .reset
= bt_ctf_field_common_sequence_reset_recursive
,
674 static struct bt_ctf_field_common_methods bt_ctf_field_array_methods
= {
675 .set_is_frozen
= bt_ctf_field_common_array_set_is_frozen_recursive
,
676 .validate
= bt_ctf_field_common_array_validate_recursive
,
678 .is_set
= bt_ctf_field_common_array_is_set_recursive
,
679 .reset
= bt_ctf_field_common_array_reset_recursive
,
683 void bt_ctf_field_variant_set_is_frozen_recursive(struct bt_ctf_field_common
*field
,
687 int bt_ctf_field_variant_validate_recursive(struct bt_ctf_field_common
*field
);
690 bt_ctf_bool
bt_ctf_field_variant_is_set_recursive(struct bt_ctf_field_common
*field
);
693 void bt_ctf_field_variant_reset_recursive(struct bt_ctf_field_common
*field
);
695 static struct bt_ctf_field_common_methods bt_ctf_field_variant_methods
= {
696 .set_is_frozen
= bt_ctf_field_variant_set_is_frozen_recursive
,
697 .validate
= bt_ctf_field_variant_validate_recursive
,
699 .is_set
= bt_ctf_field_variant_is_set_recursive
,
700 .reset
= bt_ctf_field_variant_reset_recursive
,
704 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
707 struct bt_ctf_field
*bt_ctf_field_enumeration_create(struct bt_ctf_field_type
*);
710 struct bt_ctf_field
*bt_ctf_field_floating_point_create(struct bt_ctf_field_type
*);
713 struct bt_ctf_field
*bt_ctf_field_structure_create(struct bt_ctf_field_type
*);
716 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*);
719 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*);
722 struct bt_ctf_field
*bt_ctf_field_sequence_create(struct bt_ctf_field_type
*);
725 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
728 struct bt_ctf_field
*(* const field_create_funcs
[])(struct bt_ctf_field_type
*) = {
729 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_integer_create
,
730 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_create
,
731 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = bt_ctf_field_floating_point_create
,
732 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_structure_create
,
733 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_variant_create
,
734 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_array_create
,
735 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_create
,
736 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_string_create
,
739 typedef int (*bt_ctf_field_serialize_recursive_func
)(
740 struct bt_ctf_field_common
*, struct bt_ctfser
*,
741 enum bt_ctf_byte_order
);
744 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
746 BT_LOGD("Destroying CTF writer integer field object: addr=%p", field
);
747 bt_ctf_field_common_integer_finalize((void *) field
);
752 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
754 BT_LOGD("Destroying CTF writer floating point field object: addr=%p",
756 bt_ctf_field_common_floating_point_finalize((void *) field
);
761 void bt_ctf_field_enumeration_destroy_recursive(struct bt_ctf_field
*field
)
763 struct bt_ctf_field_enumeration
*enumeration
= BT_CTF_FROM_COMMON(field
);
765 BT_LOGD("Destroying CTF writer enumeration field object: addr=%p",
767 BT_LOGD_STR("Putting container field.");
768 bt_ctf_object_put_ref(enumeration
->container
);
769 bt_ctf_field_common_finalize((void *) field
);
774 void bt_ctf_field_structure_destroy_recursive(struct bt_ctf_field
*field
)
776 BT_LOGD("Destroying CTF writer structure field object: addr=%p", field
);
777 bt_ctf_field_common_structure_finalize_recursive((void *) field
);
782 void bt_ctf_field_variant_destroy_recursive(struct bt_ctf_field
*field
)
784 struct bt_ctf_field_variant
*variant
= BT_CTF_FROM_COMMON(field
);
786 BT_LOGD("Destroying CTF writer variant field object: addr=%p", field
);
787 BT_LOGD_STR("Putting tag field.");
788 bt_ctf_object_put_ref(variant
->tag
);
789 bt_ctf_field_common_variant_finalize_recursive((void *) field
);
794 void bt_ctf_field_array_destroy_recursive(struct bt_ctf_field
*field
)
796 BT_LOGD("Destroying CTF writer array field object: addr=%p", field
);
797 bt_ctf_field_common_array_finalize_recursive((void *) field
);
802 void bt_ctf_field_sequence_destroy_recursive(struct bt_ctf_field
*field
)
804 BT_LOGD("Destroying CTF writer sequence field object: addr=%p", field
);
805 bt_ctf_field_common_sequence_finalize_recursive((void *) field
);
810 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
812 BT_LOGD("Destroying CTF writer string field object: addr=%p", field
);
813 bt_ctf_field_common_string_finalize((void *) field
);
817 int bt_ctf_field_serialize_recursive(struct bt_ctf_field
*field
,
818 struct bt_ctfser
*ctfser
,
819 enum bt_ctf_byte_order native_byte_order
)
821 struct bt_ctf_field_common
*field_common
= (void *) field
;
822 bt_ctf_field_serialize_recursive_func serialize_func
;
824 BT_ASSERT_DBG(ctfser
);
825 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Field");
826 BT_ASSERT_DBG(field_common
->spec
.writer
.serialize_func
);
827 serialize_func
= field_common
->spec
.writer
.serialize_func
;
828 return serialize_func(field_common
, ctfser
,
833 int bt_ctf_field_integer_serialize(struct bt_ctf_field_common
*field
,
834 struct bt_ctfser
*ctfser
,
835 enum bt_ctf_byte_order native_byte_order
)
838 struct bt_ctf_field_type_common_integer
*int_type
=
839 BT_CTF_FROM_COMMON(field
->type
);
840 struct bt_ctf_field_common_integer
*int_field
=
841 BT_CTF_FROM_COMMON(field
);
842 enum bt_ctf_byte_order byte_order
;
844 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "Integer field");
845 BT_LOGT("Serializing CTF writer integer field: addr=%p, native-bo=%s",
847 bt_ctf_byte_order_string(native_byte_order
));
848 byte_order
= int_type
->user_byte_order
;
849 if (byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
850 byte_order
= native_byte_order
;
853 if (int_type
->is_signed
) {
854 ret
= bt_ctfser_write_signed_int(ctfser
,
855 int_field
->payload
.signd
, int_type
->common
.alignment
,
857 byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
?
858 LITTLE_ENDIAN
: BIG_ENDIAN
);
860 ret
= bt_ctfser_write_unsigned_int(ctfser
,
861 int_field
->payload
.unsignd
, int_type
->common
.alignment
,
863 byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
?
864 LITTLE_ENDIAN
: BIG_ENDIAN
);
867 if (G_UNLIKELY(ret
)) {
868 BT_LOGE("Cannot serialize integer field: ret=%d", ret
);
877 int bt_ctf_field_enumeration_serialize_recursive(
878 struct bt_ctf_field_common
*field
, struct bt_ctfser
*ctfser
,
879 enum bt_ctf_byte_order native_byte_order
)
881 struct bt_ctf_field_enumeration
*enumeration
= (void *) field
;
883 BT_LOGT("Serializing enumeration field: addr=%p, native-bo=%s",
884 field
, bt_ctf_byte_order_string(native_byte_order
));
885 BT_LOGT_STR("Serializing enumeration field's payload field.");
886 return bt_ctf_field_serialize_recursive(
887 (void *) enumeration
->container
, ctfser
, native_byte_order
);
891 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field_common
*field
,
892 struct bt_ctfser
*ctfser
,
893 enum bt_ctf_byte_order native_byte_order
)
896 struct bt_ctf_field_type_common_floating_point
*flt_type
=
897 BT_CTF_FROM_COMMON(field
->type
);
898 struct bt_ctf_field_common_floating_point
*flt_field
= BT_CTF_FROM_COMMON(field
);
899 enum bt_ctf_byte_order byte_order
;
901 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "Floating point number field");
902 BT_LOGT("Serializing floating point number field: "
903 "addr=%p, native-bo=%s", field
,
904 bt_ctf_byte_order_string(native_byte_order
));
906 byte_order
= flt_type
->user_byte_order
;
907 if (byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
908 byte_order
= native_byte_order
;
911 if (flt_type
->mant_dig
== FLT_MANT_DIG
) {
912 ret
= bt_ctfser_write_float32(ctfser
, flt_field
->payload
,
913 flt_type
->common
.alignment
,
914 byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
?
915 LITTLE_ENDIAN
: BIG_ENDIAN
);
916 } else if (flt_type
->mant_dig
== DBL_MANT_DIG
) {
917 ret
= bt_ctfser_write_float64(ctfser
, flt_field
->payload
,
918 flt_type
->common
.alignment
,
919 byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
?
920 LITTLE_ENDIAN
: BIG_ENDIAN
);
925 if (G_UNLIKELY(ret
)) {
926 BT_LOGE("Cannot serialize floating point number field: "
936 int bt_ctf_field_structure_serialize_recursive(struct bt_ctf_field_common
*field
,
937 struct bt_ctfser
*ctfser
,
938 enum bt_ctf_byte_order native_byte_order
)
942 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
944 BT_LOGT("Serializing structure field: addr=%p, native-bo=%s",
945 field
, bt_ctf_byte_order_string(native_byte_order
));
946 ret
= bt_ctfser_align_offset_in_current_packet(ctfser
,
947 field
->type
->alignment
);
948 if (G_UNLIKELY(ret
)) {
949 BT_LOGE("Cannot align offset before serializing structure field: "
954 for (i
= 0; i
< structure
->fields
->len
; i
++) {
955 struct bt_ctf_field_common
*member
= g_ptr_array_index(
956 structure
->fields
, i
);
957 const char *field_name
= NULL
;
959 BT_LOGT("Serializing structure field's field: ser-offset=%" PRIu64
", "
960 "field-addr=%p, index=%" PRIu64
,
961 bt_ctfser_get_offset_in_current_packet_bits(ctfser
),
964 if (G_UNLIKELY(!member
)) {
965 ret
= bt_ctf_field_type_common_structure_borrow_field_by_index(
966 field
->type
, &field_name
, NULL
, i
);
967 BT_ASSERT_DBG(ret
== 0);
968 BT_LOGW("Cannot serialize structure field's field: field is not set: "
969 "struct-field-addr=%p, "
970 "field-name=\"%s\", index=%" PRId64
,
971 field
, field_name
, i
);
976 ret
= bt_ctf_field_serialize_recursive((void *) member
, ctfser
,
978 if (G_UNLIKELY(ret
)) {
979 ret
= bt_ctf_field_type_common_structure_borrow_field_by_index(
980 field
->type
, &field_name
, NULL
, i
);
981 BT_ASSERT_DBG(ret
== 0);
982 BT_LOGW("Cannot serialize structure field's field: "
983 "struct-field-addr=%p, field-addr=%p, "
984 "field-name=\"%s\", index=%" PRId64
,
985 field
->type
, member
, field_name
, i
);
995 int bt_ctf_field_variant_serialize_recursive(struct bt_ctf_field_common
*field
,
996 struct bt_ctfser
*ctfser
,
997 enum bt_ctf_byte_order native_byte_order
)
999 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
1001 BT_LOGT("Serializing variant field: addr=%p, native-bo=%s",
1002 field
, bt_ctf_byte_order_string(native_byte_order
));
1003 BT_LOGT_STR("Serializing variant field's payload field.");
1004 return bt_ctf_field_serialize_recursive(
1005 (void *) variant
->current_field
, ctfser
, native_byte_order
);
1009 int bt_ctf_field_array_serialize_recursive(struct bt_ctf_field_common
*field
,
1010 struct bt_ctfser
*ctfser
,
1011 enum bt_ctf_byte_order native_byte_order
)
1015 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
1017 BT_LOGT("Serializing array field: addr=%p, native-bo=%s",
1018 field
, bt_ctf_byte_order_string(native_byte_order
));
1020 for (i
= 0; i
< array
->elements
->len
; i
++) {
1021 struct bt_ctf_field_common
*elem_field
=
1022 g_ptr_array_index(array
->elements
, i
);
1024 BT_LOGT("Serializing array field's element field: "
1025 "ser-offset=%" PRIu64
", field-addr=%p, index=%" PRId64
,
1026 bt_ctfser_get_offset_in_current_packet_bits(ctfser
),
1028 ret
= bt_ctf_field_serialize_recursive(
1029 (void *) elem_field
, ctfser
, native_byte_order
);
1030 if (G_UNLIKELY(ret
)) {
1031 BT_LOGW("Cannot serialize array field's element field: "
1032 "array-field-addr=%p, field-addr=%p, "
1033 "index=%" PRId64
, field
, elem_field
, i
);
1043 int bt_ctf_field_sequence_serialize_recursive(struct bt_ctf_field_common
*field
,
1044 struct bt_ctfser
*ctfser
,
1045 enum bt_ctf_byte_order native_byte_order
)
1049 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
1051 BT_LOGT("Serializing sequence field: addr=%p, native-bo=%s",
1052 field
, bt_ctf_byte_order_string(native_byte_order
));
1054 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1055 struct bt_ctf_field_common
*elem_field
=
1056 g_ptr_array_index(sequence
->elements
, i
);
1058 BT_LOGT("Serializing sequence field's element field: "
1059 "ser-offset=%" PRIu64
", field-addr=%p, index=%" PRId64
,
1060 bt_ctfser_get_offset_in_current_packet_bits(ctfser
),
1062 ret
= bt_ctf_field_serialize_recursive(
1063 (void *) elem_field
, ctfser
, native_byte_order
);
1064 if (G_UNLIKELY(ret
)) {
1065 BT_LOGW("Cannot serialize sequence field's element field: "
1066 "sequence-field-addr=%p, field-addr=%p, "
1067 "index=%" PRId64
, field
, elem_field
, i
);
1077 int bt_ctf_field_string_serialize(struct bt_ctf_field_common
*field
,
1078 struct bt_ctfser
*ctfser
,
1079 enum bt_ctf_byte_order native_byte_order
)
1082 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
1084 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "String field");
1085 BT_LOGT("Serializing string field: addr=%p, native-bo=%s",
1086 field
, bt_ctf_byte_order_string((int) native_byte_order
));
1087 ret
= bt_ctfser_write_string(ctfser
, (const char *) string
->buf
->data
);
1088 if (G_UNLIKELY(ret
)) {
1089 BT_LOGE("Cannot serialize string field: ret=%d", ret
);
1098 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
1100 struct bt_ctf_field
*field
= NULL
;
1101 enum bt_ctf_field_type_id type_id
;
1103 BT_CTF_ASSERT_PRE_NON_NULL(type
, "Field type");
1104 BT_ASSERT_DBG(field_type_common_has_known_id((void *) type
));
1105 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_validate((void *) type
) == 0,
1106 "Field type is invalid: ft-addr=%p", type
);
1107 type_id
= bt_ctf_field_type_get_type_id(type
);
1108 field
= field_create_funcs
[type_id
](type
);
1113 bt_ctf_field_type_common_freeze((void *) type
);
1120 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
1122 return bt_ctf_object_get_ref(bt_ctf_field_common_borrow_type((void *) field
));
1126 enum bt_ctf_field_type_id
bt_ctf_field_get_type_id(struct bt_ctf_field
*field
)
1128 struct bt_ctf_field_common
*field_common
= (void *) field
;
1130 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Field");
1131 return (int) field_common
->type
->id
;
1135 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
1136 struct bt_ctf_field
*length_field
)
1139 struct bt_ctf_field_common
*common_length_field
= (void *) length_field
;
1142 BT_CTF_ASSERT_PRE_NON_NULL(length_field
, "Length field");
1143 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET((void *) length_field
, "Length field");
1144 BT_CTF_ASSERT_PRE(common_length_field
->type
->id
== BT_CTF_FIELD_TYPE_ID_INTEGER
||
1145 common_length_field
->type
->id
== BT_CTF_FIELD_TYPE_ID_ENUM
,
1146 "Length field must be an integer or enumeration field: field-addr=%p",
1149 if (common_length_field
->type
->id
== BT_CTF_FIELD_TYPE_ID_ENUM
) {
1150 struct bt_ctf_field_enumeration
*enumeration
= (void *)
1153 length_field
= (void *) enumeration
->container
;
1156 ret
= bt_ctf_field_integer_unsigned_get_value(length_field
, &length
);
1157 BT_ASSERT_DBG(ret
== 0);
1158 return bt_ctf_field_common_sequence_set_length((void *) field
,
1159 length
, (bt_ctf_field_common_create_func
) bt_ctf_field_create
);
1163 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
1164 struct bt_ctf_field
*field
, uint64_t index
)
1166 return bt_ctf_object_get_ref(bt_ctf_field_common_structure_borrow_field_by_index(
1167 (void *) field
, index
));
1171 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_name(
1172 struct bt_ctf_field
*field
, const char *name
)
1174 return bt_ctf_object_get_ref(bt_ctf_field_common_structure_borrow_field_by_name(
1175 (void *) field
, name
));
1179 struct bt_ctf_field
*bt_ctf_field_array_get_field(
1180 struct bt_ctf_field
*field
, uint64_t index
)
1182 return bt_ctf_object_get_ref(
1183 bt_ctf_field_common_array_borrow_field((void *) field
, index
));
1187 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(
1188 struct bt_ctf_field
*field
, uint64_t index
)
1190 return bt_ctf_object_get_ref(
1191 bt_ctf_field_common_sequence_borrow_field((void *) field
, index
));
1195 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
1196 struct bt_ctf_field
*tag_field
)
1198 struct bt_ctf_field_variant
*variant_field
= (void *) field
;
1199 struct bt_ctf_field_enumeration
*enum_field
= (void *) tag_field
;
1200 struct bt_ctf_field_type_common_variant
*variant_ft
;
1201 struct bt_ctf_field_type_common_enumeration
*tag_ft
;
1202 struct bt_ctf_field
*current_field
= NULL
;
1203 bt_ctf_bool is_signed
;
1207 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Variant field");
1208 BT_CTF_ASSERT_PRE_NON_NULL(tag_field
, "Tag field");
1209 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET((void *) tag_field
, "Tag field");
1210 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(
1211 (struct bt_ctf_field_common
*) tag_field
,
1212 BT_CTF_FIELD_TYPE_ID_ENUM
, "Tag field");
1213 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(
1214 (struct bt_ctf_field_common
*) field
,
1215 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
1217 bt_ctf_field_common_validate_recursive((void *) tag_field
) == 0,
1218 "Tag field is invalid: field-addr=%p", tag_field
);
1219 variant_ft
= BT_CTF_FROM_COMMON(variant_field
->common
.common
.type
);
1220 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare(
1221 BT_CTF_TO_COMMON(variant_ft
->tag_ft
), enum_field
->common
.type
) == 0,
1222 "Unexpected tag field's type: expected-ft-addr=%p, "
1223 "tag-ft-addr=%p", variant_ft
->tag_ft
,
1224 enum_field
->common
.type
);
1225 tag_ft
= BT_CTF_FROM_COMMON(enum_field
->common
.type
);
1226 is_signed
= tag_ft
->container_ft
->is_signed
;
1231 ret
= bt_ctf_field_integer_signed_get_value(
1232 (void *) enum_field
->container
, &tag_ival
);
1233 tag_uval
= (uint64_t) tag_ival
;
1235 ret
= bt_ctf_field_integer_unsigned_get_value(
1236 (void *) enum_field
->container
, &tag_uval
);
1239 BT_ASSERT_DBG(ret
== 0);
1240 ret
= bt_ctf_field_common_variant_set_tag((void *) field
, tag_uval
,
1246 bt_ctf_object_put_ref(variant_field
->tag
);
1247 variant_field
->tag
= bt_ctf_object_get_ref(tag_field
);
1248 current_field
= bt_ctf_field_variant_get_current_field(field
);
1249 BT_ASSERT_DBG(current_field
);
1252 return current_field
;
1256 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
1257 struct bt_ctf_field
*variant_field
)
1259 return bt_ctf_object_get_ref(bt_ctf_field_common_variant_borrow_current_field(
1260 (void *) variant_field
));
1263 struct bt_ctf_field
*bt_ctf_field_enumeration_borrow_container(
1264 struct bt_ctf_field
*field
)
1266 struct bt_ctf_field_enumeration
*enumeration
= (void *) field
;
1268 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Enumeration field");
1269 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID((struct bt_ctf_field_common
*) field
,
1270 BT_CTF_FIELD_TYPE_ID_ENUM
, "Field");
1271 BT_ASSERT_DBG(enumeration
->container
);
1272 return (void *) enumeration
->container
;
1276 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
1277 struct bt_ctf_field
*field
)
1279 return bt_ctf_object_get_ref(bt_ctf_field_enumeration_borrow_container(field
));
1283 int bt_ctf_field_integer_signed_get_value(struct bt_ctf_field
*field
,
1286 struct bt_ctf_field_common_integer
*integer
= (void *) field
;
1288 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Integer field");
1289 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
1290 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(BT_CTF_TO_COMMON(integer
), "Integer field");
1291 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer
),
1292 BT_CTF_FIELD_TYPE_ID_INTEGER
, "Field");
1293 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_integer_is_signed(
1294 integer
->common
.type
),
1295 "Field's type is unsigned: field-addr=%p", field
);
1296 *value
= integer
->payload
.signd
;
1301 int bt_ctf_field_integer_signed_set_value(struct bt_ctf_field
*field
,
1305 struct bt_ctf_field_common_integer
*integer
= (void *) field
;
1306 struct bt_ctf_field_type_common_integer
*integer_type
;
1308 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Integer field");
1309 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(BT_CTF_TO_COMMON(integer
), "Integer field");
1310 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer
),
1311 BT_CTF_FIELD_TYPE_ID_INTEGER
, "Field");
1312 integer_type
= BT_CTF_FROM_COMMON(integer
->common
.type
);
1314 bt_ctf_field_type_common_integer_is_signed(integer
->common
.type
),
1315 "Field's type is unsigned: field-addr=%p", field
);
1316 BT_CTF_ASSERT_PRE(value_is_in_range_signed(integer_type
->size
, value
),
1317 "Value is out of bounds: value=%" PRId64
", field-addr=%p",
1319 integer
->payload
.signd
= value
;
1320 bt_ctf_field_common_set(BT_CTF_TO_COMMON(integer
), true);
1325 int bt_ctf_field_integer_unsigned_get_value(struct bt_ctf_field
*field
,
1328 struct bt_ctf_field_common_integer
*integer
= (void *) field
;
1330 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Integer field");
1331 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
1332 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(BT_CTF_TO_COMMON(integer
), "Integer field");
1333 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer
),
1334 BT_CTF_FIELD_TYPE_ID_INTEGER
, "Field");
1336 !bt_ctf_field_type_common_integer_is_signed(integer
->common
.type
),
1337 "Field's type is signed: field-addr=%p", field
);
1338 *value
= integer
->payload
.unsignd
;
1343 int bt_ctf_field_integer_unsigned_set_value(struct bt_ctf_field
*field
,
1346 struct bt_ctf_field_common_integer
*integer
= (void *) field
;
1347 struct bt_ctf_field_type_common_integer
*integer_type
;
1349 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Integer field");
1350 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(BT_CTF_TO_COMMON(integer
), "Integer field");
1351 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer
),
1352 BT_CTF_FIELD_TYPE_ID_INTEGER
, "Field");
1353 integer_type
= BT_CTF_FROM_COMMON(integer
->common
.type
);
1355 !bt_ctf_field_type_common_integer_is_signed(integer
->common
.type
),
1356 "Field's type is signed: field-addr=%p", field
);
1357 BT_CTF_ASSERT_PRE(value_is_in_range_unsigned(integer_type
->size
, value
),
1358 "Value is out of bounds: value=%" PRIu64
", field-addr=%p",
1360 integer
->payload
.unsignd
= value
;
1361 bt_ctf_field_common_set(BT_CTF_TO_COMMON(integer
), true);
1366 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
1369 return bt_ctf_field_common_floating_point_get_value((void *) field
, value
);
1373 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
1376 return bt_ctf_field_common_floating_point_set_value((void *) field
, value
);
1380 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
1382 return bt_ctf_field_common_string_get_value((void *) field
);
1386 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
, const char *value
)
1388 return bt_ctf_field_common_string_set_value((void *) field
, value
);
1392 int bt_ctf_field_string_append(struct bt_ctf_field
*field
, const char *value
)
1394 return bt_ctf_field_common_string_append((void *) field
, value
);
1398 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1399 const char *value
, unsigned int length
)
1401 return bt_ctf_field_common_string_append_len((void *) field
, value
, length
);
1405 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1407 return (void *) bt_ctf_field_common_copy((void *) field
);
1411 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1413 struct bt_ctf_field_common_integer
*integer
=
1414 g_new0(struct bt_ctf_field_common_integer
, 1);
1416 BT_LOGD("Creating CTF writer integer field object: ft-addr=%p", type
);
1419 bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(integer
), (void *) type
,
1421 (bt_ctf_object_release_func
) bt_ctf_field_integer_destroy
,
1422 &bt_ctf_field_integer_methods
);
1423 integer
->common
.spec
.writer
.serialize_func
=
1424 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_integer_serialize
;
1425 BT_LOGD("Created CTF writer integer field object: addr=%p, ft-addr=%p",
1428 BT_LOGE_STR("Failed to allocate one integer field.");
1431 return (void *) integer
;
1435 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1436 struct bt_ctf_field_type
*type
)
1438 struct bt_ctf_field_type_common_enumeration
*enum_ft
= (void *) type
;
1439 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1440 struct bt_ctf_field_enumeration
, 1);
1442 BT_LOGD("Creating CTF writer enumeration field object: ft-addr=%p", type
);
1445 BT_LOGE_STR("Failed to allocate one enumeration field.");
1449 bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(enumeration
),
1451 true, (bt_ctf_object_release_func
)
1452 bt_ctf_field_enumeration_destroy_recursive
,
1453 &bt_ctf_field_enumeration_methods
);
1454 enumeration
->container
= (void *) bt_ctf_field_create(
1455 BT_CTF_FROM_COMMON(enum_ft
->container_ft
));
1456 if (!enumeration
->container
) {
1457 BT_CTF_OBJECT_PUT_REF_AND_RESET(enumeration
);
1461 enumeration
->common
.spec
.writer
.serialize_func
=
1462 (bt_ctf_field_serialize_recursive_func
)
1463 bt_ctf_field_enumeration_serialize_recursive
;
1464 BT_LOGD("Created CTF writer enumeration field object: addr=%p, ft-addr=%p",
1468 return (void *) enumeration
;
1472 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1473 struct bt_ctf_field_type
*type
)
1475 struct bt_ctf_field_common_floating_point
*floating_point
;
1477 BT_LOGD("Creating CTF writer floating point number field object: ft-addr=%p", type
);
1478 floating_point
= g_new0(struct bt_ctf_field_common_floating_point
, 1);
1480 if (floating_point
) {
1481 bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(floating_point
),
1483 true, (bt_ctf_object_release_func
)
1484 bt_ctf_field_floating_point_destroy
,
1485 &bt_ctf_field_floating_point_methods
);
1486 floating_point
->common
.spec
.writer
.serialize_func
=
1487 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_floating_point_serialize
;
1488 BT_LOGD("Created CTF writer floating point number field object: addr=%p, ft-addr=%p",
1489 floating_point
, type
);
1491 BT_LOGE_STR("Failed to allocate one floating point number field.");
1494 return (void *) floating_point
;
1498 struct bt_ctf_field
*bt_ctf_field_structure_create(
1499 struct bt_ctf_field_type
*type
)
1501 struct bt_ctf_field_common_structure
*structure
= g_new0(
1502 struct bt_ctf_field_common_structure
, 1);
1505 BT_LOGD("Creating CTF writer structure field object: ft-addr=%p", type
);
1508 BT_LOGE_STR("Failed to allocate one structure field.");
1512 iret
= bt_ctf_field_common_structure_initialize(BT_CTF_TO_COMMON(structure
),
1514 true, (bt_ctf_object_release_func
)
1515 bt_ctf_field_structure_destroy_recursive
,
1516 &bt_ctf_field_structure_methods
,
1517 (bt_ctf_field_common_create_func
) bt_ctf_field_create
,
1518 (GDestroyNotify
) bt_ctf_object_put_ref
);
1519 structure
->common
.spec
.writer
.serialize_func
=
1520 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_structure_serialize_recursive
;
1522 BT_CTF_OBJECT_PUT_REF_AND_RESET(structure
);
1526 BT_LOGD("Created CTF writer structure field object: addr=%p, ft-addr=%p",
1530 return (void *) structure
;
1534 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1536 struct bt_ctf_field_type_common_variant
*var_ft
= (void *) type
;
1537 struct bt_ctf_field_variant
*variant
= g_new0(
1538 struct bt_ctf_field_variant
, 1);
1540 BT_LOGD("Creating CTF writer variant field object: ft-addr=%p", type
);
1543 BT_LOGE_STR("Failed to allocate one variant field.");
1547 bt_ctf_field_common_variant_initialize(BT_CTF_TO_COMMON(BT_CTF_TO_COMMON(variant
)),
1549 true, (bt_ctf_object_release_func
)
1550 bt_ctf_field_variant_destroy_recursive
,
1551 &bt_ctf_field_variant_methods
,
1552 (bt_ctf_field_common_create_func
) bt_ctf_field_create
,
1553 (GDestroyNotify
) bt_ctf_object_put_ref
);
1554 variant
->tag
= (void *) bt_ctf_field_create(
1555 BT_CTF_FROM_COMMON(var_ft
->tag_ft
));
1556 variant
->common
.common
.spec
.writer
.serialize_func
=
1557 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_variant_serialize_recursive
;
1558 BT_LOGD("Created CTF writer variant field object: addr=%p, ft-addr=%p",
1562 return (void *) variant
;
1566 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1568 struct bt_ctf_field_common_array
*array
=
1569 g_new0(struct bt_ctf_field_common_array
, 1);
1572 BT_LOGD("Creating CTF writer array field object: ft-addr=%p", type
);
1573 BT_ASSERT_DBG(type
);
1576 BT_LOGE_STR("Failed to allocate one array field.");
1580 ret
= bt_ctf_field_common_array_initialize(BT_CTF_TO_COMMON(array
),
1582 true, (bt_ctf_object_release_func
)
1583 bt_ctf_field_array_destroy_recursive
,
1584 &bt_ctf_field_array_methods
,
1585 (bt_ctf_field_common_create_func
) bt_ctf_field_create
,
1586 (GDestroyNotify
) bt_ctf_object_put_ref
);
1587 array
->common
.spec
.writer
.serialize_func
=
1588 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_array_serialize_recursive
;
1590 BT_CTF_OBJECT_PUT_REF_AND_RESET(array
);
1594 BT_LOGD("Created CTF writer array field object: addr=%p, ft-addr=%p",
1598 return (void *) array
;
1602 struct bt_ctf_field
*bt_ctf_field_sequence_create(struct bt_ctf_field_type
*type
)
1604 struct bt_ctf_field_common_sequence
*sequence
= g_new0(
1605 struct bt_ctf_field_common_sequence
, 1);
1607 BT_LOGD("Creating CTF writer sequence field object: ft-addr=%p", type
);
1610 bt_ctf_field_common_sequence_initialize(BT_CTF_TO_COMMON(sequence
),
1612 true, (bt_ctf_object_release_func
)
1613 bt_ctf_field_sequence_destroy_recursive
,
1614 &bt_ctf_field_sequence_methods
,
1615 (GDestroyNotify
) bt_ctf_object_put_ref
);
1616 sequence
->common
.spec
.writer
.serialize_func
=
1617 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_sequence_serialize_recursive
;
1618 BT_LOGD("Created CTF writer sequence field object: addr=%p, ft-addr=%p",
1621 BT_LOGE_STR("Failed to allocate one sequence field.");
1624 return (void *) sequence
;
1628 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1630 struct bt_ctf_field_common_string
*string
= g_new0(
1631 struct bt_ctf_field_common_string
, 1);
1633 BT_LOGD("Creating CTF writer string field object: ft-addr=%p", type
);
1636 bt_ctf_field_common_string_initialize(BT_CTF_TO_COMMON(string
),
1638 true, (bt_ctf_object_release_func
)
1639 bt_ctf_field_string_destroy
,
1640 &bt_ctf_field_string_methods
);
1641 string
->common
.spec
.writer
.serialize_func
=
1642 (bt_ctf_field_serialize_recursive_func
) bt_ctf_field_string_serialize
;
1643 BT_LOGD("Created CTF writer string field object: addr=%p, ft-addr=%p",
1646 BT_LOGE_STR("Failed to allocate one string field.");
1649 return (void *) string
;
1653 void bt_ctf_field_enumeration_set_is_frozen_recursive(
1654 struct bt_ctf_field_common
*field
, bool is_frozen
)
1656 struct bt_ctf_field_enumeration
*enumeration
= (void *) field
;
1658 if (enumeration
->container
) {
1659 bt_ctf_field_common_set_is_frozen_recursive(
1660 (void *) enumeration
->container
, is_frozen
);
1663 bt_ctf_field_common_generic_set_is_frozen((void *) field
, is_frozen
);
1667 int bt_ctf_field_enumeration_validate_recursive(struct bt_ctf_field_common
*field
)
1670 struct bt_ctf_field_enumeration
*enumeration
= (void *) field
;
1672 if (enumeration
->container
) {
1673 ret
= bt_ctf_field_common_validate_recursive(
1674 (void *) enumeration
->container
);
1681 bt_ctf_bool
bt_ctf_field_enumeration_is_set_recursive(struct bt_ctf_field_common
*field
)
1683 bt_ctf_bool is_set
= BT_CTF_FALSE
;
1684 struct bt_ctf_field_enumeration
*enumeration
= (void *) field
;
1686 if (enumeration
->container
) {
1687 is_set
= bt_ctf_field_common_is_set_recursive(
1688 (void *) enumeration
->container
);
1695 void bt_ctf_field_enumeration_reset_recursive(struct bt_ctf_field_common
*field
)
1697 struct bt_ctf_field_enumeration
*enumeration
= (void *) field
;
1699 if (enumeration
->container
) {
1700 bt_ctf_field_common_reset_recursive(
1701 (void *) enumeration
->container
);
1704 bt_ctf_field_common_generic_reset((void *) field
);
1708 void bt_ctf_field_variant_set_is_frozen_recursive(
1709 struct bt_ctf_field_common
*field
, bool is_frozen
)
1711 struct bt_ctf_field_variant
*variant
= (void *) field
;
1714 bt_ctf_field_common_set_is_frozen_recursive(
1715 (void *) variant
->tag
, is_frozen
);
1718 bt_ctf_field_common_variant_set_is_frozen_recursive((void *) field
,
1723 int bt_ctf_field_variant_validate_recursive(struct bt_ctf_field_common
*field
)
1726 struct bt_ctf_field_variant
*variant
= (void *) field
;
1729 ret
= bt_ctf_field_common_validate_recursive(
1730 (void *) variant
->tag
);
1736 ret
= bt_ctf_field_common_variant_validate_recursive((void *) field
);
1743 bt_ctf_bool
bt_ctf_field_variant_is_set_recursive(struct bt_ctf_field_common
*field
)
1746 struct bt_ctf_field_variant
*variant
= (void *) field
;
1749 is_set
= bt_ctf_field_common_is_set_recursive(
1750 (void *) variant
->tag
);
1756 is_set
= bt_ctf_field_common_variant_is_set_recursive((void *) field
);
1763 void bt_ctf_field_variant_reset_recursive(struct bt_ctf_field_common
*field
)
1765 struct bt_ctf_field_variant
*variant
= (void *) field
;
1768 bt_ctf_field_common_reset_recursive(
1769 (void *) variant
->tag
);
1772 bt_ctf_field_common_variant_reset_recursive((void *) field
);
1775 BT_CTF_ASSERT_PRE_FUNC
1776 static inline bool field_to_set_has_expected_type(
1777 struct bt_ctf_field_common
*struct_field
,
1778 const char *name
, struct bt_ctf_field_common
*value
)
1781 struct bt_ctf_field_type_common
*expected_field_type
= NULL
;
1783 expected_field_type
=
1784 bt_ctf_field_type_common_structure_borrow_field_type_by_name(
1785 struct_field
->type
, name
);
1787 if (bt_ctf_field_type_common_compare(expected_field_type
, value
->type
)) {
1788 BT_CTF_ASSERT_PRE_MSG("Value field's type is different from the expected field type: "
1789 "value-ft-addr=%p, expected-ft-addr=%p", value
->type
,
1790 expected_field_type
);
1799 int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field
*field
,
1800 const char *name
, struct bt_ctf_field
*value
)
1804 struct bt_ctf_field_common
*common_field
= (void *) field
;
1805 struct bt_ctf_field_common_structure
*structure
=
1806 BT_CTF_FROM_COMMON(common_field
);
1807 struct bt_ctf_field_common
*common_value
= (void *) value
;
1809 GHashTable
*field_name_to_index
;
1810 struct bt_ctf_field_type_common_structure
*structure_ft
;
1812 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Parent field");
1813 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Field name");
1814 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value field");
1815 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(common_field
,
1816 BT_CTF_FIELD_TYPE_ID_STRUCT
, "Parent field");
1817 BT_CTF_ASSERT_PRE(field_to_set_has_expected_type(common_field
,
1818 name
, common_value
),
1819 "Value field's type is different from the expected field type.");
1820 field_quark
= g_quark_from_string(name
);
1821 structure_ft
= BT_CTF_FROM_COMMON(common_field
->type
);
1822 field_name_to_index
= structure_ft
->field_name_to_index
;
1823 if (!g_hash_table_lookup_extended(field_name_to_index
,
1824 GUINT_TO_POINTER(field_quark
), NULL
,
1825 (gpointer
*) &index
)) {
1826 BT_LOGT("Invalid parameter: no such field in structure field's type: "
1827 "struct-field-addr=%p, struct-ft-addr=%p, "
1828 "field-ft-addr=%p, name=\"%s\"",
1829 field
, common_field
->type
, common_value
->type
, name
);
1833 bt_ctf_object_get_ref(value
);
1834 BT_CTF_OBJECT_MOVE_REF(structure
->fields
->pdata
[index
], value
);