4 * Babeltrace CTF IR - Event Fields
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-ir/fields-internal.h>
30 #include <babeltrace/ctf-ir/field-types-internal.h>
31 #include <babeltrace/object-internal.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/compiler.h>
34 #include <babeltrace/compat/fcntl.h>
36 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
39 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
41 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
42 struct bt_ctf_field_type
*);
44 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
45 struct bt_ctf_field_type
*);
47 struct bt_ctf_field
*bt_ctf_field_structure_create(
48 struct bt_ctf_field_type
*);
50 struct bt_ctf_field
*bt_ctf_field_variant_create(
51 struct bt_ctf_field_type
*);
53 struct bt_ctf_field
*bt_ctf_field_array_create(
54 struct bt_ctf_field_type
*);
56 struct bt_ctf_field
*bt_ctf_field_sequence_create(
57 struct bt_ctf_field_type
*);
59 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
62 void bt_ctf_field_destroy(struct bt_object
*);
64 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*);
66 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*);
68 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*);
70 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*);
72 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*);
74 void bt_ctf_field_array_destroy(struct bt_ctf_field
*);
76 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*);
78 void bt_ctf_field_string_destroy(struct bt_ctf_field
*);
81 int bt_ctf_field_generic_validate(struct bt_ctf_field
*);
83 int bt_ctf_field_structure_validate(struct bt_ctf_field
*);
85 int bt_ctf_field_variant_validate(struct bt_ctf_field
*);
87 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*);
89 int bt_ctf_field_array_validate(struct bt_ctf_field
*);
91 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*);
94 int bt_ctf_field_generic_reset(struct bt_ctf_field
*);
96 int bt_ctf_field_structure_reset(struct bt_ctf_field
*);
98 int bt_ctf_field_variant_reset(struct bt_ctf_field
*);
100 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*);
102 int bt_ctf_field_array_reset(struct bt_ctf_field
*);
104 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*);
106 int bt_ctf_field_string_reset(struct bt_ctf_field
*);
109 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*,
110 struct ctf_stream_pos
*);
112 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
113 struct ctf_stream_pos
*);
115 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
116 struct ctf_stream_pos
*);
118 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
119 struct ctf_stream_pos
*);
121 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
122 struct ctf_stream_pos
*);
124 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
125 struct ctf_stream_pos
*);
127 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
128 struct ctf_stream_pos
*);
130 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
131 struct ctf_stream_pos
*);
134 int bt_ctf_field_integer_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
136 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
138 int bt_ctf_field_floating_point_copy(struct bt_ctf_field
*,
139 struct bt_ctf_field
*);
141 int bt_ctf_field_structure_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
143 int bt_ctf_field_variant_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
145 int bt_ctf_field_array_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
147 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
149 int bt_ctf_field_string_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
152 void generic_field_freeze(struct bt_ctf_field
*);
154 void bt_ctf_field_enumeration_freeze(struct bt_ctf_field
*);
156 void bt_ctf_field_structure_freeze(struct bt_ctf_field
*);
158 void bt_ctf_field_variant_freeze(struct bt_ctf_field
*);
160 void bt_ctf_field_array_freeze(struct bt_ctf_field
*);
162 void bt_ctf_field_sequence_freeze(struct bt_ctf_field
*);
165 bool bt_ctf_field_generic_is_set(struct bt_ctf_field
*);
167 bool bt_ctf_field_structure_is_set(struct bt_ctf_field
*);
169 bool bt_ctf_field_variant_is_set(struct bt_ctf_field
*);
171 bool bt_ctf_field_enumeration_is_set(struct bt_ctf_field
*);
173 bool bt_ctf_field_array_is_set(struct bt_ctf_field
*);
175 bool bt_ctf_field_sequence_is_set(struct bt_ctf_field
*);
178 int increase_packet_size(struct ctf_stream_pos
*pos
);
181 struct bt_ctf_field
*(* const field_create_funcs
[])(
182 struct bt_ctf_field_type
*) = {
183 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_create
,
184 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_create
,
185 [BT_CTF_TYPE_ID_FLOAT
] =
186 bt_ctf_field_floating_point_create
,
187 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_create
,
188 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_create
,
189 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_create
,
190 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_create
,
191 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_create
,
195 void (* const field_destroy_funcs
[])(struct bt_ctf_field
*) = {
196 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_destroy
,
197 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_destroy
,
198 [BT_CTF_TYPE_ID_FLOAT
] =
199 bt_ctf_field_floating_point_destroy
,
200 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_destroy
,
201 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_destroy
,
202 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_destroy
,
203 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
204 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_destroy
,
208 int (* const field_validate_funcs
[])(struct bt_ctf_field
*) = {
209 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_generic_validate
,
210 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_validate
,
211 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_generic_validate
,
212 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_validate
,
213 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_validate
,
214 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_validate
,
215 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_validate
,
216 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_generic_validate
,
220 int (* const field_reset_funcs
[])(struct bt_ctf_field
*) = {
221 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_generic_reset
,
222 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_reset
,
223 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_generic_reset
,
224 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_reset
,
225 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_reset
,
226 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_reset
,
227 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_reset
,
228 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_reset
,
232 int (* const field_serialize_funcs
[])(struct bt_ctf_field
*,
233 struct ctf_stream_pos
*) = {
234 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_serialize
,
235 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_serialize
,
236 [BT_CTF_TYPE_ID_FLOAT
] =
237 bt_ctf_field_floating_point_serialize
,
238 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_serialize
,
239 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_serialize
,
240 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_serialize
,
241 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
242 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_serialize
,
246 int (* const field_copy_funcs
[])(struct bt_ctf_field
*,
247 struct bt_ctf_field
*) = {
248 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_copy
,
249 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_copy
,
250 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_floating_point_copy
,
251 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_copy
,
252 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_copy
,
253 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_copy
,
254 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_copy
,
255 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_copy
,
259 void (* const field_freeze_funcs
[])(struct bt_ctf_field
*) = {
260 [BT_CTF_TYPE_ID_INTEGER
] = generic_field_freeze
,
261 [BT_CTF_TYPE_ID_FLOAT
] = generic_field_freeze
,
262 [BT_CTF_TYPE_ID_STRING
] = generic_field_freeze
,
263 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_freeze
,
264 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_freeze
,
265 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_freeze
,
266 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_freeze
,
267 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_freeze
,
271 bool (* const field_is_set_funcs
[])(struct bt_ctf_field
*) = {
272 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_generic_is_set
,
273 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_is_set
,
274 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_generic_is_set
,
275 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_is_set
,
276 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_is_set
,
277 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_is_set
,
278 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_is_set
,
279 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_generic_is_set
,
282 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
284 struct bt_ctf_field
*field
= NULL
;
285 enum bt_ctf_type_id type_id
;
292 type_id
= bt_ctf_field_type_get_type_id(type
);
293 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
294 type_id
>= BT_CTF_NR_TYPE_IDS
) {
298 /* Field class MUST be valid */
299 ret
= bt_ctf_field_type_validate(type
);
306 field
= field_create_funcs
[type_id
](type
);
311 /* The type's declaration can't change after this point */
312 bt_ctf_field_type_freeze(type
);
314 bt_object_init(field
, bt_ctf_field_destroy
);
320 void bt_ctf_field_get(struct bt_ctf_field
*field
)
325 void bt_ctf_field_put(struct bt_ctf_field
*field
)
330 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
332 struct bt_ctf_field_type
*ret
= NULL
;
344 enum bt_ctf_type_id
bt_ctf_field_get_type_id(struct bt_ctf_field
*field
)
346 enum bt_ctf_type_id ret
= BT_CTF_TYPE_ID_UNKNOWN
;
352 ret
= bt_ctf_field_type_get_type_id(field
->type
);
357 int bt_ctf_field_is_integer(struct bt_ctf_field
*field
)
359 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_INTEGER
;
362 int bt_ctf_field_is_floating_point(struct bt_ctf_field
*field
)
364 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_FLOAT
;
367 int bt_ctf_field_is_enumeration(struct bt_ctf_field
*field
)
369 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_ENUM
;
372 int bt_ctf_field_is_string(struct bt_ctf_field
*field
)
374 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_STRING
;
377 int bt_ctf_field_is_structure(struct bt_ctf_field
*field
)
379 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_STRUCT
;
382 int bt_ctf_field_is_array(struct bt_ctf_field
*field
)
384 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_ARRAY
;
387 int bt_ctf_field_is_sequence(struct bt_ctf_field
*field
)
389 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_SEQUENCE
;
392 int bt_ctf_field_is_variant(struct bt_ctf_field
*field
)
394 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_VARIANT
;
397 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
398 struct bt_ctf_field
*field
)
400 struct bt_ctf_field
*ret
= NULL
;
401 struct bt_ctf_field_sequence
*sequence
;
407 if (bt_ctf_field_type_get_type_id(field
->type
) !=
408 BT_CTF_TYPE_ID_SEQUENCE
) {
412 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
413 ret
= sequence
->length
;
419 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
420 struct bt_ctf_field
*length_field
)
423 struct bt_ctf_field_type_integer
*length_type
;
424 struct bt_ctf_field_integer
*length
;
425 struct bt_ctf_field_sequence
*sequence
;
426 uint64_t sequence_length
;
428 if (!field
|| !length_field
|| field
->frozen
) {
432 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
433 BT_CTF_TYPE_ID_INTEGER
) {
438 length_type
= container_of(length_field
->type
,
439 struct bt_ctf_field_type_integer
, parent
);
440 /* The length field must be unsigned */
441 if (length_type
->declaration
.signedness
) {
446 length
= container_of(length_field
, struct bt_ctf_field_integer
,
448 sequence_length
= length
->definition
.value
._unsigned
;
449 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
450 if (sequence
->elements
) {
451 g_ptr_array_free(sequence
->elements
, TRUE
);
452 bt_put(sequence
->length
);
455 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
456 if (!sequence
->elements
) {
461 g_ptr_array_set_free_func(sequence
->elements
,
462 (GDestroyNotify
) bt_put
);
463 g_ptr_array_set_size(sequence
->elements
, (size_t) sequence_length
);
464 bt_get(length_field
);
465 sequence
->length
= length_field
;
470 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
471 struct bt_ctf_field
*field
, const char *name
)
473 struct bt_ctf_field
*new_field
= NULL
;
475 struct bt_ctf_field_structure
*structure
;
476 struct bt_ctf_field_type
*field_type
= NULL
;
479 if (!field
|| !name
||
480 bt_ctf_field_type_get_type_id(field
->type
) !=
481 BT_CTF_TYPE_ID_STRUCT
) {
485 field_quark
= g_quark_from_string(name
);
486 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
488 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
490 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
491 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
495 if (structure
->fields
->pdata
[index
]) {
496 new_field
= structure
->fields
->pdata
[index
];
500 /* We don't want to modify this field if it's frozen */
505 new_field
= bt_ctf_field_create(field_type
);
510 structure
->fields
->pdata
[index
] = new_field
;
520 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
521 struct bt_ctf_field
*field
, int index
)
524 const char *field_name
;
525 struct bt_ctf_field_structure
*structure
;
526 struct bt_ctf_field_type
*structure_type
;
527 struct bt_ctf_field_type
*field_type
= NULL
;
528 struct bt_ctf_field
*ret_field
= NULL
;
531 bt_ctf_field_type_get_type_id(field
->type
) !=
532 BT_CTF_TYPE_ID_STRUCT
) {
536 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
537 if (index
>= structure
->fields
->len
) {
541 ret_field
= structure
->fields
->pdata
[index
];
546 /* We don't want to modify this field if it's frozen */
551 /* Field has not been instanciated yet, create it */
552 structure_type
= bt_ctf_field_get_type(field
);
553 if (!structure_type
) {
557 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
558 &field_name
, &field_type
, index
);
559 bt_put(structure_type
);
564 ret_field
= bt_ctf_field_create(field_type
);
569 structure
->fields
->pdata
[index
] = ret_field
;
578 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
579 const char *name
, struct bt_ctf_field
*value
)
583 struct bt_ctf_field_structure
*structure
;
584 struct bt_ctf_field_type
*expected_field_type
= NULL
;
587 if (!field
|| !name
|| !value
|| field
->frozen
||
588 bt_ctf_field_type_get_type_id(field
->type
) !=
589 BT_CTF_TYPE_ID_STRUCT
) {
594 field_quark
= g_quark_from_string(name
);
595 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
596 expected_field_type
=
597 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
600 if (bt_ctf_field_type_compare(expected_field_type
, value
->type
)) {
605 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
606 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
610 if (structure
->fields
->pdata
[index
]) {
611 bt_put(structure
->fields
->pdata
[index
]);
614 structure
->fields
->pdata
[index
] = value
;
617 if (expected_field_type
) {
618 bt_put(expected_field_type
);
623 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
626 struct bt_ctf_field
*new_field
= NULL
;
627 struct bt_ctf_field_type
*field_type
= NULL
;
628 struct bt_ctf_field_array
*array
;
630 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
631 BT_CTF_TYPE_ID_ARRAY
) {
635 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
636 if (index
>= array
->elements
->len
) {
640 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
641 if (array
->elements
->pdata
[(size_t)index
]) {
642 new_field
= array
->elements
->pdata
[(size_t)index
];
646 /* We don't want to modify this field if it's frozen */
651 new_field
= bt_ctf_field_create(field_type
);
652 array
->elements
->pdata
[(size_t)index
] = new_field
;
663 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
666 struct bt_ctf_field
*new_field
= NULL
;
667 struct bt_ctf_field_type
*field_type
= NULL
;
668 struct bt_ctf_field_sequence
*sequence
;
670 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
671 BT_CTF_TYPE_ID_SEQUENCE
) {
675 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
676 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
680 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
681 if (sequence
->elements
->pdata
[(size_t) index
]) {
682 new_field
= sequence
->elements
->pdata
[(size_t) index
];
686 /* We don't want to modify this field if it's frozen */
691 new_field
= bt_ctf_field_create(field_type
);
692 sequence
->elements
->pdata
[(size_t) index
] = new_field
;
703 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
704 struct bt_ctf_field
*tag_field
)
706 struct bt_ctf_field
*new_field
= NULL
;
707 struct bt_ctf_field_variant
*variant
;
708 struct bt_ctf_field_type_variant
*variant_type
;
709 struct bt_ctf_field_type
*field_type
;
710 struct bt_ctf_field
*tag_enum
= NULL
;
711 struct bt_ctf_field_integer
*tag_enum_integer
;
712 int64_t tag_enum_value
;
714 if (!field
|| !tag_field
||
715 bt_ctf_field_type_get_type_id(field
->type
) !=
716 BT_CTF_TYPE_ID_VARIANT
||
717 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
718 BT_CTF_TYPE_ID_ENUM
) {
722 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
723 variant_type
= container_of(field
->type
,
724 struct bt_ctf_field_type_variant
, parent
);
725 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
730 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
733 if (bt_ctf_field_validate(tag_field
) < 0) {
737 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
740 * If the variant currently has a tag and a payload, and if the
741 * requested tag value is the same as the current one, return
742 * the current payload instead of creating a fresh one.
744 if (variant
->tag
&& variant
->payload
) {
745 struct bt_ctf_field
*cur_tag_container
= NULL
;
746 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
747 int64_t cur_tag_value
;
750 bt_ctf_field_enumeration_get_container(variant
->tag
);
751 assert(cur_tag_container
);
752 cur_tag_enum_integer
= container_of(cur_tag_container
,
753 struct bt_ctf_field_integer
, parent
);
754 bt_put(cur_tag_container
);
755 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
757 if (cur_tag_value
== tag_enum_value
) {
758 new_field
= variant
->payload
;
764 /* We don't want to modify this field if it's frozen */
769 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
770 variant_type
, tag_enum_value
);
775 new_field
= bt_ctf_field_create(field_type
);
780 bt_put(variant
->tag
);
781 bt_put(variant
->payload
);
784 variant
->tag
= tag_field
;
785 variant
->payload
= new_field
;
791 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
792 struct bt_ctf_field
*variant_field
)
794 struct bt_ctf_field
*current_field
= NULL
;
795 struct bt_ctf_field_variant
*variant
;
797 if (!variant_field
||
798 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
799 BT_CTF_TYPE_ID_VARIANT
) {
803 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
806 if (variant
->payload
) {
807 current_field
= variant
->payload
;
808 bt_get(current_field
);
813 return current_field
;
816 struct bt_ctf_field
*bt_ctf_field_variant_get_tag(
817 struct bt_ctf_field
*variant_field
)
819 struct bt_ctf_field
*tag
= NULL
;
820 struct bt_ctf_field_variant
*variant
;
822 if (!variant_field
||
823 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
824 BT_CTF_TYPE_ID_VARIANT
) {
828 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
831 tag
= bt_get(variant
->tag
);
837 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
838 struct bt_ctf_field
*field
)
840 struct bt_ctf_field
*container
= NULL
;
841 struct bt_ctf_field_enumeration
*enumeration
;
843 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
844 BT_CTF_TYPE_ID_ENUM
) {
848 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
850 if (!enumeration
->payload
) {
851 /* We don't want to modify this field if it's frozen */
856 struct bt_ctf_field_type_enumeration
*enumeration_type
=
857 container_of(field
->type
,
858 struct bt_ctf_field_type_enumeration
, parent
);
859 enumeration
->payload
=
860 bt_ctf_field_create(enumeration_type
->container
);
863 container
= enumeration
->payload
;
869 const char *bt_ctf_field_enumeration_get_single_mapping_name(
870 struct bt_ctf_field
*field
)
873 const char *name
= NULL
;
874 struct bt_ctf_field
*container
= NULL
;
875 struct bt_ctf_field_type
*container_type
= NULL
;
876 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
877 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
879 container
= bt_ctf_field_enumeration_get_container(field
);
884 container_type
= bt_ctf_field_get_type(container
);
885 if (!container_type
) {
886 goto error_put_container
;
889 integer_type
= container_of(container_type
,
890 struct bt_ctf_field_type_integer
, parent
);
892 if (!integer_type
->declaration
.signedness
) {
895 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
898 goto error_put_container_type
;
900 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
903 goto error_put_container_type
;
905 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
906 iter
, &name
, NULL
, NULL
);
910 ret
= bt_ctf_field_signed_integer_get_value(container
,
913 goto error_put_container_type
;
915 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
918 goto error_put_container_type
;
920 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
921 iter
, &name
, NULL
, NULL
);
924 error_put_container_type
:
925 bt_put(container_type
);
933 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
937 struct bt_ctf_field_integer
*integer
;
938 struct bt_ctf_field_type_integer
*integer_type
;
940 if (!field
|| !value
|| !field
->payload_set
||
941 bt_ctf_field_type_get_type_id(field
->type
) !=
942 BT_CTF_TYPE_ID_INTEGER
) {
947 integer_type
= container_of(field
->type
,
948 struct bt_ctf_field_type_integer
, parent
);
949 if (!integer_type
->declaration
.signedness
) {
954 integer
= container_of(field
,
955 struct bt_ctf_field_integer
, parent
);
956 *value
= integer
->definition
.value
._signed
;
961 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
965 struct bt_ctf_field_integer
*integer
;
966 struct bt_ctf_field_type_integer
*integer_type
;
968 int64_t min_value
, max_value
;
970 if (!field
|| field
->frozen
||
971 bt_ctf_field_type_get_type_id(field
->type
) !=
972 BT_CTF_TYPE_ID_INTEGER
) {
977 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
978 integer_type
= container_of(field
->type
,
979 struct bt_ctf_field_type_integer
, parent
);
980 if (!integer_type
->declaration
.signedness
) {
985 size
= integer_type
->declaration
.len
;
986 min_value
= -(1ULL << (size
- 1));
987 max_value
= (1ULL << (size
- 1)) - 1;
988 if (value
< min_value
|| value
> max_value
) {
993 integer
->definition
.value
._signed
= value
;
994 integer
->parent
.payload_set
= 1;
999 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
1003 struct bt_ctf_field_integer
*integer
;
1004 struct bt_ctf_field_type_integer
*integer_type
;
1006 if (!field
|| !value
|| !field
->payload_set
||
1007 bt_ctf_field_type_get_type_id(field
->type
) !=
1008 BT_CTF_TYPE_ID_INTEGER
) {
1013 integer_type
= container_of(field
->type
,
1014 struct bt_ctf_field_type_integer
, parent
);
1015 if (integer_type
->declaration
.signedness
) {
1020 integer
= container_of(field
,
1021 struct bt_ctf_field_integer
, parent
);
1022 *value
= integer
->definition
.value
._unsigned
;
1027 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
1031 struct bt_ctf_field_integer
*integer
;
1032 struct bt_ctf_field_type_integer
*integer_type
;
1036 if (!field
|| field
->frozen
||
1037 bt_ctf_field_type_get_type_id(field
->type
) !=
1038 BT_CTF_TYPE_ID_INTEGER
) {
1043 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1044 integer_type
= container_of(field
->type
,
1045 struct bt_ctf_field_type_integer
, parent
);
1046 if (integer_type
->declaration
.signedness
) {
1051 size
= integer_type
->declaration
.len
;
1052 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
1053 if (value
> max_value
) {
1058 integer
->definition
.value
._unsigned
= value
;
1059 integer
->parent
.payload_set
= 1;
1064 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
1068 struct bt_ctf_field_floating_point
*floating_point
;
1070 if (!field
|| !value
|| !field
->payload_set
||
1071 bt_ctf_field_type_get_type_id(field
->type
) !=
1072 BT_CTF_TYPE_ID_FLOAT
) {
1077 floating_point
= container_of(field
,
1078 struct bt_ctf_field_floating_point
, parent
);
1079 *value
= floating_point
->definition
.value
;
1084 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
1088 struct bt_ctf_field_floating_point
*floating_point
;
1090 if (!field
|| field
->frozen
||
1091 bt_ctf_field_type_get_type_id(field
->type
) !=
1092 BT_CTF_TYPE_ID_FLOAT
) {
1096 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1098 floating_point
->definition
.value
= value
;
1099 floating_point
->parent
.payload_set
= 1;
1104 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
1106 const char *ret
= NULL
;
1107 struct bt_ctf_field_string
*string
;
1109 if (!field
|| !field
->payload_set
||
1110 bt_ctf_field_type_get_type_id(field
->type
) !=
1111 BT_CTF_TYPE_ID_STRING
) {
1115 string
= container_of(field
,
1116 struct bt_ctf_field_string
, parent
);
1117 ret
= string
->payload
->str
;
1122 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1126 struct bt_ctf_field_string
*string
;
1128 if (!field
|| !value
|| field
->frozen
||
1129 bt_ctf_field_type_get_type_id(field
->type
) !=
1130 BT_CTF_TYPE_ID_STRING
) {
1135 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1136 if (string
->payload
) {
1137 g_string_assign(string
->payload
, value
);
1139 string
->payload
= g_string_new(value
);
1142 string
->parent
.payload_set
= 1;
1147 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1151 struct bt_ctf_field_string
*string_field
;
1153 if (!field
|| !value
|| field
->frozen
||
1154 bt_ctf_field_type_get_type_id(field
->type
) !=
1155 BT_CTF_TYPE_ID_STRING
) {
1160 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1162 if (string_field
->payload
) {
1163 g_string_append(string_field
->payload
, value
);
1165 string_field
->payload
= g_string_new(value
);
1168 string_field
->parent
.payload_set
= 1;
1174 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1175 const char *value
, unsigned int length
)
1179 unsigned int effective_length
= length
;
1180 struct bt_ctf_field_string
*string_field
;
1182 if (!field
|| !value
|| field
->frozen
||
1183 bt_ctf_field_type_get_type_id(field
->type
) !=
1184 BT_CTF_TYPE_ID_STRING
) {
1189 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1191 /* make sure no null bytes are appended */
1192 for (i
= 0; i
< length
; ++i
) {
1193 if (value
[i
] == '\0') {
1194 effective_length
= i
;
1199 if (string_field
->payload
) {
1200 g_string_append_len(string_field
->payload
, value
,
1203 string_field
->payload
= g_string_new_len(value
,
1207 string_field
->parent
.payload_set
= 1;
1214 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1217 enum bt_ctf_type_id type_id
;
1224 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1225 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1230 ret
= field_validate_funcs
[type_id
](field
);
1236 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1239 enum bt_ctf_type_id type_id
;
1246 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1247 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1252 ret
= field_reset_funcs
[type_id
](field
);
1258 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1259 struct ctf_stream_pos
*pos
)
1262 enum bt_ctf_type_id type_id
;
1264 if (!field
|| !pos
) {
1269 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1270 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1275 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1282 bool bt_ctf_field_is_set(struct bt_ctf_field
*field
)
1284 bool is_set
= false;
1285 enum bt_ctf_type_id type_id
;
1291 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1292 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1296 is_set
= field_is_set_funcs
[type_id
](field
);
1301 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1304 struct bt_ctf_field
*copy
= NULL
;
1305 enum bt_ctf_type_id type_id
;
1311 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1312 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1316 copy
= bt_ctf_field_create(field
->type
);
1321 copy
->payload_set
= field
->payload_set
;
1322 ret
= field_copy_funcs
[type_id
](field
, copy
);
1332 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1334 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1335 struct bt_ctf_field_type_integer
, parent
);
1336 struct bt_ctf_field_integer
*integer
= g_new0(
1337 struct bt_ctf_field_integer
, 1);
1340 integer
->definition
.declaration
= &integer_type
->declaration
;
1343 return integer
? &integer
->parent
: NULL
;
1347 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1348 struct bt_ctf_field_type
*type
)
1350 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1351 struct bt_ctf_field_enumeration
, 1);
1353 return enumeration
? &enumeration
->parent
: NULL
;
1357 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1358 struct bt_ctf_field_type
*type
)
1360 struct bt_ctf_field_floating_point
*floating_point
;
1361 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1363 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1364 if (!floating_point
) {
1368 floating_point_type
= container_of(type
,
1369 struct bt_ctf_field_type_floating_point
, parent
);
1370 floating_point
->definition
.declaration
= container_of(
1371 type
->declaration
, struct declaration_float
, p
);
1374 floating_point
->definition
.sign
= &floating_point
->sign
;
1375 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1376 floating_point
->definition
.sign
->p
.declaration
=
1377 &floating_point_type
->sign
.p
;
1379 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1380 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1381 floating_point
->definition
.mantissa
->p
.declaration
=
1382 &floating_point_type
->mantissa
.p
;
1384 floating_point
->definition
.exp
= &floating_point
->exp
;
1385 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1386 floating_point
->definition
.exp
->p
.declaration
=
1387 &floating_point_type
->exp
.p
;
1390 return floating_point
? &floating_point
->parent
: NULL
;
1394 struct bt_ctf_field
*bt_ctf_field_structure_create(
1395 struct bt_ctf_field_type
*type
)
1397 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1398 struct bt_ctf_field_type_structure
, parent
);
1399 struct bt_ctf_field_structure
*structure
= g_new0(
1400 struct bt_ctf_field_structure
, 1);
1401 struct bt_ctf_field
*field
= NULL
;
1407 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1408 structure
->fields
= g_ptr_array_new_with_free_func(
1409 (GDestroyNotify
)bt_ctf_field_put
);
1410 g_ptr_array_set_size(structure
->fields
,
1411 g_hash_table_size(structure
->field_name_to_index
));
1412 field
= &structure
->parent
;
1418 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1420 struct bt_ctf_field_variant
*variant
= g_new0(
1421 struct bt_ctf_field_variant
, 1);
1422 return variant
? &variant
->parent
: NULL
;
1426 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1428 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1429 struct bt_ctf_field_type_array
*array_type
;
1430 unsigned int array_length
;
1432 if (!array
|| !type
) {
1436 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1437 array_length
= array_type
->length
;
1438 array
->elements
= g_ptr_array_sized_new(array_length
);
1439 if (!array
->elements
) {
1443 g_ptr_array_set_free_func(array
->elements
,
1444 (GDestroyNotify
)bt_ctf_field_put
);
1445 g_ptr_array_set_size(array
->elements
, array_length
);
1446 return &array
->parent
;
1453 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1454 struct bt_ctf_field_type
*type
)
1456 struct bt_ctf_field_sequence
*sequence
= g_new0(
1457 struct bt_ctf_field_sequence
, 1);
1458 return sequence
? &sequence
->parent
: NULL
;
1462 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1464 struct bt_ctf_field_string
*string
= g_new0(
1465 struct bt_ctf_field_string
, 1);
1466 return string
? &string
->parent
: NULL
;
1470 void bt_ctf_field_destroy(struct bt_object
*obj
)
1472 struct bt_ctf_field
*field
;
1473 struct bt_ctf_field_type
*type
;
1474 enum bt_ctf_type_id type_id
;
1476 field
= container_of(obj
, struct bt_ctf_field
, base
);
1478 type_id
= bt_ctf_field_type_get_type_id(type
);
1479 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
1480 type_id
>= BT_CTF_NR_TYPE_IDS
) {
1484 field_destroy_funcs
[type_id
](field
);
1489 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1491 struct bt_ctf_field_integer
*integer
;
1497 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1502 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1504 struct bt_ctf_field_enumeration
*enumeration
;
1510 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1512 bt_put(enumeration
->payload
);
1513 g_free(enumeration
);
1517 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1519 struct bt_ctf_field_floating_point
*floating_point
;
1525 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1527 g_free(floating_point
);
1531 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1533 struct bt_ctf_field_structure
*structure
;
1539 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1540 g_ptr_array_free(structure
->fields
, TRUE
);
1545 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1547 struct bt_ctf_field_variant
*variant
;
1553 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1554 bt_put(variant
->tag
);
1555 bt_put(variant
->payload
);
1560 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1562 struct bt_ctf_field_array
*array
;
1568 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1569 g_ptr_array_free(array
->elements
, TRUE
);
1574 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1576 struct bt_ctf_field_sequence
*sequence
;
1582 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1583 if (sequence
->elements
) {
1584 g_ptr_array_free(sequence
->elements
, TRUE
);
1586 bt_put(sequence
->length
);
1591 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1593 struct bt_ctf_field_string
*string
;
1598 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1599 if (string
->payload
) {
1600 g_string_free(string
->payload
, TRUE
);
1606 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1608 return (field
&& field
->payload_set
) ? 0 : -1;
1612 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1615 struct bt_ctf_field_enumeration
*enumeration
;
1622 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1624 if (!enumeration
->payload
) {
1629 ret
= bt_ctf_field_validate(enumeration
->payload
);
1635 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1639 struct bt_ctf_field_structure
*structure
;
1646 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1647 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1648 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1651 struct bt_ctf_field_type
*field_type
=
1652 bt_ctf_field_get_type(field
);
1654 (void) bt_ctf_field_type_structure_get_field(field_type
,
1656 fprintf(stderr
, "Field %s failed validation\n",
1657 name
? name
: "NULL");
1667 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1670 struct bt_ctf_field_variant
*variant
;
1677 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1678 ret
= bt_ctf_field_validate(variant
->payload
);
1684 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1688 struct bt_ctf_field_array
*array
;
1695 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1696 for (i
= 0; i
< array
->elements
->len
; i
++) {
1697 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1699 fprintf(stderr
, "Failed to validate array field #%zu\n", i
);
1708 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1712 struct bt_ctf_field_sequence
*sequence
;
1719 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1720 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1721 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1723 fprintf(stderr
, "Failed to validate sequence field #%zu\n", i
);
1732 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1741 field
->payload_set
= 0;
1747 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1750 struct bt_ctf_field_enumeration
*enumeration
;
1757 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1759 if (!enumeration
->payload
) {
1763 ret
= bt_ctf_field_reset(enumeration
->payload
);
1769 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1773 struct bt_ctf_field_structure
*structure
;
1780 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1781 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1782 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1786 * Structure members are lazily initialized; skip if
1787 * this member has not been allocated yet.
1792 ret
= bt_ctf_field_reset(member
);
1802 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1805 struct bt_ctf_field_variant
*variant
;
1812 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1813 if (variant
->payload
) {
1814 ret
= bt_ctf_field_reset(variant
->payload
);
1821 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1825 struct bt_ctf_field_array
*array
;
1832 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1833 for (i
= 0; i
< array
->elements
->len
; i
++) {
1834 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1838 * Array elements are lazily initialized; skip if
1839 * this member has not been allocated yet.
1844 ret
= bt_ctf_field_reset(member
);
1854 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1858 struct bt_ctf_field_sequence
*sequence
;
1865 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1866 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1867 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1871 * Sequence elements are lazily initialized; skip if
1872 * this member has not been allocated yet.
1877 ret
= bt_ctf_field_reset(member
);
1887 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1890 struct bt_ctf_field_string
*string
;
1897 ret
= bt_ctf_field_generic_reset(field
);
1902 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1903 if (string
->payload
) {
1904 g_string_truncate(string
->payload
, 0);
1911 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1912 struct ctf_stream_pos
*pos
)
1915 struct bt_ctf_field_integer
*integer
= container_of(field
,
1916 struct bt_ctf_field_integer
, parent
);
1919 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1920 if (ret
== -EFAULT
) {
1922 * The field is too large to fit in the current packet's
1923 * remaining space. Bump the packet size and retry.
1925 ret
= increase_packet_size(pos
);
1936 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1937 struct ctf_stream_pos
*pos
)
1939 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1940 field
, struct bt_ctf_field_enumeration
, parent
);
1942 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1946 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1947 struct ctf_stream_pos
*pos
)
1950 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1951 struct bt_ctf_field_floating_point
, parent
);
1954 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1955 if (ret
== -EFAULT
) {
1957 * The field is too large to fit in the current packet's
1958 * remaining space. Bump the packet size and retry.
1960 ret
= increase_packet_size(pos
);
1971 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1972 struct ctf_stream_pos
*pos
)
1976 struct bt_ctf_field_structure
*structure
= container_of(
1977 field
, struct bt_ctf_field_structure
, parent
);
1979 while (!ctf_pos_access_ok(pos
,
1980 offset_align(pos
->offset
,
1981 field
->type
->declaration
->alignment
))) {
1982 ret
= increase_packet_size(pos
);
1988 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1993 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1994 struct bt_ctf_field
*field
= g_ptr_array_index(
1995 structure
->fields
, i
);
1997 ret
= bt_ctf_field_serialize(field
, pos
);
2000 struct bt_ctf_field_type
*field_type
=
2001 bt_ctf_field_get_type(field
);
2003 (void) bt_ctf_field_type_structure_get_field(field_type
,
2005 fprintf(stderr
, "Field %s failed to serialize\n",
2006 name
? name
: "NULL");
2016 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
2017 struct ctf_stream_pos
*pos
)
2019 struct bt_ctf_field_variant
*variant
= container_of(
2020 field
, struct bt_ctf_field_variant
, parent
);
2022 return bt_ctf_field_serialize(variant
->payload
, pos
);
2026 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
2027 struct ctf_stream_pos
*pos
)
2031 struct bt_ctf_field_array
*array
= container_of(
2032 field
, struct bt_ctf_field_array
, parent
);
2034 for (i
= 0; i
< array
->elements
->len
; i
++) {
2035 ret
= bt_ctf_field_serialize(
2036 g_ptr_array_index(array
->elements
, i
), pos
);
2038 fprintf(stderr
, "Failed to serialize array element #%zu\n", i
);
2047 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
2048 struct ctf_stream_pos
*pos
)
2052 struct bt_ctf_field_sequence
*sequence
= container_of(
2053 field
, struct bt_ctf_field_sequence
, parent
);
2055 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
2056 ret
= bt_ctf_field_serialize(
2057 g_ptr_array_index(sequence
->elements
, i
), pos
);
2059 fprintf(stderr
, "Failed to serialize sequence element #%zu\n", i
);
2068 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
2069 struct ctf_stream_pos
*pos
)
2073 struct bt_ctf_field_string
*string
= container_of(field
,
2074 struct bt_ctf_field_string
, parent
);
2075 struct bt_ctf_field_type
*character_type
=
2076 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
2077 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
2079 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
2080 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
2081 (uint64_t) string
->payload
->str
[i
]);
2086 ret
= bt_ctf_field_integer_serialize(character
, pos
);
2093 bt_put(character_type
);
2098 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
2099 struct bt_ctf_field
*dst
)
2101 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
2103 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
2104 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
2106 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
2107 sizeof(struct definition_integer
));
2112 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
2113 struct bt_ctf_field
*dst
)
2116 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
2118 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
2119 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
2121 if (enum_src
->payload
) {
2122 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
2123 if (!enum_dst
->payload
) {
2133 int bt_ctf_field_floating_point_copy(
2134 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
2136 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
2138 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
2140 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
2143 memcpy(&float_dst
->definition
, &float_src
->definition
,
2144 sizeof(struct definition_float
));
2145 memcpy(&float_dst
->sign
, &float_src
->sign
,
2146 sizeof(struct definition_integer
));
2147 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
2148 sizeof(struct definition_integer
));
2149 memcpy(&float_dst
->exp
, &float_src
->exp
,
2150 sizeof(struct definition_integer
));
2155 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
2156 struct bt_ctf_field
*dst
)
2159 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
2161 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
2162 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
2164 /* This field_name_to_index HT is owned by the structure field type */
2165 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2166 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2168 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2169 struct bt_ctf_field
*field
=
2170 g_ptr_array_index(struct_src
->fields
, i
);
2171 struct bt_ctf_field
*field_copy
= NULL
;
2174 field_copy
= bt_ctf_field_copy(field
);
2182 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2189 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2190 struct bt_ctf_field
*dst
)
2193 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2195 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2196 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2198 if (variant_src
->tag
) {
2199 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2200 if (!variant_dst
->tag
) {
2205 if (variant_src
->payload
) {
2206 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2207 if (!variant_dst
->payload
) {
2217 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2218 struct bt_ctf_field
*dst
)
2221 struct bt_ctf_field_array
*array_src
, *array_dst
;
2223 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2224 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2226 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2227 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2228 struct bt_ctf_field
*field
=
2229 g_ptr_array_index(array_src
->elements
, i
);
2230 struct bt_ctf_field
*field_copy
= NULL
;
2233 field_copy
= bt_ctf_field_copy(field
);
2241 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2248 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2249 struct bt_ctf_field
*dst
)
2252 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2253 struct bt_ctf_field
*src_length
;
2254 struct bt_ctf_field
*dst_length
;
2256 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2257 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2259 src_length
= bt_ctf_field_sequence_get_length(src
);
2262 /* no length set yet: keep destination sequence empty */
2266 /* copy source length */
2267 dst_length
= bt_ctf_field_copy(src_length
);
2275 /* this will initialize the destination sequence's internal array */
2276 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2283 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2285 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2286 struct bt_ctf_field
*field
=
2287 g_ptr_array_index(sequence_src
->elements
, i
);
2288 struct bt_ctf_field
*field_copy
= NULL
;
2291 field_copy
= bt_ctf_field_copy(field
);
2299 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2306 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2307 struct bt_ctf_field
*dst
)
2310 struct bt_ctf_field_string
*string_src
, *string_dst
;
2312 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2313 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2315 if (string_src
->payload
) {
2316 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2317 if (!string_dst
->payload
) {
2327 int increase_packet_size(struct ctf_stream_pos
*pos
)
2332 ret
= munmap_align(pos
->base_mma
);
2337 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2339 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2340 pos
->packet_size
/ CHAR_BIT
);
2341 } while (ret
== EINTR
);
2348 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2349 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2350 if (pos
->base_mma
== MAP_FAILED
) {
2358 void generic_field_freeze(struct bt_ctf_field
*field
)
2364 void bt_ctf_field_enumeration_freeze(struct bt_ctf_field
*field
)
2366 struct bt_ctf_field_enumeration
*enum_field
=
2367 container_of(field
, struct bt_ctf_field_enumeration
, parent
);
2369 bt_ctf_field_freeze(enum_field
->payload
);
2370 generic_field_freeze(field
);
2374 void bt_ctf_field_structure_freeze(struct bt_ctf_field
*field
)
2377 struct bt_ctf_field_structure
*structure_field
=
2378 container_of(field
, struct bt_ctf_field_structure
, parent
);
2380 for (i
= 0; i
< structure_field
->fields
->len
; i
++) {
2381 struct bt_ctf_field
*field
=
2382 g_ptr_array_index(structure_field
->fields
, i
);
2384 bt_ctf_field_freeze(field
);
2387 generic_field_freeze(field
);
2391 void bt_ctf_field_variant_freeze(struct bt_ctf_field
*field
)
2393 struct bt_ctf_field_variant
*variant_field
=
2394 container_of(field
, struct bt_ctf_field_variant
, parent
);
2396 bt_ctf_field_freeze(variant_field
->tag
);
2397 bt_ctf_field_freeze(variant_field
->payload
);
2398 generic_field_freeze(field
);
2402 void bt_ctf_field_array_freeze(struct bt_ctf_field
*field
)
2405 struct bt_ctf_field_array
*array_field
=
2406 container_of(field
, struct bt_ctf_field_array
, parent
);
2408 for (i
= 0; i
< array_field
->elements
->len
; i
++) {
2409 struct bt_ctf_field
*field
=
2410 g_ptr_array_index(array_field
->elements
, i
);
2412 bt_ctf_field_freeze(field
);
2415 generic_field_freeze(field
);
2419 void bt_ctf_field_sequence_freeze(struct bt_ctf_field
*field
)
2422 struct bt_ctf_field_sequence
*sequence_field
=
2423 container_of(field
, struct bt_ctf_field_sequence
, parent
);
2425 bt_ctf_field_freeze(sequence_field
->length
);
2427 for (i
= 0; i
< sequence_field
->elements
->len
; i
++) {
2428 struct bt_ctf_field
*field
=
2429 g_ptr_array_index(sequence_field
->elements
, i
);
2431 bt_ctf_field_freeze(field
);
2434 generic_field_freeze(field
);
2438 void bt_ctf_field_freeze(struct bt_ctf_field
*field
)
2440 enum bt_ctf_type_id type_id
;
2446 type_id
= bt_ctf_field_get_type_id(field
);
2447 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
2448 type_id
>= BT_CTF_NR_TYPE_IDS
) {
2452 field_freeze_funcs
[type_id
](field
);
2458 bool bt_ctf_field_generic_is_set(struct bt_ctf_field
*field
)
2460 return field
&& field
->payload_set
;
2464 bool bt_ctf_field_enumeration_is_set(struct bt_ctf_field
*field
)
2466 bool is_set
= false;
2467 struct bt_ctf_field_enumeration
*enumeration
;
2473 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
2475 if (!enumeration
->payload
) {
2479 is_set
= bt_ctf_field_is_set(enumeration
->payload
);
2485 bool bt_ctf_field_structure_is_set(struct bt_ctf_field
*field
)
2487 bool is_set
= false;
2489 struct bt_ctf_field_structure
*structure
;
2495 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
2496 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2497 is_set
= bt_ctf_field_is_set(structure
->fields
->pdata
[i
]);
2507 bool bt_ctf_field_variant_is_set(struct bt_ctf_field
*field
)
2509 bool is_set
= false;
2510 struct bt_ctf_field_variant
*variant
;
2516 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
2517 is_set
= bt_ctf_field_is_set(variant
->payload
);
2523 bool bt_ctf_field_array_is_set(struct bt_ctf_field
*field
)
2526 bool is_set
= false;
2527 struct bt_ctf_field_array
*array
;
2533 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
2534 for (i
= 0; i
< array
->elements
->len
; i
++) {
2535 is_set
= bt_ctf_field_is_set(array
->elements
->pdata
[i
]);
2545 bool bt_ctf_field_sequence_is_set(struct bt_ctf_field
*field
)
2548 bool is_set
= false;
2549 struct bt_ctf_field_sequence
*sequence
;
2555 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
2556 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
2557 is_set
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);