4 * Babeltrace CTF IR - Event Types
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 #define BT_LOG_TAG "FIELD-TYPES"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/ctf-ir/field-types-internal.h>
33 #include <babeltrace/ctf-ir/field-path-internal.h>
34 #include <babeltrace/ctf-ir/utils.h>
35 #include <babeltrace/ref.h>
36 #include <babeltrace/ctf-ir/clock-class.h>
37 #include <babeltrace/ctf-ir/clock-class-internal.h>
38 #include <babeltrace/ctf-writer/writer-internal.h>
39 #include <babeltrace/object-internal.h>
40 #include <babeltrace/ref.h>
41 #include <babeltrace/compiler-internal.h>
42 #include <babeltrace/endian-internal.h>
47 struct range_overlap_query
{
62 void bt_ctf_field_type_destroy(struct bt_object
*);
64 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*);
66 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*);
68 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*);
70 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*);
72 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*);
74 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*);
76 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*);
78 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*);
81 void (* const type_destroy_funcs
[])(struct bt_ctf_field_type
*) = {
82 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_destroy
,
83 [BT_CTF_FIELD_TYPE_ID_ENUM
] =
84 bt_ctf_field_type_enumeration_destroy
,
85 [BT_CTF_FIELD_TYPE_ID_FLOAT
] =
86 bt_ctf_field_type_floating_point_destroy
,
87 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_destroy
,
88 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_destroy
,
89 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_destroy
,
90 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
91 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_type_string_destroy
,
95 void generic_field_type_freeze(struct bt_ctf_field_type
*);
97 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*);
99 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
101 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
103 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
105 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
107 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
110 type_freeze_func
const type_freeze_funcs
[] = {
111 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_freeze
,
112 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
113 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = generic_field_type_freeze
,
114 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_freeze
,
115 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_freeze
,
116 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_freeze
,
117 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
118 [BT_CTF_FIELD_TYPE_ID_STRING
] = generic_field_type_freeze
,
122 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
123 struct metadata_context
*);
125 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
126 struct metadata_context
*);
128 int bt_ctf_field_type_floating_point_serialize(
129 struct bt_ctf_field_type
*, struct metadata_context
*);
131 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
137 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
138 struct metadata_context
*);
140 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
141 struct metadata_context
*);
143 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
144 struct metadata_context
*);
147 type_serialize_func
const type_serialize_funcs
[] = {
148 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_serialize
,
149 [BT_CTF_FIELD_TYPE_ID_ENUM
] =
150 bt_ctf_field_type_enumeration_serialize
,
151 [BT_CTF_FIELD_TYPE_ID_FLOAT
] =
152 bt_ctf_field_type_floating_point_serialize
,
153 [BT_CTF_FIELD_TYPE_ID_STRUCT
] =
154 bt_ctf_field_type_structure_serialize
,
155 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_serialize
,
156 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_serialize
,
157 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
158 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_type_string_serialize
,
162 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
163 enum bt_ctf_byte_order byte_order
);
165 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
166 enum bt_ctf_byte_order byte_order
);
168 void bt_ctf_field_type_floating_point_set_byte_order(
169 struct bt_ctf_field_type
*, enum bt_ctf_byte_order byte_order
);
171 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
172 enum bt_ctf_byte_order byte_order
);
174 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
175 enum bt_ctf_byte_order byte_order
);
177 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
178 enum bt_ctf_byte_order byte_order
);
180 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
181 enum bt_ctf_byte_order byte_order
);
184 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
185 enum bt_ctf_byte_order
) = {
186 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
187 [BT_CTF_FIELD_TYPE_ID_ENUM
] =
188 bt_ctf_field_type_enumeration_set_byte_order
,
189 [BT_CTF_FIELD_TYPE_ID_FLOAT
] =
190 bt_ctf_field_type_floating_point_set_byte_order
,
191 [BT_CTF_FIELD_TYPE_ID_STRUCT
] =
192 bt_ctf_field_type_structure_set_byte_order
,
193 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
194 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
195 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
196 [BT_CTF_FIELD_TYPE_ID_STRING
] = NULL
,
200 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
201 struct bt_ctf_field_type
*);
203 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
204 struct bt_ctf_field_type
*);
206 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
207 struct bt_ctf_field_type
*);
209 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
210 struct bt_ctf_field_type
*);
212 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
213 struct bt_ctf_field_type
*);
215 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
216 struct bt_ctf_field_type
*);
218 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
219 struct bt_ctf_field_type
*);
221 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
222 struct bt_ctf_field_type
*);
225 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
226 struct bt_ctf_field_type
*) = {
227 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_copy
,
228 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_copy
,
229 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
230 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_copy
,
231 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_copy
,
232 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_copy
,
233 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
234 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_type_string_copy
,
238 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*,
239 struct bt_ctf_field_type
*);
241 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*,
242 struct bt_ctf_field_type
*);
244 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*,
245 struct bt_ctf_field_type
*);
247 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*,
248 struct bt_ctf_field_type
*);
250 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*,
251 struct bt_ctf_field_type
*);
253 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*,
254 struct bt_ctf_field_type
*);
256 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*,
257 struct bt_ctf_field_type
*);
259 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*,
260 struct bt_ctf_field_type
*);
263 int (* const type_compare_funcs
[])(struct bt_ctf_field_type
*,
264 struct bt_ctf_field_type
*) = {
265 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_compare
,
266 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_compare
,
267 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_compare
,
268 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_compare
,
269 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_compare
,
270 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_compare
,
271 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_compare
,
272 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_type_string_compare
,
276 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*);
278 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*);
280 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*);
282 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*);
284 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*);
286 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*);
289 int (* const type_validate_funcs
[])(struct bt_ctf_field_type
*) = {
290 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_validate
,
291 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = NULL
,
292 [BT_CTF_FIELD_TYPE_ID_STRING
] = NULL
,
293 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_validate
,
294 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_validate
,
295 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_validate
,
296 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_validate
,
297 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_validate
,
301 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
307 void destroy_structure_field(struct structure_field
*field
)
313 BT_LOGD("Destroying structure/variant field type's field object: "
314 "addr=%p, field-ft-addr=%p, field-name=\"%s\"",
315 field
, field
->type
, g_quark_to_string(field
->name
));
321 void check_ranges_overlap(gpointer element
, gpointer query
)
323 struct enumeration_mapping
*mapping
= element
;
324 struct range_overlap_query
*overlap_query
= query
;
326 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
327 && overlap_query
->range_start
._signed
<=
328 mapping
->range_end
._signed
) {
329 overlap_query
->overlaps
= 1;
330 overlap_query
->mapping_name
= mapping
->string
;
333 overlap_query
->overlaps
|=
334 mapping
->string
== overlap_query
->mapping_name
;
336 if (overlap_query
->overlaps
) {
337 BT_LOGV("Overlapping enumeration field type mappings: "
338 "mapping-name=\"%s\", "
339 "mapping-a-range-start=%" PRId64
", "
340 "mapping-a-range-end=%" PRId64
", "
341 "mapping-b-range-start=%" PRId64
", "
342 "mapping-b-range-end=%" PRId64
,
343 g_quark_to_string(mapping
->string
),
344 mapping
->range_start
._signed
,
345 mapping
->range_end
._signed
,
346 overlap_query
->range_start
._signed
,
347 overlap_query
->range_end
._signed
);
352 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
354 struct enumeration_mapping
*mapping
= element
;
355 struct range_overlap_query
*overlap_query
= query
;
357 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
358 && overlap_query
->range_start
._unsigned
<=
359 mapping
->range_end
._unsigned
) {
360 overlap_query
->overlaps
= 1;
361 overlap_query
->mapping_name
= mapping
->string
;
364 overlap_query
->overlaps
|=
365 mapping
->string
== overlap_query
->mapping_name
;
367 if (overlap_query
->overlaps
) {
368 BT_LOGW("Overlapping enumeration field type mappings: "
369 "mapping-name=\"%s\", "
370 "mapping-a-range-start=%" PRIu64
", "
371 "mapping-a-range-end=%" PRIu64
", "
372 "mapping-b-range-start=%" PRIu64
", "
373 "mapping-b-range-end=%" PRIu64
,
374 g_quark_to_string(mapping
->string
),
375 mapping
->range_start
._unsigned
,
376 mapping
->range_end
._unsigned
,
377 overlap_query
->range_start
._unsigned
,
378 overlap_query
->range_end
._unsigned
);
383 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
384 struct enumeration_mapping
**b
)
386 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
390 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
391 struct enumeration_mapping
**b
)
393 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
397 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, bt_bool init_bo
)
399 assert(type
&& (type
->id
> BT_CTF_FIELD_TYPE_ID_UNKNOWN
) &&
400 (type
->id
< BT_CTF_NR_TYPE_IDS
));
402 bt_object_init(type
, bt_ctf_field_type_destroy
);
403 type
->freeze
= type_freeze_funcs
[type
->id
];
404 type
->serialize
= type_serialize_funcs
[type
->id
];
407 int ret
= bt_ctf_field_type_set_byte_order(type
,
408 BT_CTF_BYTE_ORDER_NATIVE
);
416 int add_structure_field(GPtrArray
*fields
,
417 GHashTable
*field_name_to_index
,
418 struct bt_ctf_field_type
*field_type
,
419 const char *field_name
)
422 GQuark name_quark
= g_quark_from_string(field_name
);
423 struct structure_field
*field
;
425 /* Make sure structure does not contain a field of the same name */
426 if (g_hash_table_lookup_extended(field_name_to_index
,
427 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
428 BT_LOGW("Structure or variant field type already contains a field type with this name: "
429 "field-name=\"%s\"", field_name
);
434 field
= g_new0(struct structure_field
, 1);
436 BT_LOGE_STR("Failed to allocate one structure/variant field type field.");
442 field
->name
= name_quark
;
443 field
->type
= field_type
;
444 g_hash_table_insert(field_name_to_index
,
445 GUINT_TO_POINTER(name_quark
),
446 GUINT_TO_POINTER(fields
->len
));
447 g_ptr_array_add(fields
, field
);
448 BT_LOGV("Added structure/variant field type field: field-ft-addr=%p, "
449 "field-name=\"%s\"", field_type
, field_name
);
455 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
457 struct bt_ctf_field_type
*type
;
458 enum bt_ctf_field_type_id type_id
;
460 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
462 assert(type_id
> BT_CTF_FIELD_TYPE_ID_UNKNOWN
&&
463 type_id
< BT_CTF_NR_TYPE_IDS
);
464 type_destroy_funcs
[type_id
](type
);
468 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*type
)
472 struct bt_ctf_field_type_integer
*integer
=
473 container_of(type
, struct bt_ctf_field_type_integer
,
476 if (integer
->mapped_clock
&& integer
->is_signed
) {
477 BT_LOGW("Invalid integer field type: cannot be signed and have a mapped clock class: "
478 "ft-addr=%p, clock-class-addr=%p, clock-class-name=\"%s\"",
479 type
, integer
->mapped_clock
,
480 bt_ctf_clock_class_get_name(integer
->mapped_clock
));
490 struct enumeration_mapping
*get_enumeration_mapping(
491 struct bt_ctf_field_type
*type
, uint64_t index
)
493 struct enumeration_mapping
*mapping
= NULL
;
494 struct bt_ctf_field_type_enumeration
*enumeration
;
496 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
498 if (index
>= enumeration
->entries
->len
) {
499 BT_LOGW("Invalid parameter: index is out of bounds: "
500 "addr=%p, index=%" PRIu64
", count=%u",
501 type
, index
, enumeration
->entries
->len
);
505 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
511 * Note: This algorithm is O(n^2) vs number of enumeration mappings.
512 * Only used when freezing an enumeration.
515 void set_enumeration_range_overlap(
516 struct bt_ctf_field_type
*type
)
519 struct bt_ctf_field_type
*container_type
;
520 struct bt_ctf_field_type_enumeration
*enumeration_type
;
523 BT_LOGV("Setting enumeration field type's overlap flag: addr=%p",
525 enumeration_type
= container_of(type
,
526 struct bt_ctf_field_type_enumeration
, parent
);
528 len
= enumeration_type
->entries
->len
;
529 container_type
= enumeration_type
->container
;
530 is_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
532 for (i
= 0; i
< len
; i
++) {
533 for (j
= i
+ 1; j
< len
; j
++) {
534 struct enumeration_mapping
*mapping
[2];
536 mapping
[0] = get_enumeration_mapping(type
, i
);
537 mapping
[1] = get_enumeration_mapping(type
, j
);
539 if (mapping
[0]->range_start
._signed
540 <= mapping
[1]->range_end
._signed
541 && mapping
[0]->range_end
._signed
542 >= mapping
[1]->range_start
._signed
) {
543 enumeration_type
->has_overlapping_ranges
= BT_TRUE
;
547 if (mapping
[0]->range_start
._unsigned
548 <= mapping
[1]->range_end
._unsigned
549 && mapping
[0]->range_end
._unsigned
550 >= mapping
[1]->range_start
._unsigned
) {
551 enumeration_type
->has_overlapping_ranges
= BT_TRUE
;
559 if (enumeration_type
->has_overlapping_ranges
) {
560 BT_LOGV_STR("Enumeration field type has overlapping ranges.");
562 BT_LOGV_STR("Enumeration field type has no overlapping ranges.");
567 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*type
)
571 struct bt_ctf_field_type_enumeration
*enumeration
=
572 container_of(type
, struct bt_ctf_field_type_enumeration
,
574 struct bt_ctf_field_type
*container_type
=
575 bt_ctf_field_type_enumeration_get_container_type(type
);
577 assert(container_type
);
578 ret
= bt_ctf_field_type_validate(container_type
);
580 BT_LOGW("Invalid enumeration field type: container type is invalid: "
581 "enum-ft-addr=%p, int-ft-addr=%p",
582 type
, container_type
);
586 /* Ensure enum has entries */
587 if (enumeration
->entries
->len
== 0) {
588 BT_LOGW("Invalid enumeration field type: no entries: "
595 BT_PUT(container_type
);
600 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*type
)
603 struct bt_ctf_field_type
*element_type
= NULL
;
604 struct bt_ctf_field_type_sequence
*sequence
=
605 container_of(type
, struct bt_ctf_field_type_sequence
,
608 /* Length field name should be set at this point */
609 if (sequence
->length_field_name
->len
== 0) {
610 BT_LOGW("Invalid sequence field type: no length field name: "
616 element_type
= bt_ctf_field_type_sequence_get_element_type(type
);
617 assert(element_type
);
618 ret
= bt_ctf_field_type_validate(element_type
);
620 BT_LOGW("Invalid sequence field type: invalid element field type: "
621 "seq-ft-addr=%p, element-ft-add=%p",
626 BT_PUT(element_type
);
632 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*type
)
635 struct bt_ctf_field_type
*element_type
= NULL
;
637 element_type
= bt_ctf_field_type_array_get_element_type(type
);
638 assert(element_type
);
639 ret
= bt_ctf_field_type_validate(element_type
);
641 BT_LOGW("Invalid array field type: invalid element field type: "
642 "array-ft-addr=%p, element-ft-add=%p",
646 BT_PUT(element_type
);
651 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*type
)
654 struct bt_ctf_field_type
*child_type
= NULL
;
655 int64_t field_count
= bt_ctf_field_type_structure_get_field_count(type
);
658 assert(field_count
>= 0);
660 for (i
= 0; i
< field_count
; ++i
) {
661 const char *field_name
;
663 ret
= bt_ctf_field_type_structure_get_field_by_index(type
,
664 &field_name
, &child_type
, i
);
666 ret
= bt_ctf_field_type_validate(child_type
);
668 BT_LOGW("Invalid structure field type: "
669 "a contained field type is invalid: "
670 "struct-ft-addr=%p, field-ft-addr=%p, "
671 "field-name=\"%s\", field-index=%" PRId64
,
672 type
, child_type
, field_name
, i
);
686 bt_bool
bt_ctf_field_type_enumeration_has_overlapping_ranges(
687 struct bt_ctf_field_type_enumeration
*enumeration_type
)
689 if (!enumeration_type
->parent
.frozen
) {
690 set_enumeration_range_overlap(&enumeration_type
->parent
);
692 return enumeration_type
->has_overlapping_ranges
;
696 int bt_ctf_field_type_enumeration_get_mapping_name(
697 struct bt_ctf_field_type
*enum_field_type
,
699 const char **mapping_name
)
702 struct enumeration_mapping
*mapping
;
704 assert(enum_field_type
);
705 mapping
= get_enumeration_mapping(enum_field_type
, index
);
708 *mapping_name
= g_quark_to_string(mapping
->string
);
715 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
719 struct bt_ctf_field_type
*child_type
= NULL
;
720 struct bt_ctf_field_type_variant
*variant
=
721 container_of(type
, struct bt_ctf_field_type_variant
,
724 int64_t tag_mappings_count
;
726 if (variant
->tag_name
->len
== 0) {
727 BT_LOGW("Invalid variant field type: no tag field name: "
734 BT_LOGW("Invalid variant field type: no tag field type: "
735 "addr=%p, tag-field-name=\"%s\"", type
,
736 variant
->tag_name
->str
);
741 if (bt_ctf_field_type_enumeration_has_overlapping_ranges(
743 BT_LOGW("Invalid variant field type: enumeration tag field type has overlapping ranges: "
744 "variant-ft-addr=%p, tag-field-name=\"%s\", "
745 "enum-ft-addr=%p", type
, variant
->tag_name
->str
,
752 bt_ctf_field_type_enumeration_get_mapping_count(
753 (struct bt_ctf_field_type
*) variant
->tag
);
756 * Validate that each mapping found in the tag has a name which
757 * is also the name of a field in this variant field type.
759 * The opposite is accepted: variant FT fields which cannot be
760 * selected because the variant FT tag has no mapping named as
761 * such. This scenario, while not ideal, cannot cause any error.
763 for (i
= 0; i
< tag_mappings_count
; ++i
) {
765 struct bt_ctf_field_type
*ft
;
767 ret
= bt_ctf_field_type_enumeration_get_mapping_name(
768 (struct bt_ctf_field_type
*) variant
->tag
,
772 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
775 BT_LOGW("Invalid variant field type: "
776 "enumeration tag field type contains a mapping which does not name a variant field type field: "
777 "variant-ft-addr=%p, tag-field-name=\"%s\", "
778 "enum-ft-addr=%p, mapping-name=\"%s\"",
779 type
, variant
->tag_name
->str
, variant
->tag
,
788 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
789 if (field_count
< 0) {
790 BT_LOGW("Invalid variant field type: no fields: "
791 "addr=%p, tag-field-name=\"%s\"",
792 type
, variant
->tag_name
->str
);
797 for (i
= 0; i
< field_count
; ++i
) {
798 const char *field_name
;
800 ret
= bt_ctf_field_type_variant_get_field_by_index(type
,
801 &field_name
, &child_type
, i
);
803 ret
= bt_ctf_field_type_validate(child_type
);
805 BT_LOGW("Invalid variant field type: "
806 "a contained field type is invalid: "
807 "variant-ft-addr=%p, tag-field-name=\"%s\", "
808 "field-ft-addr=%p, field-name=\"%s\", "
809 "field-index=%" PRId64
,
810 type
, variant
->tag_name
->str
, child_type
,
825 * This function validates a given field type without considering
826 * where this field type is located. It only validates the properties
827 * of the given field type and the properties of its children if
831 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
834 enum bt_ctf_field_type_id id
= bt_ctf_field_type_get_type_id(type
);
839 /* Already marked as valid */
843 if (type_validate_funcs
[id
]) {
844 ret
= type_validate_funcs
[id
](type
);
847 if (!ret
&& type
->frozen
) {
848 /* Field type is valid */
856 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
858 struct bt_ctf_field_type_integer
*integer
=
859 g_new0(struct bt_ctf_field_type_integer
, 1);
861 BT_LOGD("Creating integer field type object: size=%u", size
);
864 BT_LOGE_STR("Failed to allocate one integer field type.");
868 if (size
== 0 || size
> 64) {
869 BT_LOGW("Invalid parameter: size must be between 1 and 64: "
874 integer
->parent
.id
= BT_CTF_FIELD_TYPE_ID_INTEGER
;
875 integer
->size
= size
;
876 integer
->base
= BT_CTF_INTEGER_BASE_DECIMAL
;
877 integer
->encoding
= BT_CTF_STRING_ENCODING_NONE
;
878 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
879 BT_LOGD("Created integer field type object: addr=%p, size=%u",
880 &integer
->parent
, size
);
881 return &integer
->parent
;
884 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
887 struct bt_ctf_field_type_integer
*integer
;
890 BT_LOGW_STR("Invalid parameter: field type is NULL.");
895 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
896 BT_LOGW("Invalid parameter: field type is not an integer field type: "
897 "addr=%p, ft-id=%s", type
,
898 bt_ctf_field_type_id_string(type
->id
));
903 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
904 ret
= (int) integer
->size
;
909 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
912 struct bt_ctf_field_type_integer
*integer
;
915 BT_LOGW_STR("Invalid parameter: field type is NULL.");
920 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
921 BT_LOGW("Invalid parameter: field type is not an integer field type: "
922 "addr=%p, ft-id=%s", type
,
923 bt_ctf_field_type_id_string(type
->id
));
928 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
929 ret
= integer
->is_signed
;
934 bt_bool
bt_ctf_field_type_integer_is_signed(
935 struct bt_ctf_field_type
*int_field_type
)
937 return bt_ctf_field_type_integer_get_signed(int_field_type
) ?
941 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
945 struct bt_ctf_field_type_integer
*integer
;
948 BT_LOGW_STR("Invalid parameter: field type is NULL.");
954 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
960 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
961 BT_LOGW("Invalid parameter: field type is not an integer field type: "
962 "addr=%p, ft-id=%s", type
,
963 bt_ctf_field_type_id_string(type
->id
));
968 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
969 integer
->is_signed
= !!is_signed
;
970 BT_LOGV("Set integer field type's signedness: addr=%p, is-signed=%d",
976 int bt_ctf_field_type_integer_set_size(struct bt_ctf_field_type
*type
,
980 struct bt_ctf_field_type_integer
*integer
;
983 BT_LOGW_STR("Invalid parameter: field type is NULL.");
989 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
995 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
996 BT_LOGW("Invalid parameter: field type is not an integer field type: "
997 "addr=%p, ft-id=%s", type
,
998 bt_ctf_field_type_id_string(type
->id
));
1003 if (size
== 0 || size
> 64) {
1004 BT_LOGW("Invalid parameter: size must be between 1 and 64: "
1005 "addr=%p, size=%u", type
, size
);
1010 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
1011 integer
->size
= size
;
1012 BT_LOGV("Set integer field type's size: addr=%p, size=%u",
1018 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
1019 struct bt_ctf_field_type
*type
)
1021 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
1022 struct bt_ctf_field_type_integer
*integer
;
1025 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1029 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1030 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1031 "addr=%p, ft-id=%s", type
,
1032 bt_ctf_field_type_id_string(type
->id
));
1036 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
1037 ret
= integer
->base
;
1042 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
1043 enum bt_ctf_integer_base base
)
1048 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1054 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1060 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1061 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1062 "addr=%p, ft-id=%s", type
,
1063 bt_ctf_field_type_id_string(type
->id
));
1069 case BT_CTF_INTEGER_BASE_BINARY
:
1070 case BT_CTF_INTEGER_BASE_OCTAL
:
1071 case BT_CTF_INTEGER_BASE_DECIMAL
:
1072 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
1074 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
1075 struct bt_ctf_field_type_integer
, parent
);
1076 integer
->base
= base
;
1080 BT_LOGW("Invalid parameter: unknown integer field type base: "
1081 "addr=%p, base=%d", type
, base
);
1085 BT_LOGV("Set integer field type's base: addr=%p, base=%s",
1086 type
, bt_ctf_integer_base_string(base
));
1092 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
1093 struct bt_ctf_field_type
*type
)
1095 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
1096 struct bt_ctf_field_type_integer
*integer
;
1099 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1103 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1104 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1105 "addr=%p, ft-id=%s", type
,
1106 bt_ctf_field_type_id_string(type
->id
));
1110 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
1111 ret
= integer
->encoding
;
1116 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
1117 enum bt_ctf_string_encoding encoding
)
1120 struct bt_ctf_field_type_integer
*integer
;
1123 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1129 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1135 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1136 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1137 "addr=%p, ft-id=%s", type
,
1138 bt_ctf_field_type_id_string(type
->id
));
1143 if (encoding
< BT_CTF_STRING_ENCODING_NONE
||
1144 encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
) {
1145 BT_LOGW("Invalid parameter: unknown string encoding: "
1146 "addr=%p, encoding=%d", type
, encoding
);
1151 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
1152 integer
->encoding
= encoding
;
1153 BT_LOGV("Set integer field type's encoding: addr=%p, encoding=%s",
1154 type
, bt_ctf_string_encoding_string(encoding
));
1159 struct bt_ctf_clock_class
*bt_ctf_field_type_integer_get_mapped_clock_class(
1160 struct bt_ctf_field_type
*type
)
1162 struct bt_ctf_field_type_integer
*integer
;
1163 struct bt_ctf_clock_class
*clock_class
= NULL
;
1166 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1170 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
1171 clock_class
= integer
->mapped_clock
;
1172 bt_get(clock_class
);
1177 int bt_ctf_field_type_integer_set_mapped_clock_class(
1178 struct bt_ctf_field_type
*type
,
1179 struct bt_ctf_clock_class
*clock_class
)
1181 struct bt_ctf_field_type_integer
*integer
;
1185 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1191 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
1197 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1203 if (!bt_ctf_clock_class_is_valid(clock_class
)) {
1204 BT_LOGW("Invalid parameter: clock class is invalid: ft-addr=%p"
1205 "clock-class-addr=%p, clock-class-name=\"%s\"",
1207 bt_ctf_clock_class_get_name(clock_class
));
1212 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
1213 bt_put(integer
->mapped_clock
);
1214 integer
->mapped_clock
= bt_get(clock_class
);
1215 BT_LOGV("Set integer field type's mapped clock class: ft-addr=%p, "
1216 "clock-class-addr=%p, clock-class-name=\"%s\"",
1217 type
, clock_class
, bt_ctf_clock_class_get_name(clock_class
));
1223 void bt_ctf_field_type_enum_iter_destroy(struct bt_object
*obj
)
1225 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
=
1227 struct bt_ctf_field_type_enumeration_mapping_iterator
,
1230 bt_put(&iter
->enumeration_type
->parent
);
1235 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1236 bt_ctf_field_type_enumeration_find_mappings_type(
1237 struct bt_ctf_field_type
*type
,
1238 enum bt_ctf_field_type_enumeration_mapping_iterator_type iterator_type
)
1240 struct bt_ctf_field_type_enumeration
*enumeration_type
;
1241 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1244 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1248 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) {
1249 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
1250 "addr=%p, ft-id=%s", type
,
1251 bt_ctf_field_type_id_string(type
->id
));
1255 enumeration_type
= container_of(type
,
1256 struct bt_ctf_field_type_enumeration
, parent
);
1257 iter
= g_new0(struct bt_ctf_field_type_enumeration_mapping_iterator
, 1);
1259 BT_LOGE_STR("Failed to allocate one enumeration field type mapping.");
1263 bt_object_init(&iter
->base
, bt_ctf_field_type_enum_iter_destroy
);
1265 iter
->enumeration_type
= enumeration_type
;
1267 iter
->type
= iterator_type
;
1272 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1273 bt_ctf_field_type_enumeration_find_mappings_by_name(
1274 struct bt_ctf_field_type
*type
, const char *name
)
1276 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1278 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1279 type
, ITERATOR_BY_NAME
);
1281 BT_LOGE("Cannot create enumeration field type mapping iterator: "
1282 "ft-addr=%p, mapping-name=\"%s\"", type
, name
);
1286 iter
->u
.name_quark
= g_quark_try_string(name
);
1287 if (!iter
->u
.name_quark
) {
1288 BT_LOGE("Cannot get GQuark: string=\"%s\"", name
);
1292 /* Advance iterator to first entry, or leave index at -1. */
1293 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1294 /* No entry found. */
1304 int bt_ctf_field_type_enumeration_mapping_iterator_next(
1305 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
)
1307 struct bt_ctf_field_type_enumeration
*enumeration
;
1308 struct bt_ctf_field_type
*type
;
1309 int i
, ret
= 0, len
;
1312 BT_LOGW_STR("Invalid parameter: enumeration field type mapping iterator is NULL.");
1317 enumeration
= iter
->enumeration_type
;
1318 type
= &enumeration
->parent
;
1319 len
= enumeration
->entries
->len
;
1320 for (i
= iter
->index
+ 1; i
< len
; i
++) {
1321 struct enumeration_mapping
*mapping
=
1322 get_enumeration_mapping(type
, i
);
1324 switch (iter
->type
) {
1325 case ITERATOR_BY_NAME
:
1326 if (mapping
->string
== iter
->u
.name_quark
) {
1331 case ITERATOR_BY_SIGNED_VALUE
:
1333 int64_t value
= iter
->u
.signed_value
;
1335 if (value
>= mapping
->range_start
._signed
&&
1336 value
<= mapping
->range_end
._signed
) {
1342 case ITERATOR_BY_UNSIGNED_VALUE
:
1344 uint64_t value
= iter
->u
.unsigned_value
;
1346 if (value
>= mapping
->range_start
._unsigned
&&
1347 value
<= mapping
->range_end
._unsigned
) {
1363 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1364 bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
1365 struct bt_ctf_field_type
*type
, int64_t value
)
1367 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1369 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1370 type
, ITERATOR_BY_SIGNED_VALUE
);
1372 BT_LOGE("Cannot create enumeration field type mapping iterator: "
1373 "ft-addr=%p, value=%" PRId64
, type
, value
);
1377 if (bt_ctf_field_type_integer_get_signed(
1378 iter
->enumeration_type
->container
) != 1) {
1379 BT_LOGW("Invalid parameter: enumeration field type is unsigned: "
1380 "enum-ft-addr=%p, int-ft-addr=%p",
1381 type
, iter
->enumeration_type
->container
);
1385 iter
->u
.signed_value
= value
;
1387 /* Advance iterator to first entry, or leave index at -1. */
1388 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1389 /* No entry found. */
1399 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1400 bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
1401 struct bt_ctf_field_type
*type
, uint64_t value
)
1403 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1406 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1410 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1411 type
, ITERATOR_BY_UNSIGNED_VALUE
);
1413 BT_LOGE("Cannot create enumeration field type mapping iterator: "
1414 "ft-addr=%p, value=%" PRIu64
, type
, value
);
1418 if (bt_ctf_field_type_integer_get_signed(
1419 iter
->enumeration_type
->container
) != 0) {
1420 BT_LOGW("Invalid parameter: enumeration field type is signed: "
1421 "enum-ft-addr=%p, int-ft-addr=%p",
1422 type
, iter
->enumeration_type
->container
);
1425 iter
->u
.unsigned_value
= value
;
1427 /* Advance iterator to first entry, or leave index at -1. */
1428 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1429 /* No entry found. */
1439 int bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1440 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1441 const char **mapping_name
, int64_t *range_begin
,
1447 BT_LOGW_STR("Invalid parameter: enumeration field type mapping iterator is NULL.");
1452 ret
= bt_ctf_field_type_enumeration_get_mapping_signed(
1453 &iter
->enumeration_type
->parent
, iter
->index
,
1454 mapping_name
, range_begin
, range_end
);
1459 int bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
1460 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1461 const char **mapping_name
, uint64_t *range_begin
,
1462 uint64_t *range_end
)
1467 BT_LOGW_STR("Invalid parameter: enumeration field type mapping iterator is NULL.");
1472 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(
1473 &iter
->enumeration_type
->parent
, iter
->index
,
1474 mapping_name
, range_begin
, range_end
);
1479 int bt_ctf_field_type_enumeration_get_mapping_signed(
1480 struct bt_ctf_field_type
*enum_field_type
,
1481 uint64_t index
, const char **mapping_name
, int64_t *range_begin
,
1485 struct enumeration_mapping
*mapping
;
1487 if (!enum_field_type
) {
1488 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1493 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1495 /* get_enumeration_mapping() reports any error */
1501 *mapping_name
= g_quark_to_string(mapping
->string
);
1505 *range_begin
= mapping
->range_start
._signed
;
1509 *range_end
= mapping
->range_end
._signed
;
1515 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1516 struct bt_ctf_field_type
*enum_field_type
,
1518 const char **mapping_name
, uint64_t *range_begin
,
1519 uint64_t *range_end
)
1522 struct enumeration_mapping
*mapping
;
1524 if (!enum_field_type
) {
1525 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1530 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1532 /* get_enumeration_mapping() reports any error */
1538 *mapping_name
= g_quark_to_string(mapping
->string
);
1542 *range_begin
= mapping
->range_start
._unsigned
;
1546 *range_end
= mapping
->range_end
._unsigned
;
1552 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
1553 struct bt_ctf_field_type
*integer_container_type
)
1555 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
1557 BT_LOGD("Creating enumeration field type object: int-ft-addr=%p",
1558 integer_container_type
);
1560 if (!integer_container_type
) {
1561 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1565 if (integer_container_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1566 BT_LOGW("Invalid parameter: container field type is not an integer field type: "
1567 "container-ft-addr=%p, container-ft-id=%s",
1568 integer_container_type
,
1569 bt_ctf_field_type_id_string(integer_container_type
->id
));
1573 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
1575 BT_LOGE_STR("Failed to allocate one enumeration field type.");
1579 enumeration
->parent
.id
= BT_CTF_FIELD_TYPE_ID_ENUM
;
1580 bt_get(integer_container_type
);
1581 enumeration
->container
= integer_container_type
;
1582 enumeration
->entries
= g_ptr_array_new_with_free_func(
1583 (GDestroyNotify
)destroy_enumeration_mapping
);
1584 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
1585 BT_LOGD("Created enumeration field type object: addr=%p, "
1586 "int-ft-addr=%p, int-ft-size=%u",
1587 &enumeration
->parent
, integer_container_type
,
1588 bt_ctf_field_type_integer_get_size(integer_container_type
));
1589 return &enumeration
->parent
;
1591 g_free(enumeration
);
1595 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
1596 struct bt_ctf_field_type
*type
)
1598 struct bt_ctf_field_type
*container_type
= NULL
;
1599 struct bt_ctf_field_type_enumeration
*enumeration_type
;
1602 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1606 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) {
1607 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
1608 "addr=%p, ft-id=%s", type
,
1609 bt_ctf_field_type_id_string(type
->id
));
1613 enumeration_type
= container_of(type
,
1614 struct bt_ctf_field_type_enumeration
, parent
);
1615 container_type
= enumeration_type
->container
;
1616 bt_get(container_type
);
1618 return container_type
;
1621 int bt_ctf_field_type_enumeration_add_mapping_signed(
1622 struct bt_ctf_field_type
*type
, const char *string
,
1623 int64_t range_start
, int64_t range_end
)
1626 GQuark mapping_name
;
1627 struct enumeration_mapping
*mapping
;
1628 struct bt_ctf_field_type_enumeration
*enumeration
;
1629 char *escaped_string
;
1632 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1638 BT_LOGW_STR("Invalid parameter: string is NULL.");
1644 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1650 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) {
1651 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
1652 "addr=%p, ft-id=%s", type
,
1653 bt_ctf_field_type_id_string(type
->id
));
1658 if (range_end
< range_start
) {
1659 BT_LOGW("Invalid parameter: range's end is lesser than range's start: "
1660 "addr=%p, range-start=%" PRId64
", range-end=%" PRId64
,
1661 type
, range_start
, range_end
);
1666 if (strlen(string
) == 0) {
1667 BT_LOGW("Invalid parameter: mapping name is an empty string: "
1668 "enum-ft-addr=%p, mapping-name-addr=%p", type
,
1674 escaped_string
= g_strescape(string
, NULL
);
1675 if (!escaped_string
) {
1676 BT_LOGE("Cannot escape mapping name: enum-ft-addr=%p, "
1677 "mapping-name-addr=%p, mapping-name=\"%s\"",
1678 type
, string
, string
);
1683 mapping
= g_new(struct enumeration_mapping
, 1);
1685 BT_LOGE_STR("Failed to allocate one enumeration mapping.");
1689 mapping_name
= g_quark_from_string(escaped_string
);
1690 *mapping
= (struct enumeration_mapping
) {
1691 .range_start
._signed
= range_start
,
1692 .range_end
._signed
= range_end
,
1693 .string
= mapping_name
,
1695 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1697 g_ptr_array_add(enumeration
->entries
, mapping
);
1698 g_ptr_array_sort(enumeration
->entries
,
1699 (GCompareFunc
)compare_enumeration_mappings_signed
);
1700 BT_LOGV("Added mapping to signed enumeration field type: addr=%p, "
1701 "name=\"%s\", range-start=%" PRId64
", "
1702 "range-end=%" PRId64
,
1703 type
, string
, range_start
, range_end
);
1705 free(escaped_string
);
1710 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
1711 struct bt_ctf_field_type
*type
, const char *string
,
1712 uint64_t range_start
, uint64_t range_end
)
1715 GQuark mapping_name
;
1716 struct enumeration_mapping
*mapping
;
1717 struct bt_ctf_field_type_enumeration
*enumeration
;
1718 char *escaped_string
;
1721 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1727 BT_LOGW_STR("Invalid parameter: string is NULL.");
1733 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1739 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) {
1740 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
1741 "addr=%p, ft-id=%s", type
,
1742 bt_ctf_field_type_id_string(type
->id
));
1747 if (range_end
< range_start
) {
1748 BT_LOGW("Invalid parameter: range's end is lesser than range's start: "
1749 "addr=%p, range-start=%" PRIu64
", range-end=%" PRIu64
,
1750 type
, range_start
, range_end
);
1755 if (strlen(string
) == 0) {
1756 BT_LOGW("Invalid parameter: mapping name is an empty string: "
1757 "enum-ft-addr=%p, mapping-name-addr=%p", type
,
1763 escaped_string
= g_strescape(string
, NULL
);
1764 if (!escaped_string
) {
1765 BT_LOGE("Cannot escape mapping name: enum-ft-addr=%p, "
1766 "mapping-name-addr=%p, mapping-name=\"%s\"",
1767 type
, string
, string
);
1772 mapping
= g_new(struct enumeration_mapping
, 1);
1774 BT_LOGE_STR("Failed to allocate one enumeration mapping.");
1778 mapping_name
= g_quark_from_string(escaped_string
);
1779 *mapping
= (struct enumeration_mapping
) {
1780 .range_start
._unsigned
= range_start
,
1781 .range_end
._unsigned
= range_end
,
1782 .string
= mapping_name
,
1784 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1786 g_ptr_array_add(enumeration
->entries
, mapping
);
1787 g_ptr_array_sort(enumeration
->entries
,
1788 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1789 BT_LOGV("Added mapping to unsigned enumeration field type: addr=%p, "
1790 "name=\"%s\", range-start=%" PRIu64
", "
1791 "range-end=%" PRIu64
,
1792 type
, string
, range_start
, range_end
);
1794 free(escaped_string
);
1799 int64_t bt_ctf_field_type_enumeration_get_mapping_count(
1800 struct bt_ctf_field_type
*type
)
1803 struct bt_ctf_field_type_enumeration
*enumeration
;
1806 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1811 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) {
1812 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
1813 "addr=%p, ft-id=%s", type
,
1814 bt_ctf_field_type_id_string(type
->id
));
1819 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1821 ret
= (int64_t) enumeration
->entries
->len
;
1826 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1828 struct bt_ctf_field_type_floating_point
*floating_point
=
1829 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1831 BT_LOGD_STR("Creating floating point number field type object.");
1833 if (!floating_point
) {
1834 BT_LOGE_STR("Failed to allocate one floating point number field type.");
1838 floating_point
->parent
.id
= BT_CTF_FIELD_TYPE_ID_FLOAT
;
1839 floating_point
->exp_dig
= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1840 floating_point
->mant_dig
= FLT_MANT_DIG
;
1841 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1842 BT_LOGD("Created floating point number field type object: addr=%p, "
1843 "exp-size=%u, mant-size=%u", &floating_point
->parent
,
1844 floating_point
->exp_dig
, floating_point
->mant_dig
);
1846 return floating_point
? &floating_point
->parent
: NULL
;
1849 int bt_ctf_field_type_floating_point_get_exponent_digits(
1850 struct bt_ctf_field_type
*type
)
1853 struct bt_ctf_field_type_floating_point
*floating_point
;
1856 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1861 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
) {
1862 BT_LOGW("Invalid parameter: field type is not a floating point number field type: "
1863 "addr=%p, ft-id=%s", type
,
1864 bt_ctf_field_type_id_string(type
->id
));
1869 floating_point
= container_of(type
,
1870 struct bt_ctf_field_type_floating_point
, parent
);
1871 ret
= (int) floating_point
->exp_dig
;
1876 int bt_ctf_field_type_floating_point_set_exponent_digits(
1877 struct bt_ctf_field_type
*type
,
1878 unsigned int exponent_digits
)
1881 struct bt_ctf_field_type_floating_point
*floating_point
;
1884 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1890 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1896 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
) {
1897 BT_LOGW("Invalid parameter: field type is not a floating point number field type: "
1898 "addr=%p, ft-id=%s", type
,
1899 bt_ctf_field_type_id_string(type
->id
));
1904 floating_point
= container_of(type
,
1905 struct bt_ctf_field_type_floating_point
, parent
);
1906 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1907 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1909 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1910 BT_LOGW("Invalid parameter: invalid exponent size: "
1911 "addr=%p, exp-size=%u", type
, exponent_digits
);
1916 floating_point
->exp_dig
= exponent_digits
;
1917 BT_LOGV("Set floating point number field type's exponent size: addr=%p, "
1918 "exp-size=%u", type
, exponent_digits
);
1923 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1924 struct bt_ctf_field_type
*type
)
1927 struct bt_ctf_field_type_floating_point
*floating_point
;
1930 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1935 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
) {
1936 BT_LOGW("Invalid parameter: field type is not a floating point number field type: "
1937 "addr=%p, ft-id=%s", type
,
1938 bt_ctf_field_type_id_string(type
->id
));
1943 floating_point
= container_of(type
,
1944 struct bt_ctf_field_type_floating_point
, parent
);
1945 ret
= (int) floating_point
->mant_dig
;
1950 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1951 struct bt_ctf_field_type
*type
,
1952 unsigned int mantissa_digits
)
1955 struct bt_ctf_field_type_floating_point
*floating_point
;
1958 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1964 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1970 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
) {
1971 BT_LOGW("Invalid parameter: field type is not a floating point number field type: "
1972 "addr=%p, ft-id=%s", type
,
1973 bt_ctf_field_type_id_string(type
->id
));
1978 floating_point
= container_of(type
,
1979 struct bt_ctf_field_type_floating_point
, parent
);
1981 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1982 (mantissa_digits
!= DBL_MANT_DIG
) &&
1983 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1984 BT_LOGW("Invalid parameter: invalid mantissa size: "
1985 "addr=%p, mant-size=%u", type
, mantissa_digits
);
1990 floating_point
->mant_dig
= mantissa_digits
;
1991 BT_LOGV("Set floating point number field type's mantissa size: addr=%p, "
1992 "mant-size=%u", type
, mantissa_digits
);
1997 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1999 struct bt_ctf_field_type_structure
*structure
=
2000 g_new0(struct bt_ctf_field_type_structure
, 1);
2002 BT_LOGD_STR("Creating structure field type object.");
2005 BT_LOGE_STR("Failed to allocate one structure field type.");
2009 structure
->parent
.id
= BT_CTF_FIELD_TYPE_ID_STRUCT
;
2010 structure
->fields
= g_ptr_array_new_with_free_func(
2011 (GDestroyNotify
)destroy_structure_field
);
2012 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
2013 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
2014 BT_LOGD("Created structure field type object: addr=%p",
2015 &structure
->parent
);
2016 return &structure
->parent
;
2021 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
2022 struct bt_ctf_field_type
*field_type
,
2023 const char *field_name
)
2026 struct bt_ctf_field_type_structure
*structure
;
2029 * TODO: check that `field_type` does not contain `type`,
2033 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2039 BT_LOGW_STR("Invalid parameter: field name is NULL.");
2045 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
2051 if (bt_ctf_validate_identifier(field_name
)) {
2052 BT_LOGW("Invalid parameter: field name is not a valid CTF identifier: "
2053 "struct-ft-addr=%p, field-name=\"%s\"",
2059 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
2060 BT_LOGW("Invalid parameter: field type is not a structure field type: "
2061 "addr=%p, ft-id=%s", type
,
2062 bt_ctf_field_type_id_string(type
->id
));
2067 if (type
== field_type
) {
2068 BT_LOGW("Invalid parameter: structure field type and field type to add are the same: "
2074 structure
= container_of(type
,
2075 struct bt_ctf_field_type_structure
, parent
);
2076 if (add_structure_field(structure
->fields
,
2077 structure
->field_name_to_index
, field_type
, field_name
)) {
2078 BT_LOGW("Cannot add field to structure field type: "
2079 "struct-ft-addr=%p, field-ft-addr=%p, field-name=\"%s\"",
2080 type
, field_type
, field_name
);
2085 BT_LOGV("Added structure field type field: struct-ft-addr=%p, "
2086 "field-ft-addr=%p, field-name=\"%s\"", type
,
2087 field_type
, field_name
);
2092 int64_t bt_ctf_field_type_structure_get_field_count(
2093 struct bt_ctf_field_type
*type
)
2096 struct bt_ctf_field_type_structure
*structure
;
2099 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2104 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
2105 BT_LOGW("Invalid parameter: field type is not a structure field type: "
2106 "addr=%p, ft-id=%s", type
,
2107 bt_ctf_field_type_id_string(type
->id
));
2112 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2114 ret
= (int64_t) structure
->fields
->len
;
2119 int bt_ctf_field_type_structure_get_field_by_index(
2120 struct bt_ctf_field_type
*type
,
2121 const char **field_name
, struct bt_ctf_field_type
**field_type
,
2124 struct bt_ctf_field_type_structure
*structure
;
2125 struct structure_field
*field
;
2129 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2134 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
2135 BT_LOGW("Invalid parameter: field type is not a structure field type: "
2136 "addr=%p, ft-id=%s", type
,
2137 bt_ctf_field_type_id_string(type
->id
));
2142 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2144 if (index
>= structure
->fields
->len
) {
2145 BT_LOGW("Invalid parameter: index is out of bounds: "
2146 "addr=%p, index=%" PRIu64
", count=%u",
2147 type
, index
, structure
->fields
->len
);
2152 field
= g_ptr_array_index(structure
->fields
, index
);
2154 *field_type
= field
->type
;
2155 bt_get(field
->type
);
2158 *field_name
= g_quark_to_string(field
->name
);
2164 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
2165 struct bt_ctf_field_type
*type
,
2170 struct structure_field
*field
;
2171 struct bt_ctf_field_type_structure
*structure
;
2172 struct bt_ctf_field_type
*field_type
= NULL
;
2175 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2180 BT_LOGW_STR("Invalid parameter: name is NULL.");
2184 name_quark
= g_quark_try_string(name
);
2186 BT_LOGE("Cannot get GQuark: string=\"%s\"", name
);
2190 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2192 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2193 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2197 field
= structure
->fields
->pdata
[index
];
2198 field_type
= field
->type
;
2204 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
2205 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
2207 struct bt_ctf_field_type_variant
*variant
= NULL
;
2209 BT_LOGD("Creating variant field type object: "
2210 "tag-ft-addr=%p, tag-field-name=\"%s\"",
2211 enum_tag
, tag_name
);
2213 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
2214 BT_LOGW("Invalid parameter: tag field name is not a valid CTF identifier: "
2215 "tag-ft-addr=%p, tag-field-name=\"%s\"",
2216 enum_tag
, tag_name
);
2220 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
2222 BT_LOGE_STR("Failed to allocate one variant field type.");
2226 variant
->parent
.id
= BT_CTF_FIELD_TYPE_ID_VARIANT
;
2227 variant
->tag_name
= g_string_new(tag_name
);
2228 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
2229 variant
->fields
= g_ptr_array_new_with_free_func(
2230 (GDestroyNotify
) destroy_structure_field
);
2233 variant
->tag
= container_of(enum_tag
,
2234 struct bt_ctf_field_type_enumeration
, parent
);
2237 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
2238 /* A variant's alignment is undefined */
2239 variant
->parent
.alignment
= 0;
2240 BT_LOGD("Created variant field type object: addr=%p, "
2241 "tag-ft-addr=%p, tag-field-name=\"%s\"",
2242 &variant
->parent
, enum_tag
, tag_name
);
2243 return &variant
->parent
;
2248 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
2249 struct bt_ctf_field_type
*type
)
2251 struct bt_ctf_field_type_variant
*variant
;
2252 struct bt_ctf_field_type
*tag_type
= NULL
;
2255 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2259 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2260 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2261 "addr=%p, ft-id=%s", type
,
2262 bt_ctf_field_type_id_string(type
->id
));
2266 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
2267 if (!variant
->tag
) {
2268 BT_LOGV("Variant field type has no tag field type: "
2273 tag_type
= &variant
->tag
->parent
;
2279 const char *bt_ctf_field_type_variant_get_tag_name(
2280 struct bt_ctf_field_type
*type
)
2282 struct bt_ctf_field_type_variant
*variant
;
2283 const char *tag_name
= NULL
;
2286 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2290 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2291 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2292 "addr=%p, ft-id=%s", type
,
2293 bt_ctf_field_type_id_string(type
->id
));
2297 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
2298 if (variant
->tag_name
->len
== 0) {
2299 BT_LOGV("Variant field type has no tag field name: "
2304 tag_name
= variant
->tag_name
->str
;
2309 int bt_ctf_field_type_variant_set_tag_name(
2310 struct bt_ctf_field_type
*type
, const char *name
)
2313 struct bt_ctf_field_type_variant
*variant
;
2316 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2322 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
2328 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2329 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2330 "addr=%p, ft-id=%s", type
,
2331 bt_ctf_field_type_id_string(type
->id
));
2336 if (bt_ctf_validate_identifier(name
)) {
2337 BT_LOGW("Invalid parameter: tag field name is not a valid CTF identifier: "
2338 "variant-ft-addr=%p, tag-field-name=\"%s\"",
2344 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
2345 g_string_assign(variant
->tag_name
, name
);
2346 BT_LOGV("Set variant field type's tag field name: addr=%p, "
2347 "tag-field-name=\"%s\"", type
, name
);
2352 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
2353 struct bt_ctf_field_type
*field_type
,
2354 const char *field_name
)
2358 struct bt_ctf_field_type_variant
*variant
;
2359 GQuark field_name_quark
= g_quark_from_string(field_name
);
2362 * TODO: check that `field_type` does not contain `type`,
2366 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2372 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
2378 if (bt_ctf_validate_identifier(field_name
)) {
2379 BT_LOGW("Invalid parameter: field name is not a valid CTF identifier: "
2380 "variant-ft-addr=%p, field-name=\"%s\"",
2386 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2387 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2388 "addr=%p, ft-id=%s", type
,
2389 bt_ctf_field_type_id_string(type
->id
));
2394 if (type
== field_type
) {
2395 BT_LOGW("Invalid parameter: variant field type and field type to add are the same: "
2401 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
2403 /* The user has explicitly provided a tag; validate against it. */
2407 /* Make sure this name is present in the enum tag */
2408 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
2409 struct enumeration_mapping
*mapping
=
2410 g_ptr_array_index(variant
->tag
->entries
, i
);
2412 if (mapping
->string
== field_name_quark
) {
2419 /* Validation failed */
2420 BT_LOGW("Invalid parameter: field name does not name a tag field type's mapping: "
2421 "variant-ft-addr=%p, tag-ft-addr=%p, "
2422 "tag-field-name=\"%s\""
2423 "field-ft-addr=%p, field-name=\"%s\"",
2424 type
, variant
->tag
, variant
->tag_name
->str
,
2425 field_type
, field_name
);
2431 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
2432 field_type
, field_name
)) {
2433 BT_LOGW("Cannot add field to variant field type: "
2434 "variant-ft-addr=%p, field-ft-addr=%p, field-name=\"%s\"",
2435 type
, field_type
, field_name
);
2440 BT_LOGV("Added variant field type field: variant-ft-addr=%p, "
2441 "field-ft-addr=%p, field-name=\"%s\"", type
,
2442 field_type
, field_name
);
2448 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
2449 struct bt_ctf_field_type
*type
,
2450 const char *field_name
)
2454 struct structure_field
*field
;
2455 struct bt_ctf_field_type_variant
*variant
;
2456 struct bt_ctf_field_type
*field_type
= NULL
;
2459 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2464 BT_LOGW_STR("Invalid parameter: field name is NULL.");
2468 name_quark
= g_quark_try_string(field_name
);
2470 BT_LOGE("Cannot get GQuark: string=\"%s\"", field_name
);
2474 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
2475 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2476 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2480 field
= g_ptr_array_index(variant
->fields
, index
);
2481 field_type
= field
->type
;
2487 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
2488 struct bt_ctf_field_type
*type
,
2489 struct bt_ctf_field
*tag
)
2492 const char *enum_value
;
2493 struct bt_ctf_field_type
*field_type
= NULL
;
2494 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
2497 BT_LOGW_STR("Invalid parameter: variant field type is NULL.");
2502 BT_LOGW_STR("Invalid parameter: tag field is NULL.");
2506 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2507 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2508 "addr=%p, ft-id=%s", type
,
2509 bt_ctf_field_type_id_string(type
->id
));
2513 iter
= bt_ctf_field_enumeration_get_mappings(tag
);
2515 BT_LOGE("Cannot get enumeration field type mapping iterator from enumeration field: "
2516 "enum-field-addr=%p", tag
);
2520 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
,
2521 &enum_value
, NULL
, NULL
);
2523 BT_LOGW("Cannot get enumeration field type mapping iterator's current mapping: "
2524 "iter-addr=%p", iter
);
2528 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
2535 int64_t bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
2538 struct bt_ctf_field_type_variant
*variant
;
2541 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2546 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2547 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2548 "addr=%p, ft-id=%s", type
,
2549 bt_ctf_field_type_id_string(type
->id
));
2554 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2556 ret
= (int64_t) variant
->fields
->len
;
2562 int bt_ctf_field_type_variant_get_field_by_index(struct bt_ctf_field_type
*type
,
2563 const char **field_name
, struct bt_ctf_field_type
**field_type
,
2566 struct bt_ctf_field_type_variant
*variant
;
2567 struct structure_field
*field
;
2571 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2576 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2577 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2578 "addr=%p, ft-id=%s", type
,
2579 bt_ctf_field_type_id_string(type
->id
));
2584 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2586 if (index
>= variant
->fields
->len
) {
2587 BT_LOGW("Invalid parameter: index is out of bounds: "
2588 "addr=%p, index=%" PRIu64
", count=%u",
2589 type
, index
, variant
->fields
->len
);
2594 field
= g_ptr_array_index(variant
->fields
, index
);
2596 *field_type
= field
->type
;
2597 bt_get(field
->type
);
2600 *field_name
= g_quark_to_string(field
->name
);
2606 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
2607 struct bt_ctf_field_type
*element_type
,
2608 unsigned int length
)
2610 struct bt_ctf_field_type_array
*array
= NULL
;
2612 BT_LOGD("Creating array field type object: element-ft-addr=%p, "
2613 "length=%u", element_type
, length
);
2615 if (!element_type
) {
2616 BT_LOGW_STR("Invalid parameter: element field type is NULL.");
2621 BT_LOGW_STR("Invalid parameter: length is zero.");
2625 array
= g_new0(struct bt_ctf_field_type_array
, 1);
2627 BT_LOGE_STR("Failed to allocate one array field type.");
2631 array
->parent
.id
= BT_CTF_FIELD_TYPE_ID_ARRAY
;
2632 bt_get(element_type
);
2633 array
->element_type
= element_type
;
2634 array
->length
= length
;
2635 bt_ctf_field_type_init(&array
->parent
, FALSE
);
2636 BT_LOGD("Created array field type object: addr=%p, "
2637 "element-ft-addr=%p, length=%u",
2638 &array
->parent
, element_type
, length
);
2639 return &array
->parent
;
2644 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
2645 struct bt_ctf_field_type
*type
)
2647 struct bt_ctf_field_type
*ret
= NULL
;
2648 struct bt_ctf_field_type_array
*array
;
2651 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2655 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
) {
2656 BT_LOGW("Invalid parameter: field type is not an array field type: "
2657 "addr=%p, ft-id=%s", type
,
2658 bt_ctf_field_type_id_string(type
->id
));
2662 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2663 ret
= array
->element_type
;
2670 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
2671 struct bt_ctf_field_type
*element_type
)
2674 struct bt_ctf_field_type_array
*array
;
2677 BT_LOGW_STR("Invalid parameter: array field type is NULL.");
2682 if (!element_type
) {
2683 BT_LOGW_STR("Invalid parameter: element field type is NULL.");
2688 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
) {
2689 BT_LOGW("Invalid parameter: field type is not an array field type: "
2690 "addr=%p, ft-id=%s", type
,
2691 bt_ctf_field_type_id_string(type
->id
));
2696 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2698 if (array
->element_type
) {
2699 BT_PUT(array
->element_type
);
2702 array
->element_type
= element_type
;
2703 bt_get(array
->element_type
);
2704 BT_LOGV("Set array field type's element field type: array-ft-addr=%p, "
2705 "element-ft-addr=%p", type
, element_type
);
2711 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
2714 struct bt_ctf_field_type_array
*array
;
2717 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2722 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
) {
2723 BT_LOGW("Invalid parameter: field type is not an array field type: "
2724 "addr=%p, ft-id=%s", type
,
2725 bt_ctf_field_type_id_string(type
->id
));
2730 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2731 ret
= (int64_t) array
->length
;
2736 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
2737 struct bt_ctf_field_type
*element_type
,
2738 const char *length_field_name
)
2740 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
2742 BT_LOGD("Creating sequence field type object: element-ft-addr=%p, "
2743 "length-field-name=\"%s\"", element_type
, length_field_name
);
2745 if (!element_type
) {
2746 BT_LOGW_STR("Invalid parameter: element field type is NULL.");
2750 if (bt_ctf_validate_identifier(length_field_name
)) {
2751 BT_LOGW("Invalid parameter: length field name is not a valid CTF identifier: "
2752 "length-field-name=\"%s\"", length_field_name
);
2756 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
2758 BT_LOGE_STR("Failed to allocate one sequence field type.");
2762 sequence
->parent
.id
= BT_CTF_FIELD_TYPE_ID_SEQUENCE
;
2763 bt_get(element_type
);
2764 sequence
->element_type
= element_type
;
2765 sequence
->length_field_name
= g_string_new(length_field_name
);
2766 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
2767 BT_LOGD("Created sequence field type object: addr=%p, "
2768 "element-ft-addr=%p, length-field-name=\"%s\"",
2769 &sequence
->parent
, element_type
, length_field_name
);
2770 return &sequence
->parent
;
2775 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
2776 struct bt_ctf_field_type
*type
)
2778 struct bt_ctf_field_type
*ret
= NULL
;
2779 struct bt_ctf_field_type_sequence
*sequence
;
2782 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2786 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
2787 BT_LOGW("Invalid parameter: field type is not a sequence field type: "
2788 "addr=%p, ft-id=%s", type
,
2789 bt_ctf_field_type_id_string(type
->id
));
2793 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2795 ret
= sequence
->element_type
;
2802 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
2803 struct bt_ctf_field_type
*element_type
)
2806 struct bt_ctf_field_type_sequence
*sequence
;
2809 BT_LOGW_STR("Invalid parameter: sequence field type is NULL.");
2814 if (!element_type
) {
2815 BT_LOGW_STR("Invalid parameter: element field type is NULL.");
2820 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
2821 BT_LOGW("Invalid parameter: field type is not a sequence field type: "
2822 "addr=%p, ft-id=%s", type
,
2823 bt_ctf_field_type_id_string(type
->id
));
2828 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
2829 if (sequence
->element_type
) {
2830 BT_PUT(sequence
->element_type
);
2833 sequence
->element_type
= element_type
;
2834 bt_get(sequence
->element_type
);
2835 BT_LOGV("Set sequence field type's element field type: sequence-ft-addr=%p, "
2836 "element-ft-addr=%p", type
, element_type
);
2842 const char *bt_ctf_field_type_sequence_get_length_field_name(
2843 struct bt_ctf_field_type
*type
)
2845 const char *ret
= NULL
;
2846 struct bt_ctf_field_type_sequence
*sequence
;
2849 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2853 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
2854 BT_LOGW("Invalid parameter: field type is not a sequence field type: "
2855 "addr=%p, ft-id=%s", type
,
2856 bt_ctf_field_type_id_string(type
->id
));
2860 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2862 ret
= sequence
->length_field_name
->str
;
2867 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
2869 struct bt_ctf_field_type_string
*string
=
2870 g_new0(struct bt_ctf_field_type_string
, 1);
2872 BT_LOGD_STR("Creating string field type object.");
2875 BT_LOGE_STR("Failed to allocate one string field type.");
2879 string
->parent
.id
= BT_CTF_FIELD_TYPE_ID_STRING
;
2880 bt_ctf_field_type_init(&string
->parent
, TRUE
);
2881 string
->encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2882 string
->parent
.alignment
= CHAR_BIT
;
2883 BT_LOGD("Created string field type object: addr=%p", &string
->parent
);
2884 return &string
->parent
;
2887 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
2888 struct bt_ctf_field_type
*type
)
2890 struct bt_ctf_field_type_string
*string
;
2891 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
2894 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2898 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRING
) {
2899 BT_LOGW("Invalid parameter: field type is not a string field type: "
2900 "addr=%p, ft-id=%s", type
,
2901 bt_ctf_field_type_id_string(type
->id
));
2905 string
= container_of(type
, struct bt_ctf_field_type_string
,
2907 ret
= string
->encoding
;
2912 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
2913 enum bt_ctf_string_encoding encoding
)
2916 struct bt_ctf_field_type_string
*string
;
2919 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2924 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRING
) {
2925 BT_LOGW("Invalid parameter: field type is not a string field type: "
2926 "addr=%p, ft-id=%s", type
,
2927 bt_ctf_field_type_id_string(type
->id
));
2932 if ((encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
2933 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
2934 BT_LOGW("Invalid parameter: unknown string encoding: "
2935 "addr=%p, encoding=%d", type
, encoding
);
2940 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
2941 string
->encoding
= encoding
;
2942 BT_LOGV("Set string field type's encoding: addr=%p, encoding=%s",
2943 type
, bt_ctf_string_encoding_string(encoding
));
2948 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2951 enum bt_ctf_field_type_id type_id
;
2954 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2960 ret
= (int) type
->alignment
;
2964 type_id
= bt_ctf_field_type_get_type_id(type
);
2966 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
2968 struct bt_ctf_field_type
*element
=
2969 bt_ctf_field_type_sequence_get_element_type(type
);
2972 ret
= bt_ctf_field_type_get_alignment(element
);
2976 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
2978 struct bt_ctf_field_type
*element
=
2979 bt_ctf_field_type_array_get_element_type(type
);
2982 ret
= bt_ctf_field_type_get_alignment(element
);
2986 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
2988 int64_t i
, element_count
;
2990 element_count
= bt_ctf_field_type_structure_get_field_count(
2992 assert(element_count
>= 0);
2994 for (i
= 0; i
< element_count
; i
++) {
2995 struct bt_ctf_field_type
*field
;
2996 int field_alignment
;
2998 ret
= bt_ctf_field_type_structure_get_field_by_index(
2999 type
, NULL
, &field
, i
);
3002 field_alignment
= bt_ctf_field_type_get_alignment(
3005 if (field_alignment
< 0) {
3006 ret
= field_alignment
;
3010 type
->alignment
= MAX(field_alignment
, type
->alignment
);
3012 ret
= (int) type
->alignment
;
3015 case BT_CTF_FIELD_TYPE_ID_UNKNOWN
:
3016 BT_LOGW("Invalid parameter: unknown field type ID: "
3017 "addr=%p, ft-id=%d", type
, type_id
);
3021 ret
= (int) type
->alignment
;
3029 int is_power_of_two(unsigned int value
)
3031 return ((value
& (value
- 1)) == 0) && value
> 0;
3034 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
3035 unsigned int alignment
)
3038 enum bt_ctf_field_type_id type_id
;
3040 /* Alignment must be a power of two */
3042 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3048 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
3054 if (!is_power_of_two(alignment
)) {
3055 BT_LOGW("Invalid parameter: alignment is not a power of two: "
3056 "addr=%p, align=%u", type
, alignment
);
3061 type_id
= bt_ctf_field_type_get_type_id(type
);
3062 if (type_id
== BT_CTF_FIELD_TYPE_ID_UNKNOWN
) {
3063 BT_LOGW("Invalid parameter: unknown field type ID: "
3064 "addr=%p, ft-id=%d", type
, type_id
);
3069 if (type
->id
== BT_CTF_FIELD_TYPE_ID_STRING
&&
3070 alignment
!= CHAR_BIT
) {
3071 BT_LOGW("Invalid parameter: alignment must be %u for a string field type: "
3072 "addr=%p, align=%u", CHAR_BIT
, type
, alignment
);
3077 if (type_id
== BT_CTF_FIELD_TYPE_ID_VARIANT
||
3078 type_id
== BT_CTF_FIELD_TYPE_ID_SEQUENCE
||
3079 type_id
== BT_CTF_FIELD_TYPE_ID_ARRAY
) {
3080 /* Setting an alignment on these types makes no sense */
3081 BT_LOGW("Invalid parameter: cannot set the alignment of this field type: "
3082 "addr=%p, ft-id=%s", type
,
3083 bt_ctf_field_type_id_string(type
->id
));
3088 type
->alignment
= alignment
;
3090 BT_LOGV("Set field type's alignment: addr=%p, align=%u",
3096 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
3097 struct bt_ctf_field_type
*type
)
3099 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3102 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3107 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
3109 struct bt_ctf_field_type_integer
*integer
= container_of(
3110 type
, struct bt_ctf_field_type_integer
, parent
);
3111 ret
= integer
->user_byte_order
;
3114 case BT_CTF_FIELD_TYPE_ID_ENUM
:
3116 struct bt_ctf_field_type_enumeration
*enum_ft
= container_of(
3117 type
, struct bt_ctf_field_type_enumeration
, parent
);
3118 ret
= bt_ctf_field_type_get_byte_order(enum_ft
->container
);
3121 case BT_CTF_FIELD_TYPE_ID_FLOAT
:
3123 struct bt_ctf_field_type_floating_point
*floating_point
=
3125 struct bt_ctf_field_type_floating_point
,
3127 ret
= floating_point
->user_byte_order
;
3131 BT_LOGW("Invalid parameter: cannot get the byte order of this field type: "
3132 "addr=%p, ft-id=%s", type
,
3133 bt_ctf_field_type_id_string(type
->id
));
3137 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
3138 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
3139 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
3140 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
3146 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
3147 enum bt_ctf_byte_order byte_order
)
3152 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3158 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
3164 if (byte_order
!= BT_CTF_BYTE_ORDER_NATIVE
&&
3165 byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
3166 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
3167 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
3168 BT_LOGW("Invalid parameter: unknown byte order: "
3169 "addr=%p, bo=%s", type
,
3170 bt_ctf_byte_order_string(byte_order
));
3175 if (set_byte_order_funcs
[type
->id
]) {
3176 set_byte_order_funcs
[type
->id
](type
, byte_order
);
3179 BT_LOGV("Set field type's byte order: addr=%p, bo=%s",
3180 type
, bt_ctf_byte_order_string(byte_order
));
3186 enum bt_ctf_field_type_id
bt_ctf_field_type_get_type_id(
3187 struct bt_ctf_field_type
*type
)
3190 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3191 return BT_CTF_FIELD_TYPE_ID_UNKNOWN
;
3197 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
3199 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
;
3202 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
3204 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_FLOAT
;
3207 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
3209 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_ENUM
;
3212 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
3214 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_STRING
;
3217 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
3219 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
;
3222 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
3224 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_ARRAY
;
3227 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
3229 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_SEQUENCE
;
3232 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
3234 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_VARIANT
;
3237 /* Pre-2.0 CTF writer backward compatibility */
3238 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
3243 /* Pre-2.0 CTF writer backward compatibility */
3244 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
3250 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
3252 if (!type
|| type
->frozen
) {
3260 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
3261 struct bt_ctf_field_type_variant
*variant
,
3264 struct bt_ctf_field_type
*type
= NULL
;
3265 GQuark field_name_quark
;
3267 struct structure_field
*field_entry
;
3268 struct range_overlap_query query
= {
3269 .range_start
._signed
= tag_value
,
3270 .range_end
._signed
= tag_value
,
3275 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
3277 if (!query
.overlaps
) {
3281 field_name_quark
= query
.mapping_name
;
3282 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
3283 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
3287 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
3288 type
= field_entry
->type
;
3294 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
3295 struct bt_ctf_field_type_variant
*variant
,
3298 struct bt_ctf_field_type
*type
= NULL
;
3299 GQuark field_name_quark
;
3301 struct structure_field
*field_entry
;
3302 struct range_overlap_query query
= {
3303 .range_start
._unsigned
= tag_value
,
3304 .range_end
._unsigned
= tag_value
,
3309 g_ptr_array_foreach(variant
->tag
->entries
,
3310 check_ranges_overlap_unsigned
,
3312 if (!query
.overlaps
) {
3316 field_name_quark
= query
.mapping_name
;
3317 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
3318 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
3322 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
3323 type
= field_entry
->type
;
3329 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
3330 struct metadata_context
*context
)
3337 /* Make sure field type is valid before serializing it */
3338 ret
= bt_ctf_field_type_validate(type
);
3340 BT_LOGW("Cannot serialize field type's metadata: field type is invalid: "
3345 ret
= type
->serialize(type
, context
);
3350 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
3352 struct bt_ctf_field_type
*copy
= NULL
;
3355 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3359 copy
= type_copy_funcs
[type
->id
](type
);
3361 BT_LOGE_STR("Cannot copy field type.");
3365 copy
->alignment
= type
->alignment
;
3371 int bt_ctf_field_type_structure_get_field_name_index(
3372 struct bt_ctf_field_type
*type
, const char *name
)
3377 struct bt_ctf_field_type_structure
*structure
;
3380 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3386 BT_LOGW_STR("Invalid parameter: field name is NULL.");
3391 if (bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_STRUCT
) {
3392 BT_LOGW("Invalid parameter: field type is not a structure field type: "
3393 "addr=%p, ft-id=%s", type
,
3394 bt_ctf_field_type_id_string(type
->id
));
3399 name_quark
= g_quark_try_string(name
);
3401 BT_LOGE("Cannot get GQuark: string=\"%s\"", name
);
3406 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3408 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
3409 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
3419 int bt_ctf_field_type_variant_get_field_name_index(
3420 struct bt_ctf_field_type
*type
, const char *name
)
3425 struct bt_ctf_field_type_variant
*variant
;
3428 BT_LOGW_STR("Invalid parameter: variant field type is NULL.");
3434 BT_LOGW_STR("Invalid parameter: field name is NULL.");
3439 if (bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_VARIANT
) {
3440 BT_LOGW("Invalid parameter: field type is not a variant field type: "
3441 "addr=%p, ft-id=%s", type
,
3442 bt_ctf_field_type_id_string(type
->id
));
3447 name_quark
= g_quark_try_string(name
);
3449 BT_LOGE("Cannot get GQuark: string=\"%s\"", name
);
3454 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3456 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
3457 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
3467 int bt_ctf_field_type_sequence_set_length_field_path(
3468 struct bt_ctf_field_type
*type
,
3469 struct bt_ctf_field_path
*path
)
3472 struct bt_ctf_field_type_sequence
*sequence
;
3475 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3480 if (bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
3481 BT_LOGW("Invalid parameter: field type is not a sequence field type: "
3482 "addr=%p, ft-id=%s", type
,
3483 bt_ctf_field_type_id_string(type
->id
));
3488 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3491 BT_MOVE(sequence
->length_field_path
, path
);
3492 BT_LOGV("Set sequence field type's length field path: ft-addr=%p, "
3493 "field-path-addr=%p", type
, path
);
3499 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
3500 struct bt_ctf_field_path
*path
)
3503 struct bt_ctf_field_type_variant
*variant
;
3506 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3511 if (bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_VARIANT
) {
3512 BT_LOGW("Invalid parameter: field type is not a variant field type: "
3513 "addr=%p, ft-id=%s", type
,
3514 bt_ctf_field_type_id_string(type
->id
));
3519 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3522 BT_MOVE(variant
->tag_field_path
, path
);
3523 BT_LOGV("Set variant field type's tag field path: ft-addr=%p, "
3524 "field-path-addr=%p", type
, path
);
3530 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
3531 struct bt_ctf_field_type
*tag
)
3534 struct bt_ctf_field_type_variant
*variant
;
3537 BT_LOGW_STR("Invalid parameter: variant field type is NULL.");
3543 BT_LOGW_STR("Invalid parameter: tag field type is NULL.");
3548 if (bt_ctf_field_type_get_type_id(tag
) != BT_CTF_FIELD_TYPE_ID_ENUM
) {
3549 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
3550 "addr=%p, ft-id=%s", type
,
3551 bt_ctf_field_type_id_string(type
->id
));
3556 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3560 bt_put(&variant
->tag
->parent
);
3562 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
3564 BT_LOGV("Set variant field type's tag field type: variant-ft-addr=%p, "
3565 "tag-ft-addr=%p", type
, tag
);
3571 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
3573 struct bt_ctf_field_type_integer
*integer
=
3574 (struct bt_ctf_field_type_integer
*) type
;
3580 BT_LOGD("Destroying integer field type object: addr=%p", type
);
3581 bt_put(integer
->mapped_clock
);
3586 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
3588 struct bt_ctf_field_type_enumeration
*enumeration
=
3589 (struct bt_ctf_field_type_enumeration
*) type
;
3595 BT_LOGD("Destroying enumeration field type object: addr=%p", type
);
3596 g_ptr_array_free(enumeration
->entries
, TRUE
);
3597 bt_put(enumeration
->container
);
3598 g_free(enumeration
);
3602 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
3604 struct bt_ctf_field_type_floating_point
*floating_point
=
3605 (struct bt_ctf_field_type_floating_point
*) type
;
3611 BT_LOGD("Destroying floating point number field type object: addr=%p", type
);
3612 g_free(floating_point
);
3616 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
3618 struct bt_ctf_field_type_structure
*structure
=
3619 (struct bt_ctf_field_type_structure
*) type
;
3625 BT_LOGD("Destroying structure field type object: addr=%p", type
);
3626 g_ptr_array_free(structure
->fields
, TRUE
);
3627 g_hash_table_destroy(structure
->field_name_to_index
);
3632 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
3634 struct bt_ctf_field_type_variant
*variant
=
3635 (struct bt_ctf_field_type_variant
*) type
;
3641 BT_LOGD("Destroying variant field type object: addr=%p", type
);
3642 g_ptr_array_free(variant
->fields
, TRUE
);
3643 g_hash_table_destroy(variant
->field_name_to_index
);
3644 g_string_free(variant
->tag_name
, TRUE
);
3645 bt_put(&variant
->tag
->parent
);
3646 BT_PUT(variant
->tag_field_path
);
3651 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
3653 struct bt_ctf_field_type_array
*array
=
3654 (struct bt_ctf_field_type_array
*) type
;
3660 BT_LOGD("Destroying array field type object: addr=%p", type
);
3661 bt_put(array
->element_type
);
3666 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
3668 struct bt_ctf_field_type_sequence
*sequence
=
3669 (struct bt_ctf_field_type_sequence
*) type
;
3675 BT_LOGD("Destroying sequence field type object: addr=%p", type
);
3676 bt_put(sequence
->element_type
);
3677 g_string_free(sequence
->length_field_name
, TRUE
);
3678 BT_PUT(sequence
->length_field_path
);
3683 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
3685 struct bt_ctf_field_type_string
*string
=
3686 (struct bt_ctf_field_type_string
*) type
;
3692 BT_LOGD("Destroying string field type object: addr=%p", type
);
3697 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
3703 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
3705 struct bt_ctf_field_type_integer
*integer_type
= container_of(
3706 type
, struct bt_ctf_field_type_integer
, parent
);
3708 BT_LOGD("Freezing integer field type object: addr=%p", type
);
3710 if (integer_type
->mapped_clock
) {
3711 bt_ctf_clock_class_freeze(integer_type
->mapped_clock
);
3714 generic_field_type_freeze(type
);
3718 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
3720 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
3721 type
, struct bt_ctf_field_type_enumeration
, parent
);
3723 BT_LOGD("Freezing enumeration field type object: addr=%p", type
);
3724 set_enumeration_range_overlap(type
);
3725 generic_field_type_freeze(type
);
3726 BT_LOGD("Freezing enumeration field type object's container field type: int-ft-addr=%p",
3727 enumeration_type
->container
);
3728 bt_ctf_field_type_freeze(enumeration_type
->container
);
3732 void freeze_structure_field(struct structure_field
*field
)
3734 BT_LOGD("Freezing structure/variant field type field: field-addr=%p, "
3735 "field-ft-addr=%p, field-name=\"%s\"", field
,
3736 field
->type
, g_quark_to_string(field
->name
));
3737 bt_ctf_field_type_freeze(field
->type
);
3741 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
3743 struct bt_ctf_field_type_structure
*structure_type
= container_of(
3744 type
, struct bt_ctf_field_type_structure
, parent
);
3746 /* Cache the alignment */
3747 BT_LOGD("Freezing structure field type object: addr=%p", type
);
3748 type
->alignment
= bt_ctf_field_type_get_alignment(type
);
3749 generic_field_type_freeze(type
);
3750 g_ptr_array_foreach(structure_type
->fields
,
3751 (GFunc
) freeze_structure_field
, NULL
);
3755 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
3757 struct bt_ctf_field_type_variant
*variant_type
= container_of(
3758 type
, struct bt_ctf_field_type_variant
, parent
);
3760 BT_LOGD("Freezing variant field type object: addr=%p", type
);
3761 generic_field_type_freeze(type
);
3762 g_ptr_array_foreach(variant_type
->fields
,
3763 (GFunc
) freeze_structure_field
, NULL
);
3767 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
3769 struct bt_ctf_field_type_array
*array_type
= container_of(
3770 type
, struct bt_ctf_field_type_array
, parent
);
3772 /* Cache the alignment */
3773 BT_LOGD("Freezing array field type object: addr=%p", type
);
3774 type
->alignment
= bt_ctf_field_type_get_alignment(type
);
3775 generic_field_type_freeze(type
);
3776 BT_LOGD("Freezing array field type object's element field type: element-ft-addr=%p",
3777 array_type
->element_type
);
3778 bt_ctf_field_type_freeze(array_type
->element_type
);
3782 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
3784 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
3785 type
, struct bt_ctf_field_type_sequence
, parent
);
3787 /* Cache the alignment */
3788 BT_LOGD("Freezing sequence field type object: addr=%p", type
);
3789 type
->alignment
= bt_ctf_field_type_get_alignment(type
);
3790 generic_field_type_freeze(type
);
3791 BT_LOGD("Freezing sequence field type object's element field type: element-ft-addr=%p",
3792 sequence_type
->element_type
);
3793 bt_ctf_field_type_freeze(sequence_type
->element_type
);
3797 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
3799 const char *encoding_string
;
3802 case BT_CTF_STRING_ENCODING_NONE
:
3803 encoding_string
= "none";
3805 case BT_CTF_STRING_ENCODING_ASCII
:
3806 encoding_string
= "ASCII";
3808 case BT_CTF_STRING_ENCODING_UTF8
:
3809 encoding_string
= "UTF8";
3812 encoding_string
= "unknown";
3816 return encoding_string
;
3820 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
3822 const char *base_string
;
3825 case BT_CTF_INTEGER_BASE_DECIMAL
:
3826 base_string
= "decimal";
3828 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
3829 base_string
= "hexadecimal";
3831 case BT_CTF_INTEGER_BASE_OCTAL
:
3832 base_string
= "octal";
3834 case BT_CTF_INTEGER_BASE_BINARY
:
3835 base_string
= "binary";
3838 base_string
= "unknown";
3846 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
3847 struct metadata_context
*context
)
3849 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
3850 struct bt_ctf_field_type_integer
, parent
);
3853 BT_LOGD("Serializing integer field type's metadata: "
3854 "ft-addr=%p, metadata-context-addr=%p", type
, context
);
3855 g_string_append_printf(context
->string
,
3856 "integer { size = %u; align = %u; signed = %s; encoding = %s; base = %s; byte_order = %s",
3857 integer
->size
, type
->alignment
,
3858 (integer
->is_signed
? "true" : "false"),
3859 get_encoding_string(integer
->encoding
),
3860 get_integer_base_string(integer
->base
),
3861 get_byte_order_string(integer
->user_byte_order
));
3862 if (integer
->mapped_clock
) {
3863 const char *clock_name
= bt_ctf_clock_class_get_name(
3864 integer
->mapped_clock
);
3867 g_string_append_printf(context
->string
,
3868 "; map = clock.%s.value", clock_name
);
3871 g_string_append(context
->string
, "; }");
3876 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
3877 struct metadata_context
*context
)
3881 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
3882 struct bt_ctf_field_type_enumeration
, parent
);
3883 struct bt_ctf_field_type
*container_type
;
3884 int container_signed
;
3886 BT_LOGD("Serializing enumeration field type's metadata: "
3887 "ft-addr=%p, metadata-context-addr=%p", type
, context
);
3888 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
3889 assert(container_type
);
3890 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
3891 assert(container_signed
>= 0);
3892 g_string_append(context
->string
, "enum : ");
3893 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
3895 BT_LOGW("Cannot serialize enumeration field type's container field type's metadata: "
3896 "container-ft-addr=%p", enumeration
->container
);
3900 g_string_append(context
->string
, " { ");
3901 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
3902 struct enumeration_mapping
*mapping
=
3903 enumeration
->entries
->pdata
[entry
];
3905 if (container_signed
) {
3906 if (mapping
->range_start
._signed
==
3907 mapping
->range_end
._signed
) {
3908 g_string_append_printf(context
->string
,
3909 "\"%s\" = %" PRId64
,
3910 g_quark_to_string(mapping
->string
),
3911 mapping
->range_start
._signed
);
3913 g_string_append_printf(context
->string
,
3914 "\"%s\" = %" PRId64
" ... %" PRId64
,
3915 g_quark_to_string(mapping
->string
),
3916 mapping
->range_start
._signed
,
3917 mapping
->range_end
._signed
);
3920 if (mapping
->range_start
._unsigned
==
3921 mapping
->range_end
._unsigned
) {
3922 g_string_append_printf(context
->string
,
3923 "\"%s\" = %" PRIu64
,
3924 g_quark_to_string(mapping
->string
),
3925 mapping
->range_start
._unsigned
);
3927 g_string_append_printf(context
->string
,
3928 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
3929 g_quark_to_string(mapping
->string
),
3930 mapping
->range_start
._unsigned
,
3931 mapping
->range_end
._unsigned
);
3935 g_string_append(context
->string
,
3936 ((entry
!= (enumeration
->entries
->len
- 1)) ?
3940 if (context
->field_name
->len
) {
3941 g_string_append_printf(context
->string
, " %s",
3942 context
->field_name
->str
);
3943 g_string_assign(context
->field_name
, "");
3946 bt_put(container_type
);
3951 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
3952 struct metadata_context
*context
)
3954 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
3955 type
, struct bt_ctf_field_type_floating_point
, parent
);
3957 BT_LOGD("Serializing floating point number field type's metadata: "
3958 "ft-addr=%p, metadata-context-addr=%p", type
, context
);
3959 g_string_append_printf(context
->string
,
3960 "floating_point { exp_dig = %u; mant_dig = %u; byte_order = %s; align = %u; }",
3961 floating_point
->exp_dig
,
3962 floating_point
->mant_dig
,
3963 get_byte_order_string(floating_point
->user_byte_order
),
3969 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3970 struct metadata_context
*context
)
3973 unsigned int indent
;
3975 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3976 struct bt_ctf_field_type_structure
, parent
);
3977 GString
*structure_field_name
= context
->field_name
;
3979 BT_LOGD("Serializing structure field type's metadata: "
3980 "ft-addr=%p, metadata-context-addr=%p", type
, context
);
3981 context
->field_name
= g_string_new("");
3983 context
->current_indentation_level
++;
3984 g_string_append(context
->string
, "struct {\n");
3986 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3987 struct structure_field
*field
= structure
->fields
->pdata
[i
];
3989 BT_LOGV("Serializing structure field type's field metadata: "
3990 "index=%" PRId64
", "
3991 "field-ft-addr=%p, field-name=\"%s\"",
3992 i
, field
, g_quark_to_string(field
->name
));
3994 for (indent
= 0; indent
< context
->current_indentation_level
;
3996 g_string_append_c(context
->string
, '\t');
3999 g_string_assign(context
->field_name
,
4000 g_quark_to_string(field
->name
));
4001 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
4003 BT_LOGW("Cannot serialize structure field type's field's metadata: "
4004 "index=%" PRId64
", "
4005 "field-ft-addr=%p, field-name=\"%s\"",
4007 g_quark_to_string(field
->name
));
4011 if (context
->field_name
->len
) {
4012 g_string_append_printf(context
->string
, " %s",
4013 context
->field_name
->str
);
4015 g_string_append(context
->string
, ";\n");
4018 context
->current_indentation_level
--;
4019 for (indent
= 0; indent
< context
->current_indentation_level
;
4021 g_string_append_c(context
->string
, '\t');
4024 g_string_append_printf(context
->string
, "} align(%u)",
4027 g_string_free(context
->field_name
, TRUE
);
4028 context
->field_name
= structure_field_name
;
4033 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
4034 struct metadata_context
*context
)
4037 unsigned int indent
;
4039 struct bt_ctf_field_type_variant
*variant
= container_of(
4040 type
, struct bt_ctf_field_type_variant
, parent
);
4041 GString
*variant_field_name
= context
->field_name
;
4043 BT_LOGD("Serializing variant field type's metadata: "
4044 "ft-addr=%p, metadata-context-addr=%p", type
, context
);
4045 context
->field_name
= g_string_new("");
4046 if (variant
->tag_name
->len
> 0) {
4047 g_string_append_printf(context
->string
,
4048 "variant <%s> {\n", variant
->tag_name
->str
);
4050 g_string_append(context
->string
, "variant {\n");
4053 context
->current_indentation_level
++;
4054 for (i
= 0; i
< variant
->fields
->len
; i
++) {
4055 struct structure_field
*field
= variant
->fields
->pdata
[i
];
4057 BT_LOGV("Serializing variant field type's field metadata: "
4058 "index=%" PRId64
", "
4059 "field-ft-addr=%p, field-name=\"%s\"",
4060 i
, field
, g_quark_to_string(field
->name
));
4062 g_string_assign(context
->field_name
,
4063 g_quark_to_string(field
->name
));
4064 for (indent
= 0; indent
< context
->current_indentation_level
;
4066 g_string_append_c(context
->string
, '\t');
4069 g_string_assign(context
->field_name
,
4070 g_quark_to_string(field
->name
));
4071 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
4073 BT_LOGW("Cannot serialize variant field type's field's metadata: "
4074 "index=%" PRId64
", "
4075 "field-ft-addr=%p, field-name=\"%s\"",
4077 g_quark_to_string(field
->name
));
4081 if (context
->field_name
->len
) {
4082 g_string_append_printf(context
->string
, " %s;",
4083 context
->field_name
->str
);
4086 g_string_append_c(context
->string
, '\n');
4089 context
->current_indentation_level
--;
4090 for (indent
= 0; indent
< context
->current_indentation_level
;
4092 g_string_append_c(context
->string
, '\t');
4095 g_string_append(context
->string
, "}");
4097 g_string_free(context
->field_name
, TRUE
);
4098 context
->field_name
= variant_field_name
;
4103 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
4104 struct metadata_context
*context
)
4107 struct bt_ctf_field_type_array
*array
= container_of(type
,
4108 struct bt_ctf_field_type_array
, parent
);
4110 BT_LOGD("Serializing array field type's metadata: "
4111 "ft-addr=%p, metadata-context-addr=%p", type
, context
);
4112 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
4114 BT_LOGW("Cannot serialize array field type's element field type's metadata: "
4115 "element-ft-addr=%p", array
->element_type
);
4119 if (context
->field_name
->len
) {
4120 g_string_append_printf(context
->string
, " %s[%u]",
4121 context
->field_name
->str
, array
->length
);
4122 g_string_assign(context
->field_name
, "");
4124 g_string_append_printf(context
->string
, "[%u]", array
->length
);
4131 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
4132 struct metadata_context
*context
)
4135 struct bt_ctf_field_type_sequence
*sequence
= container_of(
4136 type
, struct bt_ctf_field_type_sequence
, parent
);
4138 BT_LOGD("Serializing sequence field type's metadata: "
4139 "ft-addr=%p, metadata-context-addr=%p", type
, context
);
4140 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
4142 BT_LOGW("Cannot serialize sequence field type's element field type's metadata: "
4143 "element-ft-addr=%p", sequence
->element_type
);
4147 if (context
->field_name
->len
) {
4148 g_string_append_printf(context
->string
, " %s[%s]",
4149 context
->field_name
->str
,
4150 sequence
->length_field_name
->str
);
4151 g_string_assign(context
->field_name
, "");
4153 g_string_append_printf(context
->string
, "[%s]",
4154 sequence
->length_field_name
->str
);
4161 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
4162 struct metadata_context
*context
)
4164 struct bt_ctf_field_type_string
*string
= container_of(
4165 type
, struct bt_ctf_field_type_string
, parent
);
4167 BT_LOGD("Serializing string field type's metadata: "
4168 "ft-addr=%p, metadata-context-addr=%p", type
, context
);
4169 g_string_append_printf(context
->string
,
4170 "string { encoding = %s; }",
4171 get_encoding_string(string
->encoding
));
4176 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
4177 enum bt_ctf_byte_order byte_order
)
4179 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
4180 struct bt_ctf_field_type_integer
, parent
);
4182 integer_type
->user_byte_order
= byte_order
;
4186 void bt_ctf_field_type_enumeration_set_byte_order(
4187 struct bt_ctf_field_type
*type
, enum bt_ctf_byte_order byte_order
)
4189 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
4190 struct bt_ctf_field_type_enumeration
, parent
);
4192 /* Safe to assume that container is an integer */
4193 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
4198 void bt_ctf_field_type_floating_point_set_byte_order(
4199 struct bt_ctf_field_type
*type
, enum bt_ctf_byte_order byte_order
)
4201 struct bt_ctf_field_type_floating_point
*floating_point_type
=
4202 container_of(type
, struct bt_ctf_field_type_floating_point
,
4205 floating_point_type
->user_byte_order
= byte_order
;
4209 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
4210 enum bt_ctf_byte_order byte_order
)
4213 struct bt_ctf_field_type_structure
*structure_type
=
4214 container_of(type
, struct bt_ctf_field_type_structure
,
4217 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
4218 struct structure_field
*field
= g_ptr_array_index(
4219 structure_type
->fields
, i
);
4220 struct bt_ctf_field_type
*field_type
= field
->type
;
4222 if (set_byte_order_funcs
[field_type
->id
]) {
4223 set_byte_order_funcs
[field_type
->id
](
4224 field_type
, byte_order
);
4230 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
4231 enum bt_ctf_byte_order byte_order
)
4234 struct bt_ctf_field_type_variant
*variant_type
=
4235 container_of(type
, struct bt_ctf_field_type_variant
,
4238 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
4239 struct structure_field
*field
= g_ptr_array_index(
4240 variant_type
->fields
, i
);
4241 struct bt_ctf_field_type
*field_type
= field
->type
;
4243 if (set_byte_order_funcs
[field_type
->id
]) {
4244 set_byte_order_funcs
[field_type
->id
](
4245 field_type
, byte_order
);
4251 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
4252 enum bt_ctf_byte_order byte_order
)
4254 struct bt_ctf_field_type_array
*array_type
=
4255 container_of(type
, struct bt_ctf_field_type_array
,
4258 if (set_byte_order_funcs
[array_type
->element_type
->id
]) {
4259 set_byte_order_funcs
[array_type
->element_type
->id
](
4260 array_type
->element_type
, byte_order
);
4265 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
4266 enum bt_ctf_byte_order byte_order
)
4268 struct bt_ctf_field_type_sequence
*sequence_type
=
4269 container_of(type
, struct bt_ctf_field_type_sequence
,
4272 if (set_byte_order_funcs
[
4273 sequence_type
->element_type
->id
]) {
4274 set_byte_order_funcs
[
4275 sequence_type
->element_type
->id
](
4276 sequence_type
->element_type
, byte_order
);
4281 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
4282 struct bt_ctf_field_type
*type
)
4284 struct bt_ctf_field_type
*copy
;
4285 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
4287 BT_LOGD("Copying integer field type's: addr=%p", type
);
4288 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
4289 copy
= bt_ctf_field_type_integer_create(integer
->size
);
4291 BT_LOGE_STR("Cannot create integer field type.");
4295 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
4297 copy_integer
->mapped_clock
= bt_get(integer
->mapped_clock
);
4298 copy_integer
->user_byte_order
= integer
->user_byte_order
;
4299 copy_integer
->is_signed
= integer
->is_signed
;
4300 copy_integer
->size
= integer
->size
;
4301 copy_integer
->base
= integer
->base
;
4302 copy_integer
->encoding
= integer
->encoding
;
4303 BT_LOGD("Copied integer field type: original-ft-addr=%p, copy-ft-addr=%p",
4311 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
4312 struct bt_ctf_field_type
*type
)
4315 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
4316 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
4318 BT_LOGD("Copying enumeration field type's: addr=%p", type
);
4319 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
4322 /* Copy the source enumeration's container */
4323 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
4324 if (!copy_container
) {
4325 BT_LOGE_STR("Cannot copy enumeration field type's container field type.");
4329 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
4331 BT_LOGE_STR("Cannot create enumeration field type.");
4334 copy_enumeration
= container_of(copy
,
4335 struct bt_ctf_field_type_enumeration
, parent
);
4337 /* Copy all enumaration entries */
4338 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
4339 struct enumeration_mapping
*mapping
= g_ptr_array_index(
4340 enumeration
->entries
, i
);
4341 struct enumeration_mapping
*copy_mapping
= g_new0(
4342 struct enumeration_mapping
, 1);
4344 if (!copy_mapping
) {
4345 BT_LOGE_STR("Failed to allocate one enumeration mapping.");
4349 *copy_mapping
= *mapping
;
4350 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
4353 BT_LOGD("Copied enumeration field type: original-ft-addr=%p, copy-ft-addr=%p",
4357 bt_put(copy_container
);
4360 bt_put(copy_container
);
4366 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
4367 struct bt_ctf_field_type
*type
)
4369 struct bt_ctf_field_type
*copy
;
4370 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
4372 BT_LOGD("Copying floating point number field type's: addr=%p", type
);
4373 floating_point
= container_of(type
,
4374 struct bt_ctf_field_type_floating_point
, parent
);
4375 copy
= bt_ctf_field_type_floating_point_create();
4377 BT_LOGE_STR("Cannot create floating point number field type.");
4381 copy_float
= container_of(copy
,
4382 struct bt_ctf_field_type_floating_point
, parent
);
4383 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
4384 copy_float
->exp_dig
= floating_point
->exp_dig
;
4385 copy_float
->mant_dig
= floating_point
->mant_dig
;
4386 BT_LOGD("Copied floating point number field type: original-ft-addr=%p, copy-ft-addr=%p",
4393 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
4394 struct bt_ctf_field_type
*type
)
4397 GHashTableIter iter
;
4398 gpointer key
, value
;
4399 struct bt_ctf_field_type
*copy
;
4400 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
4402 BT_LOGD("Copying structure field type's: addr=%p", type
);
4403 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
4405 copy
= bt_ctf_field_type_structure_create();
4407 BT_LOGE_STR("Cannot create structure field type.");
4411 copy_structure
= container_of(copy
,
4412 struct bt_ctf_field_type_structure
, parent
);
4414 /* Copy field_name_to_index */
4415 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
4416 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
4417 g_hash_table_insert(copy_structure
->field_name_to_index
,
4421 for (i
= 0; i
< structure
->fields
->len
; i
++) {
4422 struct structure_field
*entry
, *copy_entry
;
4423 struct bt_ctf_field_type
*copy_field
;
4425 entry
= g_ptr_array_index(structure
->fields
, i
);
4426 BT_LOGV("Copying structure field type's field: "
4427 "index=%" PRId64
", "
4428 "field-ft-addr=%p, field-name=\"%s\"",
4429 i
, entry
, g_quark_to_string(entry
->name
));
4430 copy_entry
= g_new0(struct structure_field
, 1);
4432 BT_LOGE_STR("Failed to allocate one structure field type field.");
4436 copy_field
= bt_ctf_field_type_copy(entry
->type
);
4438 BT_LOGE("Cannot copy structure field type's field: "
4439 "index=%" PRId64
", "
4440 "field-ft-addr=%p, field-name=\"%s\"",
4441 i
, entry
, g_quark_to_string(entry
->name
));
4446 copy_entry
->name
= entry
->name
;
4447 copy_entry
->type
= copy_field
;
4448 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
4451 BT_LOGD("Copied structure field type: original-ft-addr=%p, copy-ft-addr=%p",
4462 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
4463 struct bt_ctf_field_type
*type
)
4466 GHashTableIter iter
;
4467 gpointer key
, value
;
4468 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
4469 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
4471 BT_LOGD("Copying variant field type's: addr=%p", type
);
4472 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4475 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
4477 BT_LOGE_STR("Cannot copy variant field type's tag field type.");
4482 copy
= bt_ctf_field_type_variant_create(copy_tag
,
4483 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
4485 BT_LOGE_STR("Cannot create variant field type.");
4489 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
4492 /* Copy field_name_to_index */
4493 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
4494 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
4495 g_hash_table_insert(copy_variant
->field_name_to_index
,
4499 for (i
= 0; i
< variant
->fields
->len
; i
++) {
4500 struct structure_field
*entry
, *copy_entry
;
4501 struct bt_ctf_field_type
*copy_field
;
4503 entry
= g_ptr_array_index(variant
->fields
, i
);
4504 BT_LOGV("Copying variant field type's field: "
4505 "index=%" PRId64
", "
4506 "field-ft-addr=%p, field-name=\"%s\"",
4507 i
, entry
, g_quark_to_string(entry
->name
));
4508 copy_entry
= g_new0(struct structure_field
, 1);
4510 BT_LOGE_STR("Failed to allocate one variant field type field.");
4514 copy_field
= bt_ctf_field_type_copy(entry
->type
);
4516 BT_LOGE("Cannot copy variant field type's field: "
4517 "index=%" PRId64
", "
4518 "field-ft-addr=%p, field-name=\"%s\"",
4519 i
, entry
, g_quark_to_string(entry
->name
));
4524 copy_entry
->name
= entry
->name
;
4525 copy_entry
->type
= copy_field
;
4526 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
4529 if (variant
->tag_field_path
) {
4530 BT_LOGV_STR("Copying variant field type's tag field path.");
4531 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
4532 variant
->tag_field_path
);
4533 if (!copy_variant
->tag_field_path
) {
4534 BT_LOGE_STR("Cannot copy variant field type's tag field path.");
4539 BT_LOGD("Copied variant field type: original-ft-addr=%p, copy-ft-addr=%p",
4552 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
4553 struct bt_ctf_field_type
*type
)
4555 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
4556 struct bt_ctf_field_type_array
*array
;
4558 BT_LOGD("Copying array field type's: addr=%p", type
);
4559 array
= container_of(type
, struct bt_ctf_field_type_array
,
4561 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
4562 if (!copy_element
) {
4563 BT_LOGE_STR("Cannot copy array field type's element field type.");
4567 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
4569 BT_LOGE_STR("Cannot create array field type.");
4573 BT_LOGD("Copied array field type: original-ft-addr=%p, copy-ft-addr=%p",
4577 bt_put(copy_element
);
4582 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
4583 struct bt_ctf_field_type
*type
)
4585 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
4586 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
4588 BT_LOGD("Copying sequence field type's: addr=%p", type
);
4589 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4591 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
4592 if (!copy_element
) {
4593 BT_LOGE_STR("Cannot copy sequence field type's element field type.");
4597 copy
= bt_ctf_field_type_sequence_create(copy_element
,
4598 sequence
->length_field_name
->len
?
4599 sequence
->length_field_name
->str
: NULL
);
4601 BT_LOGE_STR("Cannot create sequence field type.");
4605 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
4607 if (sequence
->length_field_path
) {
4608 BT_LOGV_STR("Copying sequence field type's length field path.");
4609 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
4610 sequence
->length_field_path
);
4611 if (!copy_sequence
->length_field_path
) {
4612 BT_LOGE_STR("Cannot copy sequence field type's length field path.");
4617 BT_LOGD("Copied sequence field type: original-ft-addr=%p, copy-ft-addr=%p",
4621 bt_put(copy_element
);
4629 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
4630 struct bt_ctf_field_type
*type
)
4632 struct bt_ctf_field_type
*copy
;
4633 struct bt_ctf_field_type_string
*string
;
4635 BT_LOGD("Copying string field type's: addr=%p", type
);
4636 copy
= bt_ctf_field_type_string_create();
4638 BT_LOGE_STR("Cannot create string field type.");
4642 string
= container_of(type
, struct bt_ctf_field_type_string
,
4644 BT_LOGD("Copied string field type: original-ft-addr=%p, copy-ft-addr=%p",
4651 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
4652 struct bt_ctf_field_type
*type_b
)
4655 struct bt_ctf_field_type_integer
*int_type_a
;
4656 struct bt_ctf_field_type_integer
*int_type_b
;
4658 int_type_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
4660 int_type_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
4664 if (int_type_a
->size
!= int_type_b
->size
) {
4665 BT_LOGV("Integer field types differ: different sizes: "
4666 "ft-a-size=%u, ft-b-size=%u",
4667 int_type_a
->size
, int_type_b
->size
);
4672 if (int_type_a
->user_byte_order
!= int_type_b
->user_byte_order
) {
4673 BT_LOGV("Integer field types differ: different byte orders: "
4674 "ft-a-bo=%s, ft-b-bo=%s",
4675 bt_ctf_byte_order_string(int_type_a
->user_byte_order
),
4676 bt_ctf_byte_order_string(int_type_b
->user_byte_order
));
4681 if (int_type_a
->is_signed
!= int_type_b
->is_signed
) {
4682 BT_LOGV("Integer field types differ: different signedness: "
4683 "ft-a-is-signed=%d, ft-b-is-signed=%d",
4684 int_type_a
->is_signed
,
4685 int_type_b
->is_signed
);
4690 if (int_type_a
->base
!= int_type_b
->base
) {
4691 BT_LOGV("Integer field types differ: different bases: "
4692 "ft-a-base=%s, ft-b-base=%s",
4693 bt_ctf_integer_base_string(int_type_a
->base
),
4694 bt_ctf_integer_base_string(int_type_b
->base
));
4699 if (int_type_a
->encoding
!= int_type_b
->encoding
) {
4700 BT_LOGV("Integer field types differ: different encodings: "
4701 "ft-a-encoding=%s, ft-b-encoding=%s",
4702 bt_ctf_string_encoding_string(int_type_a
->encoding
),
4703 bt_ctf_string_encoding_string(int_type_b
->encoding
));
4708 if (int_type_a
->mapped_clock
!= int_type_b
->mapped_clock
) {
4709 BT_LOGV("Integer field types differ: different mapped clock classes: "
4710 "ft-a-mapped-clock-class-addr=%p, "
4711 "ft-b-mapped-clock-class-addr=%p, ",
4712 "ft-a-mapped-clock-class-name=\"%s\", ",
4713 "ft-b-mapped-clock-class-name=\"%s\"",
4714 int_type_a
->mapped_clock
, int_type_b
->mapped_clock
,
4715 int_type_a
->mapped_clock
? bt_ctf_clock_class_get_name(int_type_a
->mapped_clock
) : "",
4716 int_type_b
->mapped_clock
? bt_ctf_clock_class_get_name(int_type_b
->mapped_clock
) : "");
4728 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
4729 struct bt_ctf_field_type
*type_b
)
4732 struct bt_ctf_field_type_floating_point
*float_a
;
4733 struct bt_ctf_field_type_floating_point
*float_b
;
4735 float_a
= container_of(type_a
,
4736 struct bt_ctf_field_type_floating_point
, parent
);
4737 float_b
= container_of(type_b
,
4738 struct bt_ctf_field_type_floating_point
, parent
);
4741 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
4742 BT_LOGV("Floating point number field types differ: different byte orders: "
4743 "ft-a-bo=%s, ft-b-bo=%s",
4744 bt_ctf_byte_order_string(float_a
->user_byte_order
),
4745 bt_ctf_byte_order_string(float_b
->user_byte_order
));
4749 /* Exponent length */
4750 if (float_a
->exp_dig
!= float_b
->exp_dig
) {
4751 BT_LOGV("Floating point number field types differ: different exponent sizes: "
4752 "ft-a-exp-size=%s, ft-b-exp-size=%s",
4753 float_a
->exp_dig
, float_b
->exp_dig
);
4757 /* Mantissa length */
4758 if (float_a
->mant_dig
!= float_b
->mant_dig
) {
4759 BT_LOGV("Floating point number field types differ: different mantissa sizes: "
4760 "ft-a-mant-size=%s, ft-b-mant-size=%s",
4761 float_a
->mant_dig
, float_b
->mant_dig
);
4773 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
4774 struct enumeration_mapping
*mapping_b
)
4779 if (mapping_a
->string
!= mapping_b
->string
) {
4780 BT_LOGV("Enumeration field type mappings differ: different names: "
4781 "mapping-a-name=\"%s\", mapping-b-name=\"%s\"",
4782 g_quark_to_string(mapping_a
->string
),
4783 g_quark_to_string(mapping_b
->string
));
4788 if (mapping_a
->range_start
._unsigned
!=
4789 mapping_b
->range_start
._unsigned
) {
4790 BT_LOGV("Enumeration field type mappings differ: different starts of range: "
4791 "mapping-a-range-start-unsigned=%" PRIu64
", "
4792 "mapping-b-range-start-unsigned=%" PRIu64
,
4793 mapping_a
->range_start
._unsigned
,
4794 mapping_b
->range_start
._unsigned
);
4799 if (mapping_a
->range_end
._unsigned
!=
4800 mapping_b
->range_end
._unsigned
) {
4801 BT_LOGV("Enumeration field type mappings differ: different ends of range: "
4802 "mapping-a-range-end-unsigned=%" PRIu64
", "
4803 "mapping-b-range-end-unsigned=%" PRIu64
,
4804 mapping_a
->range_end
._unsigned
,
4805 mapping_b
->range_end
._unsigned
);
4817 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
4818 struct bt_ctf_field_type
*type_b
)
4822 struct bt_ctf_field_type_enumeration
*enum_a
;
4823 struct bt_ctf_field_type_enumeration
*enum_b
;
4825 enum_a
= container_of(type_a
,
4826 struct bt_ctf_field_type_enumeration
, parent
);
4827 enum_b
= container_of(type_b
,
4828 struct bt_ctf_field_type_enumeration
, parent
);
4830 /* Container field type */
4831 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
4833 BT_LOGV("Enumeration field types differ: different container field types: "
4834 "ft-a-container-ft-addr=%p, ft-b-container-ft-addr=%p",
4835 enum_a
->container
, enum_b
->container
);
4842 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
4846 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
4847 struct enumeration_mapping
*mapping_a
=
4848 g_ptr_array_index(enum_a
->entries
, i
);
4849 struct enumeration_mapping
*mapping_b
=
4850 g_ptr_array_index(enum_b
->entries
, i
);
4852 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
4853 BT_LOGV("Enumeration field types differ: different mappings: "
4854 "ft-a-mapping-addr=%p, ft-b-mapping-addr=%p, "
4855 "ft-a-mapping-name=\"%s\", ft-b-mapping-name=\"%s\"",
4856 mapping_a
, mapping_b
,
4857 g_quark_to_string(mapping_a
->string
),
4858 g_quark_to_string(mapping_b
->string
));
4871 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
4872 struct bt_ctf_field_type
*type_b
)
4875 struct bt_ctf_field_type_string
*string_a
;
4876 struct bt_ctf_field_type_string
*string_b
;
4878 string_a
= container_of(type_a
,
4879 struct bt_ctf_field_type_string
, parent
);
4880 string_b
= container_of(type_b
,
4881 struct bt_ctf_field_type_string
, parent
);
4884 if (string_a
->encoding
!= string_b
->encoding
) {
4885 BT_LOGV("String field types differ: different encodings: "
4886 "ft-a-encoding=%s, ft-b-encoding=%s",
4887 bt_ctf_string_encoding_string(string_a
->encoding
),
4888 bt_ctf_string_encoding_string(string_b
->encoding
));
4900 int compare_structure_fields(struct structure_field
*field_a
,
4901 struct structure_field
*field_b
)
4906 if (field_a
->name
!= field_b
->name
) {
4907 BT_LOGV("Structure/variant field type fields differ: different names: "
4908 "field-a-name=%s, field-b-name=%s",
4909 g_quark_to_string(field_a
->name
),
4910 g_quark_to_string(field_b
->name
));
4915 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
4917 BT_LOGV("Structure/variant field type fields differ: different field types: "
4918 "field-name=\"%s\", field-a-ft-addr=%s, field-b-ft-addr=%s",
4919 g_quark_to_string(field_a
->name
),
4920 field_a
->type
, field_b
->type
);
4928 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
4929 struct bt_ctf_field_type
*type_b
)
4933 struct bt_ctf_field_type_structure
*struct_a
;
4934 struct bt_ctf_field_type_structure
*struct_b
;
4936 struct_a
= container_of(type_a
,
4937 struct bt_ctf_field_type_structure
, parent
);
4938 struct_b
= container_of(type_b
,
4939 struct bt_ctf_field_type_structure
, parent
);
4942 if (bt_ctf_field_type_get_alignment(type_a
) !=
4943 bt_ctf_field_type_get_alignment(type_b
)) {
4944 BT_LOGV("Structure field types differ: different alignments: "
4945 "ft-a-align=%u, ft-b-align=%u",
4946 bt_ctf_field_type_get_alignment(type_a
),
4947 bt_ctf_field_type_get_alignment(type_b
));
4952 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
4953 BT_LOGV("Structure field types differ: different field counts: "
4954 "ft-a-field-count=%u, ft-b-field-count=%u",
4955 struct_a
->fields
->len
, struct_b
->fields
->len
);
4959 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
4960 struct structure_field
*field_a
=
4961 g_ptr_array_index(struct_a
->fields
, i
);
4962 struct structure_field
*field_b
=
4963 g_ptr_array_index(struct_b
->fields
, i
);
4965 ret
= compare_structure_fields(field_a
, field_b
);
4967 /* compare_structure_fields() logs what differs */
4968 BT_LOGV_STR("Structure field types differ: different fields.");
4981 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
4982 struct bt_ctf_field_type
*type_b
)
4986 struct bt_ctf_field_type_variant
*variant_a
;
4987 struct bt_ctf_field_type_variant
*variant_b
;
4989 variant_a
= container_of(type_a
,
4990 struct bt_ctf_field_type_variant
, parent
);
4991 variant_b
= container_of(type_b
,
4992 struct bt_ctf_field_type_variant
, parent
);
4995 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
4996 BT_LOGV("Variant field types differ: different tag field names: "
4997 "ft-a-tag-field-name=%u, ft-b-tag-field-name=%u",
4998 variant_a
->tag_name
->str
, variant_b
->tag_name
->str
);
5003 ret
= bt_ctf_field_type_compare(
5004 (struct bt_ctf_field_type
*) variant_a
->tag
,
5005 (struct bt_ctf_field_type
*) variant_b
->tag
);
5007 BT_LOGV("Variant field types differ: different tag field types: "
5008 "ft-a-tag-ft-addr=%p, ft-b-tag-ft-addr=%p",
5009 variant_a
->tag
, variant_b
->tag
);
5016 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
5017 BT_LOGV("Structure field types differ: different field counts: "
5018 "ft-a-field-count=%u, ft-b-field-count=%u",
5019 variant_a
->fields
->len
, variant_b
->fields
->len
);
5023 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
5024 struct structure_field
*field_a
=
5025 g_ptr_array_index(variant_a
->fields
, i
);
5026 struct structure_field
*field_b
=
5027 g_ptr_array_index(variant_b
->fields
, i
);
5029 ret
= compare_structure_fields(field_a
, field_b
);
5031 /* compare_structure_fields() logs what differs */
5032 BT_LOGV_STR("Variant field types differ: different fields.");
5045 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
5046 struct bt_ctf_field_type
*type_b
)
5049 struct bt_ctf_field_type_array
*array_a
;
5050 struct bt_ctf_field_type_array
*array_b
;
5052 array_a
= container_of(type_a
,
5053 struct bt_ctf_field_type_array
, parent
);
5054 array_b
= container_of(type_b
,
5055 struct bt_ctf_field_type_array
, parent
);
5058 if (array_a
->length
!= array_b
->length
) {
5059 BT_LOGV("Structure field types differ: different lengths: "
5060 "ft-a-length=%u, ft-b-length=%u",
5061 array_a
->length
, array_b
->length
);
5066 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
5067 array_b
->element_type
);
5069 BT_LOGV("Array field types differ: different element field types: "
5070 "ft-a-element-ft-addr=%p, ft-b-element-ft-addr=%p",
5071 array_a
->element_type
, array_b
->element_type
);
5079 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
5080 struct bt_ctf_field_type
*type_b
)
5083 struct bt_ctf_field_type_sequence
*sequence_a
;
5084 struct bt_ctf_field_type_sequence
*sequence_b
;
5086 sequence_a
= container_of(type_a
,
5087 struct bt_ctf_field_type_sequence
, parent
);
5088 sequence_b
= container_of(type_b
,
5089 struct bt_ctf_field_type_sequence
, parent
);
5092 if (strcmp(sequence_a
->length_field_name
->str
,
5093 sequence_b
->length_field_name
->str
)) {
5094 BT_LOGV("Sequence field types differ: different length field names: "
5095 "ft-a-length-field-name=%u, ft-b-length-field-name=%u",
5096 sequence_a
->length_field_name
->str
,
5097 sequence_b
->length_field_name
->str
);
5102 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
5103 sequence_b
->element_type
);
5105 BT_LOGV("Sequence field types differ: different element field types: "
5106 "ft-a-element-ft-addr=%p, ft-b-element-ft-addr=%p",
5107 sequence_a
->element_type
, sequence_b
->element_type
);
5114 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
5115 struct bt_ctf_field_type
*type_b
)
5119 if (type_a
== type_b
) {
5120 /* Same reference: equal (even if both are NULL) */
5126 BT_LOGW_STR("Invalid parameter: field type A is NULL.");
5132 BT_LOGW_STR("Invalid parameter: field type B is NULL.");
5137 if (type_a
->id
!= type_b
->id
) {
5138 /* Different type IDs */
5139 BT_LOGV("Field types differ: different IDs: "
5140 "ft-a-addr=%p, ft-b-addr=%p, "
5141 "ft-a-id=%s, ft-b-id=%s",
5143 bt_ctf_field_type_id_string(type_a
->id
),
5144 bt_ctf_field_type_id_string(type_b
->id
));
5148 if (type_a
->id
== BT_CTF_FIELD_TYPE_ID_UNKNOWN
) {
5149 /* Both have unknown type IDs */
5150 BT_LOGW_STR("Invalid parameter: field type IDs are unknown.");
5154 ret
= type_compare_funcs
[type_a
->id
](type_a
, type_b
);
5156 BT_LOGV("Field types differ: ft-a-addr=%p, ft-b-addr=%p",
5165 int64_t bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
5167 int64_t field_count
= -1;
5168 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
5171 case CTF_TYPE_STRUCT
:
5173 bt_ctf_field_type_structure_get_field_count(field_type
);
5175 case CTF_TYPE_VARIANT
:
5177 bt_ctf_field_type_variant_get_field_count(field_type
);
5179 case CTF_TYPE_ARRAY
:
5180 case CTF_TYPE_SEQUENCE
:
5182 * Array and sequence types always contain a single member
5183 * (the element type).
5195 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
5196 struct bt_ctf_field_type
*field_type
, int index
)
5198 struct bt_ctf_field_type
*field
= NULL
;
5199 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
5202 case CTF_TYPE_STRUCT
:
5203 bt_ctf_field_type_structure_get_field_by_index(field_type
,
5204 NULL
, &field
, index
);
5206 case CTF_TYPE_VARIANT
:
5208 int ret
= bt_ctf_field_type_variant_get_field_by_index(
5209 field_type
, NULL
, &field
, index
);
5216 case CTF_TYPE_ARRAY
:
5217 field
= bt_ctf_field_type_array_get_element_type(field_type
);
5219 case CTF_TYPE_SEQUENCE
:
5220 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
5230 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
5233 int field_index
= -1;
5234 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
5237 case CTF_TYPE_STRUCT
:
5238 field_index
= bt_ctf_field_type_structure_get_field_name_index(
5241 case CTF_TYPE_VARIANT
:
5242 field_index
= bt_ctf_field_type_variant_get_field_name_index(
5252 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
5253 struct bt_ctf_field_type
*type
)
5255 struct bt_ctf_field_path
*field_path
= NULL
;
5256 struct bt_ctf_field_type_variant
*variant
;
5259 BT_LOGW_STR("Invalid parameter: field type is NULL.");
5263 if (!bt_ctf_field_type_is_variant(type
)) {
5264 BT_LOGW("Invalid parameter: field type is not a variant field type: "
5265 "addr=%p, ft-id=%s", type
,
5266 bt_ctf_field_type_id_string(type
->id
));
5270 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
5272 field_path
= bt_get(variant
->tag_field_path
);
5277 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
5278 struct bt_ctf_field_type
*type
)
5280 struct bt_ctf_field_path
*field_path
= NULL
;
5281 struct bt_ctf_field_type_sequence
*sequence
;
5284 BT_LOGW_STR("Invalid parameter: field type is NULL.");
5288 if (!bt_ctf_field_type_is_sequence(type
)) {
5289 BT_LOGW("Invalid parameter: field type is not a sequence field type: "
5290 "addr=%p, ft-id=%s", type
,
5291 bt_ctf_field_type_id_string(type
->id
));
5295 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
5297 field_path
= bt_get(sequence
->length_field_path
);