1 #ifndef _BABELTRACE_TYPES_H
2 #define _BABELTRACE_TYPES_H
9 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
22 #include <babeltrace/align.h>
23 #include <babeltrace/list.h>
31 /* Preallocate this many fields for structures */
32 #define DEFAULT_NR_STRUCT_FIELDS 8
38 /* Parent of per-plugin positions */
43 struct 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
;
56 /* definition scope */
57 struct definition_scope
{
58 /* Hash table mapping field name GQuark to "struct definition" */
59 GHashTable
*definitions
;
60 struct definition_scope
*parent_scope
;
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.
69 GArray
*scope_path
; /* array of GQuark */
79 CTF_TYPE_UNTAGGED_VARIANT
,
88 size_t alignment
; /* type alignment, in bits */
89 int ref
; /* number of references to the type */
91 * declaration_free called with declaration ref is decremented to 0.
93 void (*declaration_free
)(struct declaration
*declaration
);
95 (*definition_new
)(struct declaration
*declaration
,
96 struct definition_scope
*parent_scope
,
97 GQuark field_name
, int index
);
99 * definition_free called with definition ref is decremented to 0.
101 void (*definition_free
)(struct definition
*definition
);
103 * Definition copy function. Knows how to find the child
104 * definition from the parent definition.
106 void (*copy
)(struct stream_pos
*dest
, const struct format
*fdest
,
107 struct stream_pos
*src
, const struct format
*fsrc
,
108 struct definition
*definition
);
112 struct declaration
*declaration
;
113 int index
; /* Position of the definition in its container */
114 int ref
; /* number of references to the definition */
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.
122 struct declaration_integer
{
123 struct declaration p
;
124 size_t len
; /* length, in bits. */
125 int byte_order
; /* byte order */
129 struct definition_integer
{
131 struct declaration_integer
*declaration
;
132 /* Last values read */
139 struct declaration_float
{
140 struct declaration p
;
141 struct declaration_integer
*sign
;
142 struct declaration_integer
*mantissa
;
143 struct declaration_integer
*exp
;
145 /* TODO: we might want to express more info about NaN, +inf and -inf */
148 struct definition_float
{
150 struct declaration_float
*declaration
;
151 /* Last values read */
156 * enum_val_equal assumes that signed and unsigned memory layout overlap.
162 } start
; /* lowest range value */
166 } end
; /* highest range value */
169 struct enum_range_to_quark
{
170 struct cds_list_head node
;
171 struct enum_range range
;
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.
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.
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) */
193 struct declaration_enum
{
194 struct declaration p
;
195 struct declaration_integer
*integer_declaration
;
196 struct enum_table table
;
199 struct definition_enum
{
201 struct definition_integer
*integer
;
202 struct declaration_enum
*declaration
;
203 /* Last GQuark values read. Keeping a reference on the GQuark array. */
207 enum ctf_string_encoding
{
213 struct declaration_string
{
214 struct declaration p
;
215 enum ctf_string_encoding encoding
;
218 struct definition_string
{
220 struct declaration_string
*declaration
;
221 char *value
; /* freed at definition_string teardown */
224 struct declaration_field
{
226 struct declaration
*declaration
;
231 struct definition
*definition
;
234 struct 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 */
241 struct definition_struct
{
243 struct declaration_struct
*declaration
;
244 struct definition_scope
*scope
;
245 GArray
*fields
; /* Array of struct field */
248 struct 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 */
255 struct declaration_variant
{
256 struct declaration p
;
257 struct declaration_untagged_variant
*untagged_variant
;
258 GArray
*tag_name
; /* Array of GQuark */
261 /* A variant needs to be tagged to be defined. */
262 struct definition_variant
{
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 */
271 struct declaration_array
{
272 struct declaration p
;
274 struct declaration
*elem
;
275 struct declaration_scope
*scope
;
278 struct definition_array
{
280 struct declaration_array
*declaration
;
281 struct definition_scope
*scope
;
282 GArray
*elems
; /* struct field */
285 struct declaration_sequence
{
286 struct declaration p
;
287 struct declaration_integer
*len_declaration
;
288 struct declaration
*elem
;
289 struct declaration_scope
*scope
;
292 struct definition_sequence
{
294 struct declaration_sequence
*declaration
;
295 struct definition_scope
*scope
;
296 struct definition_integer
*len
;
297 GArray
*elems
; /* struct field */
300 int register_declaration(GQuark declaration_name
,
301 struct declaration
*declaration
,
302 struct declaration_scope
*scope
);
303 struct declaration
*lookup_declaration(GQuark declaration_name
,
304 struct declaration_scope
*scope
);
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.
312 int register_struct_declaration(GQuark struct_name
,
313 struct declaration_struct
*struct_declaration
,
314 struct declaration_scope
*scope
);
315 struct declaration_struct
*
316 lookup_struct_declaration(GQuark struct_name
,
317 struct declaration_scope
*scope
);
318 int register_variant_declaration(GQuark variant_name
,
319 struct declaration_untagged_variant
*untagged_variant_declaration
,
320 struct declaration_scope
*scope
);
321 struct declaration_untagged_variant
*lookup_variant_declaration(GQuark variant_name
,
322 struct declaration_scope
*scope
);
323 int register_enum_declaration(GQuark enum_name
,
324 struct declaration_enum
*enum_declaration
,
325 struct declaration_scope
*scope
);
326 struct declaration_enum
*
327 lookup_enum_declaration(GQuark enum_name
,
328 struct declaration_scope
*scope
);
330 struct declaration_scope
*
331 new_declaration_scope(struct declaration_scope
*parent_scope
);
332 void free_declaration_scope(struct declaration_scope
*scope
);
335 * field_definition is for field definitions. They are registered into
339 lookup_definition(GArray
*cur_path
, /* array of GQuark */
340 GArray
*lookup_path
, /* array of GQuark */
341 struct definition_scope
*scope
);
342 int register_field_definition(GQuark field_name
,
343 struct definition
*definition
,
344 struct definition_scope
*scope
);
345 struct definition_scope
*
346 new_definition_scope(struct definition_scope
*parent_scope
,
348 void set_dynamic_definition_scope(struct definition
*definition
,
349 struct definition_scope
*scope
,
350 const char *root_name
);
351 void free_definition_scope(struct definition_scope
*scope
);
353 void declaration_ref(struct declaration
*declaration
);
354 void declaration_unref(struct declaration
*declaration
);
356 void definition_ref(struct definition
*definition
);
357 void definition_unref(struct definition
*definition
);
359 struct declaration_integer
*integer_declaration_new(size_t len
, int byte_order
,
360 int signedness
, size_t alignment
);
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.
366 struct declaration_float
*float_declaration_new(size_t mantissa_len
,
367 size_t exp_len
, int byte_order
,
371 * A GQuark can be translated to/from strings with g_quark_from_string() and
372 * g_quark_to_string().
376 * Returns a GArray of GQuark or NULL.
377 * Caller must release the GArray with g_array_unref().
379 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
383 * Returns a GArray of GQuark or NULL.
384 * Caller must release the GArray with g_array_unref().
386 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
390 * Returns a GArray of struct enum_range or NULL.
391 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
394 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
396 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
397 int64_t start
, int64_t end
, GQuark q
);
398 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
399 uint64_t start
, uint64_t end
, GQuark q
);
400 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
402 struct declaration_enum
*
403 enum_declaration_new(struct declaration_integer
*integer_declaration
);
405 struct declaration_string
*
406 string_declaration_new(enum ctf_string_encoding encoding
);
408 struct declaration_struct
*
409 struct_declaration_new(struct declaration_scope
*parent_scope
);
410 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
411 const char *field_name
,
412 struct declaration
*field_declaration
);
414 * Returns the index of a field within a structure.
416 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
419 * field returned only valid as long as the field structure is not appended to.
421 struct declaration_field
*
422 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
425 struct_definition_get_field_from_index(struct definition_struct
*struct_definition
,
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.
433 struct declaration_untagged_variant
*untagged_variant_declaration_new(
434 struct declaration_scope
*parent_scope
);
435 struct declaration_variant
*variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
438 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
439 const char *field_name
,
440 struct declaration
*field_declaration
);
441 struct declaration_field
*
442 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
445 * Returns 0 on success, -EPERM on error.
447 int variant_definition_set_tag(struct definition_variant
*variant
,
448 struct definition
*enum_tag
);
450 * Returns the field selected by the current tag value.
451 * field returned only valid as long as the variant structure is not appended
454 struct field
*variant_get_current_field(struct definition_variant
*variant
);
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
461 struct declaration_array
*
462 array_declaration_new(size_t len
, struct declaration
*elem_declaration
,
463 struct declaration_scope
*parent_scope
);
464 uint64_t array_len(struct definition_array
*array
);
465 struct definition
*array_index(struct definition_array
*array
, uint64_t i
);
468 * int_declaration and elem_declaration passed as parameter now belong
469 * to the sequence. No need to free them explicitly.
471 struct declaration_sequence
*
472 sequence_declaration_new(struct declaration_integer
*len_declaration
,
473 struct declaration
*elem_declaration
,
474 struct declaration_scope
*parent_scope
);
475 uint64_t sequence_len(struct definition_sequence
*sequence
);
476 struct definition
*sequence_index(struct definition_sequence
*sequence
, uint64_t i
);
479 * in: path (dot separated), out: q (GArray of GQuark)
481 void append_scope_path(const char *path
, GArray
*q
);
483 #endif /* _BABELTRACE_declarationS_H */