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