4 * Babeltrace CTF IR - Event Fields
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-writer/event-fields.h>
30 #include <babeltrace/ctf-ir/event-fields-internal.h>
31 #include <babeltrace/ctf-ir/event-types-internal.h>
32 #include <babeltrace/object-internal.h>
33 #include <babeltrace/ref.h>
34 #include <babeltrace/compiler.h>
35 #include <babeltrace/compat/fcntl.h>
37 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
40 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
42 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
43 struct bt_ctf_field_type
*);
45 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
46 struct bt_ctf_field_type
*);
48 struct bt_ctf_field
*bt_ctf_field_structure_create(
49 struct bt_ctf_field_type
*);
51 struct bt_ctf_field
*bt_ctf_field_variant_create(
52 struct bt_ctf_field_type
*);
54 struct bt_ctf_field
*bt_ctf_field_array_create(
55 struct bt_ctf_field_type
*);
57 struct bt_ctf_field
*bt_ctf_field_sequence_create(
58 struct bt_ctf_field_type
*);
60 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
63 void bt_ctf_field_destroy(struct bt_object
*);
65 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*);
67 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*);
69 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*);
71 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*);
73 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*);
75 void bt_ctf_field_array_destroy(struct bt_ctf_field
*);
77 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*);
79 void bt_ctf_field_string_destroy(struct bt_ctf_field
*);
82 int bt_ctf_field_generic_validate(struct bt_ctf_field
*);
84 int bt_ctf_field_structure_validate(struct bt_ctf_field
*);
86 int bt_ctf_field_variant_validate(struct bt_ctf_field
*);
88 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*);
90 int bt_ctf_field_array_validate(struct bt_ctf_field
*);
92 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*);
95 int bt_ctf_field_generic_reset(struct bt_ctf_field
*);
97 int bt_ctf_field_structure_reset(struct bt_ctf_field
*);
99 int bt_ctf_field_variant_reset(struct bt_ctf_field
*);
101 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*);
103 int bt_ctf_field_array_reset(struct bt_ctf_field
*);
105 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*);
107 int bt_ctf_field_string_reset(struct bt_ctf_field
*);
110 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*,
111 struct ctf_stream_pos
*);
113 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
114 struct ctf_stream_pos
*);
116 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
117 struct ctf_stream_pos
*);
119 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
120 struct ctf_stream_pos
*);
122 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
123 struct ctf_stream_pos
*);
125 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
126 struct ctf_stream_pos
*);
128 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
129 struct ctf_stream_pos
*);
131 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
132 struct ctf_stream_pos
*);
135 int bt_ctf_field_integer_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
137 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
139 int bt_ctf_field_floating_point_copy(struct bt_ctf_field
*,
140 struct bt_ctf_field
*);
142 int bt_ctf_field_structure_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
144 int bt_ctf_field_variant_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
146 int bt_ctf_field_array_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
148 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
150 int bt_ctf_field_string_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
153 int increase_packet_size(struct ctf_stream_pos
*pos
);
156 struct bt_ctf_field
*(* const field_create_funcs
[])(
157 struct bt_ctf_field_type
*) = {
158 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_create
,
159 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_create
,
160 [BT_CTF_TYPE_ID_FLOAT
] =
161 bt_ctf_field_floating_point_create
,
162 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_create
,
163 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_create
,
164 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_create
,
165 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_create
,
166 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_create
,
170 void (* const field_destroy_funcs
[])(struct bt_ctf_field
*) = {
171 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_destroy
,
172 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_destroy
,
173 [BT_CTF_TYPE_ID_FLOAT
] =
174 bt_ctf_field_floating_point_destroy
,
175 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_destroy
,
176 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_destroy
,
177 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_destroy
,
178 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
179 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_destroy
,
183 int (* const field_validate_funcs
[])(struct bt_ctf_field
*) = {
184 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_generic_validate
,
185 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_validate
,
186 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_generic_validate
,
187 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_validate
,
188 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_validate
,
189 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_validate
,
190 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_validate
,
191 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_generic_validate
,
195 int (* const field_reset_funcs
[])(struct bt_ctf_field
*) = {
196 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_generic_reset
,
197 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_reset
,
198 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_generic_reset
,
199 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_reset
,
200 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_reset
,
201 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_reset
,
202 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_reset
,
203 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_reset
,
207 int (* const field_serialize_funcs
[])(struct bt_ctf_field
*,
208 struct ctf_stream_pos
*) = {
209 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_serialize
,
210 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_serialize
,
211 [BT_CTF_TYPE_ID_FLOAT
] =
212 bt_ctf_field_floating_point_serialize
,
213 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_serialize
,
214 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_serialize
,
215 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_serialize
,
216 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
217 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_serialize
,
221 int (* const field_copy_funcs
[])(struct bt_ctf_field
*,
222 struct bt_ctf_field
*) = {
223 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_copy
,
224 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_copy
,
225 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_floating_point_copy
,
226 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_copy
,
227 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_copy
,
228 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_copy
,
229 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_copy
,
230 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_copy
,
233 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
235 struct bt_ctf_field
*field
= NULL
;
236 enum bt_ctf_type_id type_id
;
243 type_id
= bt_ctf_field_type_get_type_id(type
);
244 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
245 type_id
>= BT_CTF_NR_TYPE_IDS
) {
249 /* Field class MUST be valid */
250 ret
= bt_ctf_field_type_validate(type
);
257 field
= field_create_funcs
[type_id
](type
);
262 /* The type's declaration can't change after this point */
263 bt_ctf_field_type_freeze(type
);
265 bt_object_init(field
, bt_ctf_field_destroy
);
271 void bt_ctf_field_get(struct bt_ctf_field
*field
)
276 void bt_ctf_field_put(struct bt_ctf_field
*field
)
281 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
283 struct bt_ctf_field_type
*ret
= NULL
;
295 enum bt_ctf_type_id
bt_ctf_field_get_type_id(struct bt_ctf_field
*field
)
297 enum bt_ctf_type_id ret
= BT_CTF_TYPE_ID_UNKNOWN
;
303 ret
= bt_ctf_field_type_get_type_id(field
->type
);
308 int bt_ctf_field_is_integer(struct bt_ctf_field
*field
)
310 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_INTEGER
;
313 int bt_ctf_field_is_floating_point(struct bt_ctf_field
*field
)
315 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_FLOAT
;
318 int bt_ctf_field_is_enumeration(struct bt_ctf_field
*field
)
320 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_ENUM
;
323 int bt_ctf_field_is_string(struct bt_ctf_field
*field
)
325 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_STRING
;
328 int bt_ctf_field_is_structure(struct bt_ctf_field
*field
)
330 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_STRUCT
;
333 int bt_ctf_field_is_array(struct bt_ctf_field
*field
)
335 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_ARRAY
;
338 int bt_ctf_field_is_sequence(struct bt_ctf_field
*field
)
340 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_SEQUENCE
;
343 int bt_ctf_field_is_variant(struct bt_ctf_field
*field
)
345 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_VARIANT
;
348 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
349 struct bt_ctf_field
*field
)
351 struct bt_ctf_field
*ret
= NULL
;
352 struct bt_ctf_field_sequence
*sequence
;
358 if (bt_ctf_field_type_get_type_id(field
->type
) !=
359 BT_CTF_TYPE_ID_SEQUENCE
) {
363 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
364 ret
= sequence
->length
;
370 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
371 struct bt_ctf_field
*length_field
)
374 struct bt_ctf_field_type_integer
*length_type
;
375 struct bt_ctf_field_integer
*length
;
376 struct bt_ctf_field_sequence
*sequence
;
377 uint64_t sequence_length
;
379 if (!field
|| !length_field
) {
383 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
384 BT_CTF_TYPE_ID_INTEGER
) {
389 length_type
= container_of(length_field
->type
,
390 struct bt_ctf_field_type_integer
, parent
);
391 /* The length field must be unsigned */
392 if (length_type
->declaration
.signedness
) {
397 length
= container_of(length_field
, struct bt_ctf_field_integer
,
399 sequence_length
= length
->definition
.value
._unsigned
;
400 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
401 if (sequence
->elements
) {
402 g_ptr_array_free(sequence
->elements
, TRUE
);
403 bt_put(sequence
->length
);
406 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
407 if (!sequence
->elements
) {
412 g_ptr_array_set_free_func(sequence
->elements
,
413 (GDestroyNotify
) bt_put
);
414 g_ptr_array_set_size(sequence
->elements
, (size_t) sequence_length
);
415 bt_get(length_field
);
416 sequence
->length
= length_field
;
421 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
422 struct bt_ctf_field
*field
, const char *name
)
424 struct bt_ctf_field
*new_field
= NULL
;
426 struct bt_ctf_field_structure
*structure
;
427 struct bt_ctf_field_type
*field_type
= NULL
;
430 if (!field
|| !name
||
431 bt_ctf_field_type_get_type_id(field
->type
) !=
432 BT_CTF_TYPE_ID_STRUCT
) {
436 field_quark
= g_quark_from_string(name
);
437 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
439 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
441 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
442 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
446 if (structure
->fields
->pdata
[index
]) {
447 new_field
= structure
->fields
->pdata
[index
];
451 new_field
= bt_ctf_field_create(field_type
);
456 structure
->fields
->pdata
[index
] = new_field
;
466 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
467 struct bt_ctf_field
*field
, int index
)
470 const char *field_name
;
471 struct bt_ctf_field_structure
*structure
;
472 struct bt_ctf_field_type
*structure_type
;
473 struct bt_ctf_field_type
*field_type
= NULL
;
474 struct bt_ctf_field
*ret_field
= NULL
;
477 bt_ctf_field_type_get_type_id(field
->type
) !=
478 BT_CTF_TYPE_ID_STRUCT
) {
482 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
483 if (index
>= structure
->fields
->len
) {
487 ret_field
= structure
->fields
->pdata
[index
];
492 /* Field has not been instanciated yet, create it */
493 structure_type
= bt_ctf_field_get_type(field
);
494 if (!structure_type
) {
498 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
499 &field_name
, &field_type
, index
);
500 bt_put(structure_type
);
505 ret_field
= bt_ctf_field_create(field_type
);
510 structure
->fields
->pdata
[index
] = ret_field
;
519 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
520 const char *name
, struct bt_ctf_field
*value
)
524 struct bt_ctf_field_structure
*structure
;
525 struct bt_ctf_field_type
*expected_field_type
= NULL
;
528 if (!field
|| !name
|| !value
||
529 bt_ctf_field_type_get_type_id(field
->type
) !=
530 BT_CTF_TYPE_ID_STRUCT
) {
535 field_quark
= g_quark_from_string(name
);
536 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
537 expected_field_type
=
538 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
541 if (bt_ctf_field_type_compare(expected_field_type
, value
->type
)) {
546 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
547 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
551 if (structure
->fields
->pdata
[index
]) {
552 bt_put(structure
->fields
->pdata
[index
]);
555 structure
->fields
->pdata
[index
] = value
;
558 if (expected_field_type
) {
559 bt_put(expected_field_type
);
564 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
567 struct bt_ctf_field
*new_field
= NULL
;
568 struct bt_ctf_field_type
*field_type
= NULL
;
569 struct bt_ctf_field_array
*array
;
571 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
572 BT_CTF_TYPE_ID_ARRAY
) {
576 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
577 if (index
>= array
->elements
->len
) {
581 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
582 if (array
->elements
->pdata
[(size_t)index
]) {
583 new_field
= array
->elements
->pdata
[(size_t)index
];
587 new_field
= bt_ctf_field_create(field_type
);
588 array
->elements
->pdata
[(size_t)index
] = new_field
;
599 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
602 struct bt_ctf_field
*new_field
= NULL
;
603 struct bt_ctf_field_type
*field_type
= NULL
;
604 struct bt_ctf_field_sequence
*sequence
;
606 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
607 BT_CTF_TYPE_ID_SEQUENCE
) {
611 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
612 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
616 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
617 if (sequence
->elements
->pdata
[(size_t) index
]) {
618 new_field
= sequence
->elements
->pdata
[(size_t) index
];
622 new_field
= bt_ctf_field_create(field_type
);
623 sequence
->elements
->pdata
[(size_t) index
] = new_field
;
634 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
635 struct bt_ctf_field
*tag_field
)
637 struct bt_ctf_field
*new_field
= NULL
;
638 struct bt_ctf_field_variant
*variant
;
639 struct bt_ctf_field_type_variant
*variant_type
;
640 struct bt_ctf_field_type
*field_type
;
641 struct bt_ctf_field
*tag_enum
= NULL
;
642 struct bt_ctf_field_integer
*tag_enum_integer
;
643 int64_t tag_enum_value
;
645 if (!field
|| !tag_field
||
646 bt_ctf_field_type_get_type_id(field
->type
) !=
647 BT_CTF_TYPE_ID_VARIANT
||
648 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
649 BT_CTF_TYPE_ID_ENUM
) {
653 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
654 variant_type
= container_of(field
->type
,
655 struct bt_ctf_field_type_variant
, parent
);
656 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
661 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
664 if (bt_ctf_field_validate(tag_field
) < 0) {
668 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
671 * If the variant currently has a tag and a payload, and if the
672 * requested tag value is the same as the current one, return
673 * the current payload instead of creating a fresh one.
675 if (variant
->tag
&& variant
->payload
) {
676 struct bt_ctf_field
*cur_tag_container
= NULL
;
677 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
678 int64_t cur_tag_value
;
681 bt_ctf_field_enumeration_get_container(variant
->tag
);
682 assert(cur_tag_container
);
683 cur_tag_enum_integer
= container_of(cur_tag_container
,
684 struct bt_ctf_field_integer
, parent
);
685 bt_put(cur_tag_container
);
686 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
688 if (cur_tag_value
== tag_enum_value
) {
689 new_field
= variant
->payload
;
695 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
696 variant_type
, tag_enum_value
);
701 new_field
= bt_ctf_field_create(field_type
);
706 bt_put(variant
->tag
);
707 bt_put(variant
->payload
);
710 variant
->tag
= tag_field
;
711 variant
->payload
= new_field
;
717 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
718 struct bt_ctf_field
*variant_field
)
720 struct bt_ctf_field
*current_field
= NULL
;
721 struct bt_ctf_field_variant
*variant
;
723 if (!variant_field
||
724 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
725 BT_CTF_TYPE_ID_VARIANT
) {
729 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
732 if (variant
->payload
) {
733 current_field
= variant
->payload
;
734 bt_get(current_field
);
739 return current_field
;
742 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
743 struct bt_ctf_field
*field
)
745 struct bt_ctf_field
*container
= NULL
;
746 struct bt_ctf_field_enumeration
*enumeration
;
748 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
749 BT_CTF_TYPE_ID_ENUM
) {
753 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
755 if (!enumeration
->payload
) {
756 struct bt_ctf_field_type_enumeration
*enumeration_type
=
757 container_of(field
->type
,
758 struct bt_ctf_field_type_enumeration
, parent
);
759 enumeration
->payload
=
760 bt_ctf_field_create(enumeration_type
->container
);
763 container
= enumeration
->payload
;
769 const char *bt_ctf_field_enumeration_get_mapping_name(
770 struct bt_ctf_field
*field
)
773 const char *name
= NULL
;
774 struct bt_ctf_field
*container
= NULL
;
775 struct bt_ctf_field_type
*container_type
= NULL
;
776 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
777 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
779 container
= bt_ctf_field_enumeration_get_container(field
);
784 container_type
= bt_ctf_field_get_type(container
);
785 if (!container_type
) {
786 goto error_put_container
;
789 integer_type
= container_of(container_type
,
790 struct bt_ctf_field_type_integer
, parent
);
791 enumeration_type
= container_of(field
->type
,
792 struct bt_ctf_field_type_enumeration
, parent
);
794 if (!integer_type
->declaration
.signedness
) {
796 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
799 goto error_put_container_type
;
802 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
803 enumeration_type
, value
);
806 ret
= bt_ctf_field_signed_integer_get_value(container
,
809 goto error_put_container_type
;
812 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
813 enumeration_type
, value
);
816 error_put_container_type
:
817 bt_put(container_type
);
824 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
828 struct bt_ctf_field_integer
*integer
;
829 struct bt_ctf_field_type_integer
*integer_type
;
831 if (!field
|| !value
|| !field
->payload_set
||
832 bt_ctf_field_type_get_type_id(field
->type
) !=
833 BT_CTF_TYPE_ID_INTEGER
) {
838 integer_type
= container_of(field
->type
,
839 struct bt_ctf_field_type_integer
, parent
);
840 if (!integer_type
->declaration
.signedness
) {
845 integer
= container_of(field
,
846 struct bt_ctf_field_integer
, parent
);
847 *value
= integer
->definition
.value
._signed
;
852 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
856 struct bt_ctf_field_integer
*integer
;
857 struct bt_ctf_field_type_integer
*integer_type
;
859 int64_t min_value
, max_value
;
862 bt_ctf_field_type_get_type_id(field
->type
) !=
863 BT_CTF_TYPE_ID_INTEGER
) {
868 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
869 integer_type
= container_of(field
->type
,
870 struct bt_ctf_field_type_integer
, parent
);
871 if (!integer_type
->declaration
.signedness
) {
876 size
= integer_type
->declaration
.len
;
877 min_value
= -((int64_t)1 << (size
- 1));
878 max_value
= ((int64_t)1 << (size
- 1)) - 1;
879 if (value
< min_value
|| value
> max_value
) {
884 integer
->definition
.value
._signed
= value
;
885 integer
->parent
.payload_set
= 1;
890 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
894 struct bt_ctf_field_integer
*integer
;
895 struct bt_ctf_field_type_integer
*integer_type
;
897 if (!field
|| !value
|| !field
->payload_set
||
898 bt_ctf_field_type_get_type_id(field
->type
) !=
899 BT_CTF_TYPE_ID_INTEGER
) {
904 integer_type
= container_of(field
->type
,
905 struct bt_ctf_field_type_integer
, parent
);
906 if (integer_type
->declaration
.signedness
) {
911 integer
= container_of(field
,
912 struct bt_ctf_field_integer
, parent
);
913 *value
= integer
->definition
.value
._unsigned
;
918 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
922 struct bt_ctf_field_integer
*integer
;
923 struct bt_ctf_field_type_integer
*integer_type
;
928 bt_ctf_field_type_get_type_id(field
->type
) !=
929 BT_CTF_TYPE_ID_INTEGER
) {
934 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
935 integer_type
= container_of(field
->type
,
936 struct bt_ctf_field_type_integer
, parent
);
937 if (integer_type
->declaration
.signedness
) {
942 size
= integer_type
->declaration
.len
;
943 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
944 if (value
> max_value
) {
949 integer
->definition
.value
._unsigned
= value
;
950 integer
->parent
.payload_set
= 1;
955 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
959 struct bt_ctf_field_floating_point
*floating_point
;
961 if (!field
|| !value
|| !field
->payload_set
||
962 bt_ctf_field_type_get_type_id(field
->type
) !=
963 BT_CTF_TYPE_ID_FLOAT
) {
968 floating_point
= container_of(field
,
969 struct bt_ctf_field_floating_point
, parent
);
970 *value
= floating_point
->definition
.value
;
975 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
979 struct bt_ctf_field_floating_point
*floating_point
;
982 bt_ctf_field_type_get_type_id(field
->type
) !=
983 BT_CTF_TYPE_ID_FLOAT
) {
987 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
989 floating_point
->definition
.value
= value
;
990 floating_point
->parent
.payload_set
= 1;
995 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
997 const char *ret
= NULL
;
998 struct bt_ctf_field_string
*string
;
1000 if (!field
|| !field
->payload_set
||
1001 bt_ctf_field_type_get_type_id(field
->type
) !=
1002 BT_CTF_TYPE_ID_STRING
) {
1006 string
= container_of(field
,
1007 struct bt_ctf_field_string
, parent
);
1008 ret
= string
->payload
->str
;
1013 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1017 struct bt_ctf_field_string
*string
;
1019 if (!field
|| !value
||
1020 bt_ctf_field_type_get_type_id(field
->type
) !=
1021 BT_CTF_TYPE_ID_STRING
) {
1026 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1027 if (string
->payload
) {
1028 g_string_assign(string
->payload
, value
);
1030 string
->payload
= g_string_new(value
);
1033 string
->parent
.payload_set
= 1;
1038 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1042 struct bt_ctf_field_string
*string_field
;
1044 if (!field
|| !value
||
1045 bt_ctf_field_type_get_type_id(field
->type
) !=
1046 BT_CTF_TYPE_ID_STRING
) {
1051 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1053 if (string_field
->payload
) {
1054 g_string_append(string_field
->payload
, value
);
1056 string_field
->payload
= g_string_new(value
);
1059 string_field
->parent
.payload_set
= 1;
1065 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1066 const char *value
, unsigned int length
)
1070 unsigned int effective_length
= length
;
1071 struct bt_ctf_field_string
*string_field
;
1073 if (!field
|| !value
||
1074 bt_ctf_field_type_get_type_id(field
->type
) !=
1075 BT_CTF_TYPE_ID_STRING
) {
1080 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1082 /* make sure no null bytes are appended */
1083 for (i
= 0; i
< length
; ++i
) {
1084 if (value
[i
] == '\0') {
1085 effective_length
= i
;
1090 if (string_field
->payload
) {
1091 g_string_append_len(string_field
->payload
, value
,
1094 string_field
->payload
= g_string_new_len(value
,
1098 string_field
->parent
.payload_set
= 1;
1105 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1108 enum bt_ctf_type_id type_id
;
1115 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1116 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1121 ret
= field_validate_funcs
[type_id
](field
);
1127 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1130 enum bt_ctf_type_id type_id
;
1137 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1138 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1143 ret
= field_reset_funcs
[type_id
](field
);
1149 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1150 struct ctf_stream_pos
*pos
)
1153 enum bt_ctf_type_id type_id
;
1155 if (!field
|| !pos
) {
1160 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1161 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1166 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1171 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1174 struct bt_ctf_field
*copy
= NULL
;
1175 enum bt_ctf_type_id type_id
;
1181 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1182 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1186 copy
= bt_ctf_field_create(field
->type
);
1191 copy
->payload_set
= field
->payload_set
;
1192 ret
= field_copy_funcs
[type_id
](field
, copy
);
1202 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1204 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1205 struct bt_ctf_field_type_integer
, parent
);
1206 struct bt_ctf_field_integer
*integer
= g_new0(
1207 struct bt_ctf_field_integer
, 1);
1210 integer
->definition
.declaration
= &integer_type
->declaration
;
1213 return integer
? &integer
->parent
: NULL
;
1217 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1218 struct bt_ctf_field_type
*type
)
1220 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1221 struct bt_ctf_field_enumeration
, 1);
1223 return enumeration
? &enumeration
->parent
: NULL
;
1227 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1228 struct bt_ctf_field_type
*type
)
1230 struct bt_ctf_field_floating_point
*floating_point
;
1231 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1233 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1234 if (!floating_point
) {
1238 floating_point_type
= container_of(type
,
1239 struct bt_ctf_field_type_floating_point
, parent
);
1240 floating_point
->definition
.declaration
= container_of(
1241 type
->declaration
, struct declaration_float
, p
);
1244 floating_point
->definition
.sign
= &floating_point
->sign
;
1245 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1246 floating_point
->definition
.sign
->p
.declaration
=
1247 &floating_point_type
->sign
.p
;
1249 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1250 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1251 floating_point
->definition
.mantissa
->p
.declaration
=
1252 &floating_point_type
->mantissa
.p
;
1254 floating_point
->definition
.exp
= &floating_point
->exp
;
1255 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1256 floating_point
->definition
.exp
->p
.declaration
=
1257 &floating_point_type
->exp
.p
;
1260 return floating_point
? &floating_point
->parent
: NULL
;
1264 struct bt_ctf_field
*bt_ctf_field_structure_create(
1265 struct bt_ctf_field_type
*type
)
1267 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1268 struct bt_ctf_field_type_structure
, parent
);
1269 struct bt_ctf_field_structure
*structure
= g_new0(
1270 struct bt_ctf_field_structure
, 1);
1271 struct bt_ctf_field
*field
= NULL
;
1277 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1278 structure
->fields
= g_ptr_array_new_with_free_func(
1279 (GDestroyNotify
)bt_ctf_field_put
);
1280 g_ptr_array_set_size(structure
->fields
,
1281 g_hash_table_size(structure
->field_name_to_index
));
1282 field
= &structure
->parent
;
1288 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1290 struct bt_ctf_field_variant
*variant
= g_new0(
1291 struct bt_ctf_field_variant
, 1);
1292 return variant
? &variant
->parent
: NULL
;
1296 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1298 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1299 struct bt_ctf_field_type_array
*array_type
;
1300 unsigned int array_length
;
1302 if (!array
|| !type
) {
1306 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1307 array_length
= array_type
->length
;
1308 array
->elements
= g_ptr_array_sized_new(array_length
);
1309 if (!array
->elements
) {
1313 g_ptr_array_set_free_func(array
->elements
,
1314 (GDestroyNotify
)bt_ctf_field_put
);
1315 g_ptr_array_set_size(array
->elements
, array_length
);
1316 return &array
->parent
;
1323 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1324 struct bt_ctf_field_type
*type
)
1326 struct bt_ctf_field_sequence
*sequence
= g_new0(
1327 struct bt_ctf_field_sequence
, 1);
1328 return sequence
? &sequence
->parent
: NULL
;
1332 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1334 struct bt_ctf_field_string
*string
= g_new0(
1335 struct bt_ctf_field_string
, 1);
1336 return string
? &string
->parent
: NULL
;
1340 void bt_ctf_field_destroy(struct bt_object
*obj
)
1342 struct bt_ctf_field
*field
;
1343 struct bt_ctf_field_type
*type
;
1344 enum bt_ctf_type_id type_id
;
1346 field
= container_of(obj
, struct bt_ctf_field
, base
);
1348 type_id
= bt_ctf_field_type_get_type_id(type
);
1349 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
1350 type_id
>= BT_CTF_NR_TYPE_IDS
) {
1354 field_destroy_funcs
[type_id
](field
);
1359 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1361 struct bt_ctf_field_integer
*integer
;
1367 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1372 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1374 struct bt_ctf_field_enumeration
*enumeration
;
1380 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1382 bt_put(enumeration
->payload
);
1383 g_free(enumeration
);
1387 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1389 struct bt_ctf_field_floating_point
*floating_point
;
1395 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1397 g_free(floating_point
);
1401 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1403 struct bt_ctf_field_structure
*structure
;
1409 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1410 g_ptr_array_free(structure
->fields
, TRUE
);
1415 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1417 struct bt_ctf_field_variant
*variant
;
1423 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1424 bt_put(variant
->tag
);
1425 bt_put(variant
->payload
);
1430 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1432 struct bt_ctf_field_array
*array
;
1438 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1439 g_ptr_array_free(array
->elements
, TRUE
);
1444 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1446 struct bt_ctf_field_sequence
*sequence
;
1452 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1453 if (sequence
->elements
) {
1454 g_ptr_array_free(sequence
->elements
, TRUE
);
1456 bt_put(sequence
->length
);
1461 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1463 struct bt_ctf_field_string
*string
;
1468 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1469 if (string
->payload
) {
1470 g_string_free(string
->payload
, TRUE
);
1476 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1478 return (field
&& field
->payload_set
) ? 0 : -1;
1482 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1485 struct bt_ctf_field_enumeration
*enumeration
;
1492 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1494 if (!enumeration
->payload
) {
1499 ret
= bt_ctf_field_validate(enumeration
->payload
);
1505 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1509 struct bt_ctf_field_structure
*structure
;
1516 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1517 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1518 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1528 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1531 struct bt_ctf_field_variant
*variant
;
1538 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1539 ret
= bt_ctf_field_validate(variant
->payload
);
1545 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1549 struct bt_ctf_field_array
*array
;
1556 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1557 for (i
= 0; i
< array
->elements
->len
; i
++) {
1558 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1568 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1572 struct bt_ctf_field_sequence
*sequence
;
1579 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1580 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1581 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1591 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1600 field
->payload_set
= 0;
1606 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1609 struct bt_ctf_field_enumeration
*enumeration
;
1616 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1618 if (!enumeration
->payload
) {
1622 ret
= bt_ctf_field_reset(enumeration
->payload
);
1628 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1632 struct bt_ctf_field_structure
*structure
;
1639 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1640 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1641 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1645 * Structure members are lazily initialized; skip if
1646 * this member has not been allocated yet.
1651 ret
= bt_ctf_field_reset(member
);
1661 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1664 struct bt_ctf_field_variant
*variant
;
1671 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1672 if (variant
->payload
) {
1673 ret
= bt_ctf_field_reset(variant
->payload
);
1680 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1684 struct bt_ctf_field_array
*array
;
1691 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1692 for (i
= 0; i
< array
->elements
->len
; i
++) {
1693 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1697 * Array elements are lazily initialized; skip if
1698 * this member has not been allocated yet.
1703 ret
= bt_ctf_field_reset(member
);
1713 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1717 struct bt_ctf_field_sequence
*sequence
;
1724 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1725 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1726 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1730 * Sequence elements are lazily initialized; skip if
1731 * this member has not been allocated yet.
1736 ret
= bt_ctf_field_reset(member
);
1746 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1749 struct bt_ctf_field_string
*string
;
1756 ret
= bt_ctf_field_generic_reset(field
);
1761 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1762 if (string
->payload
) {
1763 g_string_truncate(string
->payload
, 0);
1770 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1771 struct ctf_stream_pos
*pos
)
1774 struct bt_ctf_field_integer
*integer
= container_of(field
,
1775 struct bt_ctf_field_integer
, parent
);
1778 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1779 if (ret
== -EFAULT
) {
1781 * The field is too large to fit in the current packet's
1782 * remaining space. Bump the packet size and retry.
1784 ret
= increase_packet_size(pos
);
1795 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1796 struct ctf_stream_pos
*pos
)
1798 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1799 field
, struct bt_ctf_field_enumeration
, parent
);
1801 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1805 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1806 struct ctf_stream_pos
*pos
)
1809 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1810 struct bt_ctf_field_floating_point
, parent
);
1813 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1814 if (ret
== -EFAULT
) {
1816 * The field is too large to fit in the current packet's
1817 * remaining space. Bump the packet size and retry.
1819 ret
= increase_packet_size(pos
);
1830 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1831 struct ctf_stream_pos
*pos
)
1835 struct bt_ctf_field_structure
*structure
= container_of(
1836 field
, struct bt_ctf_field_structure
, parent
);
1838 while (!ctf_pos_access_ok(pos
,
1839 offset_align(pos
->offset
,
1840 field
->type
->declaration
->alignment
))) {
1841 ret
= increase_packet_size(pos
);
1847 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1852 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1853 struct bt_ctf_field
*field
= g_ptr_array_index(
1854 structure
->fields
, i
);
1856 ret
= bt_ctf_field_serialize(field
, pos
);
1866 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1867 struct ctf_stream_pos
*pos
)
1869 struct bt_ctf_field_variant
*variant
= container_of(
1870 field
, struct bt_ctf_field_variant
, parent
);
1872 return bt_ctf_field_serialize(variant
->payload
, pos
);
1876 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1877 struct ctf_stream_pos
*pos
)
1881 struct bt_ctf_field_array
*array
= container_of(
1882 field
, struct bt_ctf_field_array
, parent
);
1884 for (i
= 0; i
< array
->elements
->len
; i
++) {
1885 ret
= bt_ctf_field_serialize(
1886 g_ptr_array_index(array
->elements
, i
), pos
);
1896 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1897 struct ctf_stream_pos
*pos
)
1901 struct bt_ctf_field_sequence
*sequence
= container_of(
1902 field
, struct bt_ctf_field_sequence
, parent
);
1904 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1905 ret
= bt_ctf_field_serialize(
1906 g_ptr_array_index(sequence
->elements
, i
), pos
);
1916 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1917 struct ctf_stream_pos
*pos
)
1921 struct bt_ctf_field_string
*string
= container_of(field
,
1922 struct bt_ctf_field_string
, parent
);
1923 struct bt_ctf_field_type
*character_type
=
1924 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1925 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1927 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1928 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1929 (uint64_t) string
->payload
->str
[i
]);
1934 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1941 bt_put(character_type
);
1946 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1947 struct bt_ctf_field
*dst
)
1949 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1951 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1952 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
1954 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1955 sizeof(struct definition_integer
));
1960 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1961 struct bt_ctf_field
*dst
)
1964 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1966 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1967 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1969 if (enum_src
->payload
) {
1970 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1971 if (!enum_dst
->payload
) {
1981 int bt_ctf_field_floating_point_copy(
1982 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1984 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1986 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1988 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1991 memcpy(&float_dst
->definition
, &float_src
->definition
,
1992 sizeof(struct definition_float
));
1993 memcpy(&float_dst
->sign
, &float_src
->sign
,
1994 sizeof(struct definition_integer
));
1995 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1996 sizeof(struct definition_integer
));
1997 memcpy(&float_dst
->exp
, &float_src
->exp
,
1998 sizeof(struct definition_integer
));
2003 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
2004 struct bt_ctf_field
*dst
)
2007 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
2009 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
2010 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
2012 /* This field_name_to_index HT is owned by the structure field type */
2013 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2014 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2016 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2017 struct bt_ctf_field
*field
=
2018 g_ptr_array_index(struct_src
->fields
, i
);
2019 struct bt_ctf_field
*field_copy
= NULL
;
2022 field_copy
= bt_ctf_field_copy(field
);
2030 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2037 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2038 struct bt_ctf_field
*dst
)
2041 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2043 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2044 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2046 if (variant_src
->tag
) {
2047 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2048 if (!variant_dst
->tag
) {
2053 if (variant_src
->payload
) {
2054 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2055 if (!variant_dst
->payload
) {
2065 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2066 struct bt_ctf_field
*dst
)
2069 struct bt_ctf_field_array
*array_src
, *array_dst
;
2071 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2072 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2074 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2075 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2076 struct bt_ctf_field
*field
=
2077 g_ptr_array_index(array_src
->elements
, i
);
2078 struct bt_ctf_field
*field_copy
= NULL
;
2081 field_copy
= bt_ctf_field_copy(field
);
2089 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2096 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2097 struct bt_ctf_field
*dst
)
2100 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2101 struct bt_ctf_field
*src_length
;
2102 struct bt_ctf_field
*dst_length
;
2104 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2105 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2107 src_length
= bt_ctf_field_sequence_get_length(src
);
2110 /* no length set yet: keep destination sequence empty */
2114 /* copy source length */
2115 dst_length
= bt_ctf_field_copy(src_length
);
2123 /* this will initialize the destination sequence's internal array */
2124 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2131 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2133 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2134 struct bt_ctf_field
*field
=
2135 g_ptr_array_index(sequence_src
->elements
, i
);
2136 struct bt_ctf_field
*field_copy
= NULL
;
2139 field_copy
= bt_ctf_field_copy(field
);
2147 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2154 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2155 struct bt_ctf_field
*dst
)
2158 struct bt_ctf_field_string
*string_src
, *string_dst
;
2160 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2161 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2163 if (string_src
->payload
) {
2164 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2165 if (!string_dst
->payload
) {
2175 int increase_packet_size(struct ctf_stream_pos
*pos
)
2180 ret
= munmap_align(pos
->base_mma
);
2185 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2187 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2188 pos
->packet_size
/ CHAR_BIT
);
2189 } while (ret
== EINTR
);
2196 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2197 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2198 if (pos
->base_mma
== MAP_FAILED
) {