2348823f65b47dbaa6afea95f780d5b8561a5eff
[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 EfficiOS Inc. and Linux Foundation
10 *
11 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * SOFTWARE.
30 */
31
32 #include <babeltrace/align.h>
33 #include <babeltrace/list.h>
34 #include <babeltrace/ctf/events.h>
35 #include <stdbool.h>
36 #include <stdint.h>
37 #include <limits.h>
38 #include <string.h>
39 #include <glib.h>
40 #include <assert.h>
41
42 /* Preallocate this many fields for structures */
43 #define DEFAULT_NR_STRUCT_FIELDS 8
44
45 struct ctf_stream_definition;
46 struct stream_pos;
47 struct format;
48 struct definition;
49 struct ctf_clock;
50
51 /* type scope */
52 struct declaration_scope {
53 /* Hash table mapping type name GQuark to "struct declaration" */
54 /* Used for both typedef and typealias. */
55 GHashTable *typedef_declarations;
56 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
57 GHashTable *struct_declarations;
58 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
59 GHashTable *variant_declarations;
60 /* Hash table mapping enum name GQuark to "struct type_enum" */
61 GHashTable *enum_declarations;
62 struct declaration_scope *parent_scope;
63 };
64
65 /* definition scope */
66 struct definition_scope {
67 /* Hash table mapping field name GQuark to "struct definition" */
68 GHashTable *definitions;
69 struct definition_scope *parent_scope;
70 /*
71 * Complete "path" leading to this definition scope.
72 * Includes dynamic scope name '.' field name '.' field name '.' ....
73 * Array of GQuark elements (which are each separated by dots).
74 * The dynamic scope name can contain dots, and is encoded into
75 * a single GQuark. Thus, scope_path[0] returns the GQuark
76 * identifying the dynamic scope.
77 */
78 GArray *scope_path; /* array of GQuark */
79 };
80
81 struct declaration {
82 enum ctf_type_id id;
83 size_t alignment; /* type alignment, in bits */
84 int ref; /* number of references to the type */
85 /*
86 * declaration_free called with declaration ref is decremented to 0.
87 */
88 void (*declaration_free)(struct declaration *declaration);
89 struct definition *
90 (*definition_new)(struct declaration *declaration,
91 struct definition_scope *parent_scope,
92 GQuark field_name, int index,
93 const char *root_name);
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 GQuark name; /* Field name in its container (or 0 if unset) */
104 int ref; /* number of references to the definition */
105 GQuark path;
106 struct definition_scope *scope;
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_definition *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 enum ctf_string_encoding encoding;
143 struct ctf_clock *clock;
144 };
145
146 struct definition_integer {
147 struct definition p;
148 struct declaration_integer *declaration;
149 /* Last values read */
150 union {
151 uint64_t _unsigned;
152 int64_t _signed;
153 } value;
154 };
155
156 struct declaration_float {
157 struct declaration p;
158 struct declaration_integer *sign;
159 struct declaration_integer *mantissa;
160 struct declaration_integer *exp;
161 int byte_order;
162 /* TODO: we might want to express more info about NaN, +inf and -inf */
163 };
164
165 struct definition_float {
166 struct definition p;
167 struct declaration_float *declaration;
168 struct definition_integer *sign;
169 struct definition_integer *mantissa;
170 struct definition_integer *exp;
171 /* Last values read */
172 double value;
173 };
174
175 /*
176 * enum_val_equal assumes that signed and unsigned memory layout overlap.
177 */
178 struct enum_range {
179 union {
180 int64_t _signed;
181 uint64_t _unsigned;
182 } start; /* lowest range value */
183 union {
184 int64_t _signed;
185 uint64_t _unsigned;
186 } end; /* highest range value */
187 };
188
189 struct enum_range_to_quark {
190 struct bt_list_head node;
191 struct enum_range range;
192 GQuark quark;
193 };
194
195 /*
196 * We optimize the common case (range of size 1: single value) by creating a
197 * hash table mapping values to quark sets. We then lookup the ranges to
198 * complete the quark set.
199 *
200 * TODO: The proper structure to hold the range to quark set mapping would be an
201 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
202 * time. Using a simple O(n) list search for now for implementation speed and
203 * given that we can expect to have a _relatively_ small number of enumeration
204 * ranges. This might become untrue if we are fed with symbol tables often
205 * required to lookup function names from instruction pointer value.
206 */
207 struct enum_table {
208 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
209 struct bt_list_head range_to_quark; /* (range, GQuark) */
210 GHashTable *quark_to_range_set; /* (GQuark, range GArray) */
211 };
212
213 struct declaration_enum {
214 struct declaration p;
215 struct declaration_integer *integer_declaration;
216 struct enum_table table;
217 };
218
219 struct definition_enum {
220 struct definition p;
221 struct definition_integer *integer;
222 struct declaration_enum *declaration;
223 /* Last GQuark values read. Keeping a reference on the GQuark array. */
224 GArray *value;
225 };
226
227 struct declaration_string {
228 struct declaration p;
229 enum ctf_string_encoding encoding;
230 };
231
232 struct definition_string {
233 struct definition p;
234 struct declaration_string *declaration;
235 char *value; /* freed at definition_string teardown */
236 size_t len, alloc_len;
237 };
238
239 struct declaration_field {
240 GQuark name;
241 struct declaration *declaration;
242 };
243
244 struct declaration_struct {
245 struct declaration p;
246 GHashTable *fields_by_name; /* Tuples (field name, field index) */
247 struct declaration_scope *scope;
248 GArray *fields; /* Array of declaration_field */
249 };
250
251 struct definition_struct {
252 struct definition p;
253 struct declaration_struct *declaration;
254 GPtrArray *fields; /* Array of pointers to struct definition */
255 };
256
257 struct declaration_untagged_variant {
258 struct declaration p;
259 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
260 struct declaration_scope *scope;
261 GArray *fields; /* Array of declaration_field */
262 };
263
264 struct declaration_variant {
265 struct declaration p;
266 struct declaration_untagged_variant *untagged_variant;
267 GArray *tag_name; /* Array of GQuark */
268 };
269
270 /* A variant needs to be tagged to be defined. */
271 struct definition_variant {
272 struct definition p;
273 struct declaration_variant *declaration;
274 struct definition *enum_tag;
275 GPtrArray *fields; /* Array of pointers to struct definition */
276 struct definition *current_field; /* Last field read */
277 };
278
279 struct declaration_array {
280 struct declaration p;
281 size_t len;
282 struct declaration *elem;
283 struct declaration_scope *scope;
284 };
285
286 struct definition_array {
287 struct definition p;
288 struct declaration_array *declaration;
289 GPtrArray *elems; /* Array of pointers to struct definition */
290 GString *string; /* String for encoded integer children */
291 };
292
293 struct declaration_sequence {
294 struct declaration p;
295 GArray *length_name; /* Array of GQuark */
296 struct declaration *elem;
297 struct declaration_scope *scope;
298 };
299
300 struct definition_sequence {
301 struct definition p;
302 struct declaration_sequence *declaration;
303 struct definition_integer *length;
304 GPtrArray *elems; /* Array of pointers to struct definition */
305 GString *string; /* String for encoded integer children */
306 };
307
308 int register_declaration(GQuark declaration_name,
309 struct declaration *declaration,
310 struct declaration_scope *scope);
311 struct declaration *lookup_declaration(GQuark declaration_name,
312 struct declaration_scope *scope);
313
314 /*
315 * Type scopes also contain a separate registry for struct, variant and
316 * enum types. Those register types rather than type definitions, so
317 * that a named variant can be declared without specifying its target
318 * "choice" tag field immediately.
319 */
320 int register_struct_declaration(GQuark struct_name,
321 struct declaration_struct *struct_declaration,
322 struct declaration_scope *scope);
323 struct declaration_struct *
324 lookup_struct_declaration(GQuark struct_name,
325 struct declaration_scope *scope);
326 int register_variant_declaration(GQuark variant_name,
327 struct declaration_untagged_variant *untagged_variant_declaration,
328 struct declaration_scope *scope);
329 struct declaration_untagged_variant *lookup_variant_declaration(GQuark variant_name,
330 struct declaration_scope *scope);
331 int register_enum_declaration(GQuark enum_name,
332 struct declaration_enum *enum_declaration,
333 struct declaration_scope *scope);
334 struct declaration_enum *
335 lookup_enum_declaration(GQuark enum_name,
336 struct declaration_scope *scope);
337
338 struct declaration_scope *
339 new_declaration_scope(struct declaration_scope *parent_scope);
340 void free_declaration_scope(struct declaration_scope *scope);
341
342 /*
343 * field_definition is for field definitions. They are registered into
344 * definition scopes.
345 */
346 struct definition *
347 lookup_path_definition(GArray *cur_path, /* array of GQuark */
348 GArray *lookup_path, /* array of GQuark */
349 struct definition_scope *scope);
350 int register_field_definition(GQuark field_name,
351 struct definition *definition,
352 struct definition_scope *scope);
353 struct definition_scope *
354 new_definition_scope(struct definition_scope *parent_scope,
355 GQuark field_name, const char *root_name);
356 void free_definition_scope(struct definition_scope *scope);
357
358 GQuark new_definition_path(struct definition_scope *parent_scope,
359 GQuark field_name, const char *root_name);
360
361 static inline
362 int compare_definition_path(struct definition *definition, GQuark path)
363 {
364 return definition->path == path;
365 }
366
367 void bt_declaration_ref(struct declaration *declaration);
368 void bt_declaration_unref(struct declaration *declaration);
369
370 void definition_ref(struct definition *definition);
371 void definition_unref(struct definition *definition);
372
373 struct declaration_integer *integer_declaration_new(size_t len, int byte_order,
374 int signedness, size_t alignment,
375 int base, enum ctf_string_encoding encoding,
376 struct ctf_clock *clock);
377 uint64_t get_unsigned_int(const struct definition *field);
378 int64_t get_signed_int(const struct definition *field);
379 int get_int_signedness(const struct definition *field);
380 int get_int_byte_order(const struct definition *field);
381 int get_int_base(const struct definition *field);
382 size_t get_int_len(const struct definition *field); /* in bits */
383 enum ctf_string_encoding get_int_encoding(const struct definition *field);
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 int64_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 char *get_string(const struct definition *field);
431 enum ctf_string_encoding get_string_encoding(const struct definition *field);
432
433 struct declaration_struct *
434 struct_declaration_new(struct declaration_scope *parent_scope,
435 uint64_t min_align);
436 void struct_declaration_add_field(struct declaration_struct *struct_declaration,
437 const char *field_name,
438 struct declaration *field_declaration);
439 /*
440 * Returns the index of a field within a structure.
441 */
442 int struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
443 GQuark field_name);
444 /*
445 * field returned only valid as long as the field structure is not appended to.
446 */
447 struct declaration_field *
448 struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
449 int index);
450 struct definition *
451 struct_definition_get_field_from_index(struct definition_struct *struct_definition,
452 int index);
453 int struct_rw(struct stream_pos *pos, struct definition *definition);
454 uint64_t struct_declaration_len(struct declaration_struct *struct_declaration);
455
456 /*
457 * The tag enumeration is validated to ensure that it contains only mappings
458 * from numeric values to a single tag. Overlapping tag value ranges are
459 * therefore forbidden.
460 */
461 struct declaration_untagged_variant *untagged_variant_declaration_new(
462 struct declaration_scope *parent_scope);
463 struct declaration_variant *variant_declaration_new(struct declaration_untagged_variant *untagged_variant,
464 const char *tag);
465
466 void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
467 const char *field_name,
468 struct declaration *field_declaration);
469 struct declaration_field *
470 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration,
471 GQuark tag);
472 /*
473 * Returns 0 on success, -EPERM on error.
474 */
475 int variant_definition_set_tag(struct definition_variant *variant,
476 struct definition *enum_tag);
477 /*
478 * Returns the field selected by the current tag value.
479 * field returned only valid as long as the variant structure is not appended
480 * to.
481 */
482 struct definition *variant_get_current_field(struct definition_variant *variant);
483 int variant_rw(struct stream_pos *pos, struct definition *definition);
484
485 /*
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.
489 */
490 struct declaration_array *
491 bt_array_declaration_new(size_t len, struct declaration *elem_declaration,
492 struct declaration_scope *parent_scope);
493 uint64_t bt_array_len(struct definition_array *array);
494 struct definition *bt_array_index(struct definition_array *array, uint64_t i);
495 int bt_array_rw(struct stream_pos *pos, struct definition *definition);
496 GString *bt_get_char_array(const struct definition *field);
497 int bt_get_array_len(const struct definition *field);
498
499 /*
500 * int_declaration and elem_declaration passed as parameter now belong
501 * to the sequence. No need to free them explicitly.
502 */
503 struct declaration_sequence *
504 sequence_declaration_new(const char *length_name,
505 struct declaration *elem_declaration,
506 struct declaration_scope *parent_scope);
507 uint64_t sequence_len(struct definition_sequence *sequence);
508 struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i);
509 int sequence_rw(struct stream_pos *pos, struct definition *definition);
510
511 /*
512 * in: path (dot separated), out: q (GArray of GQuark)
513 */
514 void bt_append_scope_path(const char *path, GArray *q);
515
516 /*
517 * Lookup helpers.
518 */
519 struct definition *lookup_definition(const struct definition *definition,
520 const char *field_name);
521 struct definition_integer *lookup_integer(const struct definition *definition,
522 const char *field_name,
523 int signedness);
524 struct definition_enum *bt_lookup_enum(const struct definition *definition,
525 const char *field_name,
526 int signedness);
527 struct definition *lookup_variant(const struct definition *definition,
528 const char *field_name);
529
530 static inline
531 const char *rem_(const char *str)
532 {
533 if (str[0] == '_')
534 return &str[1];
535 else
536 return str;
537 }
538
539 #endif /* _BABELTRACE_TYPES_H */
This page took 0.050846 seconds and 3 git commands to generate.