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 struct bt_ctf_field_type_enumeration_mapping_iterator
*
870 bt_ctf_field_enumeration_get_mappings(struct bt_ctf_field
*field
)
873 struct bt_ctf_field
*container
= NULL
;
874 struct bt_ctf_field_type
*container_type
= NULL
;
875 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
876 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
878 container
= bt_ctf_field_enumeration_get_container(field
);
883 container_type
= bt_ctf_field_get_type(container
);
884 if (!container_type
) {
885 goto error_put_container
;
888 integer_type
= container_of(container_type
,
889 struct bt_ctf_field_type_integer
, parent
);
891 if (!integer_type
->declaration
.signedness
) {
894 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
897 goto error_put_container_type
;
899 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
904 ret
= bt_ctf_field_signed_integer_get_value(container
,
907 goto error_put_container_type
;
909 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
913 error_put_container_type
:
914 bt_put(container_type
);
921 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
925 struct bt_ctf_field_integer
*integer
;
926 struct bt_ctf_field_type_integer
*integer_type
;
928 if (!field
|| !value
|| !field
->payload_set
||
929 bt_ctf_field_type_get_type_id(field
->type
) !=
930 BT_CTF_TYPE_ID_INTEGER
) {
935 integer_type
= container_of(field
->type
,
936 struct bt_ctf_field_type_integer
, parent
);
937 if (!integer_type
->declaration
.signedness
) {
942 integer
= container_of(field
,
943 struct bt_ctf_field_integer
, parent
);
944 *value
= integer
->definition
.value
._signed
;
949 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
953 struct bt_ctf_field_integer
*integer
;
954 struct bt_ctf_field_type_integer
*integer_type
;
956 int64_t min_value
, max_value
;
958 if (!field
|| field
->frozen
||
959 bt_ctf_field_type_get_type_id(field
->type
) !=
960 BT_CTF_TYPE_ID_INTEGER
) {
965 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
966 integer_type
= container_of(field
->type
,
967 struct bt_ctf_field_type_integer
, parent
);
968 if (!integer_type
->declaration
.signedness
) {
973 size
= integer_type
->declaration
.len
;
974 min_value
= -(1ULL << (size
- 1));
975 max_value
= (1ULL << (size
- 1)) - 1;
976 if (value
< min_value
|| value
> max_value
) {
981 integer
->definition
.value
._signed
= value
;
982 integer
->parent
.payload_set
= 1;
987 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
991 struct bt_ctf_field_integer
*integer
;
992 struct bt_ctf_field_type_integer
*integer_type
;
994 if (!field
|| !value
|| !field
->payload_set
||
995 bt_ctf_field_type_get_type_id(field
->type
) !=
996 BT_CTF_TYPE_ID_INTEGER
) {
1001 integer_type
= container_of(field
->type
,
1002 struct bt_ctf_field_type_integer
, parent
);
1003 if (integer_type
->declaration
.signedness
) {
1008 integer
= container_of(field
,
1009 struct bt_ctf_field_integer
, parent
);
1010 *value
= integer
->definition
.value
._unsigned
;
1015 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
1019 struct bt_ctf_field_integer
*integer
;
1020 struct bt_ctf_field_type_integer
*integer_type
;
1024 if (!field
|| field
->frozen
||
1025 bt_ctf_field_type_get_type_id(field
->type
) !=
1026 BT_CTF_TYPE_ID_INTEGER
) {
1031 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1032 integer_type
= container_of(field
->type
,
1033 struct bt_ctf_field_type_integer
, parent
);
1034 if (integer_type
->declaration
.signedness
) {
1039 size
= integer_type
->declaration
.len
;
1040 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
1041 if (value
> max_value
) {
1046 integer
->definition
.value
._unsigned
= value
;
1047 integer
->parent
.payload_set
= 1;
1052 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
1056 struct bt_ctf_field_floating_point
*floating_point
;
1058 if (!field
|| !value
|| !field
->payload_set
||
1059 bt_ctf_field_type_get_type_id(field
->type
) !=
1060 BT_CTF_TYPE_ID_FLOAT
) {
1065 floating_point
= container_of(field
,
1066 struct bt_ctf_field_floating_point
, parent
);
1067 *value
= floating_point
->definition
.value
;
1072 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
1076 struct bt_ctf_field_floating_point
*floating_point
;
1078 if (!field
|| field
->frozen
||
1079 bt_ctf_field_type_get_type_id(field
->type
) !=
1080 BT_CTF_TYPE_ID_FLOAT
) {
1084 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1086 floating_point
->definition
.value
= value
;
1087 floating_point
->parent
.payload_set
= 1;
1092 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
1094 const char *ret
= NULL
;
1095 struct bt_ctf_field_string
*string
;
1097 if (!field
|| !field
->payload_set
||
1098 bt_ctf_field_type_get_type_id(field
->type
) !=
1099 BT_CTF_TYPE_ID_STRING
) {
1103 string
= container_of(field
,
1104 struct bt_ctf_field_string
, parent
);
1105 ret
= string
->payload
->str
;
1110 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1114 struct bt_ctf_field_string
*string
;
1116 if (!field
|| !value
|| field
->frozen
||
1117 bt_ctf_field_type_get_type_id(field
->type
) !=
1118 BT_CTF_TYPE_ID_STRING
) {
1123 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1124 if (string
->payload
) {
1125 g_string_assign(string
->payload
, value
);
1127 string
->payload
= g_string_new(value
);
1130 string
->parent
.payload_set
= 1;
1135 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1139 struct bt_ctf_field_string
*string_field
;
1141 if (!field
|| !value
|| field
->frozen
||
1142 bt_ctf_field_type_get_type_id(field
->type
) !=
1143 BT_CTF_TYPE_ID_STRING
) {
1148 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1150 if (string_field
->payload
) {
1151 g_string_append(string_field
->payload
, value
);
1153 string_field
->payload
= g_string_new(value
);
1156 string_field
->parent
.payload_set
= 1;
1162 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1163 const char *value
, unsigned int length
)
1167 unsigned int effective_length
= length
;
1168 struct bt_ctf_field_string
*string_field
;
1170 if (!field
|| !value
|| field
->frozen
||
1171 bt_ctf_field_type_get_type_id(field
->type
) !=
1172 BT_CTF_TYPE_ID_STRING
) {
1177 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1179 /* make sure no null bytes are appended */
1180 for (i
= 0; i
< length
; ++i
) {
1181 if (value
[i
] == '\0') {
1182 effective_length
= i
;
1187 if (string_field
->payload
) {
1188 g_string_append_len(string_field
->payload
, value
,
1191 string_field
->payload
= g_string_new_len(value
,
1195 string_field
->parent
.payload_set
= 1;
1202 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1205 enum bt_ctf_type_id type_id
;
1212 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1213 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1218 ret
= field_validate_funcs
[type_id
](field
);
1224 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1227 enum bt_ctf_type_id type_id
;
1234 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1235 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1240 ret
= field_reset_funcs
[type_id
](field
);
1246 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1247 struct ctf_stream_pos
*pos
)
1250 enum bt_ctf_type_id type_id
;
1252 if (!field
|| !pos
) {
1257 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1258 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1263 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1270 bool bt_ctf_field_is_set(struct bt_ctf_field
*field
)
1272 bool is_set
= false;
1273 enum bt_ctf_type_id type_id
;
1279 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1280 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1284 is_set
= field_is_set_funcs
[type_id
](field
);
1289 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1292 struct bt_ctf_field
*copy
= NULL
;
1293 enum bt_ctf_type_id type_id
;
1299 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1300 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1304 copy
= bt_ctf_field_create(field
->type
);
1309 copy
->payload_set
= field
->payload_set
;
1310 ret
= field_copy_funcs
[type_id
](field
, copy
);
1320 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1322 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1323 struct bt_ctf_field_type_integer
, parent
);
1324 struct bt_ctf_field_integer
*integer
= g_new0(
1325 struct bt_ctf_field_integer
, 1);
1328 integer
->definition
.declaration
= &integer_type
->declaration
;
1331 return integer
? &integer
->parent
: NULL
;
1335 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1336 struct bt_ctf_field_type
*type
)
1338 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1339 struct bt_ctf_field_enumeration
, 1);
1341 return enumeration
? &enumeration
->parent
: NULL
;
1345 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1346 struct bt_ctf_field_type
*type
)
1348 struct bt_ctf_field_floating_point
*floating_point
;
1349 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1351 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1352 if (!floating_point
) {
1356 floating_point_type
= container_of(type
,
1357 struct bt_ctf_field_type_floating_point
, parent
);
1358 floating_point
->definition
.declaration
= container_of(
1359 type
->declaration
, struct declaration_float
, p
);
1362 floating_point
->definition
.sign
= &floating_point
->sign
;
1363 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1364 floating_point
->definition
.sign
->p
.declaration
=
1365 &floating_point_type
->sign
.p
;
1367 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1368 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1369 floating_point
->definition
.mantissa
->p
.declaration
=
1370 &floating_point_type
->mantissa
.p
;
1372 floating_point
->definition
.exp
= &floating_point
->exp
;
1373 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1374 floating_point
->definition
.exp
->p
.declaration
=
1375 &floating_point_type
->exp
.p
;
1378 return floating_point
? &floating_point
->parent
: NULL
;
1382 struct bt_ctf_field
*bt_ctf_field_structure_create(
1383 struct bt_ctf_field_type
*type
)
1385 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1386 struct bt_ctf_field_type_structure
, parent
);
1387 struct bt_ctf_field_structure
*structure
= g_new0(
1388 struct bt_ctf_field_structure
, 1);
1389 struct bt_ctf_field
*field
= NULL
;
1395 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1396 structure
->fields
= g_ptr_array_new_with_free_func(
1397 (GDestroyNotify
)bt_ctf_field_put
);
1398 g_ptr_array_set_size(structure
->fields
,
1399 g_hash_table_size(structure
->field_name_to_index
));
1400 field
= &structure
->parent
;
1406 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1408 struct bt_ctf_field_variant
*variant
= g_new0(
1409 struct bt_ctf_field_variant
, 1);
1410 return variant
? &variant
->parent
: NULL
;
1414 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1416 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1417 struct bt_ctf_field_type_array
*array_type
;
1418 unsigned int array_length
;
1420 if (!array
|| !type
) {
1424 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1425 array_length
= array_type
->length
;
1426 array
->elements
= g_ptr_array_sized_new(array_length
);
1427 if (!array
->elements
) {
1431 g_ptr_array_set_free_func(array
->elements
,
1432 (GDestroyNotify
)bt_ctf_field_put
);
1433 g_ptr_array_set_size(array
->elements
, array_length
);
1434 return &array
->parent
;
1441 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1442 struct bt_ctf_field_type
*type
)
1444 struct bt_ctf_field_sequence
*sequence
= g_new0(
1445 struct bt_ctf_field_sequence
, 1);
1446 return sequence
? &sequence
->parent
: NULL
;
1450 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1452 struct bt_ctf_field_string
*string
= g_new0(
1453 struct bt_ctf_field_string
, 1);
1454 return string
? &string
->parent
: NULL
;
1458 void bt_ctf_field_destroy(struct bt_object
*obj
)
1460 struct bt_ctf_field
*field
;
1461 struct bt_ctf_field_type
*type
;
1462 enum bt_ctf_type_id type_id
;
1464 field
= container_of(obj
, struct bt_ctf_field
, base
);
1466 type_id
= bt_ctf_field_type_get_type_id(type
);
1467 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
1468 type_id
>= BT_CTF_NR_TYPE_IDS
) {
1472 field_destroy_funcs
[type_id
](field
);
1477 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1479 struct bt_ctf_field_integer
*integer
;
1485 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1490 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1492 struct bt_ctf_field_enumeration
*enumeration
;
1498 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1500 bt_put(enumeration
->payload
);
1501 g_free(enumeration
);
1505 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1507 struct bt_ctf_field_floating_point
*floating_point
;
1513 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1515 g_free(floating_point
);
1519 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1521 struct bt_ctf_field_structure
*structure
;
1527 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1528 g_ptr_array_free(structure
->fields
, TRUE
);
1533 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1535 struct bt_ctf_field_variant
*variant
;
1541 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1542 bt_put(variant
->tag
);
1543 bt_put(variant
->payload
);
1548 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1550 struct bt_ctf_field_array
*array
;
1556 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1557 g_ptr_array_free(array
->elements
, TRUE
);
1562 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1564 struct bt_ctf_field_sequence
*sequence
;
1570 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1571 if (sequence
->elements
) {
1572 g_ptr_array_free(sequence
->elements
, TRUE
);
1574 bt_put(sequence
->length
);
1579 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1581 struct bt_ctf_field_string
*string
;
1586 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1587 if (string
->payload
) {
1588 g_string_free(string
->payload
, TRUE
);
1594 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1596 return (field
&& field
->payload_set
) ? 0 : -1;
1600 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1603 struct bt_ctf_field_enumeration
*enumeration
;
1610 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1612 if (!enumeration
->payload
) {
1617 ret
= bt_ctf_field_validate(enumeration
->payload
);
1623 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1627 struct bt_ctf_field_structure
*structure
;
1634 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1635 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1636 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1639 struct bt_ctf_field_type
*field_type
=
1640 bt_ctf_field_get_type(field
);
1642 (void) bt_ctf_field_type_structure_get_field(field_type
,
1644 fprintf(stderr
, "Field %s failed validation\n",
1645 name
? name
: "NULL");
1655 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1658 struct bt_ctf_field_variant
*variant
;
1665 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1666 ret
= bt_ctf_field_validate(variant
->payload
);
1672 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1676 struct bt_ctf_field_array
*array
;
1683 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1684 for (i
= 0; i
< array
->elements
->len
; i
++) {
1685 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1687 fprintf(stderr
, "Failed to validate array field #%zu\n", i
);
1696 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1700 struct bt_ctf_field_sequence
*sequence
;
1707 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1708 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1709 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1711 fprintf(stderr
, "Failed to validate sequence field #%zu\n", i
);
1720 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1729 field
->payload_set
= 0;
1735 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1738 struct bt_ctf_field_enumeration
*enumeration
;
1745 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1747 if (!enumeration
->payload
) {
1751 ret
= bt_ctf_field_reset(enumeration
->payload
);
1757 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1761 struct bt_ctf_field_structure
*structure
;
1768 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1769 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1770 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1774 * Structure members are lazily initialized; skip if
1775 * this member has not been allocated yet.
1780 ret
= bt_ctf_field_reset(member
);
1790 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1793 struct bt_ctf_field_variant
*variant
;
1800 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1801 if (variant
->payload
) {
1802 ret
= bt_ctf_field_reset(variant
->payload
);
1809 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1813 struct bt_ctf_field_array
*array
;
1820 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1821 for (i
= 0; i
< array
->elements
->len
; i
++) {
1822 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1826 * Array elements are lazily initialized; skip if
1827 * this member has not been allocated yet.
1832 ret
= bt_ctf_field_reset(member
);
1842 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1846 struct bt_ctf_field_sequence
*sequence
;
1853 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1854 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1855 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1859 * Sequence elements are lazily initialized; skip if
1860 * this member has not been allocated yet.
1865 ret
= bt_ctf_field_reset(member
);
1875 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1878 struct bt_ctf_field_string
*string
;
1885 ret
= bt_ctf_field_generic_reset(field
);
1890 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1891 if (string
->payload
) {
1892 g_string_truncate(string
->payload
, 0);
1899 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1900 struct ctf_stream_pos
*pos
)
1903 struct bt_ctf_field_integer
*integer
= container_of(field
,
1904 struct bt_ctf_field_integer
, parent
);
1906 if (!bt_ctf_field_generic_is_set(field
)) {
1911 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1912 if (ret
== -EFAULT
) {
1914 * The field is too large to fit in the current packet's
1915 * remaining space. Bump the packet size and retry.
1917 ret
= increase_packet_size(pos
);
1928 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1929 struct ctf_stream_pos
*pos
)
1931 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1932 field
, struct bt_ctf_field_enumeration
, parent
);
1934 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1938 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1939 struct ctf_stream_pos
*pos
)
1942 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1943 struct bt_ctf_field_floating_point
, parent
);
1945 if (!bt_ctf_field_generic_is_set(field
)) {
1950 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1951 if (ret
== -EFAULT
) {
1953 * The field is too large to fit in the current packet's
1954 * remaining space. Bump the packet size and retry.
1956 ret
= increase_packet_size(pos
);
1967 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1968 struct ctf_stream_pos
*pos
)
1972 struct bt_ctf_field_structure
*structure
= container_of(
1973 field
, struct bt_ctf_field_structure
, parent
);
1975 while (!ctf_pos_access_ok(pos
,
1976 offset_align(pos
->offset
,
1977 field
->type
->declaration
->alignment
))) {
1978 ret
= increase_packet_size(pos
);
1984 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1989 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1990 struct bt_ctf_field
*member
= g_ptr_array_index(
1991 structure
->fields
, i
);
1993 ret
= bt_ctf_field_serialize(member
, pos
);
1996 struct bt_ctf_field_type
*structure_type
=
1997 bt_ctf_field_get_type(field
);
1999 (void) bt_ctf_field_type_structure_get_field(
2000 structure_type
, &name
, NULL
, i
);
2001 fprintf(stderr
, "Field %s failed to serialize\n",
2002 name
? name
: "NULL");
2003 bt_put(structure_type
);
2012 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
2013 struct ctf_stream_pos
*pos
)
2015 struct bt_ctf_field_variant
*variant
= container_of(
2016 field
, struct bt_ctf_field_variant
, parent
);
2018 return bt_ctf_field_serialize(variant
->payload
, pos
);
2022 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
2023 struct ctf_stream_pos
*pos
)
2027 struct bt_ctf_field_array
*array
= container_of(
2028 field
, struct bt_ctf_field_array
, parent
);
2030 for (i
= 0; i
< array
->elements
->len
; i
++) {
2031 ret
= bt_ctf_field_serialize(
2032 g_ptr_array_index(array
->elements
, i
), pos
);
2034 fprintf(stderr
, "Failed to serialize array element #%zu\n", i
);
2043 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
2044 struct ctf_stream_pos
*pos
)
2048 struct bt_ctf_field_sequence
*sequence
= container_of(
2049 field
, struct bt_ctf_field_sequence
, parent
);
2051 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
2052 ret
= bt_ctf_field_serialize(
2053 g_ptr_array_index(sequence
->elements
, i
), pos
);
2055 fprintf(stderr
, "Failed to serialize sequence element #%zu\n", i
);
2064 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
2065 struct ctf_stream_pos
*pos
)
2069 struct bt_ctf_field_string
*string
= container_of(field
,
2070 struct bt_ctf_field_string
, parent
);
2071 struct bt_ctf_field_type
*character_type
=
2072 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
2073 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
2075 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
2076 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
2077 (uint64_t) string
->payload
->str
[i
]);
2082 ret
= bt_ctf_field_integer_serialize(character
, pos
);
2089 bt_put(character_type
);
2094 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
2095 struct bt_ctf_field
*dst
)
2097 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
2099 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
2100 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
2102 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
2103 sizeof(struct definition_integer
));
2108 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
2109 struct bt_ctf_field
*dst
)
2112 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
2114 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
2115 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
2117 if (enum_src
->payload
) {
2118 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
2119 if (!enum_dst
->payload
) {
2129 int bt_ctf_field_floating_point_copy(
2130 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
2132 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
2134 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
2136 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
2139 memcpy(&float_dst
->definition
, &float_src
->definition
,
2140 sizeof(struct definition_float
));
2141 memcpy(&float_dst
->sign
, &float_src
->sign
,
2142 sizeof(struct definition_integer
));
2143 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
2144 sizeof(struct definition_integer
));
2145 memcpy(&float_dst
->exp
, &float_src
->exp
,
2146 sizeof(struct definition_integer
));
2151 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
2152 struct bt_ctf_field
*dst
)
2155 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
2157 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
2158 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
2160 /* This field_name_to_index HT is owned by the structure field type */
2161 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2162 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2164 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2165 struct bt_ctf_field
*field
=
2166 g_ptr_array_index(struct_src
->fields
, i
);
2167 struct bt_ctf_field
*field_copy
= NULL
;
2170 field_copy
= bt_ctf_field_copy(field
);
2178 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2185 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2186 struct bt_ctf_field
*dst
)
2189 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2191 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2192 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2194 if (variant_src
->tag
) {
2195 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2196 if (!variant_dst
->tag
) {
2201 if (variant_src
->payload
) {
2202 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2203 if (!variant_dst
->payload
) {
2213 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2214 struct bt_ctf_field
*dst
)
2217 struct bt_ctf_field_array
*array_src
, *array_dst
;
2219 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2220 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2222 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2223 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2224 struct bt_ctf_field
*field
=
2225 g_ptr_array_index(array_src
->elements
, i
);
2226 struct bt_ctf_field
*field_copy
= NULL
;
2229 field_copy
= bt_ctf_field_copy(field
);
2237 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2244 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2245 struct bt_ctf_field
*dst
)
2248 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2249 struct bt_ctf_field
*src_length
;
2250 struct bt_ctf_field
*dst_length
;
2252 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2253 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2255 src_length
= bt_ctf_field_sequence_get_length(src
);
2258 /* no length set yet: keep destination sequence empty */
2262 /* copy source length */
2263 dst_length
= bt_ctf_field_copy(src_length
);
2271 /* this will initialize the destination sequence's internal array */
2272 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2279 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2281 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2282 struct bt_ctf_field
*field
=
2283 g_ptr_array_index(sequence_src
->elements
, i
);
2284 struct bt_ctf_field
*field_copy
= NULL
;
2287 field_copy
= bt_ctf_field_copy(field
);
2295 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2302 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2303 struct bt_ctf_field
*dst
)
2306 struct bt_ctf_field_string
*string_src
, *string_dst
;
2308 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2309 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2311 if (string_src
->payload
) {
2312 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2313 if (!string_dst
->payload
) {
2323 int increase_packet_size(struct ctf_stream_pos
*pos
)
2328 ret
= munmap_align(pos
->base_mma
);
2333 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2335 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2336 pos
->packet_size
/ CHAR_BIT
);
2337 } while (ret
== EINTR
);
2344 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2345 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2346 if (pos
->base_mma
== MAP_FAILED
) {
2354 void generic_field_freeze(struct bt_ctf_field
*field
)
2360 void bt_ctf_field_enumeration_freeze(struct bt_ctf_field
*field
)
2362 struct bt_ctf_field_enumeration
*enum_field
=
2363 container_of(field
, struct bt_ctf_field_enumeration
, parent
);
2365 bt_ctf_field_freeze(enum_field
->payload
);
2366 generic_field_freeze(field
);
2370 void bt_ctf_field_structure_freeze(struct bt_ctf_field
*field
)
2373 struct bt_ctf_field_structure
*structure_field
=
2374 container_of(field
, struct bt_ctf_field_structure
, parent
);
2376 for (i
= 0; i
< structure_field
->fields
->len
; i
++) {
2377 struct bt_ctf_field
*field
=
2378 g_ptr_array_index(structure_field
->fields
, i
);
2380 bt_ctf_field_freeze(field
);
2383 generic_field_freeze(field
);
2387 void bt_ctf_field_variant_freeze(struct bt_ctf_field
*field
)
2389 struct bt_ctf_field_variant
*variant_field
=
2390 container_of(field
, struct bt_ctf_field_variant
, parent
);
2392 bt_ctf_field_freeze(variant_field
->tag
);
2393 bt_ctf_field_freeze(variant_field
->payload
);
2394 generic_field_freeze(field
);
2398 void bt_ctf_field_array_freeze(struct bt_ctf_field
*field
)
2401 struct bt_ctf_field_array
*array_field
=
2402 container_of(field
, struct bt_ctf_field_array
, parent
);
2404 for (i
= 0; i
< array_field
->elements
->len
; i
++) {
2405 struct bt_ctf_field
*field
=
2406 g_ptr_array_index(array_field
->elements
, i
);
2408 bt_ctf_field_freeze(field
);
2411 generic_field_freeze(field
);
2415 void bt_ctf_field_sequence_freeze(struct bt_ctf_field
*field
)
2418 struct bt_ctf_field_sequence
*sequence_field
=
2419 container_of(field
, struct bt_ctf_field_sequence
, parent
);
2421 bt_ctf_field_freeze(sequence_field
->length
);
2423 for (i
= 0; i
< sequence_field
->elements
->len
; i
++) {
2424 struct bt_ctf_field
*field
=
2425 g_ptr_array_index(sequence_field
->elements
, i
);
2427 bt_ctf_field_freeze(field
);
2430 generic_field_freeze(field
);
2434 void bt_ctf_field_freeze(struct bt_ctf_field
*field
)
2436 enum bt_ctf_type_id type_id
;
2442 type_id
= bt_ctf_field_get_type_id(field
);
2443 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
2444 type_id
>= BT_CTF_NR_TYPE_IDS
) {
2448 field_freeze_funcs
[type_id
](field
);
2454 bool bt_ctf_field_generic_is_set(struct bt_ctf_field
*field
)
2456 return field
&& field
->payload_set
;
2460 bool bt_ctf_field_enumeration_is_set(struct bt_ctf_field
*field
)
2462 bool is_set
= false;
2463 struct bt_ctf_field_enumeration
*enumeration
;
2469 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
2471 if (!enumeration
->payload
) {
2475 is_set
= bt_ctf_field_is_set(enumeration
->payload
);
2481 bool bt_ctf_field_structure_is_set(struct bt_ctf_field
*field
)
2483 bool is_set
= false;
2485 struct bt_ctf_field_structure
*structure
;
2491 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
2492 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2493 is_set
= bt_ctf_field_is_set(structure
->fields
->pdata
[i
]);
2503 bool bt_ctf_field_variant_is_set(struct bt_ctf_field
*field
)
2505 bool is_set
= false;
2506 struct bt_ctf_field_variant
*variant
;
2512 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
2513 is_set
= bt_ctf_field_is_set(variant
->payload
);
2519 bool bt_ctf_field_array_is_set(struct bt_ctf_field
*field
)
2522 bool is_set
= false;
2523 struct bt_ctf_field_array
*array
;
2529 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
2530 for (i
= 0; i
< array
->elements
->len
; i
++) {
2531 is_set
= bt_ctf_field_is_set(array
->elements
->pdata
[i
]);
2541 bool bt_ctf_field_sequence_is_set(struct bt_ctf_field
*field
)
2544 bool is_set
= false;
2545 struct bt_ctf_field_sequence
*sequence
;
2551 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
2552 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
2553 is_set
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);