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