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
39 struct declaration_scope
{
40 /* Hash table mapping type name GQuark to "struct declaration" */
41 /* Used for both typedef and typealias. */
42 GHashTable
*typedef_declarations
;
43 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
44 GHashTable
*struct_declarations
;
45 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
46 GHashTable
*variant_declarations
;
47 /* Hash table mapping enum name GQuark to "struct type_enum" */
48 GHashTable
*enum_declarations
;
49 struct declaration_scope
*parent_scope
;
52 /* definition scope */
53 struct definition_scope
{
54 /* Hash table mapping field name GQuark to "struct definition" */
55 GHashTable
*definitions
;
56 struct definition_scope
*parent_scope
;
58 * Complete "path" leading to this definition scope.
59 * Includes dynamic scope name '.' field name '.' field name '.' ....
60 * Array of GQuark elements (which are each separated by dots).
61 * The dynamic scope name can contain dots, and is encoded into
62 * a single GQuark. Thus, scope_path[0] returns the GQuark
63 * identifying the dynamic scope.
65 GArray
*scope_path
; /* array of GQuark */
75 CTF_TYPE_UNTAGGED_VARIANT
,
84 size_t alignment
; /* type alignment, in bits */
85 int ref
; /* number of references to the type */
87 * declaration_free called with declaration ref is decremented to 0.
89 void (*declaration_free
)(struct declaration
*declaration
);
91 (*definition_new
)(struct declaration
*declaration
,
92 struct definition_scope
*parent_scope
,
93 GQuark field_name
, int index
);
95 * definition_free called with definition ref is decremented to 0.
97 void (*definition_free
)(struct definition
*definition
);
101 struct declaration
*declaration
;
102 int index
; /* Position of the definition in its container */
103 int ref
; /* number of references to the definition */
106 typedef void (*rw_dispatch
)(struct stream_pos
*pos
,
107 struct definition
*definition
);
109 /* Parent of per-plugin positions */
111 /* read/write dispatch table. Specific to plugin used for stream. */
112 rw_dispatch
*rw_table
; /* rw dispatch table */
116 void generic_rw(struct stream_pos
*pos
, struct definition
*definition
)
118 enum ctf_type_id dispatch_id
= definition
->declaration
->id
;
121 assert(pos
->rw_table
[dispatch_id
] != NULL
);
122 call
= pos
->rw_table
[dispatch_id
];
123 call(pos
, definition
);
127 * Because we address in bits, bitfields end up being exactly the same as
128 * integers, except that their read/write functions must be able to deal with
129 * read/write non aligned on CHAR_BIT.
131 struct declaration_integer
{
132 struct declaration p
;
133 size_t len
; /* length, in bits. */
134 int byte_order
; /* byte order */
138 struct definition_integer
{
140 struct declaration_integer
*declaration
;
141 /* Last values read */
148 struct declaration_float
{
149 struct declaration p
;
150 struct declaration_integer
*sign
;
151 struct declaration_integer
*mantissa
;
152 struct declaration_integer
*exp
;
154 /* TODO: we might want to express more info about NaN, +inf and -inf */
157 struct definition_float
{
159 struct declaration_float
*declaration
;
160 struct definition_integer
*sign
;
161 struct definition_integer
*mantissa
;
162 struct definition_integer
*exp
;
163 /* Last values read */
168 * enum_val_equal assumes that signed and unsigned memory layout overlap.
174 } start
; /* lowest range value */
178 } end
; /* highest range value */
181 struct enum_range_to_quark
{
182 struct cds_list_head node
;
183 struct enum_range range
;
188 * We optimize the common case (range of size 1: single value) by creating a
189 * hash table mapping values to quark sets. We then lookup the ranges to
190 * complete the quark set.
192 * TODO: The proper structure to hold the range to quark set mapping would be an
193 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
194 * time. Using a simple O(n) list search for now for implementation speed and
195 * given that we can expect to have a _relatively_ small number of enumeration
196 * ranges. This might become untrue if we are fed with symbol tables often
197 * required to lookup function names from instruction pointer value.
200 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
201 struct cds_list_head range_to_quark
; /* (range, GQuark) */
202 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
205 struct declaration_enum
{
206 struct declaration p
;
207 struct declaration_integer
*integer_declaration
;
208 struct enum_table table
;
211 struct definition_enum
{
213 struct definition_integer
*integer
;
214 struct declaration_enum
*declaration
;
215 /* Last GQuark values read. Keeping a reference on the GQuark array. */
219 enum ctf_string_encoding
{
225 struct declaration_string
{
226 struct declaration p
;
227 enum ctf_string_encoding encoding
;
230 struct definition_string
{
232 struct declaration_string
*declaration
;
233 char *value
; /* freed at definition_string teardown */
234 size_t len
, alloc_len
;
237 struct declaration_field
{
239 struct declaration
*declaration
;
244 struct definition
*definition
;
247 struct declaration_struct
{
248 struct declaration p
;
249 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
250 struct declaration_scope
*scope
;
251 GArray
*fields
; /* Array of declaration_field */
254 struct definition_struct
{
256 struct declaration_struct
*declaration
;
257 struct definition_scope
*scope
;
258 GArray
*fields
; /* Array of struct field */
261 struct declaration_untagged_variant
{
262 struct declaration p
;
263 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
264 struct declaration_scope
*scope
;
265 GArray
*fields
; /* Array of declaration_field */
268 struct declaration_variant
{
269 struct declaration p
;
270 struct declaration_untagged_variant
*untagged_variant
;
271 GArray
*tag_name
; /* Array of GQuark */
274 /* A variant needs to be tagged to be defined. */
275 struct definition_variant
{
277 struct declaration_variant
*declaration
;
278 struct definition_scope
*scope
;
279 struct definition
*enum_tag
;
280 GArray
*fields
; /* Array of struct field */
281 struct field
*current_field
; /* Last field read */
284 struct declaration_array
{
285 struct declaration p
;
287 struct declaration
*elem
;
288 struct declaration_scope
*scope
;
291 struct definition_array
{
293 struct declaration_array
*declaration
;
294 struct definition_scope
*scope
;
295 GArray
*elems
; /* struct field */
298 struct declaration_sequence
{
299 struct declaration p
;
300 struct declaration_integer
*len_declaration
;
301 struct declaration
*elem
;
302 struct declaration_scope
*scope
;
305 struct definition_sequence
{
307 struct declaration_sequence
*declaration
;
308 struct definition_scope
*scope
;
309 struct definition_integer
*len
;
310 GArray
*elems
; /* struct field */
313 int register_declaration(GQuark declaration_name
,
314 struct declaration
*declaration
,
315 struct declaration_scope
*scope
);
316 struct declaration
*lookup_declaration(GQuark declaration_name
,
317 struct declaration_scope
*scope
);
320 * Type scopes also contain a separate registry for struct, variant and
321 * enum types. Those register types rather than type definitions, so
322 * that a named variant can be declared without specifying its target
323 * "choice" tag field immediately.
325 int register_struct_declaration(GQuark struct_name
,
326 struct declaration_struct
*struct_declaration
,
327 struct declaration_scope
*scope
);
328 struct declaration_struct
*
329 lookup_struct_declaration(GQuark struct_name
,
330 struct declaration_scope
*scope
);
331 int register_variant_declaration(GQuark variant_name
,
332 struct declaration_untagged_variant
*untagged_variant_declaration
,
333 struct declaration_scope
*scope
);
334 struct declaration_untagged_variant
*lookup_variant_declaration(GQuark variant_name
,
335 struct declaration_scope
*scope
);
336 int register_enum_declaration(GQuark enum_name
,
337 struct declaration_enum
*enum_declaration
,
338 struct declaration_scope
*scope
);
339 struct declaration_enum
*
340 lookup_enum_declaration(GQuark enum_name
,
341 struct declaration_scope
*scope
);
343 struct declaration_scope
*
344 new_declaration_scope(struct declaration_scope
*parent_scope
);
345 void free_declaration_scope(struct declaration_scope
*scope
);
348 * field_definition is for field definitions. They are registered into
352 lookup_definition(GArray
*cur_path
, /* array of GQuark */
353 GArray
*lookup_path
, /* array of GQuark */
354 struct definition_scope
*scope
);
355 int register_field_definition(GQuark field_name
,
356 struct definition
*definition
,
357 struct definition_scope
*scope
);
358 struct definition_scope
*
359 new_definition_scope(struct definition_scope
*parent_scope
,
361 void set_dynamic_definition_scope(struct definition
*definition
,
362 struct definition_scope
*scope
,
363 const char *root_name
);
364 void free_definition_scope(struct definition_scope
*scope
);
366 void declaration_ref(struct declaration
*declaration
);
367 void declaration_unref(struct declaration
*declaration
);
369 void definition_ref(struct definition
*definition
);
370 void definition_unref(struct definition
*definition
);
372 struct declaration_integer
*integer_declaration_new(size_t len
, int byte_order
,
373 int signedness
, size_t alignment
);
376 * mantissa_len is the length of the number of bytes represented by the mantissa
377 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
379 struct declaration_float
*float_declaration_new(size_t mantissa_len
,
380 size_t exp_len
, int byte_order
,
384 * A GQuark can be translated to/from strings with g_quark_from_string() and
385 * g_quark_to_string().
389 * Returns a GArray of GQuark or NULL.
390 * Caller must release the GArray with g_array_unref().
392 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
396 * Returns a GArray of GQuark or NULL.
397 * Caller must release the GArray with g_array_unref().
399 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
403 * Returns a GArray of struct enum_range or NULL.
404 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
407 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
409 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
410 int64_t start
, int64_t end
, GQuark q
);
411 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
412 uint64_t start
, uint64_t end
, GQuark q
);
413 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
415 struct declaration_enum
*
416 enum_declaration_new(struct declaration_integer
*integer_declaration
);
418 struct declaration_string
*
419 string_declaration_new(enum ctf_string_encoding encoding
);
421 struct declaration_struct
*
422 struct_declaration_new(struct declaration_scope
*parent_scope
);
423 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
424 const char *field_name
,
425 struct declaration
*field_declaration
);
427 * Returns the index of a field within a structure.
429 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
432 * field returned only valid as long as the field structure is not appended to.
434 struct declaration_field
*
435 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
438 struct_definition_get_field_from_index(struct definition_struct
*struct_definition
,
440 void struct_rw(struct stream_pos
*pos
, struct definition
*definition
);
443 * The tag enumeration is validated to ensure that it contains only mappings
444 * from numeric values to a single tag. Overlapping tag value ranges are
445 * therefore forbidden.
447 struct declaration_untagged_variant
*untagged_variant_declaration_new(
448 struct declaration_scope
*parent_scope
);
449 struct declaration_variant
*variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
452 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
453 const char *field_name
,
454 struct declaration
*field_declaration
);
455 struct declaration_field
*
456 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
459 * Returns 0 on success, -EPERM on error.
461 int variant_definition_set_tag(struct definition_variant
*variant
,
462 struct definition
*enum_tag
);
464 * Returns the field selected by the current tag value.
465 * field returned only valid as long as the variant structure is not appended
468 struct field
*variant_get_current_field(struct definition_variant
*variant
);
469 void variant_rw(struct stream_pos
*pos
, struct definition
*definition
);
472 * elem_declaration passed as parameter now belongs to the array. No
473 * need to free it explicitly. "len" is the number of elements in the
476 struct declaration_array
*
477 array_declaration_new(size_t len
, struct declaration
*elem_declaration
,
478 struct declaration_scope
*parent_scope
);
479 uint64_t array_len(struct definition_array
*array
);
480 struct definition
*array_index(struct definition_array
*array
, uint64_t i
);
481 void array_rw(struct stream_pos
*pos
, struct definition
*definition
);
484 * int_declaration and elem_declaration passed as parameter now belong
485 * to the sequence. No need to free them explicitly.
487 struct declaration_sequence
*
488 sequence_declaration_new(struct declaration_integer
*len_declaration
,
489 struct declaration
*elem_declaration
,
490 struct declaration_scope
*parent_scope
);
491 uint64_t sequence_len(struct definition_sequence
*sequence
);
492 struct definition
*sequence_index(struct definition_sequence
*sequence
, uint64_t i
);
493 void sequence_rw(struct stream_pos
*pos
, struct definition
*definition
);
496 * in: path (dot separated), out: q (GArray of GQuark)
498 void append_scope_path(const char *path
, GArray
*q
);
500 #endif /* _BABELTRACE_declarationS_H */