4 * Babeltrace CTF Writer
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_integer_serialize(struct bt_ctf_field
*,
93 struct ctf_stream_pos
*);
95 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
96 struct ctf_stream_pos
*);
98 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
99 struct ctf_stream_pos
*);
101 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
102 struct ctf_stream_pos
*);
104 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
105 struct ctf_stream_pos
*);
107 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
108 struct ctf_stream_pos
*);
110 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
111 struct ctf_stream_pos
*);
113 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
114 struct ctf_stream_pos
*);
117 int increase_packet_size(struct ctf_stream_pos
*pos
);
120 struct bt_ctf_field
*(*field_create_funcs
[])(
121 struct bt_ctf_field_type
*) = {
122 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_create
,
123 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_create
,
125 bt_ctf_field_floating_point_create
,
126 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_create
,
127 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_create
,
128 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_create
,
129 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_create
,
130 [CTF_TYPE_STRING
] = bt_ctf_field_string_create
,
134 void (*field_destroy_funcs
[])(struct bt_ctf_field
*) = {
135 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_destroy
,
136 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_destroy
,
138 bt_ctf_field_floating_point_destroy
,
139 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_destroy
,
140 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_destroy
,
141 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_destroy
,
142 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
143 [CTF_TYPE_STRING
] = bt_ctf_field_string_destroy
,
147 int (*field_validate_funcs
[])(struct bt_ctf_field
*) = {
148 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_validate
,
149 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_validate
,
150 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_validate
,
151 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_validate
,
152 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_validate
,
153 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_validate
,
154 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_validate
,
155 [CTF_TYPE_STRING
] = bt_ctf_field_generic_validate
,
159 int (*field_serialize_funcs
[])(struct bt_ctf_field
*,
160 struct ctf_stream_pos
*) = {
161 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_serialize
,
162 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_serialize
,
164 bt_ctf_field_floating_point_serialize
,
165 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_serialize
,
166 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_serialize
,
167 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_serialize
,
168 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
169 [CTF_TYPE_STRING
] = bt_ctf_field_string_serialize
,
172 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
174 struct bt_ctf_field
*field
= NULL
;
175 enum ctf_type_id type_id
;
181 type_id
= bt_ctf_field_type_get_type_id(type
);
182 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
||
183 bt_ctf_field_type_validate(type
)) {
187 field
= field_create_funcs
[type_id
](type
);
192 /* The type's declaration can't change after this point */
193 bt_ctf_field_type_freeze(type
);
194 bt_ctf_field_type_get(type
);
195 bt_ctf_ref_init(&field
->ref_count
);
201 void bt_ctf_field_get(struct bt_ctf_field
*field
)
204 bt_ctf_ref_get(&field
->ref_count
);
208 void bt_ctf_field_put(struct bt_ctf_field
*field
)
211 bt_ctf_ref_put(&field
->ref_count
, bt_ctf_field_destroy
);
215 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
217 struct bt_ctf_field_type
*ret
= NULL
;
224 bt_ctf_field_type_get(ret
);
229 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
230 struct bt_ctf_field
*field
)
232 struct bt_ctf_field
*ret
= NULL
;
233 struct bt_ctf_field_sequence
*sequence
;
239 if (bt_ctf_field_type_get_type_id(field
->type
) !=
244 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
245 ret
= sequence
->length
;
246 bt_ctf_field_get(ret
);
251 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
252 struct bt_ctf_field
*length_field
)
255 struct bt_ctf_field_type_integer
*length_type
;
256 struct bt_ctf_field_integer
*length
;
257 struct bt_ctf_field_sequence
*sequence
;
258 uint64_t sequence_length
;
260 if (!field
|| !length_field
) {
264 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
270 length_type
= container_of(length_field
->type
,
271 struct bt_ctf_field_type_integer
, parent
);
272 /* The length field must be unsigned */
273 if (length_type
->declaration
.signedness
) {
278 length
= container_of(length_field
, struct bt_ctf_field_integer
,
280 sequence_length
= length
->definition
.value
._unsigned
;
281 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
282 if (sequence
->elements
) {
283 g_ptr_array_free(sequence
->elements
, TRUE
);
284 bt_ctf_field_put(sequence
->length
);
287 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
288 if (!sequence
->elements
) {
293 g_ptr_array_set_free_func(sequence
->elements
,
294 (GDestroyNotify
)bt_ctf_field_put
);
295 g_ptr_array_set_size(sequence
->elements
, (size_t)sequence_length
);
296 bt_ctf_field_get(length_field
);
297 sequence
->length
= length_field
;
302 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
303 struct bt_ctf_field
*field
, const char *name
)
305 struct bt_ctf_field
*new_field
= NULL
;
307 struct bt_ctf_field_structure
*structure
;
308 struct bt_ctf_field_type
*field_type
= NULL
;
311 if (!field
|| !name
||
312 bt_ctf_field_type_get_type_id(field
->type
) !=
317 field_quark
= g_quark_from_string(name
);
318 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
320 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
322 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
323 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
327 if (structure
->fields
->pdata
[index
]) {
328 new_field
= structure
->fields
->pdata
[index
];
332 new_field
= bt_ctf_field_create(field_type
);
337 structure
->fields
->pdata
[index
] = new_field
;
339 bt_ctf_field_get(new_field
);
342 bt_ctf_field_type_put(field_type
);
347 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
348 struct bt_ctf_field
*field
, size_t index
)
351 const char *field_name
;
352 struct bt_ctf_field_structure
*structure
;
353 struct bt_ctf_field_type
*structure_type
;
354 struct bt_ctf_field_type
*field_type
= NULL
;
355 struct bt_ctf_field
*ret_field
= NULL
;
358 bt_ctf_field_type_get_type_id(field
->type
) != CTF_TYPE_STRUCT
) {
362 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
363 if (index
>= structure
->fields
->len
) {
367 ret_field
= structure
->fields
->pdata
[index
];
372 /* Field has not been instanciated yet, create it */
373 structure_type
= bt_ctf_field_get_type(field
);
374 if (!structure_type
) {
378 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
379 &field_name
, &field_type
, index
);
380 bt_ctf_field_type_put(structure_type
);
385 ret_field
= bt_ctf_field_create(field_type
);
390 structure
->fields
->pdata
[index
] = ret_field
;
392 bt_ctf_field_get(ret_field
);
395 bt_ctf_field_type_put(field_type
);
401 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
402 const char *name
, struct bt_ctf_field
*value
)
406 struct bt_ctf_field_structure
*structure
;
407 struct bt_ctf_field_type
*expected_field_type
= NULL
;
410 if (!field
|| !name
|| !value
||
411 bt_ctf_field_type_get_type_id(field
->type
) !=
417 field_quark
= g_quark_from_string(name
);
418 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
419 expected_field_type
=
420 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
422 if (expected_field_type
!= value
->type
) {
427 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
428 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
432 if (structure
->fields
->pdata
[index
]) {
433 bt_ctf_field_put(structure
->fields
->pdata
[index
]);
436 structure
->fields
->pdata
[index
] = value
;
437 bt_ctf_field_get(value
);
439 if (expected_field_type
) {
440 bt_ctf_field_type_put(expected_field_type
);
445 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
448 struct bt_ctf_field
*new_field
= NULL
;
449 struct bt_ctf_field_type
*field_type
= NULL
;
450 struct bt_ctf_field_array
*array
;
452 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
457 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
458 if (index
>= array
->elements
->len
) {
462 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
463 if (array
->elements
->pdata
[(size_t)index
]) {
464 new_field
= array
->elements
->pdata
[(size_t)index
];
468 new_field
= bt_ctf_field_create(field_type
);
469 bt_ctf_field_get(new_field
);
470 array
->elements
->pdata
[(size_t)index
] = new_field
;
473 bt_ctf_field_type_put(field_type
);
478 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
481 struct bt_ctf_field
*new_field
= NULL
;
482 struct bt_ctf_field_type
*field_type
= NULL
;
483 struct bt_ctf_field_sequence
*sequence
;
485 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
490 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
491 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
495 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
496 if (sequence
->elements
->pdata
[(size_t)index
]) {
497 new_field
= sequence
->elements
->pdata
[(size_t)index
];
501 new_field
= bt_ctf_field_create(field_type
);
502 bt_ctf_field_get(new_field
);
503 sequence
->elements
->pdata
[(size_t)index
] = new_field
;
506 bt_ctf_field_type_put(field_type
);
511 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
512 struct bt_ctf_field
*tag_field
)
514 struct bt_ctf_field
*new_field
= NULL
;
515 struct bt_ctf_field_variant
*variant
;
516 struct bt_ctf_field_type_variant
*variant_type
;
517 struct bt_ctf_field_type
*field_type
;
518 struct bt_ctf_field
*tag_enum
= NULL
;
519 struct bt_ctf_field_integer
*tag_enum_integer
;
520 int64_t tag_enum_value
;
522 if (!field
|| !tag_field
||
523 bt_ctf_field_type_get_type_id(field
->type
) !=
525 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
530 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
531 variant_type
= container_of(field
->type
,
532 struct bt_ctf_field_type_variant
, parent
);
533 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
538 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
541 if (!bt_ctf_field_validate(variant
->tag
)) {
545 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
546 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
547 variant_type
, tag_enum_value
);
552 new_field
= bt_ctf_field_create(field_type
);
557 bt_ctf_field_put(variant
->tag
);
558 bt_ctf_field_put(variant
->payload
);
559 bt_ctf_field_get(new_field
);
560 bt_ctf_field_get(tag_field
);
561 variant
->tag
= tag_field
;
562 variant
->payload
= new_field
;
564 bt_ctf_field_put(tag_enum
);
568 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
569 struct bt_ctf_field
*field
)
571 struct bt_ctf_field
*container
= NULL
;
572 struct bt_ctf_field_enumeration
*enumeration
;
574 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
579 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
581 if (!enumeration
->payload
) {
582 struct bt_ctf_field_type_enumeration
*enumeration_type
=
583 container_of(field
->type
,
584 struct bt_ctf_field_type_enumeration
, parent
);
585 enumeration
->payload
=
586 bt_ctf_field_create(enumeration_type
->container
);
589 container
= enumeration
->payload
;
590 bt_ctf_field_get(container
);
595 const char *bt_ctf_field_enumeration_get_mapping_name(
596 struct bt_ctf_field
*field
)
599 const char *name
= NULL
;
600 struct bt_ctf_field
*container
= NULL
;
601 struct bt_ctf_field_type
*container_type
= NULL
;
602 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
603 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
605 container
= bt_ctf_field_enumeration_get_container(field
);
610 container_type
= bt_ctf_field_get_type(container
);
611 if (!container_type
) {
612 goto error_put_container
;
615 integer_type
= container_of(container_type
,
616 struct bt_ctf_field_type_integer
, parent
);
617 enumeration_type
= container_of(field
->type
,
618 struct bt_ctf_field_type_enumeration
, parent
);
620 if (!integer_type
->declaration
.signedness
) {
622 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
625 goto error_put_container_type
;
628 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
629 enumeration_type
, value
);
632 ret
= bt_ctf_field_signed_integer_get_value(container
,
635 goto error_put_container_type
;
638 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
639 enumeration_type
, value
);
642 error_put_container_type
:
643 bt_ctf_field_type_put(container_type
);
645 bt_ctf_field_put(container
);
650 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
654 struct bt_ctf_field_integer
*integer
;
655 struct bt_ctf_field_type_integer
*integer_type
;
657 if (!field
|| !value
|| !field
->payload_set
||
658 bt_ctf_field_type_get_type_id(field
->type
) !=
664 integer_type
= container_of(field
->type
,
665 struct bt_ctf_field_type_integer
, parent
);
666 if (!integer_type
->declaration
.signedness
) {
671 integer
= container_of(field
,
672 struct bt_ctf_field_integer
, parent
);
673 *value
= integer
->definition
.value
._signed
;
678 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
682 struct bt_ctf_field_integer
*integer
;
683 struct bt_ctf_field_type_integer
*integer_type
;
685 int64_t min_value
, max_value
;
688 bt_ctf_field_type_get_type_id(field
->type
) !=
694 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
695 integer_type
= container_of(field
->type
,
696 struct bt_ctf_field_type_integer
, parent
);
697 if (!integer_type
->declaration
.signedness
) {
702 size
= integer_type
->declaration
.len
;
703 min_value
= -((int64_t)1 << (size
- 1));
704 max_value
= ((int64_t)1 << (size
- 1)) - 1;
705 if (value
< min_value
|| value
> max_value
) {
710 integer
->definition
.value
._signed
= value
;
711 integer
->parent
.payload_set
= 1;
716 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
720 struct bt_ctf_field_integer
*integer
;
721 struct bt_ctf_field_type_integer
*integer_type
;
723 if (!field
|| !value
|| !field
->payload_set
||
724 bt_ctf_field_type_get_type_id(field
->type
) !=
730 integer_type
= container_of(field
->type
,
731 struct bt_ctf_field_type_integer
, parent
);
732 if (integer_type
->declaration
.signedness
) {
737 integer
= container_of(field
,
738 struct bt_ctf_field_integer
, parent
);
739 *value
= integer
->definition
.value
._unsigned
;
744 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
748 struct bt_ctf_field_integer
*integer
;
749 struct bt_ctf_field_type_integer
*integer_type
;
754 bt_ctf_field_type_get_type_id(field
->type
) !=
760 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
761 integer_type
= container_of(field
->type
,
762 struct bt_ctf_field_type_integer
, parent
);
763 if (integer_type
->declaration
.signedness
) {
768 size
= integer_type
->declaration
.len
;
769 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
770 if (value
> max_value
) {
775 integer
->definition
.value
._unsigned
= value
;
776 integer
->parent
.payload_set
= 1;
781 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
785 struct bt_ctf_field_floating_point
*floating_point
;
787 if (!field
|| !value
|| !field
->payload_set
||
788 bt_ctf_field_type_get_type_id(field
->type
) !=
794 floating_point
= container_of(field
,
795 struct bt_ctf_field_floating_point
, parent
);
796 *value
= floating_point
->definition
.value
;
801 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
805 struct bt_ctf_field_floating_point
*floating_point
;
808 bt_ctf_field_type_get_type_id(field
->type
) !=
813 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
815 floating_point
->definition
.value
= value
;
816 floating_point
->parent
.payload_set
= 1;
821 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
823 const char *ret
= NULL
;
824 struct bt_ctf_field_string
*string
;
826 if (!field
|| !field
->payload_set
||
827 bt_ctf_field_type_get_type_id(field
->type
) !=
832 string
= container_of(field
,
833 struct bt_ctf_field_string
, parent
);
834 ret
= string
->payload
->str
;
839 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
843 struct bt_ctf_field_string
*string
;
845 if (!field
|| !value
||
846 bt_ctf_field_type_get_type_id(field
->type
) !=
852 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
853 if (string
->payload
) {
854 g_string_assign(string
->payload
, value
);
856 string
->payload
= g_string_new(value
);
859 string
->parent
.payload_set
= 1;
865 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
868 enum ctf_type_id type_id
;
875 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
876 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
881 ret
= field_validate_funcs
[type_id
](field
);
887 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
888 struct ctf_stream_pos
*pos
)
891 enum ctf_type_id type_id
;
893 if (!field
|| !pos
) {
898 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
899 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
904 ret
= field_serialize_funcs
[type_id
](field
, pos
);
910 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
912 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
913 struct bt_ctf_field_type_integer
, parent
);
914 struct bt_ctf_field_integer
*integer
= g_new0(
915 struct bt_ctf_field_integer
, 1);
918 integer
->definition
.declaration
= &integer_type
->declaration
;
921 return integer
? &integer
->parent
: NULL
;
925 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
926 struct bt_ctf_field_type
*type
)
928 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
929 struct bt_ctf_field_enumeration
, 1);
931 return enumeration
? &enumeration
->parent
: NULL
;
935 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
936 struct bt_ctf_field_type
*type
)
938 struct bt_ctf_field_floating_point
*floating_point
;
939 struct bt_ctf_field_type_floating_point
*floating_point_type
;
941 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
942 if (!floating_point
) {
946 floating_point_type
= container_of(type
,
947 struct bt_ctf_field_type_floating_point
, parent
);
948 floating_point
->definition
.declaration
= container_of(
949 type
->declaration
, struct declaration_float
, p
);
952 floating_point
->definition
.sign
= &floating_point
->sign
;
953 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
954 floating_point
->definition
.sign
->p
.declaration
=
955 &floating_point_type
->sign
.p
;
957 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
958 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
959 floating_point
->definition
.mantissa
->p
.declaration
=
960 &floating_point_type
->mantissa
.p
;
962 floating_point
->definition
.exp
= &floating_point
->exp
;
963 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
964 floating_point
->definition
.exp
->p
.declaration
=
965 &floating_point_type
->exp
.p
;
968 return floating_point
? &floating_point
->parent
: NULL
;
972 struct bt_ctf_field
*bt_ctf_field_structure_create(
973 struct bt_ctf_field_type
*type
)
975 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
976 struct bt_ctf_field_type_structure
, parent
);
977 struct bt_ctf_field_structure
*structure
= g_new0(
978 struct bt_ctf_field_structure
, 1);
979 struct bt_ctf_field
*field
= NULL
;
981 if (!structure
|| !structure_type
->fields
->len
) {
985 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
986 structure
->fields
= g_ptr_array_new_with_free_func(
987 (GDestroyNotify
)bt_ctf_field_put
);
988 g_ptr_array_set_size(structure
->fields
,
989 g_hash_table_size(structure
->field_name_to_index
));
990 field
= &structure
->parent
;
996 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
998 struct bt_ctf_field_variant
*variant
= g_new0(
999 struct bt_ctf_field_variant
, 1);
1000 return variant
? &variant
->parent
: NULL
;
1004 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1006 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1007 struct bt_ctf_field_type_array
*array_type
;
1008 unsigned int array_length
;
1010 if (!array
|| !type
) {
1014 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1015 array_length
= array_type
->length
;
1016 array
->elements
= g_ptr_array_sized_new(array_length
);
1017 if (!array
->elements
) {
1021 g_ptr_array_set_free_func(array
->elements
,
1022 (GDestroyNotify
)bt_ctf_field_put
);
1023 g_ptr_array_set_size(array
->elements
, array_length
);
1024 return &array
->parent
;
1031 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1032 struct bt_ctf_field_type
*type
)
1034 struct bt_ctf_field_sequence
*sequence
= g_new0(
1035 struct bt_ctf_field_sequence
, 1);
1036 return sequence
? &sequence
->parent
: NULL
;
1040 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1042 struct bt_ctf_field_string
*string
= g_new0(
1043 struct bt_ctf_field_string
, 1);
1044 return string
? &string
->parent
: NULL
;
1048 void bt_ctf_field_destroy(struct bt_ctf_ref
*ref
)
1050 struct bt_ctf_field
*field
;
1051 struct bt_ctf_field_type
*type
;
1052 enum ctf_type_id type_id
;
1058 field
= container_of(ref
, struct bt_ctf_field
, ref_count
);
1060 type_id
= bt_ctf_field_type_get_type_id(type
);
1061 if (type_id
<= CTF_TYPE_UNKNOWN
||
1062 type_id
>= NR_CTF_TYPES
) {
1066 field_destroy_funcs
[type_id
](field
);
1068 bt_ctf_field_type_put(type
);
1073 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1075 struct bt_ctf_field_integer
*integer
;
1081 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1086 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1088 struct bt_ctf_field_enumeration
*enumeration
;
1094 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1096 bt_ctf_field_put(enumeration
->payload
);
1097 g_free(enumeration
);
1101 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1103 struct bt_ctf_field_floating_point
*floating_point
;
1109 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1111 g_free(floating_point
);
1115 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1117 struct bt_ctf_field_structure
*structure
;
1123 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1124 g_ptr_array_free(structure
->fields
, TRUE
);
1129 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1131 struct bt_ctf_field_variant
*variant
;
1137 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1138 bt_ctf_field_put(variant
->tag
);
1139 bt_ctf_field_put(variant
->payload
);
1144 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1146 struct bt_ctf_field_array
*array
;
1152 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1153 g_ptr_array_free(array
->elements
, TRUE
);
1158 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1160 struct bt_ctf_field_sequence
*sequence
;
1166 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1167 g_ptr_array_free(sequence
->elements
, TRUE
);
1168 bt_ctf_field_put(sequence
->length
);
1173 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1175 struct bt_ctf_field_string
*string
;
1180 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1181 g_string_free(string
->payload
, TRUE
);
1186 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1188 return (field
&& field
->payload_set
) ? 0 : -1;
1192 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1195 struct bt_ctf_field_enumeration
*enumeration
;
1202 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1204 if (!enumeration
->payload
) {
1209 ret
= bt_ctf_field_validate(enumeration
->payload
);
1215 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1219 struct bt_ctf_field_structure
*structure
;
1226 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1227 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1228 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1238 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1241 struct bt_ctf_field_variant
*variant
;
1248 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1249 ret
= bt_ctf_field_validate(variant
->payload
);
1255 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1259 struct bt_ctf_field_array
*array
;
1266 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1267 for (i
= 0; i
< array
->elements
->len
; i
++) {
1268 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1278 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1282 struct bt_ctf_field_sequence
*sequence
;
1289 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1290 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1291 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1301 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1302 struct ctf_stream_pos
*pos
)
1305 struct bt_ctf_field_integer
*integer
= container_of(field
,
1306 struct bt_ctf_field_integer
, parent
);
1309 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1310 if (ret
== -EFAULT
) {
1312 * The field is too large to fit in the current packet's
1313 * remaining space. Bump the packet size and retry.
1315 ret
= increase_packet_size(pos
);
1326 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1327 struct ctf_stream_pos
*pos
)
1329 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1330 field
, struct bt_ctf_field_enumeration
, parent
);
1332 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1336 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1337 struct ctf_stream_pos
*pos
)
1340 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1341 struct bt_ctf_field_floating_point
, parent
);
1344 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1345 if (ret
== -EFAULT
) {
1347 * The field is too large to fit in the current packet's
1348 * remaining space. Bump the packet size and retry.
1350 ret
= increase_packet_size(pos
);
1361 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1362 struct ctf_stream_pos
*pos
)
1366 struct bt_ctf_field_structure
*structure
= container_of(
1367 field
, struct bt_ctf_field_structure
, parent
);
1369 while (!ctf_pos_access_ok(pos
,
1370 offset_align(pos
->offset
,
1371 field
->type
->declaration
->alignment
))) {
1372 ret
= increase_packet_size(pos
);
1378 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1383 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1384 struct bt_ctf_field
*field
= g_ptr_array_index(
1385 structure
->fields
, i
);
1387 ret
= bt_ctf_field_serialize(field
, pos
);
1397 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1398 struct ctf_stream_pos
*pos
)
1400 struct bt_ctf_field_variant
*variant
= container_of(
1401 field
, struct bt_ctf_field_variant
, parent
);
1403 return bt_ctf_field_serialize(variant
->payload
, pos
);
1407 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1408 struct ctf_stream_pos
*pos
)
1412 struct bt_ctf_field_array
*array
= container_of(
1413 field
, struct bt_ctf_field_array
, parent
);
1415 for (i
= 0; i
< array
->elements
->len
; i
++) {
1416 ret
= bt_ctf_field_serialize(
1417 g_ptr_array_index(array
->elements
, i
), pos
);
1427 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1428 struct ctf_stream_pos
*pos
)
1432 struct bt_ctf_field_sequence
*sequence
= container_of(
1433 field
, struct bt_ctf_field_sequence
, parent
);
1435 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1436 ret
= bt_ctf_field_serialize(
1437 g_ptr_array_index(sequence
->elements
, i
), pos
);
1447 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1448 struct ctf_stream_pos
*pos
)
1452 struct bt_ctf_field_string
*string
= container_of(field
,
1453 struct bt_ctf_field_string
, parent
);
1454 struct bt_ctf_field_type
*character_type
=
1455 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1456 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1458 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1459 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1460 (uint64_t) string
->payload
->str
[i
]);
1465 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1471 bt_ctf_field_put(character
);
1472 bt_ctf_field_type_put(character_type
);
1477 int increase_packet_size(struct ctf_stream_pos
*pos
)
1482 ret
= munmap_align(pos
->base_mma
);
1487 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
1488 ret
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1489 pos
->packet_size
/ CHAR_BIT
);
1494 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1495 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1496 if (pos
->base_mma
== MAP_FAILED
) {