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,
126 CTF_TYPE_UNTAGGED_VARIANT
,
135 GQuark name
; /* type name */
136 size_t alignment
; /* type alignment, in bits */
137 int ref
; /* number of references to the type */
139 * declaration_free called with declaration ref is decremented to 0.
141 void (*declaration_free
)(struct declaration
*declaration
);
143 (*definition_new
)(struct declaration
*declaration
,
144 struct definition_scope
*parent_scope
,
145 GQuark field_name
, int index
);
147 * definition_free called with definition ref is decremented to 0.
149 void (*definition_free
)(struct definition
*definition
);
151 * Definition copy function. Knows how to find the child
152 * definition from the parent definition.
154 void (*copy
)(struct stream_pos
*dest
, const struct format
*fdest
,
155 struct stream_pos
*src
, const struct format
*fsrc
,
156 struct definition
*definition
);
160 struct declaration
*declaration
;
161 int index
; /* Position of the definition in its container */
162 int ref
; /* number of references to the definition */
166 * Because we address in bits, bitfields end up being exactly the same as
167 * integers, except that their read/write functions must be able to deal with
168 * read/write non aligned on CHAR_BIT.
170 struct declaration_integer
{
171 struct declaration p
;
172 size_t len
; /* length, in bits. */
173 int byte_order
; /* byte order */
177 struct definition_integer
{
179 struct declaration_integer
*declaration
;
180 /* Last values read */
187 struct declaration_float
{
188 struct declaration p
;
189 struct declaration_integer
*sign
;
190 struct declaration_integer
*mantissa
;
191 struct declaration_integer
*exp
;
193 /* TODO: we might want to express more info about NaN, +inf and -inf */
196 struct definition_float
{
198 struct declaration_float
*declaration
;
199 /* Last values read */
204 * enum_val_equal assumes that signed and unsigned memory layout overlap.
210 } start
; /* lowest range value */
214 } end
; /* highest range value */
217 struct enum_range_to_quark
{
218 struct cds_list_head node
;
219 struct enum_range range
;
224 * We optimize the common case (range of size 1: single value) by creating a
225 * hash table mapping values to quark sets. We then lookup the ranges to
226 * complete the quark set.
228 * TODO: The proper structure to hold the range to quark set mapping would be an
229 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
230 * time. Using a simple O(n) list search for now for implementation speed and
231 * given that we can expect to have a _relatively_ small number of enumeration
232 * ranges. This might become untrue if we are fed with symbol tables often
233 * required to lookup function names from instruction pointer value.
236 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
237 struct cds_list_head range_to_quark
; /* (range, GQuark) */
238 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
241 struct declaration_enum
{
242 struct declaration p
;
243 struct declaration_integer
*integer_declaration
;
244 struct enum_table table
;
247 struct definition_enum
{
249 struct definition_integer
*integer
;
250 struct declaration_enum
*declaration
;
251 /* Last GQuark values read. Keeping a reference on the GQuark array. */
255 struct declaration_string
{
256 struct declaration p
;
259 struct definition_string
{
261 struct declaration_string
*declaration
;
262 char *value
; /* freed at definition_string teardown */
265 struct declaration_field
{
267 struct declaration
*declaration
;
272 struct definition
*definition
;
275 struct declaration_struct
{
276 struct declaration p
;
277 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
278 struct declaration_scope
*scope
;
279 GArray
*fields
; /* Array of declaration_field */
282 struct definition_struct
{
284 struct declaration_struct
*declaration
;
285 struct definition_scope
*scope
;
286 GArray
*fields
; /* Array of struct field */
289 struct declaration_untagged_variant
{
290 struct declaration p
;
291 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
292 struct declaration_scope
*scope
;
293 GArray
*fields
; /* Array of declaration_field */
296 struct declaration_variant
{
297 struct declaration p
;
298 struct declaration_untagged_variant
*untagged_variant
;
299 GArray
*tag_name
; /* Array of GQuark */
302 /* A variant needs to be tagged to be defined. */
303 struct definition_variant
{
305 struct declaration_variant
*declaration
;
306 struct definition_scope
*scope
;
307 struct definition
*enum_tag
;
308 GArray
*fields
; /* Array of struct field */
309 struct field
*current_field
; /* Last field read */
312 struct declaration_array
{
313 struct declaration p
;
315 struct declaration
*elem
;
316 struct declaration_scope
*scope
;
319 struct definition_array
{
321 struct declaration_array
*declaration
;
322 struct definition_scope
*scope
;
323 struct field current_element
; /* struct field */
326 struct declaration_sequence
{
327 struct declaration p
;
328 struct declaration_integer
*len_declaration
;
329 struct declaration
*elem
;
330 struct declaration_scope
*scope
;
333 struct definition_sequence
{
335 struct declaration_sequence
*declaration
;
336 struct definition_scope
*scope
;
337 struct definition_integer
*len
;
338 struct field current_element
; /* struct field */
341 int register_declaration(GQuark declaration_name
,
342 struct declaration
*declaration
,
343 struct declaration_scope
*scope
);
344 struct declaration
*lookup_declaration(GQuark declaration_name
,
345 struct declaration_scope
*scope
);
348 * Type scopes also contain a separate registry for struct, variant and
349 * enum types. Those register types rather than type definitions, so
350 * that a named variant can be declared without specifying its target
351 * "choice" tag field immediately.
353 int register_struct_declaration(GQuark struct_name
,
354 struct declaration_struct
*struct_declaration
,
355 struct declaration_scope
*scope
);
356 struct declaration_struct
*
357 lookup_struct_declaration(GQuark struct_name
,
358 struct declaration_scope
*scope
);
359 int register_variant_declaration(GQuark variant_name
,
360 struct declaration_variant
*variant_declaration
,
361 struct declaration_scope
*scope
);
362 struct declaration_variant
*lookup_variant_declaration(GQuark variant_name
,
363 struct declaration_scope
*scope
);
364 int register_enum_declaration(GQuark enum_name
,
365 struct declaration_enum
*enum_declaration
,
366 struct declaration_scope
*scope
);
367 struct declaration_enum
*
368 lookup_enum_declaration(GQuark enum_name
,
369 struct declaration_scope
*scope
);
371 struct declaration_scope
*
372 new_declaration_scope(struct declaration_scope
*parent_scope
);
373 void free_declaration_scope(struct declaration_scope
*scope
);
376 * field_definition is for field definitions. They are registered into
380 lookup_definition(GArray
*cur_path
, /* array of GQuark */
381 GArray
*lookup_path
, /* array of GQuark */
382 struct definition_scope
*scope
);
383 int register_field_definition(GQuark field_name
,
384 struct definition
*definition
,
385 struct definition_scope
*scope
);
386 struct definition_scope
*
387 new_definition_scope(struct definition_scope
*parent_scope
,
389 void set_dynamic_definition_scope(struct definition
*definition
,
390 struct definition_scope
*scope
,
391 const char *root_name
);
392 void free_definition_scope(struct definition_scope
*scope
);
394 void declaration_ref(struct declaration
*declaration
);
395 void declaration_unref(struct declaration
*declaration
);
397 void definition_ref(struct definition
*definition
);
398 void definition_unref(struct definition
*definition
);
400 /* Nameless declarations can be created by passing a NULL name */
402 struct declaration_integer
*integer_declaration_new(const char *name
,
403 size_t len
, int byte_order
,
404 int signedness
, size_t alignment
);
407 * mantissa_len is the length of the number of bytes represented by the mantissa
408 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
410 struct declaration_float
*float_declaration_new(const char *name
,
412 size_t exp_len
, int byte_order
,
416 * A GQuark can be translated to/from strings with g_quark_from_string() and
417 * g_quark_to_string().
421 * Returns a GArray of GQuark or NULL.
422 * Caller must release the GArray with g_array_unref().
424 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
428 * Returns a GArray of GQuark or NULL.
429 * Caller must release the GArray with g_array_unref().
431 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
435 * Returns a GArray of struct enum_range or NULL.
436 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
439 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
441 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
442 int64_t start
, int64_t end
, GQuark q
);
443 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
444 uint64_t start
, uint64_t end
, GQuark q
);
445 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
447 struct declaration_enum
*
448 enum_declaration_new(const char *name
,
449 struct declaration_integer
*integer_declaration
);
451 struct declaration_struct
*
452 struct_declaration_new(const char *name
,
453 struct declaration_scope
*parent_scope
);
454 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
455 const char *field_name
,
456 struct declaration
*field_declaration
);
458 * Returns the index of a field within a structure.
460 unsigned long struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
463 * field returned only valid as long as the field structure is not appended to.
465 struct declaration_field
*
466 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
467 unsigned long index
);
469 struct_get_field_from_index(struct definition_struct
*struct_definition
,
470 unsigned long index
);
473 * The tag enumeration is validated to ensure that it contains only mappings
474 * from numeric values to a single tag. Overlapping tag value ranges are
475 * therefore forbidden.
477 struct declaration_untagged_variant
*untagged_variant_declaration_new(const char *name
,
478 struct declaration_scope
*parent_scope
);
479 struct declaration_variant
*variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
482 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
483 const char *field_name
,
484 struct declaration
*field_declaration
);
485 struct declaration_field
*
486 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
489 * Returns 0 on success, -EPERM on error.
491 int variant_definition_set_tag(struct definition_variant
*variant
,
492 struct definition
*enum_tag
);
494 * Returns the field selected by the current tag value.
495 * field returned only valid as long as the variant structure is not appended
498 struct field
*variant_get_current_field(struct definition_variant
*variant
);
501 * elem_declaration passed as parameter now belongs to the array. No
502 * need to free it explicitly. "len" is the number of elements in the
505 struct declaration_array
*
506 array_declaration_new(const char *name
,
507 size_t len
, struct declaration
*elem_declaration
,
508 struct declaration_scope
*parent_scope
);
511 * int_declaration and elem_declaration passed as parameter now belong
512 * to the sequence. No need to free them explicitly.
514 struct declaration_sequence
*
515 sequence_declaration_new(const char *name
,
516 struct declaration_integer
*len_declaration
,
517 struct declaration
*elem_declaration
,
518 struct declaration_scope
*parent_scope
);
521 * in: path (dot separated), out: q (GArray of GQuark)
523 void append_scope_path(const char *path
, GArray
*q
);
525 #endif /* _BABELTRACE_declarationS_H */