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/ctf-writer/serialize-internal.h>
32 #include <babeltrace/object-internal.h>
33 #include <babeltrace/ref.h>
34 #include <babeltrace/compiler.h>
35 #include <babeltrace/compat/fcntl.h>
36 #include <babeltrace/align.h>
38 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
41 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
43 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
44 struct bt_ctf_field_type
*);
46 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
47 struct bt_ctf_field_type
*);
49 struct bt_ctf_field
*bt_ctf_field_structure_create(
50 struct bt_ctf_field_type
*);
52 struct bt_ctf_field
*bt_ctf_field_variant_create(
53 struct bt_ctf_field_type
*);
55 struct bt_ctf_field
*bt_ctf_field_array_create(
56 struct bt_ctf_field_type
*);
58 struct bt_ctf_field
*bt_ctf_field_sequence_create(
59 struct bt_ctf_field_type
*);
61 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
64 void bt_ctf_field_destroy(struct bt_object
*);
66 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*);
68 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*);
70 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*);
72 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*);
74 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*);
76 void bt_ctf_field_array_destroy(struct bt_ctf_field
*);
78 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*);
80 void bt_ctf_field_string_destroy(struct bt_ctf_field
*);
83 int bt_ctf_field_generic_validate(struct bt_ctf_field
*);
85 int bt_ctf_field_structure_validate(struct bt_ctf_field
*);
87 int bt_ctf_field_variant_validate(struct bt_ctf_field
*);
89 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*);
91 int bt_ctf_field_array_validate(struct bt_ctf_field
*);
93 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*);
96 int bt_ctf_field_generic_reset(struct bt_ctf_field
*);
98 int bt_ctf_field_structure_reset(struct bt_ctf_field
*);
100 int bt_ctf_field_variant_reset(struct bt_ctf_field
*);
102 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*);
104 int bt_ctf_field_array_reset(struct bt_ctf_field
*);
106 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*);
108 int bt_ctf_field_string_reset(struct bt_ctf_field
*);
111 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*,
112 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
114 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
115 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
117 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
118 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
120 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
121 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
123 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
124 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
126 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
127 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
129 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
130 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
132 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
133 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
136 int bt_ctf_field_integer_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
138 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
140 int bt_ctf_field_floating_point_copy(struct bt_ctf_field
*,
141 struct bt_ctf_field
*);
143 int bt_ctf_field_structure_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
145 int bt_ctf_field_variant_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
147 int bt_ctf_field_array_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
149 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
151 int bt_ctf_field_string_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
154 void generic_field_freeze(struct bt_ctf_field
*);
156 void bt_ctf_field_enumeration_freeze(struct bt_ctf_field
*);
158 void bt_ctf_field_structure_freeze(struct bt_ctf_field
*);
160 void bt_ctf_field_variant_freeze(struct bt_ctf_field
*);
162 void bt_ctf_field_array_freeze(struct bt_ctf_field
*);
164 void bt_ctf_field_sequence_freeze(struct bt_ctf_field
*);
167 bool bt_ctf_field_generic_is_set(struct bt_ctf_field
*);
169 bool bt_ctf_field_structure_is_set(struct bt_ctf_field
*);
171 bool bt_ctf_field_variant_is_set(struct bt_ctf_field
*);
173 bool bt_ctf_field_enumeration_is_set(struct bt_ctf_field
*);
175 bool bt_ctf_field_array_is_set(struct bt_ctf_field
*);
177 bool bt_ctf_field_sequence_is_set(struct bt_ctf_field
*);
180 int increase_packet_size(struct bt_ctf_stream_pos
*pos
);
183 struct bt_ctf_field
*(* const field_create_funcs
[])(
184 struct bt_ctf_field_type
*) = {
185 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_integer_create
,
186 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_create
,
187 [BT_CTF_FIELD_TYPE_ID_FLOAT
] =
188 bt_ctf_field_floating_point_create
,
189 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_structure_create
,
190 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_variant_create
,
191 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_array_create
,
192 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_create
,
193 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_string_create
,
197 void (* const field_destroy_funcs
[])(struct bt_ctf_field
*) = {
198 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_integer_destroy
,
199 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_destroy
,
200 [BT_CTF_FIELD_TYPE_ID_FLOAT
] =
201 bt_ctf_field_floating_point_destroy
,
202 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_structure_destroy
,
203 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_variant_destroy
,
204 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_array_destroy
,
205 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
206 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_string_destroy
,
210 int (* const field_validate_funcs
[])(struct bt_ctf_field
*) = {
211 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_generic_validate
,
212 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_validate
,
213 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = bt_ctf_field_generic_validate
,
214 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_structure_validate
,
215 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_variant_validate
,
216 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_array_validate
,
217 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_validate
,
218 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_generic_validate
,
222 int (* const field_reset_funcs
[])(struct bt_ctf_field
*) = {
223 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_generic_reset
,
224 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_reset
,
225 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = bt_ctf_field_generic_reset
,
226 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_structure_reset
,
227 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_variant_reset
,
228 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_array_reset
,
229 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_reset
,
230 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_string_reset
,
234 int (* const field_serialize_funcs
[])(struct bt_ctf_field
*,
235 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
) = {
236 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_integer_serialize
,
237 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_serialize
,
238 [BT_CTF_FIELD_TYPE_ID_FLOAT
] =
239 bt_ctf_field_floating_point_serialize
,
240 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_structure_serialize
,
241 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_variant_serialize
,
242 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_array_serialize
,
243 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
244 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_string_serialize
,
248 int (* const field_copy_funcs
[])(struct bt_ctf_field
*,
249 struct bt_ctf_field
*) = {
250 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_integer_copy
,
251 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_copy
,
252 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = bt_ctf_field_floating_point_copy
,
253 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_structure_copy
,
254 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_variant_copy
,
255 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_array_copy
,
256 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_copy
,
257 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_string_copy
,
261 void (* const field_freeze_funcs
[])(struct bt_ctf_field
*) = {
262 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = generic_field_freeze
,
263 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = generic_field_freeze
,
264 [BT_CTF_FIELD_TYPE_ID_STRING
] = generic_field_freeze
,
265 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_freeze
,
266 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_structure_freeze
,
267 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_variant_freeze
,
268 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_array_freeze
,
269 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_freeze
,
273 bool (* const field_is_set_funcs
[])(struct bt_ctf_field
*) = {
274 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_generic_is_set
,
275 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_is_set
,
276 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = bt_ctf_field_generic_is_set
,
277 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_structure_is_set
,
278 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_variant_is_set
,
279 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_array_is_set
,
280 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_is_set
,
281 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_generic_is_set
,
284 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
286 struct bt_ctf_field
*field
= NULL
;
287 enum bt_ctf_field_type_id type_id
;
294 type_id
= bt_ctf_field_type_get_type_id(type
);
295 if (type_id
<= BT_CTF_FIELD_TYPE_ID_UNKNOWN
||
296 type_id
>= BT_CTF_NR_TYPE_IDS
) {
300 /* Field class MUST be valid */
301 ret
= bt_ctf_field_type_validate(type
);
308 field
= field_create_funcs
[type_id
](type
);
313 /* The type's declaration can't change after this point */
314 bt_ctf_field_type_freeze(type
);
316 bt_object_init(field
, bt_ctf_field_destroy
);
322 void bt_ctf_field_get(struct bt_ctf_field
*field
)
327 void bt_ctf_field_put(struct bt_ctf_field
*field
)
332 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
334 struct bt_ctf_field_type
*ret
= NULL
;
346 enum bt_ctf_field_type_id
bt_ctf_field_get_type_id(struct bt_ctf_field
*field
)
348 enum bt_ctf_field_type_id ret
= BT_CTF_FIELD_TYPE_ID_UNKNOWN
;
354 ret
= bt_ctf_field_type_get_type_id(field
->type
);
359 int bt_ctf_field_is_integer(struct bt_ctf_field
*field
)
361 return bt_ctf_field_get_type_id(field
) == BT_CTF_FIELD_TYPE_ID_INTEGER
;
364 int bt_ctf_field_is_floating_point(struct bt_ctf_field
*field
)
366 return bt_ctf_field_get_type_id(field
) == BT_CTF_FIELD_TYPE_ID_FLOAT
;
369 int bt_ctf_field_is_enumeration(struct bt_ctf_field
*field
)
371 return bt_ctf_field_get_type_id(field
) == BT_CTF_FIELD_TYPE_ID_ENUM
;
374 int bt_ctf_field_is_string(struct bt_ctf_field
*field
)
376 return bt_ctf_field_get_type_id(field
) == BT_CTF_FIELD_TYPE_ID_STRING
;
379 int bt_ctf_field_is_structure(struct bt_ctf_field
*field
)
381 return bt_ctf_field_get_type_id(field
) == BT_CTF_FIELD_TYPE_ID_STRUCT
;
384 int bt_ctf_field_is_array(struct bt_ctf_field
*field
)
386 return bt_ctf_field_get_type_id(field
) == BT_CTF_FIELD_TYPE_ID_ARRAY
;
389 int bt_ctf_field_is_sequence(struct bt_ctf_field
*field
)
391 return bt_ctf_field_get_type_id(field
) == BT_CTF_FIELD_TYPE_ID_SEQUENCE
;
394 int bt_ctf_field_is_variant(struct bt_ctf_field
*field
)
396 return bt_ctf_field_get_type_id(field
) == BT_CTF_FIELD_TYPE_ID_VARIANT
;
399 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
400 struct bt_ctf_field
*field
)
402 struct bt_ctf_field
*ret
= NULL
;
403 struct bt_ctf_field_sequence
*sequence
;
409 if (bt_ctf_field_type_get_type_id(field
->type
) !=
410 BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
414 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
415 ret
= sequence
->length
;
421 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
422 struct bt_ctf_field
*length_field
)
425 struct bt_ctf_field_type_integer
*length_type
;
426 struct bt_ctf_field_integer
*length
;
427 struct bt_ctf_field_sequence
*sequence
;
428 uint64_t sequence_length
;
430 if (!field
|| !length_field
|| field
->frozen
) {
434 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
435 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
440 length_type
= container_of(length_field
->type
,
441 struct bt_ctf_field_type_integer
, parent
);
442 /* The length field must be unsigned */
443 if (length_type
->is_signed
) {
448 length
= container_of(length_field
, struct bt_ctf_field_integer
,
450 sequence_length
= length
->payload
.unsignd
;
451 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
452 if (sequence
->elements
) {
453 g_ptr_array_free(sequence
->elements
, TRUE
);
454 bt_put(sequence
->length
);
457 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
458 if (!sequence
->elements
) {
463 g_ptr_array_set_free_func(sequence
->elements
,
464 (GDestroyNotify
) bt_put
);
465 g_ptr_array_set_size(sequence
->elements
, (size_t) sequence_length
);
466 bt_get(length_field
);
467 sequence
->length
= length_field
;
472 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
473 struct bt_ctf_field
*field
, const char *name
)
475 struct bt_ctf_field
*new_field
= NULL
;
477 struct bt_ctf_field_structure
*structure
;
478 struct bt_ctf_field_type
*field_type
= NULL
;
481 if (!field
|| !name
||
482 bt_ctf_field_type_get_type_id(field
->type
) !=
483 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
487 field_quark
= g_quark_from_string(name
);
488 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
490 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
492 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
493 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
497 if (structure
->fields
->pdata
[index
]) {
498 new_field
= structure
->fields
->pdata
[index
];
502 /* We don't want to modify this field if it's frozen */
507 new_field
= bt_ctf_field_create(field_type
);
512 structure
->fields
->pdata
[index
] = new_field
;
522 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
523 struct bt_ctf_field
*field
, int index
)
526 const char *field_name
;
527 struct bt_ctf_field_structure
*structure
;
528 struct bt_ctf_field_type
*structure_type
;
529 struct bt_ctf_field_type
*field_type
= NULL
;
530 struct bt_ctf_field
*ret_field
= NULL
;
533 bt_ctf_field_type_get_type_id(field
->type
) !=
534 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
538 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
539 if (index
>= structure
->fields
->len
) {
543 ret_field
= structure
->fields
->pdata
[index
];
548 /* We don't want to modify this field if it's frozen */
553 /* Field has not been instanciated yet, create it */
554 structure_type
= bt_ctf_field_get_type(field
);
555 if (!structure_type
) {
559 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
560 &field_name
, &field_type
, index
);
561 bt_put(structure_type
);
566 ret_field
= bt_ctf_field_create(field_type
);
571 structure
->fields
->pdata
[index
] = ret_field
;
579 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
580 const char *name
, struct bt_ctf_field
*value
)
584 struct bt_ctf_field_structure
*structure
;
585 struct bt_ctf_field_type
*expected_field_type
= NULL
;
588 if (!field
|| !name
|| !value
|| field
->frozen
||
589 bt_ctf_field_type_get_type_id(field
->type
) !=
590 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
595 field_quark
= g_quark_from_string(name
);
596 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
597 expected_field_type
=
598 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
601 if (bt_ctf_field_type_compare(expected_field_type
, value
->type
)) {
606 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
607 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
611 if (structure
->fields
->pdata
[index
]) {
612 bt_put(structure
->fields
->pdata
[index
]);
615 structure
->fields
->pdata
[index
] = value
;
618 if (expected_field_type
) {
619 bt_put(expected_field_type
);
624 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
627 struct bt_ctf_field
*new_field
= NULL
;
628 struct bt_ctf_field_type
*field_type
= NULL
;
629 struct bt_ctf_field_array
*array
;
631 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
632 BT_CTF_FIELD_TYPE_ID_ARRAY
) {
636 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
637 if (index
>= array
->elements
->len
) {
641 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
642 if (array
->elements
->pdata
[(size_t)index
]) {
643 new_field
= array
->elements
->pdata
[(size_t)index
];
647 /* We don't want to modify this field if it's frozen */
652 new_field
= bt_ctf_field_create(field_type
);
653 array
->elements
->pdata
[(size_t)index
] = new_field
;
664 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
667 struct bt_ctf_field
*new_field
= NULL
;
668 struct bt_ctf_field_type
*field_type
= NULL
;
669 struct bt_ctf_field_sequence
*sequence
;
671 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
672 BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
676 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
677 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
681 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
682 if (sequence
->elements
->pdata
[(size_t) index
]) {
683 new_field
= sequence
->elements
->pdata
[(size_t) index
];
687 /* We don't want to modify this field if it's frozen */
692 new_field
= bt_ctf_field_create(field_type
);
693 sequence
->elements
->pdata
[(size_t) index
] = new_field
;
704 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
705 struct bt_ctf_field
*tag_field
)
707 struct bt_ctf_field
*new_field
= NULL
;
708 struct bt_ctf_field_variant
*variant
;
709 struct bt_ctf_field_type_variant
*variant_type
;
710 struct bt_ctf_field_type
*field_type
;
711 struct bt_ctf_field
*tag_enum
= NULL
;
712 struct bt_ctf_field_integer
*tag_enum_integer
;
713 int64_t tag_enum_value
;
715 if (!field
|| !tag_field
||
716 bt_ctf_field_type_get_type_id(field
->type
) !=
717 BT_CTF_FIELD_TYPE_ID_VARIANT
||
718 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
719 BT_CTF_FIELD_TYPE_ID_ENUM
) {
723 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
724 variant_type
= container_of(field
->type
,
725 struct bt_ctf_field_type_variant
, parent
);
726 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
731 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
734 if (bt_ctf_field_validate(tag_field
) < 0) {
738 tag_enum_value
= tag_enum_integer
->payload
.signd
;
741 * If the variant currently has a tag and a payload, and if the
742 * requested tag value is the same as the current one, return
743 * the current payload instead of creating a fresh one.
745 if (variant
->tag
&& variant
->payload
) {
746 struct bt_ctf_field
*cur_tag_container
= NULL
;
747 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
748 int64_t cur_tag_value
;
751 bt_ctf_field_enumeration_get_container(variant
->tag
);
752 assert(cur_tag_container
);
753 cur_tag_enum_integer
= container_of(cur_tag_container
,
754 struct bt_ctf_field_integer
, parent
);
755 bt_put(cur_tag_container
);
756 cur_tag_value
= cur_tag_enum_integer
->payload
.signd
;
758 if (cur_tag_value
== tag_enum_value
) {
759 new_field
= variant
->payload
;
765 /* We don't want to modify this field if it's frozen */
770 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
771 variant_type
, tag_enum_value
);
776 new_field
= bt_ctf_field_create(field_type
);
781 bt_put(variant
->tag
);
782 bt_put(variant
->payload
);
785 variant
->tag
= tag_field
;
786 variant
->payload
= new_field
;
792 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
793 struct bt_ctf_field
*variant_field
)
795 struct bt_ctf_field
*current_field
= NULL
;
796 struct bt_ctf_field_variant
*variant
;
798 if (!variant_field
||
799 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
800 BT_CTF_FIELD_TYPE_ID_VARIANT
) {
804 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
807 if (variant
->payload
) {
808 current_field
= variant
->payload
;
809 bt_get(current_field
);
814 return current_field
;
817 struct bt_ctf_field
*bt_ctf_field_variant_get_tag(
818 struct bt_ctf_field
*variant_field
)
820 struct bt_ctf_field
*tag
= NULL
;
821 struct bt_ctf_field_variant
*variant
;
823 if (!variant_field
||
824 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
825 BT_CTF_FIELD_TYPE_ID_VARIANT
) {
829 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
832 tag
= bt_get(variant
->tag
);
838 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
839 struct bt_ctf_field
*field
)
841 struct bt_ctf_field
*container
= NULL
;
842 struct bt_ctf_field_enumeration
*enumeration
;
844 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
845 BT_CTF_FIELD_TYPE_ID_ENUM
) {
849 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
851 if (!enumeration
->payload
) {
852 /* We don't want to modify this field if it's frozen */
857 struct bt_ctf_field_type_enumeration
*enumeration_type
=
858 container_of(field
->type
,
859 struct bt_ctf_field_type_enumeration
, parent
);
860 enumeration
->payload
=
861 bt_ctf_field_create(enumeration_type
->container
);
864 container
= enumeration
->payload
;
870 struct bt_ctf_field_type_enumeration_mapping_iterator
*
871 bt_ctf_field_enumeration_get_mappings(struct bt_ctf_field
*field
)
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
->is_signed
) {
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(
905 ret
= bt_ctf_field_signed_integer_get_value(container
,
908 goto error_put_container_type
;
910 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
914 error_put_container_type
:
915 bt_put(container_type
);
922 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
926 struct bt_ctf_field_integer
*integer
;
927 struct bt_ctf_field_type_integer
*integer_type
;
929 if (!field
|| !value
|| !field
->payload_set
||
930 bt_ctf_field_type_get_type_id(field
->type
) !=
931 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
936 integer_type
= container_of(field
->type
,
937 struct bt_ctf_field_type_integer
, parent
);
938 if (!integer_type
->is_signed
) {
943 integer
= container_of(field
,
944 struct bt_ctf_field_integer
, parent
);
945 *value
= integer
->payload
.signd
;
950 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
954 struct bt_ctf_field_integer
*integer
;
955 struct bt_ctf_field_type_integer
*integer_type
;
957 int64_t min_value
, max_value
;
959 if (!field
|| field
->frozen
||
960 bt_ctf_field_type_get_type_id(field
->type
) !=
961 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
966 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
967 integer_type
= container_of(field
->type
,
968 struct bt_ctf_field_type_integer
, parent
);
969 if (!integer_type
->is_signed
) {
974 size
= integer_type
->size
;
975 min_value
= -(1ULL << (size
- 1));
976 max_value
= (1ULL << (size
- 1)) - 1;
977 if (value
< min_value
|| value
> max_value
) {
982 integer
->payload
.signd
= value
;
983 integer
->parent
.payload_set
= 1;
988 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
992 struct bt_ctf_field_integer
*integer
;
993 struct bt_ctf_field_type_integer
*integer_type
;
995 if (!field
|| !value
|| !field
->payload_set
||
996 bt_ctf_field_type_get_type_id(field
->type
) !=
997 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1002 integer_type
= container_of(field
->type
,
1003 struct bt_ctf_field_type_integer
, parent
);
1004 if (integer_type
->is_signed
) {
1009 integer
= container_of(field
,
1010 struct bt_ctf_field_integer
, parent
);
1011 *value
= integer
->payload
.unsignd
;
1016 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
1020 struct bt_ctf_field_integer
*integer
;
1021 struct bt_ctf_field_type_integer
*integer_type
;
1025 if (!field
|| field
->frozen
||
1026 bt_ctf_field_type_get_type_id(field
->type
) !=
1027 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1032 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1033 integer_type
= container_of(field
->type
,
1034 struct bt_ctf_field_type_integer
, parent
);
1035 if (integer_type
->is_signed
) {
1040 size
= integer_type
->size
;
1041 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
1042 if (value
> max_value
) {
1047 integer
->payload
.unsignd
= value
;
1048 integer
->parent
.payload_set
= 1;
1053 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
1057 struct bt_ctf_field_floating_point
*floating_point
;
1059 if (!field
|| !value
|| !field
->payload_set
||
1060 bt_ctf_field_type_get_type_id(field
->type
) !=
1061 BT_CTF_FIELD_TYPE_ID_FLOAT
) {
1066 floating_point
= container_of(field
,
1067 struct bt_ctf_field_floating_point
, parent
);
1068 *value
= floating_point
->payload
;
1073 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
1077 struct bt_ctf_field_floating_point
*floating_point
;
1079 if (!field
|| field
->frozen
||
1080 bt_ctf_field_type_get_type_id(field
->type
) !=
1081 BT_CTF_FIELD_TYPE_ID_FLOAT
) {
1085 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1087 floating_point
->payload
= value
;
1088 floating_point
->parent
.payload_set
= 1;
1093 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
1095 const char *ret
= NULL
;
1096 struct bt_ctf_field_string
*string
;
1098 if (!field
|| !field
->payload_set
||
1099 bt_ctf_field_type_get_type_id(field
->type
) !=
1100 BT_CTF_FIELD_TYPE_ID_STRING
) {
1104 string
= container_of(field
,
1105 struct bt_ctf_field_string
, parent
);
1106 ret
= string
->payload
->str
;
1111 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1115 struct bt_ctf_field_string
*string
;
1117 if (!field
|| !value
|| field
->frozen
||
1118 bt_ctf_field_type_get_type_id(field
->type
) !=
1119 BT_CTF_FIELD_TYPE_ID_STRING
) {
1124 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1125 if (string
->payload
) {
1126 g_string_assign(string
->payload
, value
);
1128 string
->payload
= g_string_new(value
);
1131 string
->parent
.payload_set
= 1;
1136 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1140 struct bt_ctf_field_string
*string_field
;
1142 if (!field
|| !value
|| field
->frozen
||
1143 bt_ctf_field_type_get_type_id(field
->type
) !=
1144 BT_CTF_FIELD_TYPE_ID_STRING
) {
1149 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1151 if (string_field
->payload
) {
1152 g_string_append(string_field
->payload
, value
);
1154 string_field
->payload
= g_string_new(value
);
1157 string_field
->parent
.payload_set
= 1;
1163 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1164 const char *value
, unsigned int length
)
1168 unsigned int effective_length
= length
;
1169 struct bt_ctf_field_string
*string_field
;
1171 if (!field
|| !value
|| field
->frozen
||
1172 bt_ctf_field_type_get_type_id(field
->type
) !=
1173 BT_CTF_FIELD_TYPE_ID_STRING
) {
1178 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1180 /* make sure no null bytes are appended */
1181 for (i
= 0; i
< length
; ++i
) {
1182 if (value
[i
] == '\0') {
1183 effective_length
= i
;
1188 if (string_field
->payload
) {
1189 g_string_append_len(string_field
->payload
, value
,
1192 string_field
->payload
= g_string_new_len(value
,
1196 string_field
->parent
.payload_set
= 1;
1203 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1206 enum bt_ctf_field_type_id type_id
;
1213 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1214 if (type_id
<= BT_CTF_FIELD_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1219 ret
= field_validate_funcs
[type_id
](field
);
1225 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1228 enum bt_ctf_field_type_id type_id
;
1235 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1236 if (type_id
<= BT_CTF_FIELD_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1241 ret
= field_reset_funcs
[type_id
](field
);
1247 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1248 struct bt_ctf_stream_pos
*pos
,
1249 enum bt_ctf_byte_order native_byte_order
)
1252 enum bt_ctf_field_type_id type_id
;
1254 if (!field
|| !pos
) {
1259 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1260 if (type_id
<= BT_CTF_FIELD_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1265 ret
= field_serialize_funcs
[type_id
](field
, pos
, native_byte_order
);
1272 bool bt_ctf_field_is_set(struct bt_ctf_field
*field
)
1274 bool is_set
= false;
1275 enum bt_ctf_field_type_id type_id
;
1281 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1282 if (type_id
<= BT_CTF_FIELD_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1286 is_set
= field_is_set_funcs
[type_id
](field
);
1291 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1294 struct bt_ctf_field
*copy
= NULL
;
1295 enum bt_ctf_field_type_id type_id
;
1301 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1302 if (type_id
<= BT_CTF_FIELD_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1306 copy
= bt_ctf_field_create(field
->type
);
1311 copy
->payload_set
= field
->payload_set
;
1312 ret
= field_copy_funcs
[type_id
](field
, copy
);
1322 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1324 struct bt_ctf_field_integer
*integer
= g_new0(
1325 struct bt_ctf_field_integer
, 1);
1327 return integer
? &integer
->parent
: NULL
;
1331 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1332 struct bt_ctf_field_type
*type
)
1334 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1335 struct bt_ctf_field_enumeration
, 1);
1337 return enumeration
? &enumeration
->parent
: NULL
;
1341 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1342 struct bt_ctf_field_type
*type
)
1344 struct bt_ctf_field_floating_point
*floating_point
;
1346 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1347 return floating_point
? &floating_point
->parent
: NULL
;
1351 struct bt_ctf_field
*bt_ctf_field_structure_create(
1352 struct bt_ctf_field_type
*type
)
1354 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1355 struct bt_ctf_field_type_structure
, parent
);
1356 struct bt_ctf_field_structure
*structure
= g_new0(
1357 struct bt_ctf_field_structure
, 1);
1358 struct bt_ctf_field
*field
= NULL
;
1364 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1365 structure
->fields
= g_ptr_array_new_with_free_func(
1366 (GDestroyNotify
)bt_ctf_field_put
);
1367 g_ptr_array_set_size(structure
->fields
,
1368 g_hash_table_size(structure
->field_name_to_index
));
1369 field
= &structure
->parent
;
1375 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1377 struct bt_ctf_field_variant
*variant
= g_new0(
1378 struct bt_ctf_field_variant
, 1);
1379 return variant
? &variant
->parent
: NULL
;
1383 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1385 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1386 struct bt_ctf_field_type_array
*array_type
;
1387 unsigned int array_length
;
1389 if (!array
|| !type
) {
1393 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1394 array_length
= array_type
->length
;
1395 array
->elements
= g_ptr_array_sized_new(array_length
);
1396 if (!array
->elements
) {
1400 g_ptr_array_set_free_func(array
->elements
,
1401 (GDestroyNotify
)bt_ctf_field_put
);
1402 g_ptr_array_set_size(array
->elements
, array_length
);
1403 return &array
->parent
;
1410 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1411 struct bt_ctf_field_type
*type
)
1413 struct bt_ctf_field_sequence
*sequence
= g_new0(
1414 struct bt_ctf_field_sequence
, 1);
1415 return sequence
? &sequence
->parent
: NULL
;
1419 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1421 struct bt_ctf_field_string
*string
= g_new0(
1422 struct bt_ctf_field_string
, 1);
1423 return string
? &string
->parent
: NULL
;
1427 void bt_ctf_field_destroy(struct bt_object
*obj
)
1429 struct bt_ctf_field
*field
;
1430 struct bt_ctf_field_type
*type
;
1431 enum bt_ctf_field_type_id type_id
;
1433 field
= container_of(obj
, struct bt_ctf_field
, base
);
1435 type_id
= bt_ctf_field_type_get_type_id(type
);
1436 if (type_id
<= BT_CTF_FIELD_TYPE_ID_UNKNOWN
||
1437 type_id
>= BT_CTF_NR_TYPE_IDS
) {
1441 field_destroy_funcs
[type_id
](field
);
1446 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1448 struct bt_ctf_field_integer
*integer
;
1454 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1459 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1461 struct bt_ctf_field_enumeration
*enumeration
;
1467 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1469 bt_put(enumeration
->payload
);
1470 g_free(enumeration
);
1474 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1476 struct bt_ctf_field_floating_point
*floating_point
;
1482 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1484 g_free(floating_point
);
1488 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1490 struct bt_ctf_field_structure
*structure
;
1496 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1497 g_ptr_array_free(structure
->fields
, TRUE
);
1502 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1504 struct bt_ctf_field_variant
*variant
;
1510 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1511 bt_put(variant
->tag
);
1512 bt_put(variant
->payload
);
1517 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1519 struct bt_ctf_field_array
*array
;
1525 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1526 g_ptr_array_free(array
->elements
, TRUE
);
1531 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1533 struct bt_ctf_field_sequence
*sequence
;
1539 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1540 if (sequence
->elements
) {
1541 g_ptr_array_free(sequence
->elements
, TRUE
);
1543 bt_put(sequence
->length
);
1548 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1550 struct bt_ctf_field_string
*string
;
1555 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1556 if (string
->payload
) {
1557 g_string_free(string
->payload
, TRUE
);
1563 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1565 return (field
&& field
->payload_set
) ? 0 : -1;
1569 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1572 struct bt_ctf_field_enumeration
*enumeration
;
1579 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1581 if (!enumeration
->payload
) {
1586 ret
= bt_ctf_field_validate(enumeration
->payload
);
1592 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1596 struct bt_ctf_field_structure
*structure
;
1603 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1604 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1605 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1608 struct bt_ctf_field_type
*field_type
=
1609 bt_ctf_field_get_type(field
);
1611 (void) bt_ctf_field_type_structure_get_field(field_type
,
1613 fprintf(stderr
, "Field %s failed validation\n",
1614 name
? name
: "NULL");
1624 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1627 struct bt_ctf_field_variant
*variant
;
1634 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1635 ret
= bt_ctf_field_validate(variant
->payload
);
1641 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1645 struct bt_ctf_field_array
*array
;
1652 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1653 for (i
= 0; i
< array
->elements
->len
; i
++) {
1654 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1656 fprintf(stderr
, "Failed to validate array field #%zu\n", i
);
1665 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1669 struct bt_ctf_field_sequence
*sequence
;
1676 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1677 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1678 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1680 fprintf(stderr
, "Failed to validate sequence field #%zu\n", i
);
1689 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1698 field
->payload_set
= 0;
1704 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1707 struct bt_ctf_field_enumeration
*enumeration
;
1714 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1716 if (!enumeration
->payload
) {
1720 ret
= bt_ctf_field_reset(enumeration
->payload
);
1726 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1730 struct bt_ctf_field_structure
*structure
;
1737 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1738 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1739 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1743 * Structure members are lazily initialized; skip if
1744 * this member has not been allocated yet.
1749 ret
= bt_ctf_field_reset(member
);
1759 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1762 struct bt_ctf_field_variant
*variant
;
1769 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1770 if (variant
->payload
) {
1771 ret
= bt_ctf_field_reset(variant
->payload
);
1778 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1782 struct bt_ctf_field_array
*array
;
1789 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1790 for (i
= 0; i
< array
->elements
->len
; i
++) {
1791 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1795 * Array elements are lazily initialized; skip if
1796 * this member has not been allocated yet.
1801 ret
= bt_ctf_field_reset(member
);
1811 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1815 struct bt_ctf_field_sequence
*sequence
;
1822 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1823 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1824 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1828 * Sequence elements are lazily initialized; skip if
1829 * this member has not been allocated yet.
1834 ret
= bt_ctf_field_reset(member
);
1844 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1847 struct bt_ctf_field_string
*string
;
1854 ret
= bt_ctf_field_generic_reset(field
);
1859 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1860 if (string
->payload
) {
1861 g_string_truncate(string
->payload
, 0);
1868 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1869 struct bt_ctf_stream_pos
*pos
,
1870 enum bt_ctf_byte_order native_byte_order
)
1873 struct bt_ctf_field_integer
*integer
= container_of(field
,
1874 struct bt_ctf_field_integer
, parent
);
1876 if (!bt_ctf_field_generic_is_set(field
)) {
1881 ret
= bt_ctf_field_integer_write(integer
, pos
, native_byte_order
);
1882 if (ret
== -EFAULT
) {
1884 * The field is too large to fit in the current packet's
1885 * remaining space. Bump the packet size and retry.
1887 ret
= increase_packet_size(pos
);
1898 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1899 struct bt_ctf_stream_pos
*pos
,
1900 enum bt_ctf_byte_order native_byte_order
)
1902 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1903 field
, struct bt_ctf_field_enumeration
, parent
);
1905 return bt_ctf_field_serialize(enumeration
->payload
, pos
,
1910 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1911 struct bt_ctf_stream_pos
*pos
,
1912 enum bt_ctf_byte_order native_byte_order
)
1915 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1916 struct bt_ctf_field_floating_point
, parent
);
1918 if (!bt_ctf_field_generic_is_set(field
)) {
1923 ret
= bt_ctf_field_floating_point_write(floating_point
, pos
,
1925 if (ret
== -EFAULT
) {
1927 * The field is too large to fit in the current packet's
1928 * remaining space. Bump the packet size and retry.
1930 ret
= increase_packet_size(pos
);
1941 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1942 struct bt_ctf_stream_pos
*pos
,
1943 enum bt_ctf_byte_order native_byte_order
)
1947 struct bt_ctf_field_structure
*structure
= container_of(
1948 field
, struct bt_ctf_field_structure
, parent
);
1950 while (!bt_ctf_stream_pos_access_ok(pos
,
1951 offset_align(pos
->offset
, field
->type
->alignment
))) {
1952 ret
= increase_packet_size(pos
);
1958 if (!bt_ctf_stream_pos_align(pos
, field
->type
->alignment
)) {
1963 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1964 struct bt_ctf_field
*member
= g_ptr_array_index(
1965 structure
->fields
, i
);
1967 ret
= bt_ctf_field_serialize(member
, pos
, native_byte_order
);
1970 struct bt_ctf_field_type
*structure_type
=
1971 bt_ctf_field_get_type(field
);
1973 (void) bt_ctf_field_type_structure_get_field(
1974 structure_type
, &name
, NULL
, i
);
1975 fprintf(stderr
, "Field %s failed to serialize\n",
1976 name
? name
: "NULL");
1977 bt_put(structure_type
);
1986 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1987 struct bt_ctf_stream_pos
*pos
,
1988 enum bt_ctf_byte_order native_byte_order
)
1990 struct bt_ctf_field_variant
*variant
= container_of(
1991 field
, struct bt_ctf_field_variant
, parent
);
1993 return bt_ctf_field_serialize(variant
->payload
, pos
,
1998 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1999 struct bt_ctf_stream_pos
*pos
,
2000 enum bt_ctf_byte_order native_byte_order
)
2004 struct bt_ctf_field_array
*array
= container_of(
2005 field
, struct bt_ctf_field_array
, parent
);
2007 for (i
= 0; i
< array
->elements
->len
; i
++) {
2008 ret
= bt_ctf_field_serialize(
2009 g_ptr_array_index(array
->elements
, i
), pos
,
2012 fprintf(stderr
, "Failed to serialize array element #%zu\n", i
);
2021 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
2022 struct bt_ctf_stream_pos
*pos
,
2023 enum bt_ctf_byte_order native_byte_order
)
2027 struct bt_ctf_field_sequence
*sequence
= container_of(
2028 field
, struct bt_ctf_field_sequence
, parent
);
2030 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
2031 ret
= bt_ctf_field_serialize(
2032 g_ptr_array_index(sequence
->elements
, i
), pos
,
2035 fprintf(stderr
, "Failed to serialize sequence element #%zu\n", i
);
2044 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
2045 struct bt_ctf_stream_pos
*pos
,
2046 enum bt_ctf_byte_order native_byte_order
)
2050 struct bt_ctf_field_string
*string
= container_of(field
,
2051 struct bt_ctf_field_string
, parent
);
2052 struct bt_ctf_field_type
*character_type
=
2053 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
2054 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
2056 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
2057 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
2058 (uint64_t) string
->payload
->str
[i
]);
2063 ret
= bt_ctf_field_integer_serialize(character
, pos
,
2071 bt_put(character_type
);
2076 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
2077 struct bt_ctf_field
*dst
)
2079 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
2081 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
2082 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
2083 integer_dst
->payload
= integer_src
->payload
;
2088 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
2089 struct bt_ctf_field
*dst
)
2092 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
2094 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
2095 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
2097 if (enum_src
->payload
) {
2098 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
2099 if (!enum_dst
->payload
) {
2109 int bt_ctf_field_floating_point_copy(
2110 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
2112 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
2114 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
2116 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
2118 float_dst
->payload
= float_src
->payload
;
2123 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
2124 struct bt_ctf_field
*dst
)
2127 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
2129 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
2130 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
2132 /* This field_name_to_index HT is owned by the structure field type */
2133 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2134 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2136 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2137 struct bt_ctf_field
*field
=
2138 g_ptr_array_index(struct_src
->fields
, i
);
2139 struct bt_ctf_field
*field_copy
= NULL
;
2142 field_copy
= bt_ctf_field_copy(field
);
2150 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2157 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2158 struct bt_ctf_field
*dst
)
2161 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2163 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2164 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2166 if (variant_src
->tag
) {
2167 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2168 if (!variant_dst
->tag
) {
2173 if (variant_src
->payload
) {
2174 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2175 if (!variant_dst
->payload
) {
2185 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2186 struct bt_ctf_field
*dst
)
2189 struct bt_ctf_field_array
*array_src
, *array_dst
;
2191 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2192 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2194 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2195 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2196 struct bt_ctf_field
*field
=
2197 g_ptr_array_index(array_src
->elements
, i
);
2198 struct bt_ctf_field
*field_copy
= NULL
;
2201 field_copy
= bt_ctf_field_copy(field
);
2209 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2216 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2217 struct bt_ctf_field
*dst
)
2220 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2221 struct bt_ctf_field
*src_length
;
2222 struct bt_ctf_field
*dst_length
;
2224 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2225 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2227 src_length
= bt_ctf_field_sequence_get_length(src
);
2230 /* no length set yet: keep destination sequence empty */
2234 /* copy source length */
2235 dst_length
= bt_ctf_field_copy(src_length
);
2243 /* this will initialize the destination sequence's internal array */
2244 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2251 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2253 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2254 struct bt_ctf_field
*field
=
2255 g_ptr_array_index(sequence_src
->elements
, i
);
2256 struct bt_ctf_field
*field_copy
= NULL
;
2259 field_copy
= bt_ctf_field_copy(field
);
2267 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2274 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2275 struct bt_ctf_field
*dst
)
2278 struct bt_ctf_field_string
*string_src
, *string_dst
;
2280 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2281 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2283 if (string_src
->payload
) {
2284 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2285 if (!string_dst
->payload
) {
2295 int increase_packet_size(struct bt_ctf_stream_pos
*pos
)
2300 ret
= munmap_align(pos
->base_mma
);
2305 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2307 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2308 pos
->packet_size
/ CHAR_BIT
);
2309 } while (ret
== EINTR
);
2316 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2317 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2318 if (pos
->base_mma
== MAP_FAILED
) {
2326 void generic_field_freeze(struct bt_ctf_field
*field
)
2332 void bt_ctf_field_enumeration_freeze(struct bt_ctf_field
*field
)
2334 struct bt_ctf_field_enumeration
*enum_field
=
2335 container_of(field
, struct bt_ctf_field_enumeration
, parent
);
2337 bt_ctf_field_freeze(enum_field
->payload
);
2338 generic_field_freeze(field
);
2342 void bt_ctf_field_structure_freeze(struct bt_ctf_field
*field
)
2345 struct bt_ctf_field_structure
*structure_field
=
2346 container_of(field
, struct bt_ctf_field_structure
, parent
);
2348 for (i
= 0; i
< structure_field
->fields
->len
; i
++) {
2349 struct bt_ctf_field
*field
=
2350 g_ptr_array_index(structure_field
->fields
, i
);
2352 bt_ctf_field_freeze(field
);
2355 generic_field_freeze(field
);
2359 void bt_ctf_field_variant_freeze(struct bt_ctf_field
*field
)
2361 struct bt_ctf_field_variant
*variant_field
=
2362 container_of(field
, struct bt_ctf_field_variant
, parent
);
2364 bt_ctf_field_freeze(variant_field
->tag
);
2365 bt_ctf_field_freeze(variant_field
->payload
);
2366 generic_field_freeze(field
);
2370 void bt_ctf_field_array_freeze(struct bt_ctf_field
*field
)
2373 struct bt_ctf_field_array
*array_field
=
2374 container_of(field
, struct bt_ctf_field_array
, parent
);
2376 for (i
= 0; i
< array_field
->elements
->len
; i
++) {
2377 struct bt_ctf_field
*field
=
2378 g_ptr_array_index(array_field
->elements
, i
);
2380 bt_ctf_field_freeze(field
);
2383 generic_field_freeze(field
);
2387 void bt_ctf_field_sequence_freeze(struct bt_ctf_field
*field
)
2390 struct bt_ctf_field_sequence
*sequence_field
=
2391 container_of(field
, struct bt_ctf_field_sequence
, parent
);
2393 bt_ctf_field_freeze(sequence_field
->length
);
2395 for (i
= 0; i
< sequence_field
->elements
->len
; i
++) {
2396 struct bt_ctf_field
*field
=
2397 g_ptr_array_index(sequence_field
->elements
, i
);
2399 bt_ctf_field_freeze(field
);
2402 generic_field_freeze(field
);
2406 void bt_ctf_field_freeze(struct bt_ctf_field
*field
)
2408 enum bt_ctf_field_type_id type_id
;
2414 type_id
= bt_ctf_field_get_type_id(field
);
2415 if (type_id
<= BT_CTF_FIELD_TYPE_ID_UNKNOWN
||
2416 type_id
>= BT_CTF_NR_TYPE_IDS
) {
2420 field_freeze_funcs
[type_id
](field
);
2426 bool bt_ctf_field_generic_is_set(struct bt_ctf_field
*field
)
2428 return field
&& field
->payload_set
;
2432 bool bt_ctf_field_enumeration_is_set(struct bt_ctf_field
*field
)
2434 bool is_set
= false;
2435 struct bt_ctf_field_enumeration
*enumeration
;
2441 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
2443 if (!enumeration
->payload
) {
2447 is_set
= bt_ctf_field_is_set(enumeration
->payload
);
2453 bool bt_ctf_field_structure_is_set(struct bt_ctf_field
*field
)
2455 bool is_set
= false;
2457 struct bt_ctf_field_structure
*structure
;
2463 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
2464 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2465 is_set
= bt_ctf_field_is_set(structure
->fields
->pdata
[i
]);
2475 bool bt_ctf_field_variant_is_set(struct bt_ctf_field
*field
)
2477 bool is_set
= false;
2478 struct bt_ctf_field_variant
*variant
;
2484 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
2485 is_set
= bt_ctf_field_is_set(variant
->payload
);
2491 bool bt_ctf_field_array_is_set(struct bt_ctf_field
*field
)
2494 bool is_set
= false;
2495 struct bt_ctf_field_array
*array
;
2501 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
2502 for (i
= 0; i
< array
->elements
->len
; i
++) {
2503 is_set
= bt_ctf_field_is_set(array
->elements
->pdata
[i
]);
2513 bool bt_ctf_field_sequence_is_set(struct bt_ctf_field
*field
)
2516 bool is_set
= false;
2517 struct bt_ctf_field_sequence
*sequence
;
2523 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
2524 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
2525 is_set
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);