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-writer/event-fields.h>
30 #include <babeltrace/ctf-ir/event-fields-internal.h>
31 #include <babeltrace/ctf-ir/event-types-internal.h>
32 #include <babeltrace/compiler.h>
34 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
37 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
39 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
40 struct bt_ctf_field_type
*);
42 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
43 struct bt_ctf_field_type
*);
45 struct bt_ctf_field
*bt_ctf_field_structure_create(
46 struct bt_ctf_field_type
*);
48 struct bt_ctf_field
*bt_ctf_field_variant_create(
49 struct bt_ctf_field_type
*);
51 struct bt_ctf_field
*bt_ctf_field_array_create(
52 struct bt_ctf_field_type
*);
54 struct bt_ctf_field
*bt_ctf_field_sequence_create(
55 struct bt_ctf_field_type
*);
57 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
60 void bt_ctf_field_destroy(struct bt_ctf_ref
*);
62 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*);
64 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*);
66 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*);
68 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*);
70 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*);
72 void bt_ctf_field_array_destroy(struct bt_ctf_field
*);
74 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*);
76 void bt_ctf_field_string_destroy(struct bt_ctf_field
*);
79 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
);
81 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
);
83 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
);
85 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
);
87 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
);
89 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
);
92 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
);
94 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
);
96 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
);
98 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
);
100 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
);
102 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
);
104 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
);
107 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*,
108 struct ctf_stream_pos
*);
110 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
111 struct ctf_stream_pos
*);
113 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
114 struct ctf_stream_pos
*);
116 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
117 struct ctf_stream_pos
*);
119 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
120 struct ctf_stream_pos
*);
122 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
123 struct ctf_stream_pos
*);
125 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
126 struct ctf_stream_pos
*);
128 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
129 struct ctf_stream_pos
*);
132 int increase_packet_size(struct ctf_stream_pos
*pos
);
135 struct bt_ctf_field
*(*field_create_funcs
[])(
136 struct bt_ctf_field_type
*) = {
137 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_create
,
138 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_create
,
140 bt_ctf_field_floating_point_create
,
141 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_create
,
142 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_create
,
143 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_create
,
144 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_create
,
145 [CTF_TYPE_STRING
] = bt_ctf_field_string_create
,
149 void (*field_destroy_funcs
[])(struct bt_ctf_field
*) = {
150 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_destroy
,
151 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_destroy
,
153 bt_ctf_field_floating_point_destroy
,
154 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_destroy
,
155 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_destroy
,
156 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_destroy
,
157 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
158 [CTF_TYPE_STRING
] = bt_ctf_field_string_destroy
,
162 int (*field_validate_funcs
[])(struct bt_ctf_field
*) = {
163 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_validate
,
164 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_validate
,
165 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_validate
,
166 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_validate
,
167 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_validate
,
168 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_validate
,
169 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_validate
,
170 [CTF_TYPE_STRING
] = bt_ctf_field_generic_validate
,
174 int (*field_reset_funcs
[])(struct bt_ctf_field
*) = {
175 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_reset
,
176 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_reset
,
177 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_reset
,
178 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_reset
,
179 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_reset
,
180 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_reset
,
181 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_reset
,
182 [CTF_TYPE_STRING
] = bt_ctf_field_string_reset
,
186 int (*field_serialize_funcs
[])(struct bt_ctf_field
*,
187 struct ctf_stream_pos
*) = {
188 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_serialize
,
189 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_serialize
,
191 bt_ctf_field_floating_point_serialize
,
192 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_serialize
,
193 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_serialize
,
194 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_serialize
,
195 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
196 [CTF_TYPE_STRING
] = bt_ctf_field_string_serialize
,
199 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
201 struct bt_ctf_field
*field
= NULL
;
202 enum ctf_type_id type_id
;
208 type_id
= bt_ctf_field_type_get_type_id(type
);
209 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
||
210 bt_ctf_field_type_validate(type
)) {
214 field
= field_create_funcs
[type_id
](type
);
219 /* The type's declaration can't change after this point */
220 bt_ctf_field_type_freeze(type
);
221 bt_ctf_field_type_get(type
);
222 bt_ctf_ref_init(&field
->ref_count
);
228 void bt_ctf_field_get(struct bt_ctf_field
*field
)
231 bt_ctf_ref_get(&field
->ref_count
);
235 void bt_ctf_field_put(struct bt_ctf_field
*field
)
238 bt_ctf_ref_put(&field
->ref_count
, bt_ctf_field_destroy
);
242 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
244 struct bt_ctf_field_type
*ret
= NULL
;
251 bt_ctf_field_type_get(ret
);
256 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
257 struct bt_ctf_field
*field
)
259 struct bt_ctf_field
*ret
= NULL
;
260 struct bt_ctf_field_sequence
*sequence
;
266 if (bt_ctf_field_type_get_type_id(field
->type
) !=
271 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
272 ret
= sequence
->length
;
273 bt_ctf_field_get(ret
);
278 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
279 struct bt_ctf_field
*length_field
)
282 struct bt_ctf_field_type_integer
*length_type
;
283 struct bt_ctf_field_integer
*length
;
284 struct bt_ctf_field_sequence
*sequence
;
285 uint64_t sequence_length
;
287 if (!field
|| !length_field
) {
291 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
297 length_type
= container_of(length_field
->type
,
298 struct bt_ctf_field_type_integer
, parent
);
299 /* The length field must be unsigned */
300 if (length_type
->declaration
.signedness
) {
305 length
= container_of(length_field
, struct bt_ctf_field_integer
,
307 sequence_length
= length
->definition
.value
._unsigned
;
308 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
309 if (sequence
->elements
) {
310 g_ptr_array_free(sequence
->elements
, TRUE
);
311 bt_ctf_field_put(sequence
->length
);
314 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
315 if (!sequence
->elements
) {
320 g_ptr_array_set_free_func(sequence
->elements
,
321 (GDestroyNotify
)bt_ctf_field_put
);
322 g_ptr_array_set_size(sequence
->elements
, (size_t)sequence_length
);
323 bt_ctf_field_get(length_field
);
324 sequence
->length
= length_field
;
329 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
330 struct bt_ctf_field
*field
, const char *name
)
332 struct bt_ctf_field
*new_field
= NULL
;
334 struct bt_ctf_field_structure
*structure
;
335 struct bt_ctf_field_type
*field_type
= NULL
;
338 if (!field
|| !name
||
339 bt_ctf_field_type_get_type_id(field
->type
) !=
344 field_quark
= g_quark_from_string(name
);
345 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
347 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
349 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
350 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
354 if (structure
->fields
->pdata
[index
]) {
355 new_field
= structure
->fields
->pdata
[index
];
359 new_field
= bt_ctf_field_create(field_type
);
364 structure
->fields
->pdata
[index
] = new_field
;
366 bt_ctf_field_get(new_field
);
369 bt_ctf_field_type_put(field_type
);
374 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
375 struct bt_ctf_field
*field
, size_t index
)
378 const char *field_name
;
379 struct bt_ctf_field_structure
*structure
;
380 struct bt_ctf_field_type
*structure_type
;
381 struct bt_ctf_field_type
*field_type
= NULL
;
382 struct bt_ctf_field
*ret_field
= NULL
;
385 bt_ctf_field_type_get_type_id(field
->type
) != CTF_TYPE_STRUCT
) {
389 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
390 if (index
>= structure
->fields
->len
) {
394 ret_field
= structure
->fields
->pdata
[index
];
399 /* Field has not been instanciated yet, create it */
400 structure_type
= bt_ctf_field_get_type(field
);
401 if (!structure_type
) {
405 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
406 &field_name
, &field_type
, index
);
407 bt_ctf_field_type_put(structure_type
);
412 ret_field
= bt_ctf_field_create(field_type
);
417 structure
->fields
->pdata
[index
] = ret_field
;
419 bt_ctf_field_get(ret_field
);
422 bt_ctf_field_type_put(field_type
);
428 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
429 const char *name
, struct bt_ctf_field
*value
)
433 struct bt_ctf_field_structure
*structure
;
434 struct bt_ctf_field_type
*expected_field_type
= NULL
;
437 if (!field
|| !name
|| !value
||
438 bt_ctf_field_type_get_type_id(field
->type
) !=
444 field_quark
= g_quark_from_string(name
);
445 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
446 expected_field_type
=
447 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
449 if (expected_field_type
!= value
->type
) {
454 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
455 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
459 if (structure
->fields
->pdata
[index
]) {
460 bt_ctf_field_put(structure
->fields
->pdata
[index
]);
463 structure
->fields
->pdata
[index
] = value
;
464 bt_ctf_field_get(value
);
466 if (expected_field_type
) {
467 bt_ctf_field_type_put(expected_field_type
);
472 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
475 struct bt_ctf_field
*new_field
= NULL
;
476 struct bt_ctf_field_type
*field_type
= NULL
;
477 struct bt_ctf_field_array
*array
;
479 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
484 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
485 if (index
>= array
->elements
->len
) {
489 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
490 if (array
->elements
->pdata
[(size_t)index
]) {
491 new_field
= array
->elements
->pdata
[(size_t)index
];
495 new_field
= bt_ctf_field_create(field_type
);
496 bt_ctf_field_get(new_field
);
497 array
->elements
->pdata
[(size_t)index
] = new_field
;
500 bt_ctf_field_type_put(field_type
);
505 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
508 struct bt_ctf_field
*new_field
= NULL
;
509 struct bt_ctf_field_type
*field_type
= NULL
;
510 struct bt_ctf_field_sequence
*sequence
;
512 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
517 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
518 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
522 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
523 if (sequence
->elements
->pdata
[(size_t)index
]) {
524 new_field
= sequence
->elements
->pdata
[(size_t)index
];
528 new_field
= bt_ctf_field_create(field_type
);
529 bt_ctf_field_get(new_field
);
530 sequence
->elements
->pdata
[(size_t)index
] = new_field
;
533 bt_ctf_field_type_put(field_type
);
538 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
539 struct bt_ctf_field
*tag_field
)
541 struct bt_ctf_field
*new_field
= NULL
;
542 struct bt_ctf_field_variant
*variant
;
543 struct bt_ctf_field_type_variant
*variant_type
;
544 struct bt_ctf_field_type
*field_type
;
545 struct bt_ctf_field
*tag_enum
= NULL
;
546 struct bt_ctf_field_integer
*tag_enum_integer
;
547 int64_t tag_enum_value
;
549 if (!field
|| !tag_field
||
550 bt_ctf_field_type_get_type_id(field
->type
) !=
552 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
557 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
558 variant_type
= container_of(field
->type
,
559 struct bt_ctf_field_type_variant
, parent
);
560 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
565 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
568 if (!bt_ctf_field_validate(variant
->tag
)) {
572 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
573 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
574 variant_type
, tag_enum_value
);
579 new_field
= bt_ctf_field_create(field_type
);
584 bt_ctf_field_put(variant
->tag
);
585 bt_ctf_field_put(variant
->payload
);
586 bt_ctf_field_get(new_field
);
587 bt_ctf_field_get(tag_field
);
588 variant
->tag
= tag_field
;
589 variant
->payload
= new_field
;
591 bt_ctf_field_put(tag_enum
);
595 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
596 struct bt_ctf_field
*field
)
598 struct bt_ctf_field
*container
= NULL
;
599 struct bt_ctf_field_enumeration
*enumeration
;
601 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
606 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
608 if (!enumeration
->payload
) {
609 struct bt_ctf_field_type_enumeration
*enumeration_type
=
610 container_of(field
->type
,
611 struct bt_ctf_field_type_enumeration
, parent
);
612 enumeration
->payload
=
613 bt_ctf_field_create(enumeration_type
->container
);
616 container
= enumeration
->payload
;
617 bt_ctf_field_get(container
);
622 const char *bt_ctf_field_enumeration_get_mapping_name(
623 struct bt_ctf_field
*field
)
626 const char *name
= NULL
;
627 struct bt_ctf_field
*container
= NULL
;
628 struct bt_ctf_field_type
*container_type
= NULL
;
629 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
630 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
632 container
= bt_ctf_field_enumeration_get_container(field
);
637 container_type
= bt_ctf_field_get_type(container
);
638 if (!container_type
) {
639 goto error_put_container
;
642 integer_type
= container_of(container_type
,
643 struct bt_ctf_field_type_integer
, parent
);
644 enumeration_type
= container_of(field
->type
,
645 struct bt_ctf_field_type_enumeration
, parent
);
647 if (!integer_type
->declaration
.signedness
) {
649 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
652 goto error_put_container_type
;
655 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
656 enumeration_type
, value
);
659 ret
= bt_ctf_field_signed_integer_get_value(container
,
662 goto error_put_container_type
;
665 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
666 enumeration_type
, value
);
669 error_put_container_type
:
670 bt_ctf_field_type_put(container_type
);
672 bt_ctf_field_put(container
);
677 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
681 struct bt_ctf_field_integer
*integer
;
682 struct bt_ctf_field_type_integer
*integer_type
;
684 if (!field
|| !value
|| !field
->payload_set
||
685 bt_ctf_field_type_get_type_id(field
->type
) !=
691 integer_type
= container_of(field
->type
,
692 struct bt_ctf_field_type_integer
, parent
);
693 if (!integer_type
->declaration
.signedness
) {
698 integer
= container_of(field
,
699 struct bt_ctf_field_integer
, parent
);
700 *value
= integer
->definition
.value
._signed
;
705 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
709 struct bt_ctf_field_integer
*integer
;
710 struct bt_ctf_field_type_integer
*integer_type
;
712 int64_t min_value
, max_value
;
715 bt_ctf_field_type_get_type_id(field
->type
) !=
721 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
722 integer_type
= container_of(field
->type
,
723 struct bt_ctf_field_type_integer
, parent
);
724 if (!integer_type
->declaration
.signedness
) {
729 size
= integer_type
->declaration
.len
;
730 min_value
= -((int64_t)1 << (size
- 1));
731 max_value
= ((int64_t)1 << (size
- 1)) - 1;
732 if (value
< min_value
|| value
> max_value
) {
737 integer
->definition
.value
._signed
= value
;
738 integer
->parent
.payload_set
= 1;
743 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
747 struct bt_ctf_field_integer
*integer
;
748 struct bt_ctf_field_type_integer
*integer_type
;
750 if (!field
|| !value
|| !field
->payload_set
||
751 bt_ctf_field_type_get_type_id(field
->type
) !=
757 integer_type
= container_of(field
->type
,
758 struct bt_ctf_field_type_integer
, parent
);
759 if (integer_type
->declaration
.signedness
) {
764 integer
= container_of(field
,
765 struct bt_ctf_field_integer
, parent
);
766 *value
= integer
->definition
.value
._unsigned
;
771 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
775 struct bt_ctf_field_integer
*integer
;
776 struct bt_ctf_field_type_integer
*integer_type
;
781 bt_ctf_field_type_get_type_id(field
->type
) !=
787 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
788 integer_type
= container_of(field
->type
,
789 struct bt_ctf_field_type_integer
, parent
);
790 if (integer_type
->declaration
.signedness
) {
795 size
= integer_type
->declaration
.len
;
796 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
797 if (value
> max_value
) {
802 integer
->definition
.value
._unsigned
= value
;
803 integer
->parent
.payload_set
= 1;
808 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
812 struct bt_ctf_field_floating_point
*floating_point
;
814 if (!field
|| !value
|| !field
->payload_set
||
815 bt_ctf_field_type_get_type_id(field
->type
) !=
821 floating_point
= container_of(field
,
822 struct bt_ctf_field_floating_point
, parent
);
823 *value
= floating_point
->definition
.value
;
828 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
832 struct bt_ctf_field_floating_point
*floating_point
;
835 bt_ctf_field_type_get_type_id(field
->type
) !=
840 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
842 floating_point
->definition
.value
= value
;
843 floating_point
->parent
.payload_set
= 1;
848 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
850 const char *ret
= NULL
;
851 struct bt_ctf_field_string
*string
;
853 if (!field
|| !field
->payload_set
||
854 bt_ctf_field_type_get_type_id(field
->type
) !=
859 string
= container_of(field
,
860 struct bt_ctf_field_string
, parent
);
861 ret
= string
->payload
->str
;
866 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
870 struct bt_ctf_field_string
*string
;
872 if (!field
|| !value
||
873 bt_ctf_field_type_get_type_id(field
->type
) !=
879 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
880 if (string
->payload
) {
881 g_string_assign(string
->payload
, value
);
883 string
->payload
= g_string_new(value
);
886 string
->parent
.payload_set
= 1;
892 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
895 enum ctf_type_id type_id
;
902 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
903 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
908 ret
= field_validate_funcs
[type_id
](field
);
914 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
917 enum ctf_type_id type_id
;
924 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
925 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
930 ret
= field_reset_funcs
[type_id
](field
);
936 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
937 struct ctf_stream_pos
*pos
)
940 enum ctf_type_id type_id
;
942 if (!field
|| !pos
) {
947 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
948 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
953 ret
= field_serialize_funcs
[type_id
](field
, pos
);
959 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
961 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
962 struct bt_ctf_field_type_integer
, parent
);
963 struct bt_ctf_field_integer
*integer
= g_new0(
964 struct bt_ctf_field_integer
, 1);
967 integer
->definition
.declaration
= &integer_type
->declaration
;
970 return integer
? &integer
->parent
: NULL
;
974 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
975 struct bt_ctf_field_type
*type
)
977 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
978 struct bt_ctf_field_enumeration
, 1);
980 return enumeration
? &enumeration
->parent
: NULL
;
984 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
985 struct bt_ctf_field_type
*type
)
987 struct bt_ctf_field_floating_point
*floating_point
;
988 struct bt_ctf_field_type_floating_point
*floating_point_type
;
990 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
991 if (!floating_point
) {
995 floating_point_type
= container_of(type
,
996 struct bt_ctf_field_type_floating_point
, parent
);
997 floating_point
->definition
.declaration
= container_of(
998 type
->declaration
, struct declaration_float
, p
);
1001 floating_point
->definition
.sign
= &floating_point
->sign
;
1002 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1003 floating_point
->definition
.sign
->p
.declaration
=
1004 &floating_point_type
->sign
.p
;
1006 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1007 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1008 floating_point
->definition
.mantissa
->p
.declaration
=
1009 &floating_point_type
->mantissa
.p
;
1011 floating_point
->definition
.exp
= &floating_point
->exp
;
1012 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1013 floating_point
->definition
.exp
->p
.declaration
=
1014 &floating_point_type
->exp
.p
;
1017 return floating_point
? &floating_point
->parent
: NULL
;
1021 struct bt_ctf_field
*bt_ctf_field_structure_create(
1022 struct bt_ctf_field_type
*type
)
1024 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1025 struct bt_ctf_field_type_structure
, parent
);
1026 struct bt_ctf_field_structure
*structure
= g_new0(
1027 struct bt_ctf_field_structure
, 1);
1028 struct bt_ctf_field
*field
= NULL
;
1030 if (!structure
|| !structure_type
->fields
->len
) {
1034 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1035 structure
->fields
= g_ptr_array_new_with_free_func(
1036 (GDestroyNotify
)bt_ctf_field_put
);
1037 g_ptr_array_set_size(structure
->fields
,
1038 g_hash_table_size(structure
->field_name_to_index
));
1039 field
= &structure
->parent
;
1045 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1047 struct bt_ctf_field_variant
*variant
= g_new0(
1048 struct bt_ctf_field_variant
, 1);
1049 return variant
? &variant
->parent
: NULL
;
1053 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1055 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1056 struct bt_ctf_field_type_array
*array_type
;
1057 unsigned int array_length
;
1059 if (!array
|| !type
) {
1063 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1064 array_length
= array_type
->length
;
1065 array
->elements
= g_ptr_array_sized_new(array_length
);
1066 if (!array
->elements
) {
1070 g_ptr_array_set_free_func(array
->elements
,
1071 (GDestroyNotify
)bt_ctf_field_put
);
1072 g_ptr_array_set_size(array
->elements
, array_length
);
1073 return &array
->parent
;
1080 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1081 struct bt_ctf_field_type
*type
)
1083 struct bt_ctf_field_sequence
*sequence
= g_new0(
1084 struct bt_ctf_field_sequence
, 1);
1085 return sequence
? &sequence
->parent
: NULL
;
1089 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1091 struct bt_ctf_field_string
*string
= g_new0(
1092 struct bt_ctf_field_string
, 1);
1093 return string
? &string
->parent
: NULL
;
1097 void bt_ctf_field_destroy(struct bt_ctf_ref
*ref
)
1099 struct bt_ctf_field
*field
;
1100 struct bt_ctf_field_type
*type
;
1101 enum ctf_type_id type_id
;
1107 field
= container_of(ref
, struct bt_ctf_field
, ref_count
);
1109 type_id
= bt_ctf_field_type_get_type_id(type
);
1110 if (type_id
<= CTF_TYPE_UNKNOWN
||
1111 type_id
>= NR_CTF_TYPES
) {
1115 field_destroy_funcs
[type_id
](field
);
1117 bt_ctf_field_type_put(type
);
1122 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1124 struct bt_ctf_field_integer
*integer
;
1130 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1135 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1137 struct bt_ctf_field_enumeration
*enumeration
;
1143 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1145 bt_ctf_field_put(enumeration
->payload
);
1146 g_free(enumeration
);
1150 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1152 struct bt_ctf_field_floating_point
*floating_point
;
1158 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1160 g_free(floating_point
);
1164 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1166 struct bt_ctf_field_structure
*structure
;
1172 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1173 g_ptr_array_free(structure
->fields
, TRUE
);
1178 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1180 struct bt_ctf_field_variant
*variant
;
1186 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1187 bt_ctf_field_put(variant
->tag
);
1188 bt_ctf_field_put(variant
->payload
);
1193 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1195 struct bt_ctf_field_array
*array
;
1201 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1202 g_ptr_array_free(array
->elements
, TRUE
);
1207 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1209 struct bt_ctf_field_sequence
*sequence
;
1215 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1216 g_ptr_array_free(sequence
->elements
, TRUE
);
1217 bt_ctf_field_put(sequence
->length
);
1222 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1224 struct bt_ctf_field_string
*string
;
1229 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1230 g_string_free(string
->payload
, TRUE
);
1235 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1237 return (field
&& field
->payload_set
) ? 0 : -1;
1241 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1244 struct bt_ctf_field_enumeration
*enumeration
;
1251 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1253 if (!enumeration
->payload
) {
1258 ret
= bt_ctf_field_validate(enumeration
->payload
);
1264 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1268 struct bt_ctf_field_structure
*structure
;
1275 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1276 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1277 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1287 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1290 struct bt_ctf_field_variant
*variant
;
1297 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1298 ret
= bt_ctf_field_validate(variant
->payload
);
1304 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1308 struct bt_ctf_field_array
*array
;
1315 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1316 for (i
= 0; i
< array
->elements
->len
; i
++) {
1317 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1327 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1331 struct bt_ctf_field_sequence
*sequence
;
1338 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1339 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1340 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1350 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1359 field
->payload_set
= 0;
1365 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1368 struct bt_ctf_field_enumeration
*enumeration
;
1375 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1377 if (!enumeration
->payload
) {
1381 ret
= bt_ctf_field_reset(enumeration
->payload
);
1387 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1391 struct bt_ctf_field_structure
*structure
;
1398 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1399 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1400 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1404 * Structure members are lazily initialized; skip if
1405 * this member has not been allocated yet.
1410 ret
= bt_ctf_field_reset(member
);
1420 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1423 struct bt_ctf_field_variant
*variant
;
1430 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1431 if (variant
->payload
) {
1432 ret
= bt_ctf_field_reset(variant
->payload
);
1439 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1443 struct bt_ctf_field_array
*array
;
1450 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1451 for (i
= 0; i
< array
->elements
->len
; i
++) {
1452 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1456 * Array elements are lazily initialized; skip if
1457 * this member has not been allocated yet.
1462 ret
= bt_ctf_field_reset(member
);
1472 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1476 struct bt_ctf_field_sequence
*sequence
;
1483 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1484 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1485 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1489 * Sequence elements are lazily initialized; skip if
1490 * this member has not been allocated yet.
1495 ret
= bt_ctf_field_reset(member
);
1505 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1508 struct bt_ctf_field_string
*string
;
1515 ret
= bt_ctf_field_generic_reset(field
);
1520 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1521 if (string
->payload
) {
1522 g_string_truncate(string
->payload
, 0);
1529 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1530 struct ctf_stream_pos
*pos
)
1533 struct bt_ctf_field_integer
*integer
= container_of(field
,
1534 struct bt_ctf_field_integer
, parent
);
1537 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1538 if (ret
== -EFAULT
) {
1540 * The field is too large to fit in the current packet's
1541 * remaining space. Bump the packet size and retry.
1543 ret
= increase_packet_size(pos
);
1554 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1555 struct ctf_stream_pos
*pos
)
1557 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1558 field
, struct bt_ctf_field_enumeration
, parent
);
1560 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1564 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1565 struct ctf_stream_pos
*pos
)
1568 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1569 struct bt_ctf_field_floating_point
, parent
);
1572 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1573 if (ret
== -EFAULT
) {
1575 * The field is too large to fit in the current packet's
1576 * remaining space. Bump the packet size and retry.
1578 ret
= increase_packet_size(pos
);
1589 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1590 struct ctf_stream_pos
*pos
)
1594 struct bt_ctf_field_structure
*structure
= container_of(
1595 field
, struct bt_ctf_field_structure
, parent
);
1597 while (!ctf_pos_access_ok(pos
,
1598 offset_align(pos
->offset
,
1599 field
->type
->declaration
->alignment
))) {
1600 ret
= increase_packet_size(pos
);
1606 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1611 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1612 struct bt_ctf_field
*field
= g_ptr_array_index(
1613 structure
->fields
, i
);
1615 ret
= bt_ctf_field_serialize(field
, pos
);
1625 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1626 struct ctf_stream_pos
*pos
)
1628 struct bt_ctf_field_variant
*variant
= container_of(
1629 field
, struct bt_ctf_field_variant
, parent
);
1631 return bt_ctf_field_serialize(variant
->payload
, pos
);
1635 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1636 struct ctf_stream_pos
*pos
)
1640 struct bt_ctf_field_array
*array
= container_of(
1641 field
, struct bt_ctf_field_array
, parent
);
1643 for (i
= 0; i
< array
->elements
->len
; i
++) {
1644 ret
= bt_ctf_field_serialize(
1645 g_ptr_array_index(array
->elements
, i
), pos
);
1655 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1656 struct ctf_stream_pos
*pos
)
1660 struct bt_ctf_field_sequence
*sequence
= container_of(
1661 field
, struct bt_ctf_field_sequence
, parent
);
1663 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1664 ret
= bt_ctf_field_serialize(
1665 g_ptr_array_index(sequence
->elements
, i
), pos
);
1675 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1676 struct ctf_stream_pos
*pos
)
1680 struct bt_ctf_field_string
*string
= container_of(field
,
1681 struct bt_ctf_field_string
, parent
);
1682 struct bt_ctf_field_type
*character_type
=
1683 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1684 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1686 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1687 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1688 (uint64_t) string
->payload
->str
[i
]);
1693 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1699 bt_ctf_field_put(character
);
1700 bt_ctf_field_type_put(character_type
);
1705 int increase_packet_size(struct ctf_stream_pos
*pos
)
1710 ret
= munmap_align(pos
->base_mma
);
1715 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
1716 ret
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1717 pos
->packet_size
/ CHAR_BIT
);
1722 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1723 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1724 if (pos
->base_mma
== MAP_FAILED
) {