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 /* Used for both typedef and typealias. */
94 GHashTable
*typedef_declarations
;
95 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
96 GHashTable
*struct_declarations
;
97 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
98 GHashTable
*variant_declarations
;
99 /* Hash table mapping enum name GQuark to "struct type_enum" */
100 GHashTable
*enum_declarations
;
101 struct declaration_scope
*parent_scope
;
104 /* definition scope */
105 struct definition_scope
{
106 /* Hash table mapping field name GQuark to "struct definition" */
107 GHashTable
*definitions
;
108 struct definition_scope
*parent_scope
;
110 * Complete "path" leading to this definition scope.
111 * Includes dynamic scope name '.' field name '.' field name '.' ....
112 * Array of GQuark elements (which are each separated by dots).
113 * The dynamic scope name can contain dots, and is encoded into
114 * a single GQuark. Thus, scope_path[0] returns the GQuark
115 * identifying the dynamic scope.
117 GArray
*scope_path
; /* array of GQuark */
121 CTF_TYPE_UNKNOWN
= 0,
127 CTF_TYPE_UNTAGGED_VARIANT
,
136 GQuark name
; /* type name */
137 size_t alignment
; /* type alignment, in bits */
138 int ref
; /* number of references to the type */
140 * declaration_free called with declaration ref is decremented to 0.
142 void (*declaration_free
)(struct declaration
*declaration
);
144 (*definition_new
)(struct declaration
*declaration
,
145 struct definition_scope
*parent_scope
,
146 GQuark field_name
, int index
);
148 * definition_free called with definition ref is decremented to 0.
150 void (*definition_free
)(struct definition
*definition
);
152 * Definition copy function. Knows how to find the child
153 * definition from the parent definition.
155 void (*copy
)(struct stream_pos
*dest
, const struct format
*fdest
,
156 struct stream_pos
*src
, const struct format
*fsrc
,
157 struct definition
*definition
);
161 struct declaration
*declaration
;
162 int index
; /* Position of the definition in its container */
163 int ref
; /* number of references to the definition */
167 * Because we address in bits, bitfields end up being exactly the same as
168 * integers, except that their read/write functions must be able to deal with
169 * read/write non aligned on CHAR_BIT.
171 struct declaration_integer
{
172 struct declaration p
;
173 size_t len
; /* length, in bits. */
174 int byte_order
; /* byte order */
178 struct definition_integer
{
180 struct declaration_integer
*declaration
;
181 /* Last values read */
188 struct declaration_float
{
189 struct declaration p
;
190 struct declaration_integer
*sign
;
191 struct declaration_integer
*mantissa
;
192 struct declaration_integer
*exp
;
194 /* TODO: we might want to express more info about NaN, +inf and -inf */
197 struct definition_float
{
199 struct declaration_float
*declaration
;
200 /* Last values read */
205 * enum_val_equal assumes that signed and unsigned memory layout overlap.
211 } start
; /* lowest range value */
215 } end
; /* highest range value */
218 struct enum_range_to_quark
{
219 struct cds_list_head node
;
220 struct enum_range range
;
225 * We optimize the common case (range of size 1: single value) by creating a
226 * hash table mapping values to quark sets. We then lookup the ranges to
227 * complete the quark set.
229 * TODO: The proper structure to hold the range to quark set mapping would be an
230 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
231 * time. Using a simple O(n) list search for now for implementation speed and
232 * given that we can expect to have a _relatively_ small number of enumeration
233 * ranges. This might become untrue if we are fed with symbol tables often
234 * required to lookup function names from instruction pointer value.
237 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
238 struct cds_list_head range_to_quark
; /* (range, GQuark) */
239 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
242 struct declaration_enum
{
243 struct declaration p
;
244 struct declaration_integer
*integer_declaration
;
245 struct enum_table table
;
248 struct definition_enum
{
250 struct definition_integer
*integer
;
251 struct declaration_enum
*declaration
;
252 /* Last GQuark values read. Keeping a reference on the GQuark array. */
256 struct declaration_string
{
257 struct declaration p
;
260 struct definition_string
{
262 struct declaration_string
*declaration
;
263 char *value
; /* freed at definition_string teardown */
266 struct declaration_field
{
268 struct declaration
*declaration
;
273 struct definition
*definition
;
276 struct declaration_struct
{
277 struct declaration p
;
278 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
279 struct declaration_scope
*scope
;
280 GArray
*fields
; /* Array of declaration_field */
283 struct definition_struct
{
285 struct declaration_struct
*declaration
;
286 struct definition_scope
*scope
;
287 GArray
*fields
; /* Array of struct field */
290 struct declaration_untagged_variant
{
291 struct declaration p
;
292 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
293 struct declaration_scope
*scope
;
294 GArray
*fields
; /* Array of declaration_field */
297 struct declaration_variant
{
298 struct declaration p
;
299 struct declaration_untagged_variant
*untagged_variant
;
300 GArray
*tag_name
; /* Array of GQuark */
303 /* A variant needs to be tagged to be defined. */
304 struct definition_variant
{
306 struct declaration_variant
*declaration
;
307 struct definition_scope
*scope
;
308 struct definition
*enum_tag
;
309 GArray
*fields
; /* Array of struct field */
310 struct field
*current_field
; /* Last field read */
313 struct declaration_array
{
314 struct declaration p
;
316 struct declaration
*elem
;
317 struct declaration_scope
*scope
;
320 struct definition_array
{
322 struct declaration_array
*declaration
;
323 struct definition_scope
*scope
;
324 struct field current_element
; /* struct field */
327 struct declaration_sequence
{
328 struct declaration p
;
329 struct declaration_integer
*len_declaration
;
330 struct declaration
*elem
;
331 struct declaration_scope
*scope
;
334 struct definition_sequence
{
336 struct declaration_sequence
*declaration
;
337 struct definition_scope
*scope
;
338 struct definition_integer
*len
;
339 struct field current_element
; /* struct field */
342 int register_declaration(GQuark declaration_name
,
343 struct declaration
*declaration
,
344 struct declaration_scope
*scope
);
345 struct declaration
*lookup_declaration(GQuark declaration_name
,
346 struct declaration_scope
*scope
);
349 * Type scopes also contain a separate registry for struct, variant and
350 * enum types. Those register types rather than type definitions, so
351 * that a named variant can be declared without specifying its target
352 * "choice" tag field immediately.
354 int register_struct_declaration(GQuark struct_name
,
355 struct declaration_struct
*struct_declaration
,
356 struct declaration_scope
*scope
);
357 struct declaration_struct
*
358 lookup_struct_declaration(GQuark struct_name
,
359 struct declaration_scope
*scope
);
360 int register_variant_declaration(GQuark variant_name
,
361 struct declaration_variant
*variant_declaration
,
362 struct declaration_scope
*scope
);
363 struct declaration_variant
*lookup_variant_declaration(GQuark variant_name
,
364 struct declaration_scope
*scope
);
365 int register_enum_declaration(GQuark enum_name
,
366 struct declaration_enum
*enum_declaration
,
367 struct declaration_scope
*scope
);
368 struct declaration_enum
*
369 lookup_enum_declaration(GQuark enum_name
,
370 struct declaration_scope
*scope
);
372 struct declaration_scope
*
373 new_declaration_scope(struct declaration_scope
*parent_scope
);
374 void free_declaration_scope(struct declaration_scope
*scope
);
377 * field_definition is for field definitions. They are registered into
381 lookup_definition(GArray
*cur_path
, /* array of GQuark */
382 GArray
*lookup_path
, /* array of GQuark */
383 struct definition_scope
*scope
);
384 int register_field_definition(GQuark field_name
,
385 struct definition
*definition
,
386 struct definition_scope
*scope
);
387 struct definition_scope
*
388 new_definition_scope(struct definition_scope
*parent_scope
,
390 void set_dynamic_definition_scope(struct definition
*definition
,
391 struct definition_scope
*scope
,
392 const char *root_name
);
393 void free_definition_scope(struct definition_scope
*scope
);
395 void declaration_ref(struct declaration
*declaration
);
396 void declaration_unref(struct declaration
*declaration
);
398 void definition_ref(struct definition
*definition
);
399 void definition_unref(struct definition
*definition
);
401 /* Nameless declarations can be created by passing a NULL name */
403 struct declaration_integer
*integer_declaration_new(const char *name
,
404 size_t len
, int byte_order
,
405 int signedness
, size_t alignment
);
408 * mantissa_len is the length of the number of bytes represented by the mantissa
409 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
411 struct declaration_float
*float_declaration_new(const char *name
,
413 size_t exp_len
, int byte_order
,
417 * A GQuark can be translated to/from strings with g_quark_from_string() and
418 * g_quark_to_string().
422 * Returns a GArray of GQuark or NULL.
423 * Caller must release the GArray with g_array_unref().
425 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
429 * Returns a GArray of GQuark or NULL.
430 * Caller must release the GArray with g_array_unref().
432 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
436 * Returns a GArray of struct enum_range or NULL.
437 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
440 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
442 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
443 int64_t start
, int64_t end
, GQuark q
);
444 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
445 uint64_t start
, uint64_t end
, GQuark q
);
446 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
448 struct declaration_enum
*
449 enum_declaration_new(const char *name
,
450 struct declaration_integer
*integer_declaration
);
452 struct declaration_struct
*
453 struct_declaration_new(const char *name
,
454 struct declaration_scope
*parent_scope
);
455 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
456 const char *field_name
,
457 struct declaration
*field_declaration
);
459 * Returns the index of a field within a structure.
461 unsigned long struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
464 * field returned only valid as long as the field structure is not appended to.
466 struct declaration_field
*
467 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
468 unsigned long index
);
470 struct_get_field_from_index(struct definition_struct
*struct_definition
,
471 unsigned long index
);
474 * The tag enumeration is validated to ensure that it contains only mappings
475 * from numeric values to a single tag. Overlapping tag value ranges are
476 * therefore forbidden.
478 struct declaration_untagged_variant
*untagged_variant_declaration_new(const char *name
,
479 struct declaration_scope
*parent_scope
);
480 struct declaration_variant
*variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
483 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
484 const char *field_name
,
485 struct declaration
*field_declaration
);
486 struct declaration_field
*
487 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
490 * Returns 0 on success, -EPERM on error.
492 int variant_definition_set_tag(struct definition_variant
*variant
,
493 struct definition
*enum_tag
);
495 * Returns the field selected by the current tag value.
496 * field returned only valid as long as the variant structure is not appended
499 struct field
*variant_get_current_field(struct definition_variant
*variant
);
502 * elem_declaration passed as parameter now belongs to the array. No
503 * need to free it explicitly. "len" is the number of elements in the
506 struct declaration_array
*
507 array_declaration_new(const char *name
,
508 size_t len
, struct declaration
*elem_declaration
,
509 struct declaration_scope
*parent_scope
);
512 * int_declaration and elem_declaration passed as parameter now belong
513 * to the sequence. No need to free them explicitly.
515 struct declaration_sequence
*
516 sequence_declaration_new(const char *name
,
517 struct declaration_integer
*len_declaration
,
518 struct declaration
*elem_declaration
,
519 struct declaration_scope
*parent_scope
);
522 * in: path (dot separated), out: q (GArray of GQuark)
524 void append_scope_path(const char *path
, GArray
*q
);
526 #endif /* _BABELTRACE_declarationS_H */