Commit | Line | Data |
---|---|---|
d79865b9 MD |
1 | #ifndef _BABELTRACE_TYPES_H |
2 | #define _BABELTRACE_TYPES_H | |
3 | ||
4 | /* | |
5 | * BabelTrace | |
6 | * | |
fc93b2bd | 7 | * Type Header |
d79865b9 | 8 | * |
c054553d | 9 | * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
d79865b9 | 10 | * |
ccd7e1c8 MD |
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: | |
d79865b9 | 17 | * |
ccd7e1c8 MD |
18 | * The above copyright notice and this permission notice shall be included in |
19 | * all copies or substantial portions of the Software. | |
d79865b9 MD |
20 | */ |
21 | ||
bed864a7 | 22 | #include <babeltrace/align.h> |
d65d8abb | 23 | #include <babeltrace/list.h> |
4c8bfb7e MD |
24 | #include <stdbool.h> |
25 | #include <stdint.h> | |
26 | #include <limits.h> | |
bed864a7 | 27 | #include <string.h> |
4c8bfb7e | 28 | #include <glib.h> |
8eab883c | 29 | #include <assert.h> |
bed864a7 | 30 | |
11796b96 MD |
31 | /* Preallocate this many fields for structures */ |
32 | #define DEFAULT_NR_STRUCT_FIELDS 8 | |
33 | ||
dd2544fd | 34 | struct stream_pos; |
4c8bfb7e | 35 | struct format; |
e1151715 | 36 | struct definition; |
4c8bfb7e | 37 | |
46322b33 MD |
38 | /* Parent of per-plugin positions */ |
39 | struct stream_pos { | |
40 | }; | |
41 | ||
64893f33 | 42 | /* type scope */ |
f6625916 | 43 | struct declaration_scope { |
c13cbf74 | 44 | /* Hash table mapping type name GQuark to "struct declaration" */ |
1ee8e81d | 45 | /* Used for both typedef and typealias. */ |
f6625916 MD |
46 | GHashTable *typedef_declarations; |
47 | /* Hash table mapping struct name GQuark to "struct declaration_struct" */ | |
48 | GHashTable *struct_declarations; | |
49 | /* Hash table mapping variant name GQuark to "struct declaration_variant" */ | |
50 | GHashTable *variant_declarations; | |
c13cbf74 | 51 | /* Hash table mapping enum name GQuark to "struct type_enum" */ |
f6625916 MD |
52 | GHashTable *enum_declarations; |
53 | struct declaration_scope *parent_scope; | |
64893f33 MD |
54 | }; |
55 | ||
e1151715 MD |
56 | /* definition scope */ |
57 | struct definition_scope { | |
58 | /* Hash table mapping field name GQuark to "struct definition" */ | |
59 | GHashTable *definitions; | |
60 | struct definition_scope *parent_scope; | |
05c749e5 MD |
61 | /* |
62 | * Complete "path" leading to this definition scope. | |
9e29e16e | 63 | * Includes dynamic scope name '.' field name '.' field name '.' .... |
05c749e5 | 64 | * Array of GQuark elements (which are each separated by dots). |
9e29e16e MD |
65 | * The dynamic scope name can contain dots, and is encoded into |
66 | * a single GQuark. Thus, scope_path[0] returns the GQuark | |
67 | * identifying the dynamic scope. | |
05c749e5 MD |
68 | */ |
69 | GArray *scope_path; /* array of GQuark */ | |
6b71274a MD |
70 | }; |
71 | ||
05628561 MD |
72 | enum ctf_type_id { |
73 | CTF_TYPE_UNKNOWN = 0, | |
74 | CTF_TYPE_INTEGER, | |
75 | CTF_TYPE_FLOAT, | |
76 | CTF_TYPE_ENUM, | |
77 | CTF_TYPE_STRING, | |
78 | CTF_TYPE_STRUCT, | |
d60cb676 | 79 | CTF_TYPE_UNTAGGED_VARIANT, |
05628561 MD |
80 | CTF_TYPE_VARIANT, |
81 | CTF_TYPE_ARRAY, | |
82 | CTF_TYPE_SEQUENCE, | |
83 | NR_CTF_TYPES, | |
84 | }; | |
85 | ||
f6625916 | 86 | struct declaration { |
05628561 | 87 | enum ctf_type_id id; |
fc93b2bd | 88 | size_t alignment; /* type alignment, in bits */ |
e19c3d69 | 89 | int ref; /* number of references to the type */ |
c054553d | 90 | /* |
f6625916 | 91 | * declaration_free called with declaration ref is decremented to 0. |
c054553d | 92 | */ |
f6625916 | 93 | void (*declaration_free)(struct declaration *declaration); |
e1151715 | 94 | struct definition * |
f6625916 | 95 | (*definition_new)(struct declaration *declaration, |
05c749e5 MD |
96 | struct definition_scope *parent_scope, |
97 | GQuark field_name, int index); | |
c054553d | 98 | /* |
e1151715 | 99 | * definition_free called with definition ref is decremented to 0. |
e19c3d69 | 100 | */ |
e1151715 | 101 | void (*definition_free)(struct definition *definition); |
e19c3d69 | 102 | /* |
e1151715 MD |
103 | * Definition copy function. Knows how to find the child |
104 | * definition from the parent definition. | |
fc93b2bd | 105 | */ |
4c8bfb7e MD |
106 | void (*copy)(struct stream_pos *dest, const struct format *fdest, |
107 | struct stream_pos *src, const struct format *fsrc, | |
e1151715 | 108 | struct definition *definition); |
c054553d MD |
109 | }; |
110 | ||
e1151715 | 111 | struct definition { |
f6625916 | 112 | struct declaration *declaration; |
05c749e5 | 113 | int index; /* Position of the definition in its container */ |
e1151715 | 114 | int ref; /* number of references to the definition */ |
fc93b2bd MD |
115 | }; |
116 | ||
bed864a7 MD |
117 | /* |
118 | * Because we address in bits, bitfields end up being exactly the same as | |
119 | * integers, except that their read/write functions must be able to deal with | |
120 | * read/write non aligned on CHAR_BIT. | |
121 | */ | |
f6625916 MD |
122 | struct declaration_integer { |
123 | struct declaration p; | |
7fe00194 MD |
124 | size_t len; /* length, in bits. */ |
125 | int byte_order; /* byte order */ | |
126 | int signedness; | |
fc93b2bd MD |
127 | }; |
128 | ||
e1151715 MD |
129 | struct definition_integer { |
130 | struct definition p; | |
f6625916 | 131 | struct declaration_integer *declaration; |
c054553d MD |
132 | /* Last values read */ |
133 | union { | |
134 | uint64_t _unsigned; | |
135 | int64_t _signed; | |
136 | } value; | |
137 | }; | |
138 | ||
f6625916 MD |
139 | struct declaration_float { |
140 | struct declaration p; | |
141 | struct declaration_integer *sign; | |
142 | struct declaration_integer *mantissa; | |
143 | struct declaration_integer *exp; | |
fc93b2bd | 144 | int byte_order; |
0a46062b | 145 | /* TODO: we might want to express more info about NaN, +inf and -inf */ |
fc93b2bd MD |
146 | }; |
147 | ||
e1151715 MD |
148 | struct definition_float { |
149 | struct definition p; | |
f6625916 | 150 | struct declaration_float *declaration; |
c054553d MD |
151 | /* Last values read */ |
152 | long double value; | |
153 | }; | |
154 | ||
d65d8abb MD |
155 | /* |
156 | * enum_val_equal assumes that signed and unsigned memory layout overlap. | |
157 | */ | |
158 | struct enum_range { | |
159 | union { | |
160 | int64_t _signed; | |
161 | uint64_t _unsigned; | |
162 | } start; /* lowest range value */ | |
163 | union { | |
164 | int64_t _signed; | |
165 | uint64_t _unsigned; | |
166 | } end; /* highest range value */ | |
167 | }; | |
168 | ||
169 | struct enum_range_to_quark { | |
170 | struct cds_list_head node; | |
171 | struct enum_range range; | |
172 | GQuark quark; | |
173 | }; | |
174 | ||
175 | /* | |
176 | * We optimize the common case (range of size 1: single value) by creating a | |
177 | * hash table mapping values to quark sets. We then lookup the ranges to | |
178 | * complete the quark set. | |
179 | * | |
180 | * TODO: The proper structure to hold the range to quark set mapping would be an | |
181 | * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query | |
182 | * time. Using a simple O(n) list search for now for implementation speed and | |
183 | * given that we can expect to have a _relatively_ small number of enumeration | |
184 | * ranges. This might become untrue if we are fed with symbol tables often | |
185 | * required to lookup function names from instruction pointer value. | |
186 | */ | |
448d3cc7 | 187 | struct enum_table { |
d65d8abb MD |
188 | GHashTable *value_to_quark_set; /* (value, GQuark GArray) */ |
189 | struct cds_list_head range_to_quark; /* (range, GQuark) */ | |
190 | GHashTable *quark_to_range_set; /* (GQuark, range GArray) */ | |
448d3cc7 MD |
191 | }; |
192 | ||
f6625916 MD |
193 | struct declaration_enum { |
194 | struct declaration p; | |
195 | struct declaration_integer *integer_declaration; | |
448d3cc7 | 196 | struct enum_table table; |
fc93b2bd MD |
197 | }; |
198 | ||
e1151715 MD |
199 | struct definition_enum { |
200 | struct definition p; | |
201 | struct definition_integer *integer; | |
f6625916 | 202 | struct declaration_enum *declaration; |
c054553d MD |
203 | /* Last GQuark values read. Keeping a reference on the GQuark array. */ |
204 | GArray *value; | |
205 | }; | |
206 | ||
ab4cf058 MD |
207 | enum ctf_string_encoding { |
208 | CTF_STRING_UTF8 = 0, | |
209 | CTF_STRING_ASCII, | |
210 | CTF_STRING_UNKNOWN, | |
211 | }; | |
212 | ||
f6625916 MD |
213 | struct declaration_string { |
214 | struct declaration p; | |
ab4cf058 | 215 | enum ctf_string_encoding encoding; |
c054553d MD |
216 | }; |
217 | ||
e1151715 MD |
218 | struct definition_string { |
219 | struct definition p; | |
f6625916 | 220 | struct declaration_string *declaration; |
e1151715 | 221 | char *value; /* freed at definition_string teardown */ |
11796b96 MD |
222 | }; |
223 | ||
f6625916 | 224 | struct declaration_field { |
e19c3d69 | 225 | GQuark name; |
f6625916 | 226 | struct declaration *declaration; |
c054553d MD |
227 | }; |
228 | ||
e19c3d69 | 229 | struct field { |
ac88af75 | 230 | GQuark name; |
e1151715 | 231 | struct definition *definition; |
c054553d MD |
232 | }; |
233 | ||
f6625916 MD |
234 | struct declaration_struct { |
235 | struct declaration p; | |
e19c3d69 | 236 | GHashTable *fields_by_name; /* Tuples (field name, field index) */ |
f6625916 MD |
237 | struct declaration_scope *scope; |
238 | GArray *fields; /* Array of declaration_field */ | |
e19c3d69 MD |
239 | }; |
240 | ||
e1151715 MD |
241 | struct definition_struct { |
242 | struct definition p; | |
f6625916 | 243 | struct declaration_struct *declaration; |
e1151715 | 244 | struct definition_scope *scope; |
c054553d MD |
245 | GArray *fields; /* Array of struct field */ |
246 | }; | |
247 | ||
d60cb676 | 248 | struct declaration_untagged_variant { |
f6625916 | 249 | struct declaration p; |
c054553d | 250 | GHashTable *fields_by_tag; /* Tuples (field tag, field index) */ |
f6625916 MD |
251 | struct declaration_scope *scope; |
252 | GArray *fields; /* Array of declaration_field */ | |
c054553d MD |
253 | }; |
254 | ||
d60cb676 MD |
255 | struct declaration_variant { |
256 | struct declaration p; | |
257 | struct declaration_untagged_variant *untagged_variant; | |
258 | GArray *tag_name; /* Array of GQuark */ | |
259 | }; | |
260 | ||
261 | /* A variant needs to be tagged to be defined. */ | |
e1151715 MD |
262 | struct definition_variant { |
263 | struct definition p; | |
f6625916 | 264 | struct declaration_variant *declaration; |
e1151715 MD |
265 | struct definition_scope *scope; |
266 | struct definition *enum_tag; | |
c054553d MD |
267 | GArray *fields; /* Array of struct field */ |
268 | struct field *current_field; /* Last field read */ | |
11796b96 MD |
269 | }; |
270 | ||
f6625916 MD |
271 | struct declaration_array { |
272 | struct declaration p; | |
11796b96 | 273 | size_t len; |
f6625916 MD |
274 | struct declaration *elem; |
275 | struct declaration_scope *scope; | |
11796b96 MD |
276 | }; |
277 | ||
e1151715 MD |
278 | struct definition_array { |
279 | struct definition p; | |
f6625916 | 280 | struct declaration_array *declaration; |
e1151715 | 281 | struct definition_scope *scope; |
0f980a35 | 282 | GArray *elems; /* struct field */ |
c054553d MD |
283 | }; |
284 | ||
f6625916 MD |
285 | struct declaration_sequence { |
286 | struct declaration p; | |
287 | struct declaration_integer *len_declaration; | |
288 | struct declaration *elem; | |
289 | struct declaration_scope *scope; | |
e19c3d69 MD |
290 | }; |
291 | ||
e1151715 MD |
292 | struct definition_sequence { |
293 | struct definition p; | |
f6625916 | 294 | struct declaration_sequence *declaration; |
e1151715 MD |
295 | struct definition_scope *scope; |
296 | struct definition_integer *len; | |
0f980a35 | 297 | GArray *elems; /* struct field */ |
c054553d MD |
298 | }; |
299 | ||
f6625916 MD |
300 | int register_declaration(GQuark declaration_name, |
301 | struct declaration *declaration, | |
302 | struct declaration_scope *scope); | |
303 | struct declaration *lookup_declaration(GQuark declaration_name, | |
78af2bcd | 304 | struct declaration_scope *scope); |
c13cbf74 MD |
305 | |
306 | /* | |
307 | * Type scopes also contain a separate registry for struct, variant and | |
e1151715 | 308 | * enum types. Those register types rather than type definitions, so |
c13cbf74 MD |
309 | * that a named variant can be declared without specifying its target |
310 | * "choice" tag field immediately. | |
311 | */ | |
f6625916 MD |
312 | int register_struct_declaration(GQuark struct_name, |
313 | struct declaration_struct *struct_declaration, | |
314 | struct declaration_scope *scope); | |
315 | struct declaration_struct * | |
316 | lookup_struct_declaration(GQuark struct_name, | |
317 | struct declaration_scope *scope); | |
318 | int register_variant_declaration(GQuark variant_name, | |
a0720417 | 319 | struct declaration_untagged_variant *untagged_variant_declaration, |
f6625916 | 320 | struct declaration_scope *scope); |
a0720417 | 321 | struct declaration_untagged_variant *lookup_variant_declaration(GQuark variant_name, |
f6625916 MD |
322 | struct declaration_scope *scope); |
323 | int register_enum_declaration(GQuark enum_name, | |
324 | struct declaration_enum *enum_declaration, | |
325 | struct declaration_scope *scope); | |
326 | struct declaration_enum * | |
327 | lookup_enum_declaration(GQuark enum_name, | |
328 | struct declaration_scope *scope); | |
329 | ||
330 | struct declaration_scope * | |
331 | new_declaration_scope(struct declaration_scope *parent_scope); | |
332 | void free_declaration_scope(struct declaration_scope *scope); | |
c054553d | 333 | |
c13cbf74 | 334 | /* |
e1151715 MD |
335 | * field_definition is for field definitions. They are registered into |
336 | * definition scopes. | |
c13cbf74 | 337 | */ |
e1151715 | 338 | struct definition * |
05c749e5 MD |
339 | lookup_definition(GArray *cur_path, /* array of GQuark */ |
340 | GArray *lookup_path, /* array of GQuark */ | |
341 | struct definition_scope *scope); | |
e1151715 MD |
342 | int register_field_definition(GQuark field_name, |
343 | struct definition *definition, | |
344 | struct definition_scope *scope); | |
345 | struct definition_scope * | |
05c749e5 MD |
346 | new_definition_scope(struct definition_scope *parent_scope, |
347 | GQuark field_name); | |
6a36ddca MD |
348 | void set_dynamic_definition_scope(struct definition *definition, |
349 | struct definition_scope *scope, | |
d00d17d1 | 350 | const char *root_name); |
e1151715 | 351 | void free_definition_scope(struct definition_scope *scope); |
4c8bfb7e | 352 | |
f6625916 MD |
353 | void declaration_ref(struct declaration *declaration); |
354 | void declaration_unref(struct declaration *declaration); | |
64893f33 | 355 | |
e1151715 MD |
356 | void definition_ref(struct definition *definition); |
357 | void definition_unref(struct definition *definition); | |
698f0fe4 | 358 | |
add40b62 | 359 | struct declaration_integer *integer_declaration_new(size_t len, int byte_order, |
e19c3d69 | 360 | int signedness, size_t alignment); |
0a46062b | 361 | |
11d43b90 MD |
362 | /* |
363 | * mantissa_len is the length of the number of bytes represented by the mantissa | |
364 | * (e.g. result of DBL_MANT_DIG). It includes the leading 1. | |
365 | */ | |
add40b62 | 366 | struct declaration_float *float_declaration_new(size_t mantissa_len, |
e19c3d69 MD |
367 | size_t exp_len, int byte_order, |
368 | size_t alignment); | |
0a46062b | 369 | |
448d3cc7 MD |
370 | /* |
371 | * A GQuark can be translated to/from strings with g_quark_from_string() and | |
372 | * g_quark_to_string(). | |
373 | */ | |
47e0f2e2 MD |
374 | |
375 | /* | |
376 | * Returns a GArray of GQuark or NULL. | |
377 | * Caller must release the GArray with g_array_unref(). | |
378 | */ | |
f6625916 MD |
379 | GArray *enum_uint_to_quark_set(const struct declaration_enum *enum_declaration, |
380 | uint64_t v); | |
d65d8abb MD |
381 | |
382 | /* | |
47e0f2e2 | 383 | * Returns a GArray of GQuark or NULL. |
d65d8abb MD |
384 | * Caller must release the GArray with g_array_unref(). |
385 | */ | |
f6625916 MD |
386 | GArray *enum_int_to_quark_set(const struct declaration_enum *enum_declaration, |
387 | uint64_t v); | |
47e0f2e2 MD |
388 | |
389 | /* | |
390 | * Returns a GArray of struct enum_range or NULL. | |
fdacfb73 MD |
391 | * Callers do _not_ own the returned GArray (and therefore _don't_ need to |
392 | * release it). | |
47e0f2e2 | 393 | */ |
f6625916 MD |
394 | GArray *enum_quark_to_range_set(const struct declaration_enum *enum_declaration, |
395 | GQuark q); | |
396 | void enum_signed_insert(struct declaration_enum *enum_declaration, | |
d65d8abb | 397 | int64_t start, int64_t end, GQuark q); |
f6625916 | 398 | void enum_unsigned_insert(struct declaration_enum *enum_declaration, |
d65d8abb | 399 | uint64_t start, uint64_t end, GQuark q); |
f6625916 MD |
400 | size_t enum_get_nr_enumerators(struct declaration_enum *enum_declaration); |
401 | ||
402 | struct declaration_enum * | |
ab4cf058 | 403 | enum_declaration_new(struct declaration_integer *integer_declaration); |
f6625916 | 404 | |
e397791f MD |
405 | struct declaration_string * |
406 | string_declaration_new(enum ctf_string_encoding encoding); | |
407 | ||
f6625916 | 408 | struct declaration_struct * |
ab4cf058 | 409 | struct_declaration_new(struct declaration_scope *parent_scope); |
f6625916 MD |
410 | void struct_declaration_add_field(struct declaration_struct *struct_declaration, |
411 | const char *field_name, | |
412 | struct declaration *field_declaration); | |
11796b96 MD |
413 | /* |
414 | * Returns the index of a field within a structure. | |
415 | */ | |
0f980a35 | 416 | int struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration, |
f6625916 | 417 | GQuark field_name); |
11796b96 MD |
418 | /* |
419 | * field returned only valid as long as the field structure is not appended to. | |
420 | */ | |
f6625916 MD |
421 | struct declaration_field * |
422 | struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration, | |
0f980a35 | 423 | int index); |
e19c3d69 | 424 | struct field * |
0f980a35 MD |
425 | struct_definition_get_field_from_index(struct definition_struct *struct_definition, |
426 | int index); | |
11796b96 | 427 | |
c054553d MD |
428 | /* |
429 | * The tag enumeration is validated to ensure that it contains only mappings | |
430 | * from numeric values to a single tag. Overlapping tag value ranges are | |
431 | * therefore forbidden. | |
432 | */ | |
ab4cf058 | 433 | struct declaration_untagged_variant *untagged_variant_declaration_new( |
1934b94f | 434 | struct declaration_scope *parent_scope); |
d60cb676 MD |
435 | struct declaration_variant *variant_declaration_new(struct declaration_untagged_variant *untagged_variant, |
436 | const char *tag); | |
437 | ||
438 | void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration, | |
439 | const char *field_name, | |
440 | struct declaration *field_declaration); | |
f6625916 | 441 | struct declaration_field * |
d60cb676 | 442 | untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration, |
1934b94f | 443 | GQuark tag); |
c054553d MD |
444 | /* |
445 | * Returns 0 on success, -EPERM on error. | |
446 | */ | |
e1151715 MD |
447 | int variant_definition_set_tag(struct definition_variant *variant, |
448 | struct definition *enum_tag); | |
c054553d MD |
449 | /* |
450 | * Returns the field selected by the current tag value. | |
451 | * field returned only valid as long as the variant structure is not appended | |
452 | * to. | |
453 | */ | |
f6625916 | 454 | struct field *variant_get_current_field(struct definition_variant *variant); |
c054553d | 455 | |
d06d03db | 456 | /* |
f6625916 MD |
457 | * elem_declaration passed as parameter now belongs to the array. No |
458 | * need to free it explicitly. "len" is the number of elements in the | |
459 | * array. | |
d06d03db | 460 | */ |
f6625916 | 461 | struct declaration_array * |
ab4cf058 | 462 | array_declaration_new(size_t len, struct declaration *elem_declaration, |
1934b94f | 463 | struct declaration_scope *parent_scope); |
3838df27 | 464 | uint64_t array_len(struct definition_array *array); |
0f980a35 | 465 | struct definition *array_index(struct definition_array *array, uint64_t i); |
11796b96 | 466 | |
d06d03db | 467 | /* |
f6625916 MD |
468 | * int_declaration and elem_declaration passed as parameter now belong |
469 | * to the sequence. No need to free them explicitly. | |
d06d03db | 470 | */ |
f6625916 | 471 | struct declaration_sequence * |
ab4cf058 | 472 | sequence_declaration_new(struct declaration_integer *len_declaration, |
1934b94f MD |
473 | struct declaration *elem_declaration, |
474 | struct declaration_scope *parent_scope); | |
3838df27 | 475 | uint64_t sequence_len(struct definition_sequence *sequence); |
0f980a35 | 476 | struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i); |
11796b96 | 477 | |
d60cb676 MD |
478 | /* |
479 | * in: path (dot separated), out: q (GArray of GQuark) | |
480 | */ | |
481 | void append_scope_path(const char *path, GArray *q); | |
482 | ||
f6625916 | 483 | #endif /* _BABELTRACE_declarationS_H */ |