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