1 #ifndef BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H
2 #define BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H
5 * BabelTrace - CTF IR: Event field types internal
7 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <babeltrace/ctf-writer/event-types.h>
31 #include <babeltrace/ctf-writer/event-fields.h>
32 #include <babeltrace/ctf-writer/writer.h>
33 #include <babeltrace/ctf-ir/trace-internal.h>
34 #include <babeltrace/babeltrace-internal.h>
35 #include <babeltrace/object-internal.h>
36 #include <babeltrace/types.h>
37 #include <babeltrace/ctf/events.h>
40 typedef void (*type_freeze_func
)(struct bt_ctf_field_type
*);
41 typedef int (*type_serialize_func
)(struct bt_ctf_field_type
*,
42 struct metadata_context
*);
44 enum bt_ctf_ir_scope
{
45 BT_CTF_SCOPE_UNKNOWN
= -1,
47 BT_CTF_SCOPE_TRACE_PACKET_HEADER
= 1,
48 BT_CTF_SCOPE_STREAM_PACKET_CONTEXT
= 2,
49 BT_CTF_SCOPE_STREAM_EVENT_HEADER
= 3,
50 BT_CTF_SCOPE_STREAM_EVENT_CONTEXT
= 4,
51 BT_CTF_SCOPE_EVENT_CONTEXT
= 5,
52 BT_CTF_SCOPE_EVENT_FIELDS
= 6,
55 struct bt_ctf_field_type
{
56 struct bt_object base
;
57 struct bt_declaration
*declaration
;
58 type_freeze_func freeze
;
59 type_serialize_func serialize
;
61 * A type can't be modified once it is added to an event or after a
62 * a field has been instanciated from it.
67 * This flag indicates if the field type is valid. A valid
68 * field type is _always_ frozen. All the nested field types of
69 * a valid field type are also valid (and thus frozen).
74 struct bt_ctf_field_type_integer
{
75 struct bt_ctf_field_type parent
;
76 struct declaration_integer declaration
;
77 struct bt_ctf_clock
*mapped_clock
;
80 * This is what the user sets and is never modified by internal
83 * This field must contain a `BT_CTF_BYTE_ORDER_*` value.
85 enum bt_ctf_byte_order user_byte_order
;
88 struct enumeration_mapping
{
101 struct bt_ctf_field_type_enumeration
{
102 struct bt_ctf_field_type parent
;
103 struct bt_ctf_field_type
*container
;
104 GPtrArray
*entries
; /* Array of ptrs to struct enumeration_mapping */
105 struct declaration_enum declaration
;
108 struct bt_ctf_field_type_floating_point
{
109 struct bt_ctf_field_type parent
;
110 struct declaration_float declaration
;
113 * The `declaration` field above contains 3 pointers pointing
114 * to the fields below. This avoids unnecessary dynamic
117 struct declaration_integer sign
;
118 struct declaration_integer mantissa
;
119 struct declaration_integer exp
;
122 * This is what the user sets and is never modified by internal
125 * This field must contain a `BT_CTF_BYTE_ORDER_*` value.
127 enum bt_ctf_byte_order user_byte_order
;
130 struct structure_field
{
132 struct bt_ctf_field_type
*type
;
135 struct bt_ctf_field_type_structure
{
136 struct bt_ctf_field_type parent
;
137 GHashTable
*field_name_to_index
;
138 GPtrArray
*fields
; /* Array of pointers to struct structure_field */
139 struct declaration_struct declaration
;
142 struct bt_ctf_field_type_variant
{
143 struct bt_ctf_field_type parent
;
145 struct bt_ctf_field_type_enumeration
*tag
;
146 struct bt_ctf_field_path
*tag_field_path
;
147 GHashTable
*field_name_to_index
;
148 GPtrArray
*fields
; /* Array of pointers to struct structure_field */
149 struct declaration_variant declaration
;
152 struct bt_ctf_field_type_array
{
153 struct bt_ctf_field_type parent
;
154 struct bt_ctf_field_type
*element_type
;
155 unsigned int length
; /* Number of elements */
156 struct declaration_array declaration
;
159 struct bt_ctf_field_type_sequence
{
160 struct bt_ctf_field_type parent
;
161 struct bt_ctf_field_type
*element_type
;
162 GString
*length_field_name
;
163 struct bt_ctf_field_path
*length_field_path
;
164 struct declaration_sequence declaration
;
167 struct bt_ctf_field_type_string
{
168 struct bt_ctf_field_type parent
;
169 struct declaration_string declaration
;
173 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
);
176 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
177 struct bt_ctf_field_type_variant
*variant
, int64_t tag_value
);
180 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
181 struct bt_ctf_field_type_variant
*variant
, uint64_t tag_value
);
184 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
185 struct metadata_context
*context
);
188 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
);
191 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
192 struct bt_ctf_field_type_enumeration
*enumeration_type
,
196 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
197 struct bt_ctf_field_type_enumeration
*enumeration_type
,
200 /* Override field type's byte order only if it is set to "native" */
202 void bt_ctf_field_type_set_native_byte_order(
203 struct bt_ctf_field_type
*type
, int byte_order
);
205 /* Deep copy a field type */
207 struct bt_ctf_field_type
*bt_ctf_field_type_copy(
208 struct bt_ctf_field_type
*type
);
211 int bt_ctf_field_type_structure_get_field_name_index(
212 struct bt_ctf_field_type
*structure
, const char *name
);
214 /* Replace an existing field's type in a structure */
216 int bt_ctf_field_type_structure_set_field_index(
217 struct bt_ctf_field_type
*structure
,
218 struct bt_ctf_field_type
*field
, int index
);
221 int bt_ctf_field_type_variant_get_field_name_index(
222 struct bt_ctf_field_type
*variant
, const char *name
);
225 int bt_ctf_field_type_sequence_set_length_field_path(
226 struct bt_ctf_field_type
*type
,
227 struct bt_ctf_field_path
*path
);
230 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
231 struct bt_ctf_field_path
*path
);
234 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
235 struct bt_ctf_field_type
*tag_type
);
237 /* Replace an existing field's type in a variant */
239 int bt_ctf_field_type_variant_set_field_index(
240 struct bt_ctf_field_type
*variant
,
241 struct bt_ctf_field_type
*field
, int index
);
244 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*array
,
245 struct bt_ctf_field_type
*element_type
);
248 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*array
,
249 struct bt_ctf_field_type
*element_type
);
252 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*type
);
255 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
256 struct bt_ctf_field_type
*type
, int index
);
259 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*type
,
263 * bt_ctf_field_type_integer_get_size: get an integer type's size.
265 * Get an integer type's size.
267 * @param integer Integer type.
269 * Returns the integer type's size, a negative value on error.
272 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*integer
);
275 * bt_ctf_field_type_integer_get_base: get an integer type's base.
277 * Get an integer type's base used to pretty-print the resulting trace.
279 * @param integer Integer type.
281 * Returns the integer type's base on success, BT_CTF_INTEGER_BASE_UNKNOWN on
285 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
286 struct bt_ctf_field_type
*integer
);
289 * bt_ctf_field_type_integer_get_encoding: get an integer type's encoding.
291 * @param integer Integer type.
293 * Returns the string field's encoding on success,
294 * BT_CTF_STRING_ENCODING_UNKNOWN on error.
297 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
298 struct bt_ctf_field_type
*integer
);
301 * bt_ctf_field_type_integer_get_mapped_clock: get an integer type's mapped clock.
303 * @param integer Integer type.
305 * Returns the integer's mapped clock (if any), NULL on error.
308 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
309 struct bt_ctf_field_type
*integer
);
312 * bt_ctf_field_type_integer_set_mapped_clock: set an integer type's mapped clock.
314 * @param integer Integer type.
315 * @param clock Clock to map.
317 * Returns 0 on success, a negative value on error.
320 int bt_ctf_field_type_integer_set_mapped_clock(
321 struct bt_ctf_field_type
*integer
,
322 struct bt_ctf_clock
*clock
);
325 * bt_ctf_field_type_enumeration_get_container_type: get underlying container.
327 * Get the enumeration type's underlying integer container type.
329 * @param enumeration Enumeration type.
331 * Returns an allocated field type on success, NULL on error.
334 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
335 struct bt_ctf_field_type
*enumeration
);
338 * bt_ctf_field_type_enumeration_add_mapping_unsigned: add an enumeration
341 * Add a mapping to the enumeration. The range's values are inclusive.
343 * @param enumeration Enumeration type.
344 * @param name Enumeration mapping name (will be copied).
345 * @param range_start Enumeration mapping range start.
346 * @param range_end Enumeration mapping range end.
348 * Returns 0 on success, a negative value on error.
351 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
352 struct bt_ctf_field_type
*enumeration
, const char *name
,
353 uint64_t range_start
, uint64_t range_end
);
356 * bt_ctf_field_type_enumeration_get_mapping_count: Get the number of mappings
357 * defined in the enumeration.
359 * @param enumeration Enumeration type.
361 * Returns the mapping count on success, a negative value on error.
364 int bt_ctf_field_type_enumeration_get_mapping_count(
365 struct bt_ctf_field_type
*enumeration
);
368 * bt_ctf_field_type_enumeration_get_mapping: get an enumeration mapping.
370 * @param enumeration Enumeration type.
371 * @param index Index of mapping.
372 * @param name Pointer where the mapping's name will be returned (valid for
373 * the lifetime of the enumeration).
374 * @param range_start Pointer where the enumeration mapping's range start will
376 * @param range_end Pointer where the enumeration mapping's range end will
379 * Returns 0 on success, a negative value on error.
382 int bt_ctf_field_type_enumeration_get_mapping(
383 struct bt_ctf_field_type
*enumeration
, int index
,
384 const char **name
, int64_t *range_start
, int64_t *range_end
);
387 * bt_ctf_field_type_enumeration_get_mapping_unsigned: get a mapping.
389 * @param enumeration Enumeration type.
390 * @param index Index of mapping.
391 * @param name Pointer where the mapping's name will be returned (valid for
392 * the lifetime of the enumeration).
393 * @param range_start Pointer where the enumeration mapping's range start will
395 * @param range_end Pointer where the enumeration mapping's range end will
398 * Returns 0 on success, a negative value on error.
401 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
402 struct bt_ctf_field_type
*enumeration
, int index
,
403 const char **name
, uint64_t *range_start
,
404 uint64_t *range_end
);
407 * bt_ctf_field_type_enumeration_get_mapping_index_by_name: get an enumerations'
408 * mapping index by name.
410 * @param enumeration Enumeration type.
411 * @param name Mapping name.
413 * Returns mapping index on success, a negative value on error.
416 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
417 struct bt_ctf_field_type
*enumeration
, const char *name
);
420 * bt_ctf_field_type_enumeration_get_mapping_index_by_value: get an
421 * enumerations' mapping index by value.
423 * @param enumeration Enumeration type.
424 * @param value Value.
426 * Returns mapping index on success, a negative value on error.
429 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
430 struct bt_ctf_field_type
*enumeration
, int64_t value
);
433 * bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value: get an
434 * enumerations' mapping index by value.
436 * @param enumeration Enumeration type.
437 * @param value Value.
439 * Returns 0 on success, a negative value on error.
442 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
443 struct bt_ctf_field_type
*enumeration
, uint64_t value
);
446 * bt_ctf_field_type_floating_point_get_exponent_digits: get exponent digit
449 * @param floating_point Floating point type.
451 * Returns the exponent digit count on success, a negative value on error.
454 int bt_ctf_field_type_floating_point_get_exponent_digits(
455 struct bt_ctf_field_type
*floating_point
);
458 * bt_ctf_field_type_floating_point_get_mantissa_digits: get mantissa digit
461 * @param floating_point Floating point type.
463 * Returns the mantissa digit count on success, a negative value on error.
466 int bt_ctf_field_type_floating_point_get_mantissa_digits(
467 struct bt_ctf_field_type
*floating_point
);
470 * bt_ctf_field_type_structure_get_field_count: Get the number of fields defined
473 * @param structure Structure type.
475 * Returns the field count on success, a negative value on error.
478 int bt_ctf_field_type_structure_get_field_count(
479 struct bt_ctf_field_type
*structure
);
482 * bt_ctf_field_type_structure_get_field_type_by_name: get a structure field's
485 * @param structure Structure type.
486 * @param field_name Name of the structure's field.
488 * Returns a field type instance on success, NULL on error.
491 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
492 struct bt_ctf_field_type
*structure
, const char *field_name
);
495 * bt_ctf_field_type_variant_get_tag_type: get a variant's tag type.
497 * @param variant Variant type.
499 * Returns a field type instance on success, NULL if unset.
502 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
503 struct bt_ctf_field_type
*variant
);
506 * bt_ctf_field_type_variant_get_tag_name: get a variant's tag name.
508 * @param variant Variant type.
510 * Returns the tag field's name, NULL if unset.
513 const char *bt_ctf_field_type_variant_get_tag_name(
514 struct bt_ctf_field_type
*variant
);
517 * bt_ctf_field_type_variant_set_tag_name: set a variant's tag name.
519 * @param variant Variant type.
520 * @param name Tag field name.
522 * Returns 0 on success, a negative value on error.
525 int bt_ctf_field_type_variant_set_tag_name(
526 struct bt_ctf_field_type
*variant
, const char *name
);
529 * bt_ctf_field_type_variant_get_field_type_by_name: get variant field's type.
531 * @param structure Variant type.
532 * @param field_name Name of the variant's field.
534 * Returns a field type instance on success, NULL on error.
537 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
538 struct bt_ctf_field_type
*variant
, const char *field_name
);
541 * bt_ctf_field_type_variant_get_field_type_from_tag: get variant field's type.
543 * @param variant Variant type.
544 * @param tag Type tag (enum).
546 * Returns a field type instance on success, NULL on error.
549 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
550 struct bt_ctf_field_type
*variant
, struct bt_ctf_field
*tag
);
553 * bt_ctf_field_type_variant_get_field_count: Get the number of fields defined
556 * @param variant Variant type.
558 * Returns the field count on success, a negative value on error.
561 int bt_ctf_field_type_variant_get_field_count(
562 struct bt_ctf_field_type
*variant
);
565 * bt_ctf_field_type_variant_get_field: get a variant's field name and type.
567 * @param variant Variant type.
568 * @param field_type Pointer to a const char* where the field's name will
570 * @param field_type Pointer to a bt_ctf_field_type* where the field's type will
572 * @param index Index of field.
574 * Returns 0 on success, a negative value on error.
577 int bt_ctf_field_type_variant_get_field(
578 struct bt_ctf_field_type
*variant
, const char **field_name
,
579 struct bt_ctf_field_type
**field_type
, int index
);
582 * bt_ctf_field_type_array_get_element_type: get an array's element type.
584 * @param array Array type.
586 * Returns a field type instance on success, NULL on error.
589 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
590 struct bt_ctf_field_type
*array
);
593 * bt_ctf_field_type_array_get_length: get an array's length.
595 * @param array Array type.
597 * Returns the array's length on success, a negative value on error.
600 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*array
);
603 * bt_ctf_field_type_sequence_get_element_type: get a sequence's element type.
605 * @param sequence Sequence type.
607 * Returns a field type instance on success, NULL on error.
610 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
611 struct bt_ctf_field_type
*sequence
);
614 * bt_ctf_field_type_sequence_get_length_field_name: get length field name.
616 * @param sequence Sequence type.
618 * Returns the sequence's length field on success, NULL on error.
621 const char *bt_ctf_field_type_sequence_get_length_field_name(
622 struct bt_ctf_field_type
*sequence
);
625 * bt_ctf_field_type_string_get_encoding: get a string type's encoding.
627 * Get the string type's encoding.
629 * @param string_type String type.
631 * Returns the string's encoding on success, a BT_CTF_STRING_ENCODING_UNKNOWN
635 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
636 struct bt_ctf_field_type
*string_type
);
639 * bt_ctf_field_type_get_alignment: get a field type's alignment.
641 * Get the field type's alignment.
643 * @param type Field type.
645 * Returns the field type's alignment on success, a negative value on error and
646 * 0 if the alignment is undefined (as in the case of a variant).
649 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
);
652 * bt_ctf_field_type_get_byte_order: get a field type's byte order.
654 * @param type Field type.
656 * Returns the field type's byte order on success, a negative value on error.
659 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
660 struct bt_ctf_field_type
*type
);
664 * bt_ctf_field_type_variant_get_tag_field_path: get a variant's tag's field
667 * Get the variant's tag's field path.
669 * @param type Field type.
671 * Returns the field path on success, NULL on error or if no field path is set.
674 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
675 struct bt_ctf_field_type
*type
);
678 * bt_ctf_field_type_sequence_get_length_field_path: get a sequence's length's
681 * Get the sequence's length's field path.
683 * @param type Field type.
685 * Returns the field path on success, NULL on error or if no field path is set.
688 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
689 struct bt_ctf_field_type
*type
);
692 * bt_ctf_field_type_compare: compare two field types recursively
694 * Compare two field types recursively.
696 * The registered tag field type of a variant field type is ignored:
697 * only the tag strings are compared.
699 * @param type_a Field type A.
700 * @param type_b Field type B.
702 * Returns 0 if both field types are semantically equivalent, a positive
703 * value if they are not equivalent, or a negative value on error.
706 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
707 struct bt_ctf_field_type
*type_b
);
710 * bt_ctf_field_type_get_type_id: get a field type's bt_ctf_type_id.
712 * @param type Field type.
714 * Returns the field type's bt_ctf_type_id, CTF_TYPE_UNKNOWN on error.
717 enum ctf_type_id
bt_ctf_field_type_get_type_id(
718 struct bt_ctf_field_type
*type
);
721 * bt_ctf_field_type_is_integer: returns whether or not a given field
722 * type is an integer type.
724 * @param type Field type.
726 * Returns 1 if the field type is an integer type, 0 otherwise.
729 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
);
732 * bt_ctf_field_type_is_floating_point: returns whether or not a given field
733 * type is a floating point number type.
735 * @param type Field type.
737 * Returns 1 if the field type is a floating point number type, 0 otherwise.
740 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
);
743 * bt_ctf_field_type_is_enumeration: returns whether or not a given field
744 * type is an enumeration type.
746 * @param type Field type.
748 * Returns 1 if the field type is an enumeration type, 0 otherwise.
751 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
);
754 * bt_ctf_field_type_is_string: returns whether or not a given field
755 * type is a string type.
757 * @param type Field type.
759 * Returns 1 if the field type is a string type, 0 otherwise.
762 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
);
765 * bt_ctf_field_type_is_structure: returns whether or not a given field
766 * type is a structure type.
768 * @param type Field type.
770 * Returns 1 if the field type is a structure type, 0 otherwise.
773 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
);
776 * bt_ctf_field_type_is_array: returns whether or not a given field
777 * type is an array type.
779 * @param type Field type.
781 * Returns 1 if the field type is an array type, 0 otherwise.
784 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
);
787 * bt_ctf_field_type_is_sequence: returns whether or not a given field
788 * type is a sequence type.
790 * @param type Field type.
792 * Returns 1 if the field type is a sequence type, 0 otherwise.
795 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
);
798 * bt_ctf_field_type_is_variant: returns whether or not a given field
799 * type is a variant type.
801 * @param type Field type.
803 * Returns 1 if the field type is a variant type, 0 otherwise.
806 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
);
808 #endif /* BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H */