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
35 * Always update stream_pos with move_pos and init_pos.
38 char *base
; /* Base address */
39 size_t offset
; /* Offset from base, in bits */
40 int dummy
; /* Dummy position, for length calculation */
44 void init_pos(struct stream_pos
*pos
, char *base
)
46 pos
->base
= base
; /* initial base, page-aligned */
52 * move_pos - move position of a relative bit offset
54 * TODO: allow larger files by updating base too.
57 void move_pos(struct stream_pos
*pos
, size_t offset
)
59 pos
->offset
= pos
->offset
+ offset
;
63 * align_pos - align position on a bit offset (> 0)
65 * TODO: allow larger files by updating base too.
68 void align_pos(struct stream_pos
*pos
, size_t offset
)
70 pos
->offset
+= offset_align(pos
->offset
, offset
);
74 void copy_pos(struct stream_pos
*dest
, struct stream_pos
*src
)
76 memcpy(dest
, src
, sizeof(struct stream_pos
));
80 char *get_pos_addr(struct stream_pos
*pos
)
82 /* Only makes sense to get the address after aligning on CHAR_BIT */
83 assert(!(pos
->offset
% CHAR_BIT
));
84 return pos
->base
+ (pos
->offset
/ CHAR_BIT
);
91 struct declaration_scope
{
92 /* Hash table mapping type name GQuark to "struct declaration" */
93 GHashTable
*typedef_declarations
;
94 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
95 GHashTable
*struct_declarations
;
96 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
97 GHashTable
*variant_declarations
;
98 /* Hash table mapping enum name GQuark to "struct type_enum" */
99 GHashTable
*enum_declarations
;
100 struct declaration_scope
*parent_scope
;
103 /* definition scope */
104 struct definition_scope
{
105 /* Hash table mapping field name GQuark to "struct definition" */
106 GHashTable
*definitions
;
107 struct definition_scope
*parent_scope
;
109 * Complete "path" leading to this definition scope.
110 * Includes dynamic scope name '.' field name '.' field name '.' ....
111 * Array of GQuark elements (which are each separated by dots).
112 * The dynamic scope name can contain dots, and is encoded into
113 * a single GQuark. Thus, scope_path[0] returns the GQuark
114 * identifying the dynamic scope.
116 GArray
*scope_path
; /* array of GQuark */
120 CTF_TYPE_UNKNOWN
= 0,
134 GQuark name
; /* type name */
135 size_t alignment
; /* type alignment, in bits */
136 int ref
; /* number of references to the type */
138 * declaration_free called with declaration ref is decremented to 0.
140 void (*declaration_free
)(struct declaration
*declaration
);
142 (*definition_new
)(struct declaration
*declaration
,
143 struct definition_scope
*parent_scope
,
144 GQuark field_name
, int index
);
146 * definition_free called with definition ref is decremented to 0.
148 void (*definition_free
)(struct definition
*definition
);
150 * Definition copy function. Knows how to find the child
151 * definition from the parent definition.
153 void (*copy
)(struct stream_pos
*dest
, const struct format
*fdest
,
154 struct stream_pos
*src
, const struct format
*fsrc
,
155 struct definition
*definition
);
159 struct declaration
*declaration
;
160 int index
; /* Position of the definition in its container */
161 int ref
; /* number of references to the definition */
165 * Because we address in bits, bitfields end up being exactly the same as
166 * integers, except that their read/write functions must be able to deal with
167 * read/write non aligned on CHAR_BIT.
169 struct declaration_integer
{
170 struct declaration p
;
171 size_t len
; /* length, in bits. */
172 int byte_order
; /* byte order */
176 struct definition_integer
{
178 struct declaration_integer
*declaration
;
179 /* Last values read */
186 struct declaration_float
{
187 struct declaration p
;
188 struct declaration_integer
*sign
;
189 struct declaration_integer
*mantissa
;
190 struct declaration_integer
*exp
;
192 /* TODO: we might want to express more info about NaN, +inf and -inf */
195 struct definition_float
{
197 struct declaration_float
*declaration
;
198 /* Last values read */
203 * enum_val_equal assumes that signed and unsigned memory layout overlap.
209 } start
; /* lowest range value */
213 } end
; /* highest range value */
216 struct enum_range_to_quark
{
217 struct cds_list_head node
;
218 struct enum_range range
;
223 * We optimize the common case (range of size 1: single value) by creating a
224 * hash table mapping values to quark sets. We then lookup the ranges to
225 * complete the quark set.
227 * TODO: The proper structure to hold the range to quark set mapping would be an
228 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
229 * time. Using a simple O(n) list search for now for implementation speed and
230 * given that we can expect to have a _relatively_ small number of enumeration
231 * ranges. This might become untrue if we are fed with symbol tables often
232 * required to lookup function names from instruction pointer value.
235 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
236 struct cds_list_head range_to_quark
; /* (range, GQuark) */
237 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
240 struct declaration_enum
{
241 struct declaration p
;
242 struct declaration_integer
*integer_declaration
;
243 struct enum_table table
;
246 struct definition_enum
{
248 struct definition_integer
*integer
;
249 struct declaration_enum
*declaration
;
250 /* Last GQuark values read. Keeping a reference on the GQuark array. */
254 struct declaration_string
{
255 struct declaration p
;
258 struct definition_string
{
260 struct declaration_string
*declaration
;
261 char *value
; /* freed at definition_string teardown */
264 struct declaration_field
{
266 struct declaration
*declaration
;
271 struct definition
*definition
;
274 struct declaration_struct
{
275 struct declaration p
;
276 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
277 struct declaration_scope
*scope
;
278 GArray
*fields
; /* Array of declaration_field */
281 struct definition_struct
{
283 struct declaration_struct
*declaration
;
284 struct definition_scope
*scope
;
285 GArray
*fields
; /* Array of struct field */
288 struct declaration_variant
{
289 struct declaration p
;
290 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
291 struct declaration_scope
*scope
;
292 GArray
*fields
; /* Array of declaration_field */
293 GQuark tag_name
; /* TODO */
294 /* Tag name must be nonzero and must exist when defining the variant */
297 struct definition_variant
{
299 struct declaration_variant
*declaration
;
300 struct definition_scope
*scope
;
301 struct definition
*enum_tag
;
302 GArray
*fields
; /* Array of struct field */
303 struct field
*current_field
; /* Last field read */
306 struct declaration_array
{
307 struct declaration p
;
309 struct declaration
*elem
;
310 struct declaration_scope
*scope
;
313 struct definition_array
{
315 struct declaration_array
*declaration
;
316 struct definition_scope
*scope
;
317 struct field current_element
; /* struct field */
320 struct declaration_sequence
{
321 struct declaration p
;
322 struct declaration_integer
*len_declaration
;
323 struct declaration
*elem
;
324 struct declaration_scope
*scope
;
327 struct definition_sequence
{
329 struct declaration_sequence
*declaration
;
330 struct definition_scope
*scope
;
331 struct definition_integer
*len
;
332 struct field current_element
; /* struct field */
335 int register_declaration(GQuark declaration_name
,
336 struct declaration
*declaration
,
337 struct declaration_scope
*scope
);
338 struct declaration
*lookup_declaration(GQuark declaration_name
,
339 struct declaration_scope
*scope
);
342 * Type scopes also contain a separate registry for struct, variant and
343 * enum types. Those register types rather than type definitions, so
344 * that a named variant can be declared without specifying its target
345 * "choice" tag field immediately.
347 int register_struct_declaration(GQuark struct_name
,
348 struct declaration_struct
*struct_declaration
,
349 struct declaration_scope
*scope
);
350 struct declaration_struct
*
351 lookup_struct_declaration(GQuark struct_name
,
352 struct declaration_scope
*scope
);
353 int register_variant_declaration(GQuark variant_name
,
354 struct declaration_variant
*variant_declaration
,
355 struct declaration_scope
*scope
);
356 struct declaration_variant
*lookup_variant_declaration(GQuark variant_name
,
357 struct declaration_scope
*scope
);
358 int register_enum_declaration(GQuark enum_name
,
359 struct declaration_enum
*enum_declaration
,
360 struct declaration_scope
*scope
);
361 struct declaration_enum
*
362 lookup_enum_declaration(GQuark enum_name
,
363 struct declaration_scope
*scope
);
365 struct declaration_scope
*
366 new_declaration_scope(struct declaration_scope
*parent_scope
);
367 void free_declaration_scope(struct declaration_scope
*scope
);
370 * field_definition is for field definitions. They are registered into
374 lookup_definition(GArray
*cur_path
, /* array of GQuark */
375 GArray
*lookup_path
, /* array of GQuark */
376 struct definition_scope
*scope
);
377 int register_field_definition(GQuark field_name
,
378 struct definition
*definition
,
379 struct definition_scope
*scope
);
380 struct definition_scope
*
381 new_definition_scope(struct definition_scope
*parent_scope
,
383 struct definition_scope
*
384 new_dynamic_definition_scope(struct definition_scope
*parent_scope
,
386 void free_definition_scope(struct definition_scope
*scope
);
388 void declaration_ref(struct declaration
*declaration
);
389 void declaration_unref(struct declaration
*declaration
);
391 void definition_ref(struct definition
*definition
);
392 void definition_unref(struct definition
*definition
);
394 /* Nameless declarations can be created by passing a NULL name */
396 struct declaration_integer
*integer_declaration_new(const char *name
,
397 size_t len
, int byte_order
,
398 int signedness
, size_t alignment
);
401 * mantissa_len is the length of the number of bytes represented by the mantissa
402 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
404 struct declaration_float
*float_declaration_new(const char *name
,
406 size_t exp_len
, int byte_order
,
410 * A GQuark can be translated to/from strings with g_quark_from_string() and
411 * g_quark_to_string().
415 * Returns a GArray of GQuark or NULL.
416 * Caller must release the GArray with g_array_unref().
418 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
422 * Returns a GArray of GQuark or NULL.
423 * Caller must release the GArray with g_array_unref().
425 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
429 * Returns a GArray of struct enum_range or NULL.
430 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
433 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
435 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
436 int64_t start
, int64_t end
, GQuark q
);
437 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
438 uint64_t start
, uint64_t end
, GQuark q
);
439 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
441 struct declaration_enum
*
442 enum_declaration_new(const char *name
,
443 struct declaration_integer
*integer_declaration
);
445 struct declaration_struct
*
446 struct_declaration_new(const char *name
,
447 struct declaration_scope
*parent_scope
);
448 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
449 const char *field_name
,
450 struct declaration
*field_declaration
);
452 * Returns the index of a field within a structure.
454 unsigned long struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
457 * field returned only valid as long as the field structure is not appended to.
459 struct declaration_field
*
460 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
461 unsigned long index
);
463 struct_get_field_from_index(struct definition_struct
*struct_definition
,
464 unsigned long index
);
467 * The tag enumeration is validated to ensure that it contains only mappings
468 * from numeric values to a single tag. Overlapping tag value ranges are
469 * therefore forbidden.
471 struct declaration_variant
*variant_declaration_new(const char *name
,
472 struct declaration_scope
*parent_scope
);
473 void variant_declaration_add_field(struct declaration_variant
*variant_declaration
,
474 const char *tag_name
,
475 struct declaration
*tag_declaration
);
476 struct declaration_field
*
477 variant_declaration_get_field_from_tag(struct declaration_variant
*variant_declaration
,
480 * Returns 0 on success, -EPERM on error.
482 int variant_definition_set_tag(struct definition_variant
*variant
,
483 struct definition
*enum_tag
);
485 * Returns the field selected by the current tag value.
486 * field returned only valid as long as the variant structure is not appended
489 struct field
*variant_get_current_field(struct definition_variant
*variant
);
492 * elem_declaration passed as parameter now belongs to the array. No
493 * need to free it explicitly. "len" is the number of elements in the
496 struct declaration_array
*
497 array_declaration_new(const char *name
,
498 size_t len
, struct declaration
*elem_declaration
,
499 struct declaration_scope
*parent_scope
);
502 * int_declaration and elem_declaration passed as parameter now belong
503 * to the sequence. No need to free them explicitly.
505 struct declaration_sequence
*
506 sequence_declaration_new(const char *name
,
507 struct declaration_integer
*len_declaration
,
508 struct declaration
*elem_declaration
,
509 struct declaration_scope
*parent_scope
);
511 #endif /* _BABELTRACE_declarationS_H */