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; |
c054553d | 88 | struct type; |
4c8bfb7e | 89 | |
fc93b2bd MD |
90 | struct type_class { |
91 | GQuark name; /* type name */ | |
92 | size_t alignment; /* type alignment, in bits */ | |
c054553d | 93 | int ref; /* number of references to the type class */ |
fc93b2bd | 94 | /* |
c054553d MD |
95 | * class_free called with type class ref is decremented to 0. |
96 | */ | |
97 | void (*class_free)(struct type_class *type_class); | |
98 | struct type *(*type_new)(struct type_class *_class, | |
99 | struct declaration_scope *parent_scope); | |
100 | /* | |
101 | * type_free called with type ref is decremented to 0. | |
102 | */ | |
103 | void (*type_free)(struct type *type); | |
104 | /* | |
105 | * Type copy function. Knows how to find the child type from the parent | |
106 | * type. | |
fc93b2bd | 107 | */ |
4c8bfb7e MD |
108 | void (*copy)(struct stream_pos *dest, const struct format *fdest, |
109 | struct stream_pos *src, const struct format *fsrc, | |
c054553d MD |
110 | struct type *type); |
111 | }; | |
112 | ||
113 | struct type { | |
114 | struct type_class *_class; | |
115 | int ref; /* number of references to the type instance */ | |
fc93b2bd MD |
116 | }; |
117 | ||
bed864a7 MD |
118 | /* |
119 | * Because we address in bits, bitfields end up being exactly the same as | |
120 | * integers, except that their read/write functions must be able to deal with | |
121 | * read/write non aligned on CHAR_BIT. | |
122 | */ | |
7fe00194 MD |
123 | struct type_class_integer { |
124 | struct type_class p; | |
125 | size_t len; /* length, in bits. */ | |
126 | int byte_order; /* byte order */ | |
127 | int signedness; | |
fc93b2bd MD |
128 | }; |
129 | ||
c054553d MD |
130 | struct type_integer { |
131 | struct type p; | |
132 | struct type_class_integer *_class; | |
133 | /* Last values read */ | |
134 | union { | |
135 | uint64_t _unsigned; | |
136 | int64_t _signed; | |
137 | } value; | |
138 | }; | |
139 | ||
fc93b2bd MD |
140 | struct type_class_float { |
141 | struct type_class p; | |
4c8bfb7e MD |
142 | struct type_class_integer *sign; |
143 | struct type_class_integer *mantissa; | |
144 | struct type_class_integer *exp; | |
fc93b2bd | 145 | int byte_order; |
0a46062b | 146 | /* TODO: we might want to express more info about NaN, +inf and -inf */ |
fc93b2bd MD |
147 | }; |
148 | ||
c054553d MD |
149 | struct type_float { |
150 | struct type p; | |
151 | struct type_class_float *_class; | |
152 | /* Last values read */ | |
153 | long double value; | |
154 | }; | |
155 | ||
d65d8abb MD |
156 | /* |
157 | * enum_val_equal assumes that signed and unsigned memory layout overlap. | |
158 | */ | |
159 | struct enum_range { | |
160 | union { | |
161 | int64_t _signed; | |
162 | uint64_t _unsigned; | |
163 | } start; /* lowest range value */ | |
164 | union { | |
165 | int64_t _signed; | |
166 | uint64_t _unsigned; | |
167 | } end; /* highest range value */ | |
168 | }; | |
169 | ||
170 | struct enum_range_to_quark { | |
171 | struct cds_list_head node; | |
172 | struct enum_range range; | |
173 | GQuark quark; | |
174 | }; | |
175 | ||
176 | /* | |
177 | * We optimize the common case (range of size 1: single value) by creating a | |
178 | * hash table mapping values to quark sets. We then lookup the ranges to | |
179 | * complete the quark set. | |
180 | * | |
181 | * TODO: The proper structure to hold the range to quark set mapping would be an | |
182 | * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query | |
183 | * time. Using a simple O(n) list search for now for implementation speed and | |
184 | * given that we can expect to have a _relatively_ small number of enumeration | |
185 | * ranges. This might become untrue if we are fed with symbol tables often | |
186 | * required to lookup function names from instruction pointer value. | |
187 | */ | |
448d3cc7 | 188 | struct enum_table { |
d65d8abb MD |
189 | GHashTable *value_to_quark_set; /* (value, GQuark GArray) */ |
190 | struct cds_list_head range_to_quark; /* (range, GQuark) */ | |
191 | GHashTable *quark_to_range_set; /* (GQuark, range GArray) */ | |
448d3cc7 MD |
192 | }; |
193 | ||
fc93b2bd | 194 | struct type_class_enum { |
4c8bfb7e | 195 | struct type_class_integer p; /* inherit from integer */ |
448d3cc7 | 196 | struct enum_table table; |
fc93b2bd MD |
197 | }; |
198 | ||
c054553d MD |
199 | struct type_enum { |
200 | struct type p; | |
201 | struct type_class_enum *_class; | |
202 | /* Last GQuark values read. Keeping a reference on the GQuark array. */ | |
203 | GArray *value; | |
204 | }; | |
205 | ||
bed864a7 MD |
206 | struct type_class_string { |
207 | struct type_class p; | |
208 | }; | |
209 | ||
c054553d MD |
210 | struct type_string { |
211 | struct type p; | |
212 | struct type_class_string *_class; | |
213 | char *value; /* freed at type_string teardown */ | |
214 | }; | |
215 | ||
216 | struct type_class_field { | |
11796b96 MD |
217 | GQuark name; |
218 | struct type_class *type_class; | |
219 | }; | |
220 | ||
c054553d MD |
221 | struct field { |
222 | struct type *type; | |
223 | }; | |
224 | ||
fc93b2bd MD |
225 | struct type_class_struct { |
226 | struct type_class p; | |
11796b96 | 227 | GHashTable *fields_by_name; /* Tuples (field name, field index) */ |
c054553d MD |
228 | GArray *fields; /* Array of type_class_field */ |
229 | }; | |
230 | ||
231 | struct type_struct { | |
232 | struct type p; | |
233 | struct type_class_struct *_class; | |
234 | struct declaration_scope *scope; | |
235 | GArray *fields; /* Array of struct field */ | |
236 | }; | |
237 | ||
238 | struct type_class_variant { | |
239 | struct type_class p; | |
240 | GHashTable *fields_by_tag; /* Tuples (field tag, field index) */ | |
241 | GArray *fields; /* Array of type_class_field */ | |
242 | }; | |
243 | ||
244 | struct type_variant { | |
245 | struct type p; | |
246 | struct type_class_variant *_class; | |
247 | struct declaration_scope *scope; | |
248 | struct type *tag; | |
249 | GArray *fields; /* Array of struct field */ | |
250 | struct field *current_field; /* Last field read */ | |
11796b96 MD |
251 | }; |
252 | ||
253 | struct type_class_array { | |
254 | struct type_class p; | |
255 | size_t len; | |
256 | struct type_class *elem; | |
257 | }; | |
258 | ||
c054553d MD |
259 | struct type_array { |
260 | struct type p; | |
261 | struct type_class_array *_class; | |
262 | struct declaration_scope *scope; | |
263 | struct field current_element; /* struct field */ | |
264 | }; | |
265 | ||
11796b96 MD |
266 | struct type_class_sequence { |
267 | struct type_class p; | |
2e7d72cf | 268 | struct type_class_integer *len_class; |
11796b96 | 269 | struct type_class *elem; |
fc93b2bd MD |
270 | }; |
271 | ||
c054553d MD |
272 | struct type_sequence { |
273 | struct type p; | |
274 | struct type_class_sequence *_class; | |
275 | struct declaration_scope *scope; | |
276 | struct type_integer *len; | |
277 | struct field current_element; /* struct field */ | |
278 | }; | |
279 | ||
280 | /* Type declaration scope */ | |
281 | struct declaration_scope { | |
282 | /* Hash table mapping type name GQuark to struct type_class */ | |
283 | GHashTable *type_classes; | |
284 | struct declaration_scope *parent_scope; | |
285 | }; | |
286 | ||
287 | struct type_class *lookup_type_class(GQuark qname, | |
288 | struct declaration_scope *scope); | |
289 | int register_type_class(struct type_class *type_class, | |
290 | struct declaration_scope *scope); | |
291 | ||
292 | void type_class_ref(struct type_class *type_class); | |
293 | void type_class_unref(struct type_class *type_class); | |
294 | ||
295 | struct declaration_scope * | |
296 | new_declaration_scope(struct declaration_scope *parent_scope); | |
297 | void free_declaration_scope(struct declaration_scope *scope); | |
4c8bfb7e | 298 | |
c054553d MD |
299 | void type_ref(struct type *type); |
300 | void type_unref(struct type *type); | |
698f0fe4 | 301 | |
0a46062b MD |
302 | /* Nameless types can be created by passing a NULL name */ |
303 | ||
c054553d | 304 | struct type_class_integer *integer_type_class_new(const char *name, |
0a46062b | 305 | size_t len, int byte_order, |
11d43b90 MD |
306 | int signedness, |
307 | size_t alignment); | |
0a46062b | 308 | |
11d43b90 MD |
309 | /* |
310 | * mantissa_len is the length of the number of bytes represented by the mantissa | |
311 | * (e.g. result of DBL_MANT_DIG). It includes the leading 1. | |
312 | */ | |
c054553d | 313 | struct type_class_float *float_type_class_new(const char *name, |
0a46062b MD |
314 | size_t mantissa_len, |
315 | size_t exp_len, int byte_order, | |
316 | size_t alignment); | |
0a46062b | 317 | |
448d3cc7 MD |
318 | /* |
319 | * A GQuark can be translated to/from strings with g_quark_from_string() and | |
320 | * g_quark_to_string(). | |
321 | */ | |
47e0f2e2 MD |
322 | |
323 | /* | |
324 | * Returns a GArray of GQuark or NULL. | |
325 | * Caller must release the GArray with g_array_unref(). | |
326 | */ | |
d65d8abb MD |
327 | GArray *enum_uint_to_quark_set(const struct type_class_enum *enum_class, |
328 | uint64_t v); | |
329 | ||
330 | /* | |
47e0f2e2 | 331 | * Returns a GArray of GQuark or NULL. |
d65d8abb MD |
332 | * Caller must release the GArray with g_array_unref(). |
333 | */ | |
334 | GArray *enum_int_to_quark_set(const struct type_class_enum *enum_class, | |
335 | uint64_t v); | |
47e0f2e2 MD |
336 | |
337 | /* | |
338 | * Returns a GArray of struct enum_range or NULL. | |
fdacfb73 MD |
339 | * Callers do _not_ own the returned GArray (and therefore _don't_ need to |
340 | * release it). | |
47e0f2e2 | 341 | */ |
d65d8abb MD |
342 | GArray *enum_quark_to_range_set(const struct type_class_enum *enum_class, |
343 | GQuark q); | |
448d3cc7 | 344 | void enum_signed_insert(struct type_class_enum *enum_class, |
d65d8abb | 345 | int64_t start, int64_t end, GQuark q); |
448d3cc7 | 346 | void enum_unsigned_insert(struct type_class_enum *enum_class, |
d65d8abb | 347 | uint64_t start, uint64_t end, GQuark q); |
c054553d | 348 | size_t enum_get_nr_enumerators(struct type_class_enum *enum_class); |
448d3cc7 | 349 | |
c054553d | 350 | struct type_class_enum *enum_type_class_new(const char *name, |
448d3cc7 MD |
351 | size_t len, int byte_order, |
352 | int signedness, | |
353 | size_t alignment); | |
448d3cc7 | 354 | |
c054553d MD |
355 | struct type_class_struct *struct_type_class_new(const char *name); |
356 | void struct_type_class_add_field(struct type_class_struct *struct_class, | |
357 | const char *field_name, | |
358 | struct type_class *type_class); | |
11796b96 MD |
359 | /* |
360 | * Returns the index of a field within a structure. | |
361 | */ | |
362 | unsigned long | |
c054553d MD |
363 | struct_type_class_lookup_field_index(struct type_class_struct *struct_class, |
364 | GQuark field_name); | |
11796b96 MD |
365 | /* |
366 | * field returned only valid as long as the field structure is not appended to. | |
367 | */ | |
c054553d MD |
368 | struct type_class_field * |
369 | struct_type_class_get_field_from_index(struct type_class_struct *struct_class, | |
370 | unsigned long index); | |
11796b96 | 371 | struct field * |
c054553d | 372 | struct_type_get_field_from_index(struct type_struct *_struct, |
11796b96 MD |
373 | unsigned long index); |
374 | ||
c054553d MD |
375 | /* |
376 | * The tag enumeration is validated to ensure that it contains only mappings | |
377 | * from numeric values to a single tag. Overlapping tag value ranges are | |
378 | * therefore forbidden. | |
379 | */ | |
380 | struct type_class_variant *variant_type_class_new(const char *name); | |
381 | void variant_type_class_add_field(struct type_class_variant *variant_class, | |
382 | const char *tag_name, | |
383 | struct type_class *type_class); | |
384 | struct type_class_field * | |
385 | variant_type_class_get_field_from_tag(struct type_class_variant *variant_class, | |
386 | GQuark tag); | |
387 | /* | |
388 | * Returns 0 on success, -EPERM on error. | |
389 | */ | |
390 | int variant_type_set_tag(struct type_variant *variant, | |
391 | struct type *enum_tag_instance); | |
392 | /* | |
393 | * Returns the field selected by the current tag value. | |
394 | * field returned only valid as long as the variant structure is not appended | |
395 | * to. | |
396 | */ | |
397 | struct field * | |
398 | variant_type_get_current_field(struct type_variant *variant); | |
399 | ||
d06d03db MD |
400 | /* |
401 | * elem_class passed as parameter now belongs to the array. No need to free it | |
c054553d | 402 | * explicitly. "len" is the number of elements in the array. |
d06d03db | 403 | */ |
c054553d | 404 | struct type_class_array *array_type_class_new(const char *name, |
11796b96 MD |
405 | size_t len, |
406 | struct type_class *elem_class); | |
11796b96 | 407 | |
d06d03db MD |
408 | /* |
409 | * int_class and elem_class passed as parameter now belongs to the sequence. No | |
c054553d | 410 | * need to free them explicitly. |
d06d03db | 411 | */ |
c054553d | 412 | struct type_class_sequence *sequence_type_class_new(const char *name, |
2e7d72cf | 413 | struct type_class_integer *len_class, |
11796b96 | 414 | struct type_class *elem_class); |
11796b96 | 415 | |
d79865b9 | 416 | #endif /* _BABELTRACE_TYPES_H */ |