| 1 | #ifndef BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H |
| 2 | #define BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * BabelTrace - CTF IR: Event field types internal |
| 6 | * |
| 7 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 8 | * |
| 9 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 10 | * |
| 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: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 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 |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include <stdint.h> |
| 31 | #include <babeltrace/ctf-writer/event-types.h> |
| 32 | #include <babeltrace/ctf-writer/event-fields.h> |
| 33 | #include <babeltrace/ctf-writer/writer.h> |
| 34 | #include <babeltrace/ctf-ir/trace-internal.h> |
| 35 | #include <babeltrace/ctf-ir/clock-class.h> |
| 36 | #include <babeltrace/ctf-ir/field-types.h> |
| 37 | #include <babeltrace/babeltrace-internal.h> |
| 38 | #include <babeltrace/object-internal.h> |
| 39 | #include <babeltrace/types.h> |
| 40 | #include <glib.h> |
| 41 | |
| 42 | typedef void (*type_freeze_func)(struct bt_field_type *); |
| 43 | typedef int (*type_serialize_func)(struct bt_field_type *, |
| 44 | struct metadata_context *); |
| 45 | |
| 46 | struct bt_field_type { |
| 47 | struct bt_object base; |
| 48 | enum bt_field_type_id id; |
| 49 | unsigned int alignment; |
| 50 | type_freeze_func freeze; |
| 51 | type_serialize_func serialize; |
| 52 | /* |
| 53 | * A type can't be modified once it is added to an event or after a |
| 54 | * a field has been instanciated from it. |
| 55 | */ |
| 56 | int frozen; |
| 57 | |
| 58 | /* |
| 59 | * This flag indicates if the field type is valid. A valid |
| 60 | * field type is _always_ frozen. All the nested field types of |
| 61 | * a valid field type are also valid (and thus frozen). |
| 62 | */ |
| 63 | int valid; |
| 64 | }; |
| 65 | |
| 66 | struct bt_field_type_integer { |
| 67 | struct bt_field_type parent; |
| 68 | struct bt_clock_class *mapped_clock; |
| 69 | enum bt_byte_order user_byte_order; |
| 70 | bt_bool is_signed; |
| 71 | unsigned int size; |
| 72 | enum bt_integer_base base; |
| 73 | enum bt_string_encoding encoding; |
| 74 | }; |
| 75 | |
| 76 | struct enumeration_mapping { |
| 77 | union { |
| 78 | uint64_t _unsigned; |
| 79 | int64_t _signed; |
| 80 | } range_start; |
| 81 | |
| 82 | union { |
| 83 | uint64_t _unsigned; |
| 84 | int64_t _signed; |
| 85 | } range_end; |
| 86 | GQuark string; |
| 87 | }; |
| 88 | |
| 89 | struct bt_field_type_enumeration { |
| 90 | struct bt_field_type parent; |
| 91 | struct bt_field_type *container; |
| 92 | GPtrArray *entries; /* Array of ptrs to struct enumeration_mapping */ |
| 93 | /* Only set during validation. */ |
| 94 | bt_bool has_overlapping_ranges; |
| 95 | }; |
| 96 | |
| 97 | enum bt_field_type_enumeration_mapping_iterator_type { |
| 98 | ITERATOR_BY_NAME, |
| 99 | ITERATOR_BY_SIGNED_VALUE, |
| 100 | ITERATOR_BY_UNSIGNED_VALUE, |
| 101 | }; |
| 102 | |
| 103 | struct bt_field_type_enumeration_mapping_iterator { |
| 104 | struct bt_object base; |
| 105 | struct bt_field_type_enumeration *enumeration_type; |
| 106 | enum bt_field_type_enumeration_mapping_iterator_type type; |
| 107 | int index; |
| 108 | union { |
| 109 | GQuark name_quark; |
| 110 | int64_t signed_value; |
| 111 | uint64_t unsigned_value; |
| 112 | } u; |
| 113 | }; |
| 114 | |
| 115 | struct bt_field_type_floating_point { |
| 116 | struct bt_field_type parent; |
| 117 | enum bt_byte_order user_byte_order; |
| 118 | unsigned int exp_dig; |
| 119 | unsigned int mant_dig; |
| 120 | }; |
| 121 | |
| 122 | struct structure_field { |
| 123 | GQuark name; |
| 124 | struct bt_field_type *type; |
| 125 | }; |
| 126 | |
| 127 | struct bt_field_type_structure { |
| 128 | struct bt_field_type parent; |
| 129 | GHashTable *field_name_to_index; |
| 130 | GPtrArray *fields; /* Array of pointers to struct structure_field */ |
| 131 | }; |
| 132 | |
| 133 | struct bt_field_type_variant { |
| 134 | struct bt_field_type parent; |
| 135 | GString *tag_name; |
| 136 | struct bt_field_type_enumeration *tag; |
| 137 | struct bt_field_path *tag_field_path; |
| 138 | GHashTable *field_name_to_index; |
| 139 | GPtrArray *fields; /* Array of pointers to struct structure_field */ |
| 140 | }; |
| 141 | |
| 142 | struct bt_field_type_array { |
| 143 | struct bt_field_type parent; |
| 144 | struct bt_field_type *element_type; |
| 145 | unsigned int length; /* Number of elements */ |
| 146 | }; |
| 147 | |
| 148 | struct bt_field_type_sequence { |
| 149 | struct bt_field_type parent; |
| 150 | struct bt_field_type *element_type; |
| 151 | GString *length_field_name; |
| 152 | struct bt_field_path *length_field_path; |
| 153 | }; |
| 154 | |
| 155 | struct bt_field_type_string { |
| 156 | struct bt_field_type parent; |
| 157 | enum bt_string_encoding encoding; |
| 158 | }; |
| 159 | |
| 160 | BT_HIDDEN |
| 161 | void bt_field_type_freeze(struct bt_field_type *type); |
| 162 | |
| 163 | BT_HIDDEN |
| 164 | struct bt_field_type *bt_field_type_variant_get_field_type_signed( |
| 165 | struct bt_field_type_variant *variant, int64_t tag_value); |
| 166 | |
| 167 | BT_HIDDEN |
| 168 | struct bt_field_type *bt_field_type_variant_get_field_type_unsigned( |
| 169 | struct bt_field_type_variant *variant, uint64_t tag_value); |
| 170 | |
| 171 | BT_HIDDEN |
| 172 | int bt_field_type_serialize(struct bt_field_type *type, |
| 173 | struct metadata_context *context); |
| 174 | |
| 175 | BT_HIDDEN |
| 176 | int bt_field_type_validate(struct bt_field_type *type); |
| 177 | |
| 178 | BT_HIDDEN |
| 179 | int bt_field_type_structure_get_field_name_index( |
| 180 | struct bt_field_type *structure, const char *name); |
| 181 | |
| 182 | BT_HIDDEN |
| 183 | int bt_field_type_variant_get_field_name_index( |
| 184 | struct bt_field_type *variant, const char *name); |
| 185 | |
| 186 | BT_HIDDEN |
| 187 | int bt_field_type_sequence_set_length_field_path( |
| 188 | struct bt_field_type *type, |
| 189 | struct bt_field_path *path); |
| 190 | |
| 191 | BT_HIDDEN |
| 192 | int bt_field_type_variant_set_tag_field_path(struct bt_field_type *type, |
| 193 | struct bt_field_path *path); |
| 194 | |
| 195 | BT_HIDDEN |
| 196 | int bt_field_type_variant_set_tag_field_type(struct bt_field_type *type, |
| 197 | struct bt_field_type *tag_type); |
| 198 | |
| 199 | BT_HIDDEN |
| 200 | int bt_field_type_array_set_element_type(struct bt_field_type *array, |
| 201 | struct bt_field_type *element_type); |
| 202 | |
| 203 | BT_HIDDEN |
| 204 | int bt_field_type_sequence_set_element_type(struct bt_field_type *array, |
| 205 | struct bt_field_type *element_type); |
| 206 | |
| 207 | BT_HIDDEN |
| 208 | int64_t bt_field_type_get_field_count(struct bt_field_type *type); |
| 209 | |
| 210 | BT_HIDDEN |
| 211 | struct bt_field_type *bt_field_type_get_field_at_index( |
| 212 | struct bt_field_type *type, int index); |
| 213 | |
| 214 | BT_HIDDEN |
| 215 | int bt_field_type_get_field_index(struct bt_field_type *type, |
| 216 | const char *name); |
| 217 | |
| 218 | BT_HIDDEN |
| 219 | int bt_field_type_integer_set_mapped_clock_class_no_check( |
| 220 | struct bt_field_type *int_field_type, |
| 221 | struct bt_clock_class *clock_class); |
| 222 | |
| 223 | static inline |
| 224 | const char *bt_field_type_id_string(enum bt_field_type_id type_id) |
| 225 | { |
| 226 | switch (type_id) { |
| 227 | case BT_FIELD_TYPE_ID_UNKNOWN: |
| 228 | return "BT_FIELD_TYPE_ID_UNKNOWN"; |
| 229 | case BT_FIELD_TYPE_ID_INTEGER: |
| 230 | return "BT_FIELD_TYPE_ID_INTEGER"; |
| 231 | case BT_FIELD_TYPE_ID_FLOAT: |
| 232 | return "BT_FIELD_TYPE_ID_FLOAT"; |
| 233 | case BT_FIELD_TYPE_ID_ENUM: |
| 234 | return "BT_FIELD_TYPE_ID_ENUM"; |
| 235 | case BT_FIELD_TYPE_ID_STRING: |
| 236 | return "BT_FIELD_TYPE_ID_STRING"; |
| 237 | case BT_FIELD_TYPE_ID_STRUCT: |
| 238 | return "BT_FIELD_TYPE_ID_STRUCT"; |
| 239 | case BT_FIELD_TYPE_ID_ARRAY: |
| 240 | return "BT_FIELD_TYPE_ID_ARRAY"; |
| 241 | case BT_FIELD_TYPE_ID_SEQUENCE: |
| 242 | return "BT_FIELD_TYPE_ID_SEQUENCE"; |
| 243 | case BT_FIELD_TYPE_ID_VARIANT: |
| 244 | return "BT_FIELD_TYPE_ID_VARIANT"; |
| 245 | default: |
| 246 | return "(unknown)"; |
| 247 | } |
| 248 | }; |
| 249 | |
| 250 | static inline |
| 251 | const char *bt_byte_order_string(enum bt_byte_order bo) |
| 252 | { |
| 253 | switch (bo) { |
| 254 | case BT_BYTE_ORDER_UNKNOWN: |
| 255 | return "BT_BYTE_ORDER_UNKNOWN"; |
| 256 | case BT_BYTE_ORDER_UNSPECIFIED: |
| 257 | return "BT_BYTE_ORDER_UNSPECIFIED"; |
| 258 | case BT_BYTE_ORDER_NATIVE: |
| 259 | return "BT_BYTE_ORDER_NATIVE"; |
| 260 | case BT_BYTE_ORDER_LITTLE_ENDIAN: |
| 261 | return "BT_BYTE_ORDER_LITTLE_ENDIAN"; |
| 262 | case BT_BYTE_ORDER_BIG_ENDIAN: |
| 263 | return "BT_BYTE_ORDER_BIG_ENDIAN"; |
| 264 | case BT_BYTE_ORDER_NETWORK: |
| 265 | return "BT_BYTE_ORDER_NETWORK"; |
| 266 | default: |
| 267 | return "(unknown)"; |
| 268 | } |
| 269 | }; |
| 270 | |
| 271 | static inline |
| 272 | const char *bt_string_encoding_string(enum bt_string_encoding encoding) |
| 273 | { |
| 274 | switch (encoding) { |
| 275 | case BT_STRING_ENCODING_UNKNOWN: |
| 276 | return "BT_STRING_ENCODING_UNKNOWN"; |
| 277 | case BT_STRING_ENCODING_NONE: |
| 278 | return "BT_STRING_ENCODING_NONE"; |
| 279 | case BT_STRING_ENCODING_UTF8: |
| 280 | return "BT_STRING_ENCODING_UTF8"; |
| 281 | case BT_STRING_ENCODING_ASCII: |
| 282 | return "BT_STRING_ENCODING_ASCII"; |
| 283 | default: |
| 284 | return "(unknown)"; |
| 285 | } |
| 286 | }; |
| 287 | |
| 288 | static inline |
| 289 | const char *bt_integer_base_string(enum bt_integer_base base) |
| 290 | { |
| 291 | switch (base) { |
| 292 | case BT_INTEGER_BASE_UNKNOWN: |
| 293 | return "BT_INTEGER_BASE_UNKNOWN"; |
| 294 | case BT_INTEGER_BASE_UNSPECIFIED: |
| 295 | return "BT_INTEGER_BASE_UNSPECIFIED"; |
| 296 | case BT_INTEGER_BASE_BINARY: |
| 297 | return "BT_INTEGER_BASE_BINARY"; |
| 298 | case BT_INTEGER_BASE_OCTAL: |
| 299 | return "BT_INTEGER_BASE_OCTAL"; |
| 300 | case BT_INTEGER_BASE_DECIMAL: |
| 301 | return "BT_INTEGER_BASE_DECIMAL"; |
| 302 | case BT_INTEGER_BASE_HEXADECIMAL: |
| 303 | return "BT_INTEGER_BASE_HEXADECIMAL"; |
| 304 | default: |
| 305 | return "(unknown)"; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static inline |
| 310 | const char *bt_scope_string(enum bt_scope scope) |
| 311 | { |
| 312 | switch (scope) { |
| 313 | case BT_SCOPE_UNKNOWN: |
| 314 | return "BT_SCOPE_UNKNOWN"; |
| 315 | case BT_SCOPE_TRACE_PACKET_HEADER: |
| 316 | return "BT_SCOPE_TRACE_PACKET_HEADER"; |
| 317 | case BT_SCOPE_STREAM_PACKET_CONTEXT: |
| 318 | return "BT_SCOPE_STREAM_PACKET_CONTEXT"; |
| 319 | case BT_SCOPE_STREAM_EVENT_HEADER: |
| 320 | return "BT_SCOPE_STREAM_EVENT_HEADER"; |
| 321 | case BT_SCOPE_STREAM_EVENT_CONTEXT: |
| 322 | return "BT_SCOPE_STREAM_EVENT_CONTEXT"; |
| 323 | case BT_SCOPE_EVENT_CONTEXT: |
| 324 | return "BT_SCOPE_EVENT_CONTEXT"; |
| 325 | case BT_SCOPE_EVENT_PAYLOAD: |
| 326 | return "BT_SCOPE_EVENT_PAYLOAD"; |
| 327 | case BT_SCOPE_ENV: |
| 328 | return "BT_SCOPE_ENV"; |
| 329 | default: |
| 330 | return "(unknown)"; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | #endif /* BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H */ |