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