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-writer/event-fields-internal.h>
31 #include <babeltrace/ctf-writer/event-types-internal.h>
32 #include <babeltrace/compiler.h>
33 #include <babeltrace/compat/fcntl.h>
35 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
38 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
40 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
41 struct bt_ctf_field_type
*);
43 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
44 struct bt_ctf_field_type
*);
46 struct bt_ctf_field
*bt_ctf_field_structure_create(
47 struct bt_ctf_field_type
*);
49 struct bt_ctf_field
*bt_ctf_field_variant_create(
50 struct bt_ctf_field_type
*);
52 struct bt_ctf_field
*bt_ctf_field_array_create(
53 struct bt_ctf_field_type
*);
55 struct bt_ctf_field
*bt_ctf_field_sequence_create(
56 struct bt_ctf_field_type
*);
58 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
61 void bt_ctf_field_destroy(struct bt_ctf_ref
*);
63 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*);
65 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*);
67 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*);
69 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*);
71 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*);
73 void bt_ctf_field_array_destroy(struct bt_ctf_field
*);
75 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*);
77 void bt_ctf_field_string_destroy(struct bt_ctf_field
*);
80 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
);
82 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
);
84 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
);
86 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
);
88 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
);
90 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
);
93 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*,
94 struct ctf_stream_pos
*);
96 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
97 struct ctf_stream_pos
*);
99 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
100 struct ctf_stream_pos
*);
102 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
103 struct ctf_stream_pos
*);
105 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
106 struct ctf_stream_pos
*);
108 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
109 struct ctf_stream_pos
*);
111 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
112 struct ctf_stream_pos
*);
114 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
115 struct ctf_stream_pos
*);
118 int increase_packet_size(struct ctf_stream_pos
*pos
);
121 struct bt_ctf_field
*(*field_create_funcs
[])(
122 struct bt_ctf_field_type
*) = {
123 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_create
,
124 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_create
,
126 bt_ctf_field_floating_point_create
,
127 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_create
,
128 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_create
,
129 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_create
,
130 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_create
,
131 [CTF_TYPE_STRING
] = bt_ctf_field_string_create
,
135 void (*field_destroy_funcs
[])(struct bt_ctf_field
*) = {
136 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_destroy
,
137 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_destroy
,
139 bt_ctf_field_floating_point_destroy
,
140 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_destroy
,
141 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_destroy
,
142 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_destroy
,
143 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
144 [CTF_TYPE_STRING
] = bt_ctf_field_string_destroy
,
148 int (*field_validate_funcs
[])(struct bt_ctf_field
*) = {
149 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_validate
,
150 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_validate
,
151 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_validate
,
152 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_validate
,
153 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_validate
,
154 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_validate
,
155 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_validate
,
156 [CTF_TYPE_STRING
] = bt_ctf_field_generic_validate
,
160 int (*field_serialize_funcs
[])(struct bt_ctf_field
*,
161 struct ctf_stream_pos
*) = {
162 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_serialize
,
163 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_serialize
,
165 bt_ctf_field_floating_point_serialize
,
166 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_serialize
,
167 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_serialize
,
168 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_serialize
,
169 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
170 [CTF_TYPE_STRING
] = bt_ctf_field_string_serialize
,
173 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
175 struct bt_ctf_field
*field
= NULL
;
176 enum ctf_type_id type_id
;
182 type_id
= bt_ctf_field_type_get_type_id(type
);
183 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
||
184 bt_ctf_field_type_validate(type
)) {
188 field
= field_create_funcs
[type_id
](type
);
193 /* The type's declaration can't change after this point */
194 bt_ctf_field_type_freeze(type
);
195 bt_ctf_field_type_get(type
);
196 bt_ctf_ref_init(&field
->ref_count
);
202 void bt_ctf_field_get(struct bt_ctf_field
*field
)
205 bt_ctf_ref_get(&field
->ref_count
);
209 void bt_ctf_field_put(struct bt_ctf_field
*field
)
212 bt_ctf_ref_put(&field
->ref_count
, bt_ctf_field_destroy
);
216 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
217 struct bt_ctf_field
*length_field
)
220 struct bt_ctf_field_type_integer
*length_type
;
221 struct bt_ctf_field_integer
*length
;
222 struct bt_ctf_field_sequence
*sequence
;
223 uint64_t sequence_length
;
225 if (!field
|| !length_field
) {
229 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
235 length_type
= container_of(length_field
->type
,
236 struct bt_ctf_field_type_integer
, parent
);
237 if (length_type
->declaration
.signedness
) {
242 length
= container_of(length_field
, struct bt_ctf_field_integer
,
244 sequence_length
= length
->definition
.value
._unsigned
;
245 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
246 if (sequence
->elements
) {
247 g_ptr_array_free(sequence
->elements
, TRUE
);
248 bt_ctf_field_put(sequence
->length
);
251 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
252 if (!sequence
->elements
) {
257 g_ptr_array_set_free_func(sequence
->elements
,
258 (GDestroyNotify
)bt_ctf_field_put
);
259 g_ptr_array_set_size(sequence
->elements
, (size_t)sequence_length
);
260 bt_ctf_field_get(length_field
);
261 sequence
->length
= length_field
;
266 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
267 struct bt_ctf_field
*field
, const char *name
)
269 struct bt_ctf_field
*new_field
= NULL
;
271 struct bt_ctf_field_structure
*structure
;
272 struct bt_ctf_field_type_structure
*structure_type
;
273 struct bt_ctf_field_type
*field_type
;
276 if (!field
|| !name
||
277 bt_ctf_field_type_get_type_id(field
->type
) !=
282 field_quark
= g_quark_from_string(name
);
283 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
284 structure_type
= container_of(field
->type
,
285 struct bt_ctf_field_type_structure
, parent
);
286 field_type
= bt_ctf_field_type_structure_get_type(structure_type
, name
);
287 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
288 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
292 if (structure
->fields
->pdata
[index
]) {
293 new_field
= structure
->fields
->pdata
[index
];
297 new_field
= bt_ctf_field_create(field_type
);
302 structure
->fields
->pdata
[index
] = new_field
;
304 bt_ctf_field_get(new_field
);
310 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
311 const char *name
, struct bt_ctf_field
*value
)
315 struct bt_ctf_field_structure
*structure
;
316 struct bt_ctf_field_type_structure
*structure_type
;
317 struct bt_ctf_field_type
*expected_field_type
;
320 if (!field
|| !name
|| !value
||
321 bt_ctf_field_type_get_type_id(field
->type
) !=
327 field_quark
= g_quark_from_string(name
);
328 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
329 structure_type
= container_of(field
->type
,
330 struct bt_ctf_field_type_structure
, parent
);
331 expected_field_type
= bt_ctf_field_type_structure_get_type(
332 structure_type
, name
);
333 if (expected_field_type
!= value
->type
) {
338 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
339 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
343 if (structure
->fields
->pdata
[index
]) {
344 bt_ctf_field_put(structure
->fields
->pdata
[index
]);
347 structure
->fields
->pdata
[index
] = value
;
348 bt_ctf_field_get(value
);
353 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
356 struct bt_ctf_field
*new_field
= NULL
;
357 struct bt_ctf_field_array
*array
;
358 struct bt_ctf_field_type_array
*array_type
;
359 struct bt_ctf_field_type
*field_type
;
361 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
366 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
367 if (index
>= array
->elements
->len
) {
371 array_type
= container_of(field
->type
, struct bt_ctf_field_type_array
,
373 field_type
= bt_ctf_field_type_array_get_element_type(array_type
);
374 if (array
->elements
->pdata
[(size_t)index
]) {
375 new_field
= array
->elements
->pdata
[(size_t)index
];
379 new_field
= bt_ctf_field_create(field_type
);
380 bt_ctf_field_get(new_field
);
381 array
->elements
->pdata
[(size_t)index
] = new_field
;
386 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
389 struct bt_ctf_field
*new_field
= NULL
;
390 struct bt_ctf_field_sequence
*sequence
;
391 struct bt_ctf_field_type_sequence
*sequence_type
;
392 struct bt_ctf_field_type
*field_type
;
394 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
399 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
400 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
404 sequence_type
= container_of(field
->type
,
405 struct bt_ctf_field_type_sequence
, parent
);
406 field_type
= bt_ctf_field_type_sequence_get_element_type(sequence_type
);
407 if (sequence
->elements
->pdata
[(size_t)index
]) {
408 new_field
= sequence
->elements
->pdata
[(size_t)index
];
412 new_field
= bt_ctf_field_create(field_type
);
413 bt_ctf_field_get(new_field
);
414 sequence
->elements
->pdata
[(size_t)index
] = new_field
;
419 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
420 struct bt_ctf_field
*tag_field
)
422 struct bt_ctf_field
*new_field
= NULL
;
423 struct bt_ctf_field_variant
*variant
;
424 struct bt_ctf_field_type_variant
*variant_type
;
425 struct bt_ctf_field_type
*field_type
;
426 struct bt_ctf_field
*tag_enum
= NULL
;
427 struct bt_ctf_field_integer
*tag_enum_integer
;
428 int64_t tag_enum_value
;
430 if (!field
|| !tag_field
||
431 bt_ctf_field_type_get_type_id(field
->type
) !=
433 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
438 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
439 variant_type
= container_of(field
->type
,
440 struct bt_ctf_field_type_variant
, parent
);
441 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
446 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
449 if (bt_ctf_field_validate(tag_field
) < 0) {
453 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
454 field_type
= bt_ctf_field_type_variant_get_field_type(variant_type
,
460 new_field
= bt_ctf_field_create(field_type
);
465 bt_ctf_field_put(variant
->tag
);
466 bt_ctf_field_put(variant
->payload
);
467 bt_ctf_field_get(new_field
);
468 bt_ctf_field_get(tag_field
);
469 variant
->tag
= tag_field
;
470 variant
->payload
= new_field
;
472 bt_ctf_field_put(tag_enum
);
476 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
477 struct bt_ctf_field
*field
)
479 struct bt_ctf_field
*container
= NULL
;
480 struct bt_ctf_field_enumeration
*enumeration
;
486 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
488 if (!enumeration
->payload
) {
489 struct bt_ctf_field_type_enumeration
*enumeration_type
=
490 container_of(field
->type
,
491 struct bt_ctf_field_type_enumeration
, parent
);
492 enumeration
->payload
=
493 bt_ctf_field_create(enumeration_type
->container
);
496 container
= enumeration
->payload
;
497 bt_ctf_field_get(container
);
502 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
506 struct bt_ctf_field_integer
*integer
;
507 struct bt_ctf_field_type_integer
*integer_type
;
509 int64_t min_value
, max_value
;
512 bt_ctf_field_type_get_type_id(field
->type
) !=
518 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
519 integer_type
= container_of(field
->type
,
520 struct bt_ctf_field_type_integer
, parent
);
521 if (!integer_type
->declaration
.signedness
) {
526 size
= integer_type
->declaration
.len
;
527 min_value
= -((int64_t)1 << (size
- 1));
528 max_value
= ((int64_t)1 << (size
- 1)) - 1;
529 if (value
< min_value
|| value
> max_value
) {
534 integer
->definition
.value
._signed
= value
;
535 integer
->parent
.payload_set
= 1;
540 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
544 struct bt_ctf_field_integer
*integer
;
545 struct bt_ctf_field_type_integer
*integer_type
;
550 bt_ctf_field_type_get_type_id(field
->type
) !=
556 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
557 integer_type
= container_of(field
->type
,
558 struct bt_ctf_field_type_integer
, parent
);
559 if (integer_type
->declaration
.signedness
) {
564 size
= integer_type
->declaration
.len
;
565 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
566 if (value
> max_value
) {
571 integer
->definition
.value
._unsigned
= value
;
572 integer
->parent
.payload_set
= 1;
577 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
581 struct bt_ctf_field_floating_point
*floating_point
;
584 bt_ctf_field_type_get_type_id(field
->type
) !=
589 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
591 floating_point
->definition
.value
= value
;
592 floating_point
->parent
.payload_set
= 1;
597 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
601 struct bt_ctf_field_string
*string
;
603 if (!field
|| !value
||
604 bt_ctf_field_type_get_type_id(field
->type
) !=
610 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
611 if (string
->payload
) {
612 g_string_free(string
->payload
, TRUE
);
615 string
->payload
= g_string_new(value
);
616 string
->parent
.payload_set
= 1;
622 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
625 enum ctf_type_id type_id
;
632 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
633 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
638 ret
= field_validate_funcs
[type_id
](field
);
644 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
645 struct ctf_stream_pos
*pos
)
648 enum ctf_type_id type_id
;
650 if (!field
|| !pos
) {
655 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
656 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
661 ret
= field_serialize_funcs
[type_id
](field
, pos
);
667 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
669 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
670 struct bt_ctf_field_type_integer
, parent
);
671 struct bt_ctf_field_integer
*integer
= g_new0(
672 struct bt_ctf_field_integer
, 1);
675 integer
->definition
.declaration
= &integer_type
->declaration
;
678 return integer
? &integer
->parent
: NULL
;
682 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
683 struct bt_ctf_field_type
*type
)
685 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
686 struct bt_ctf_field_enumeration
, 1);
688 return enumeration
? &enumeration
->parent
: NULL
;
692 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
693 struct bt_ctf_field_type
*type
)
695 struct bt_ctf_field_floating_point
*floating_point
;
696 struct bt_ctf_field_type_floating_point
*floating_point_type
;
698 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
699 if (!floating_point
) {
703 floating_point_type
= container_of(type
,
704 struct bt_ctf_field_type_floating_point
, parent
);
705 floating_point
->definition
.declaration
= container_of(
706 type
->declaration
, struct declaration_float
, p
);
709 floating_point
->definition
.sign
= &floating_point
->sign
;
710 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
711 floating_point
->definition
.sign
->p
.declaration
=
712 &floating_point_type
->sign
.p
;
714 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
715 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
716 floating_point
->definition
.mantissa
->p
.declaration
=
717 &floating_point_type
->mantissa
.p
;
719 floating_point
->definition
.exp
= &floating_point
->exp
;
720 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
721 floating_point
->definition
.exp
->p
.declaration
=
722 &floating_point_type
->exp
.p
;
725 return floating_point
? &floating_point
->parent
: NULL
;
729 struct bt_ctf_field
*bt_ctf_field_structure_create(
730 struct bt_ctf_field_type
*type
)
732 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
733 struct bt_ctf_field_type_structure
, parent
);
734 struct bt_ctf_field_structure
*structure
= g_new0(
735 struct bt_ctf_field_structure
, 1);
736 struct bt_ctf_field
*field
= NULL
;
738 if (!structure
|| !structure_type
->fields
->len
) {
742 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
743 structure
->fields
= g_ptr_array_new_with_free_func(
744 (GDestroyNotify
)bt_ctf_field_put
);
745 g_ptr_array_set_size(structure
->fields
,
746 g_hash_table_size(structure
->field_name_to_index
));
747 field
= &structure
->parent
;
753 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
755 struct bt_ctf_field_variant
*variant
= g_new0(
756 struct bt_ctf_field_variant
, 1);
757 return variant
? &variant
->parent
: NULL
;
761 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
763 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
764 struct bt_ctf_field_type_array
*array_type
;
765 unsigned int array_length
;
767 if (!array
|| !type
) {
771 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
772 array_length
= array_type
->length
;
773 array
->elements
= g_ptr_array_sized_new(array_length
);
774 if (!array
->elements
) {
778 g_ptr_array_set_free_func(array
->elements
,
779 (GDestroyNotify
)bt_ctf_field_put
);
780 g_ptr_array_set_size(array
->elements
, array_length
);
781 return &array
->parent
;
788 struct bt_ctf_field
*bt_ctf_field_sequence_create(
789 struct bt_ctf_field_type
*type
)
791 struct bt_ctf_field_sequence
*sequence
= g_new0(
792 struct bt_ctf_field_sequence
, 1);
793 return sequence
? &sequence
->parent
: NULL
;
797 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
799 struct bt_ctf_field_string
*string
= g_new0(
800 struct bt_ctf_field_string
, 1);
801 return string
? &string
->parent
: NULL
;
805 void bt_ctf_field_destroy(struct bt_ctf_ref
*ref
)
807 struct bt_ctf_field
*field
;
808 struct bt_ctf_field_type
*type
;
809 enum ctf_type_id type_id
;
815 field
= container_of(ref
, struct bt_ctf_field
, ref_count
);
817 type_id
= bt_ctf_field_type_get_type_id(type
);
818 if (type_id
<= CTF_TYPE_UNKNOWN
||
819 type_id
>= NR_CTF_TYPES
) {
823 field_destroy_funcs
[type_id
](field
);
825 bt_ctf_field_type_put(type
);
830 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
832 struct bt_ctf_field_integer
*integer
;
838 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
843 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
845 struct bt_ctf_field_enumeration
*enumeration
;
851 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
853 bt_ctf_field_put(enumeration
->payload
);
858 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
860 struct bt_ctf_field_floating_point
*floating_point
;
866 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
868 g_free(floating_point
);
872 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
874 struct bt_ctf_field_structure
*structure
;
880 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
881 g_ptr_array_free(structure
->fields
, TRUE
);
886 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
888 struct bt_ctf_field_variant
*variant
;
894 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
895 bt_ctf_field_put(variant
->tag
);
896 bt_ctf_field_put(variant
->payload
);
901 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
903 struct bt_ctf_field_array
*array
;
909 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
910 g_ptr_array_free(array
->elements
, TRUE
);
915 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
917 struct bt_ctf_field_sequence
*sequence
;
923 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
924 g_ptr_array_free(sequence
->elements
, TRUE
);
925 bt_ctf_field_put(sequence
->length
);
930 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
932 struct bt_ctf_field_string
*string
;
937 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
938 g_string_free(string
->payload
, TRUE
);
943 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
945 return (field
&& field
->payload_set
) ? 0 : -1;
949 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
952 struct bt_ctf_field_enumeration
*enumeration
;
959 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
961 if (!enumeration
->payload
) {
966 ret
= bt_ctf_field_validate(enumeration
->payload
);
972 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
976 struct bt_ctf_field_structure
*structure
;
983 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
984 for (i
= 0; i
< structure
->fields
->len
; i
++) {
985 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
995 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
998 struct bt_ctf_field_variant
*variant
;
1005 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1006 ret
= bt_ctf_field_validate(variant
->payload
);
1012 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1016 struct bt_ctf_field_array
*array
;
1023 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1024 for (i
= 0; i
< array
->elements
->len
; i
++) {
1025 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1035 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1039 struct bt_ctf_field_sequence
*sequence
;
1046 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1047 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1048 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1058 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1059 struct ctf_stream_pos
*pos
)
1062 struct bt_ctf_field_integer
*integer
= container_of(field
,
1063 struct bt_ctf_field_integer
, parent
);
1066 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1067 if (ret
== -EFAULT
) {
1069 * The field is too large to fit in the current packet's
1070 * remaining space. Bump the packet size and retry.
1072 ret
= increase_packet_size(pos
);
1083 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1084 struct ctf_stream_pos
*pos
)
1086 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1087 field
, struct bt_ctf_field_enumeration
, parent
);
1089 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1093 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1094 struct ctf_stream_pos
*pos
)
1097 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1098 struct bt_ctf_field_floating_point
, parent
);
1101 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1102 if (ret
== -EFAULT
) {
1104 * The field is too large to fit in the current packet's
1105 * remaining space. Bump the packet size and retry.
1107 ret
= increase_packet_size(pos
);
1118 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1119 struct ctf_stream_pos
*pos
)
1123 struct bt_ctf_field_structure
*structure
= container_of(
1124 field
, struct bt_ctf_field_structure
, parent
);
1126 while (!ctf_pos_access_ok(pos
,
1127 offset_align(pos
->offset
,
1128 field
->type
->declaration
->alignment
))) {
1129 ret
= increase_packet_size(pos
);
1135 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1140 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1141 struct bt_ctf_field
*field
= g_ptr_array_index(
1142 structure
->fields
, i
);
1144 ret
= bt_ctf_field_serialize(field
, pos
);
1154 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1155 struct ctf_stream_pos
*pos
)
1157 struct bt_ctf_field_variant
*variant
= container_of(
1158 field
, struct bt_ctf_field_variant
, parent
);
1160 return bt_ctf_field_serialize(variant
->payload
, pos
);
1164 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1165 struct ctf_stream_pos
*pos
)
1169 struct bt_ctf_field_array
*array
= container_of(
1170 field
, struct bt_ctf_field_array
, parent
);
1172 for (i
= 0; i
< array
->elements
->len
; i
++) {
1173 ret
= bt_ctf_field_serialize(
1174 g_ptr_array_index(array
->elements
, i
), pos
);
1184 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1185 struct ctf_stream_pos
*pos
)
1189 struct bt_ctf_field_sequence
*sequence
= container_of(
1190 field
, struct bt_ctf_field_sequence
, parent
);
1192 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1193 ret
= bt_ctf_field_serialize(
1194 g_ptr_array_index(sequence
->elements
, i
), pos
);
1204 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1205 struct ctf_stream_pos
*pos
)
1209 struct bt_ctf_field_string
*string
= container_of(field
,
1210 struct bt_ctf_field_string
, parent
);
1211 struct bt_ctf_field_type
*character_type
=
1212 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1213 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1215 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1216 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1217 (uint64_t) string
->payload
->str
[i
]);
1222 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1228 bt_ctf_field_put(character
);
1229 bt_ctf_field_type_put(character_type
);
1234 int increase_packet_size(struct ctf_stream_pos
*pos
)
1239 ret
= munmap_align(pos
->base_mma
);
1244 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
1246 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1247 pos
->packet_size
/ CHAR_BIT
);
1248 } while (ret
== EINTR
);
1255 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1256 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1257 if (pos
->base_mma
== MAP_FAILED
) {