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