4 * Babeltrace CTF Writer
6 * Copyright 2013 EfficiOS Inc.
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 if (length_type
->declaration
.signedness
) {
277 length
= container_of(length_field
, struct bt_ctf_field_integer
,
279 sequence_length
= length
->definition
.value
._unsigned
;
280 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
281 if (sequence
->elements
) {
282 g_ptr_array_free(sequence
->elements
, TRUE
);
283 bt_ctf_field_put(sequence
->length
);
286 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
287 if (!sequence
->elements
) {
292 g_ptr_array_set_free_func(sequence
->elements
,
293 (GDestroyNotify
)bt_ctf_field_put
);
294 g_ptr_array_set_size(sequence
->elements
, (size_t)sequence_length
);
295 bt_ctf_field_get(length_field
);
296 sequence
->length
= length_field
;
301 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
302 struct bt_ctf_field
*field
, const char *name
)
304 struct bt_ctf_field
*new_field
= NULL
;
306 struct bt_ctf_field_structure
*structure
;
307 struct bt_ctf_field_type_structure
*structure_type
;
308 struct bt_ctf_field_type
*field_type
;
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
);
319 structure_type
= container_of(field
->type
,
320 struct bt_ctf_field_type_structure
, parent
);
321 field_type
= bt_ctf_field_type_structure_get_type(structure_type
, name
);
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
);
344 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
345 struct bt_ctf_field
*field
, size_t index
)
348 const char *field_name
;
349 struct bt_ctf_field_structure
*structure
;
350 struct bt_ctf_field_type
*structure_type
;
351 struct bt_ctf_field_type
*field_type
= NULL
;
352 struct bt_ctf_field
*ret_field
= NULL
;
355 bt_ctf_field_type_get_type_id(field
->type
) != CTF_TYPE_STRUCT
) {
359 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
360 if (index
>= structure
->fields
->len
) {
364 ret_field
= structure
->fields
->pdata
[index
];
369 /* Field has not been instanciated yet, create it */
370 structure_type
= bt_ctf_field_get_type(field
);
371 if (!structure_type
) {
375 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
376 &field_name
, &field_type
, index
);
377 bt_ctf_field_type_put(structure_type
);
382 ret_field
= bt_ctf_field_create(field_type
);
387 structure
->fields
->pdata
[index
] = ret_field
;
389 bt_ctf_field_get(ret_field
);
392 bt_ctf_field_type_put(field_type
);
398 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
399 const char *name
, struct bt_ctf_field
*value
)
403 struct bt_ctf_field_structure
*structure
;
404 struct bt_ctf_field_type_structure
*structure_type
;
405 struct bt_ctf_field_type
*expected_field_type
;
408 if (!field
|| !name
|| !value
||
409 bt_ctf_field_type_get_type_id(field
->type
) !=
415 field_quark
= g_quark_from_string(name
);
416 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
417 structure_type
= container_of(field
->type
,
418 struct bt_ctf_field_type_structure
, parent
);
419 expected_field_type
= bt_ctf_field_type_structure_get_type(
420 structure_type
, name
);
421 if (expected_field_type
!= value
->type
) {
426 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
427 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
431 if (structure
->fields
->pdata
[index
]) {
432 bt_ctf_field_put(structure
->fields
->pdata
[index
]);
435 structure
->fields
->pdata
[index
] = value
;
436 bt_ctf_field_get(value
);
441 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
444 struct bt_ctf_field
*new_field
= NULL
;
445 struct bt_ctf_field_array
*array
;
446 struct bt_ctf_field_type_array
*array_type
;
447 struct bt_ctf_field_type
*field_type
;
449 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
454 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
455 if (index
>= array
->elements
->len
) {
459 array_type
= container_of(field
->type
, struct bt_ctf_field_type_array
,
461 field_type
= bt_ctf_field_type_array_get_element_type(array_type
);
462 if (array
->elements
->pdata
[(size_t)index
]) {
463 new_field
= array
->elements
->pdata
[(size_t)index
];
467 new_field
= bt_ctf_field_create(field_type
);
468 bt_ctf_field_get(new_field
);
469 array
->elements
->pdata
[(size_t)index
] = new_field
;
474 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
477 struct bt_ctf_field
*new_field
= NULL
;
478 struct bt_ctf_field_sequence
*sequence
;
479 struct bt_ctf_field_type_sequence
*sequence_type
;
480 struct bt_ctf_field_type
*field_type
;
482 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
487 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
488 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
492 sequence_type
= container_of(field
->type
,
493 struct bt_ctf_field_type_sequence
, parent
);
494 field_type
= bt_ctf_field_type_sequence_get_element_type(sequence_type
);
495 if (sequence
->elements
->pdata
[(size_t)index
]) {
496 new_field
= sequence
->elements
->pdata
[(size_t)index
];
500 new_field
= bt_ctf_field_create(field_type
);
501 bt_ctf_field_get(new_field
);
502 sequence
->elements
->pdata
[(size_t)index
] = new_field
;
507 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
508 struct bt_ctf_field
*tag_field
)
510 struct bt_ctf_field
*new_field
= NULL
;
511 struct bt_ctf_field_variant
*variant
;
512 struct bt_ctf_field_type_variant
*variant_type
;
513 struct bt_ctf_field_type
*field_type
;
514 struct bt_ctf_field
*tag_enum
= NULL
;
515 struct bt_ctf_field_integer
*tag_enum_integer
;
516 int64_t tag_enum_value
;
518 if (!field
|| !tag_field
||
519 bt_ctf_field_type_get_type_id(field
->type
) !=
521 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
526 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
527 variant_type
= container_of(field
->type
,
528 struct bt_ctf_field_type_variant
, parent
);
529 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
534 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
537 if (!bt_ctf_field_validate(variant
->tag
)) {
541 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
542 field_type
= bt_ctf_field_type_variant_get_field_type(variant_type
,
548 new_field
= bt_ctf_field_create(field_type
);
553 bt_ctf_field_put(variant
->tag
);
554 bt_ctf_field_put(variant
->payload
);
555 bt_ctf_field_get(new_field
);
556 bt_ctf_field_get(tag_field
);
557 variant
->tag
= tag_field
;
558 variant
->payload
= new_field
;
560 bt_ctf_field_put(tag_enum
);
564 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
565 struct bt_ctf_field
*field
)
567 struct bt_ctf_field
*container
= NULL
;
568 struct bt_ctf_field_enumeration
*enumeration
;
574 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
576 if (!enumeration
->payload
) {
577 struct bt_ctf_field_type_enumeration
*enumeration_type
=
578 container_of(field
->type
,
579 struct bt_ctf_field_type_enumeration
, parent
);
580 enumeration
->payload
=
581 bt_ctf_field_create(enumeration_type
->container
);
584 container
= enumeration
->payload
;
585 bt_ctf_field_get(container
);
590 const char *bt_ctf_field_enumeration_get_mapping_name(
591 struct bt_ctf_field
*field
)
594 const char *name
= NULL
;
595 struct bt_ctf_field
*container
= NULL
;
596 struct bt_ctf_field_type
*container_type
= NULL
;
597 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
598 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
600 container
= bt_ctf_field_enumeration_get_container(field
);
605 container_type
= bt_ctf_field_get_type(container
);
606 if (!container_type
) {
607 goto error_put_container
;
610 integer_type
= container_of(container_type
,
611 struct bt_ctf_field_type_integer
, parent
);
612 enumeration_type
= container_of(field
->type
,
613 struct bt_ctf_field_type_enumeration
, parent
);
615 if (integer_type
->declaration
.signedness
) {
617 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
620 goto error_put_container_type
;
623 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
624 enumeration_type
, value
);
627 ret
= bt_ctf_field_signed_integer_get_value(container
,
630 goto error_put_container_type
;
633 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
634 enumeration_type
, value
);
637 error_put_container_type
:
638 bt_ctf_field_type_put(container_type
);
640 bt_ctf_field_put(container
);
645 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
649 struct bt_ctf_field_integer
*integer
;
650 struct bt_ctf_field_type_integer
*integer_type
;
652 if (!field
|| !value
|| !field
->payload_set
||
653 bt_ctf_field_type_get_type_id(field
->type
) !=
659 integer_type
= container_of(field
->type
,
660 struct bt_ctf_field_type_integer
, parent
);
661 if (!integer_type
->declaration
.signedness
) {
666 integer
= container_of(field
,
667 struct bt_ctf_field_integer
, parent
);
668 *value
= integer
->definition
.value
._signed
;
673 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
677 struct bt_ctf_field_integer
*integer
;
678 struct bt_ctf_field_type_integer
*integer_type
;
680 int64_t min_value
, max_value
;
683 bt_ctf_field_type_get_type_id(field
->type
) !=
689 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
690 integer_type
= container_of(field
->type
,
691 struct bt_ctf_field_type_integer
, parent
);
692 if (!integer_type
->declaration
.signedness
) {
697 size
= integer_type
->declaration
.len
;
698 min_value
= -((int64_t)1 << (size
- 1));
699 max_value
= ((int64_t)1 << (size
- 1)) - 1;
700 if (value
< min_value
|| value
> max_value
) {
705 integer
->definition
.value
._signed
= value
;
706 integer
->parent
.payload_set
= 1;
711 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
715 struct bt_ctf_field_integer
*integer
;
716 struct bt_ctf_field_type_integer
*integer_type
;
718 if (!field
|| !value
|| !field
->payload_set
||
719 bt_ctf_field_type_get_type_id(field
->type
) !=
725 integer_type
= container_of(field
->type
,
726 struct bt_ctf_field_type_integer
, parent
);
727 if (integer_type
->declaration
.signedness
) {
732 integer
= container_of(field
,
733 struct bt_ctf_field_integer
, parent
);
734 *value
= integer
->definition
.value
._unsigned
;
739 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
743 struct bt_ctf_field_integer
*integer
;
744 struct bt_ctf_field_type_integer
*integer_type
;
749 bt_ctf_field_type_get_type_id(field
->type
) !=
755 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
756 integer_type
= container_of(field
->type
,
757 struct bt_ctf_field_type_integer
, parent
);
758 if (integer_type
->declaration
.signedness
) {
763 size
= integer_type
->declaration
.len
;
764 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
765 if (value
> max_value
) {
770 integer
->definition
.value
._unsigned
= value
;
771 integer
->parent
.payload_set
= 1;
776 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
780 struct bt_ctf_field_floating_point
*floating_point
;
782 if (!field
|| !value
|| !field
->payload_set
||
783 bt_ctf_field_type_get_type_id(field
->type
) !=
789 floating_point
= container_of(field
,
790 struct bt_ctf_field_floating_point
, parent
);
791 *value
= floating_point
->definition
.value
;
796 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
800 struct bt_ctf_field_floating_point
*floating_point
;
803 bt_ctf_field_type_get_type_id(field
->type
) !=
808 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
810 floating_point
->definition
.value
= value
;
811 floating_point
->parent
.payload_set
= 1;
816 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
818 const char *ret
= NULL
;
819 struct bt_ctf_field_string
*string
;
821 if (!field
|| !field
->payload_set
||
822 bt_ctf_field_type_get_type_id(field
->type
) !=
827 string
= container_of(field
,
828 struct bt_ctf_field_string
, parent
);
829 ret
= string
->payload
->str
;
834 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
838 struct bt_ctf_field_string
*string
;
840 if (!field
|| !value
||
841 bt_ctf_field_type_get_type_id(field
->type
) !=
847 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
848 if (string
->payload
) {
849 g_string_free(string
->payload
, TRUE
);
852 string
->payload
= g_string_new(value
);
853 string
->parent
.payload_set
= 1;
859 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
862 enum ctf_type_id type_id
;
869 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
870 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
875 ret
= field_validate_funcs
[type_id
](field
);
881 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
882 struct ctf_stream_pos
*pos
)
885 enum ctf_type_id type_id
;
887 if (!field
|| !pos
) {
892 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
893 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
898 ret
= field_serialize_funcs
[type_id
](field
, pos
);
904 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
906 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
907 struct bt_ctf_field_type_integer
, parent
);
908 struct bt_ctf_field_integer
*integer
= g_new0(
909 struct bt_ctf_field_integer
, 1);
912 integer
->definition
.declaration
= &integer_type
->declaration
;
915 return integer
? &integer
->parent
: NULL
;
919 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
920 struct bt_ctf_field_type
*type
)
922 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
923 struct bt_ctf_field_enumeration
, 1);
925 return enumeration
? &enumeration
->parent
: NULL
;
929 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
930 struct bt_ctf_field_type
*type
)
932 struct bt_ctf_field_floating_point
*floating_point
;
933 struct bt_ctf_field_type_floating_point
*floating_point_type
;
935 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
936 if (!floating_point
) {
940 floating_point_type
= container_of(type
,
941 struct bt_ctf_field_type_floating_point
, parent
);
942 floating_point
->definition
.declaration
= container_of(
943 type
->declaration
, struct declaration_float
, p
);
946 floating_point
->definition
.sign
= &floating_point
->sign
;
947 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
948 floating_point
->definition
.sign
->p
.declaration
=
949 &floating_point_type
->sign
.p
;
951 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
952 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
953 floating_point
->definition
.mantissa
->p
.declaration
=
954 &floating_point_type
->mantissa
.p
;
956 floating_point
->definition
.exp
= &floating_point
->exp
;
957 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
958 floating_point
->definition
.exp
->p
.declaration
=
959 &floating_point_type
->exp
.p
;
962 return floating_point
? &floating_point
->parent
: NULL
;
966 struct bt_ctf_field
*bt_ctf_field_structure_create(
967 struct bt_ctf_field_type
*type
)
969 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
970 struct bt_ctf_field_type_structure
, parent
);
971 struct bt_ctf_field_structure
*structure
= g_new0(
972 struct bt_ctf_field_structure
, 1);
973 struct bt_ctf_field
*field
= NULL
;
975 if (!structure
|| !structure_type
->fields
->len
) {
979 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
980 structure
->fields
= g_ptr_array_new_with_free_func(
981 (GDestroyNotify
)bt_ctf_field_put
);
982 g_ptr_array_set_size(structure
->fields
,
983 g_hash_table_size(structure
->field_name_to_index
));
984 field
= &structure
->parent
;
990 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
992 struct bt_ctf_field_variant
*variant
= g_new0(
993 struct bt_ctf_field_variant
, 1);
994 return variant
? &variant
->parent
: NULL
;
998 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1000 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1001 struct bt_ctf_field_type_array
*array_type
;
1002 unsigned int array_length
;
1004 if (!array
|| !type
) {
1008 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1009 array_length
= array_type
->length
;
1010 array
->elements
= g_ptr_array_sized_new(array_length
);
1011 if (!array
->elements
) {
1015 g_ptr_array_set_free_func(array
->elements
,
1016 (GDestroyNotify
)bt_ctf_field_put
);
1017 g_ptr_array_set_size(array
->elements
, array_length
);
1018 return &array
->parent
;
1025 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1026 struct bt_ctf_field_type
*type
)
1028 struct bt_ctf_field_sequence
*sequence
= g_new0(
1029 struct bt_ctf_field_sequence
, 1);
1030 return sequence
? &sequence
->parent
: NULL
;
1034 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1036 struct bt_ctf_field_string
*string
= g_new0(
1037 struct bt_ctf_field_string
, 1);
1038 return string
? &string
->parent
: NULL
;
1042 void bt_ctf_field_destroy(struct bt_ctf_ref
*ref
)
1044 struct bt_ctf_field
*field
;
1045 struct bt_ctf_field_type
*type
;
1046 enum ctf_type_id type_id
;
1052 field
= container_of(ref
, struct bt_ctf_field
, ref_count
);
1054 type_id
= bt_ctf_field_type_get_type_id(type
);
1055 if (type_id
<= CTF_TYPE_UNKNOWN
||
1056 type_id
>= NR_CTF_TYPES
) {
1060 field_destroy_funcs
[type_id
](field
);
1062 bt_ctf_field_type_put(type
);
1067 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1069 struct bt_ctf_field_integer
*integer
;
1075 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1080 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1082 struct bt_ctf_field_enumeration
*enumeration
;
1088 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1090 bt_ctf_field_put(enumeration
->payload
);
1091 g_free(enumeration
);
1095 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1097 struct bt_ctf_field_floating_point
*floating_point
;
1103 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1105 g_free(floating_point
);
1109 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1111 struct bt_ctf_field_structure
*structure
;
1117 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1118 g_ptr_array_free(structure
->fields
, TRUE
);
1123 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1125 struct bt_ctf_field_variant
*variant
;
1131 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1132 bt_ctf_field_put(variant
->tag
);
1133 bt_ctf_field_put(variant
->payload
);
1138 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1140 struct bt_ctf_field_array
*array
;
1146 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1147 g_ptr_array_free(array
->elements
, TRUE
);
1152 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1154 struct bt_ctf_field_sequence
*sequence
;
1160 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1161 g_ptr_array_free(sequence
->elements
, TRUE
);
1162 bt_ctf_field_put(sequence
->length
);
1167 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1169 struct bt_ctf_field_string
*string
;
1174 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1175 g_string_free(string
->payload
, TRUE
);
1180 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1182 return (field
&& field
->payload_set
) ? 0 : -1;
1186 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1189 struct bt_ctf_field_enumeration
*enumeration
;
1196 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1198 if (!enumeration
->payload
) {
1203 ret
= bt_ctf_field_validate(enumeration
->payload
);
1209 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1213 struct bt_ctf_field_structure
*structure
;
1220 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1221 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1222 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1232 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1235 struct bt_ctf_field_variant
*variant
;
1242 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1243 ret
= bt_ctf_field_validate(variant
->payload
);
1249 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1253 struct bt_ctf_field_array
*array
;
1260 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1261 for (i
= 0; i
< array
->elements
->len
; i
++) {
1262 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1272 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1276 struct bt_ctf_field_sequence
*sequence
;
1283 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1284 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1285 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1295 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1296 struct ctf_stream_pos
*pos
)
1299 struct bt_ctf_field_integer
*integer
= container_of(field
,
1300 struct bt_ctf_field_integer
, parent
);
1303 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1304 if (ret
== -EFAULT
) {
1306 * The field is too large to fit in the current packet's
1307 * remaining space. Bump the packet size and retry.
1309 ret
= increase_packet_size(pos
);
1320 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1321 struct ctf_stream_pos
*pos
)
1323 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1324 field
, struct bt_ctf_field_enumeration
, parent
);
1326 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1330 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1331 struct ctf_stream_pos
*pos
)
1334 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1335 struct bt_ctf_field_floating_point
, parent
);
1338 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1339 if (ret
== -EFAULT
) {
1341 * The field is too large to fit in the current packet's
1342 * remaining space. Bump the packet size and retry.
1344 ret
= increase_packet_size(pos
);
1355 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1356 struct ctf_stream_pos
*pos
)
1360 struct bt_ctf_field_structure
*structure
= container_of(
1361 field
, struct bt_ctf_field_structure
, parent
);
1363 while (!ctf_pos_access_ok(pos
,
1364 offset_align(pos
->offset
,
1365 field
->type
->declaration
->alignment
))) {
1366 ret
= increase_packet_size(pos
);
1372 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1377 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1378 struct bt_ctf_field
*field
= g_ptr_array_index(
1379 structure
->fields
, i
);
1381 ret
= bt_ctf_field_serialize(field
, pos
);
1391 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1392 struct ctf_stream_pos
*pos
)
1394 struct bt_ctf_field_variant
*variant
= container_of(
1395 field
, struct bt_ctf_field_variant
, parent
);
1397 return bt_ctf_field_serialize(variant
->payload
, pos
);
1401 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1402 struct ctf_stream_pos
*pos
)
1406 struct bt_ctf_field_array
*array
= container_of(
1407 field
, struct bt_ctf_field_array
, parent
);
1409 for (i
= 0; i
< array
->elements
->len
; i
++) {
1410 ret
= bt_ctf_field_serialize(
1411 g_ptr_array_index(array
->elements
, i
), pos
);
1421 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1422 struct ctf_stream_pos
*pos
)
1426 struct bt_ctf_field_sequence
*sequence
= container_of(
1427 field
, struct bt_ctf_field_sequence
, parent
);
1429 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1430 ret
= bt_ctf_field_serialize(
1431 g_ptr_array_index(sequence
->elements
, i
), pos
);
1441 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1442 struct ctf_stream_pos
*pos
)
1446 struct bt_ctf_field_string
*string
= container_of(field
,
1447 struct bt_ctf_field_string
, parent
);
1448 struct bt_ctf_field_type
*character_type
=
1449 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1450 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1452 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1453 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1454 (uint64_t) string
->payload
->str
[i
]);
1459 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1465 bt_ctf_field_put(character
);
1466 bt_ctf_field_type_put(character_type
);
1471 int increase_packet_size(struct ctf_stream_pos
*pos
)
1476 ret
= munmap_align(pos
->base_mma
);
1481 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
1482 ret
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1483 pos
->packet_size
/ CHAR_BIT
);
1488 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1489 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1490 if (pos
->base_mma
== MAP_FAILED
) {