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
34 struct ctf_stream_class
;
40 struct declaration_scope
{
41 /* Hash table mapping type name GQuark to "struct declaration" */
42 /* Used for both typedef and typealias. */
43 GHashTable
*typedef_declarations
;
44 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
45 GHashTable
*struct_declarations
;
46 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
47 GHashTable
*variant_declarations
;
48 /* Hash table mapping enum name GQuark to "struct type_enum" */
49 GHashTable
*enum_declarations
;
50 struct declaration_scope
*parent_scope
;
53 /* definition scope */
54 struct definition_scope
{
55 /* Hash table mapping field name GQuark to "struct definition" */
56 GHashTable
*definitions
;
57 struct definition_scope
*parent_scope
;
59 * Complete "path" leading to this definition scope.
60 * Includes dynamic scope name '.' field name '.' field name '.' ....
61 * Array of GQuark elements (which are each separated by dots).
62 * The dynamic scope name can contain dots, and is encoded into
63 * a single GQuark. Thus, scope_path[0] returns the GQuark
64 * identifying the dynamic scope.
66 GArray
*scope_path
; /* array of GQuark */
76 CTF_TYPE_UNTAGGED_VARIANT
,
85 size_t alignment
; /* type alignment, in bits */
86 int ref
; /* number of references to the type */
88 * declaration_free called with declaration ref is decremented to 0.
90 void (*declaration_free
)(struct declaration
*declaration
);
92 (*definition_new
)(struct declaration
*declaration
,
93 struct definition_scope
*parent_scope
,
94 GQuark field_name
, int index
);
96 * definition_free called with definition ref is decremented to 0.
98 void (*definition_free
)(struct definition
*definition
);
102 struct declaration
*declaration
;
103 int index
; /* Position of the definition in its container */
104 GQuark name
; /* Field name in its container (or 0 if unset) */
105 int ref
; /* number of references to the definition */
109 typedef int (*rw_dispatch
)(struct stream_pos
*pos
,
110 struct definition
*definition
);
112 /* Parent of per-plugin positions */
114 /* read/write dispatch table. Specific to plugin used for stream. */
115 rw_dispatch
*rw_table
; /* rw dispatch table */
116 int (*event_cb
)(struct stream_pos
*pos
,
117 struct ctf_stream_class
*stream_class
);
121 int generic_rw(struct stream_pos
*pos
, struct definition
*definition
)
123 enum ctf_type_id dispatch_id
= definition
->declaration
->id
;
126 assert(pos
->rw_table
[dispatch_id
] != NULL
);
127 call
= pos
->rw_table
[dispatch_id
];
128 return call(pos
, definition
);
132 * Because we address in bits, bitfields end up being exactly the same as
133 * integers, except that their read/write functions must be able to deal with
134 * read/write non aligned on CHAR_BIT.
136 struct declaration_integer
{
137 struct declaration p
;
138 size_t len
; /* length, in bits. */
139 int byte_order
; /* byte order */
143 struct definition_integer
{
145 struct declaration_integer
*declaration
;
146 /* Last values read */
153 struct declaration_float
{
154 struct declaration p
;
155 struct declaration_integer
*sign
;
156 struct declaration_integer
*mantissa
;
157 struct declaration_integer
*exp
;
159 /* TODO: we might want to express more info about NaN, +inf and -inf */
162 struct definition_float
{
164 struct declaration_float
*declaration
;
165 struct definition_integer
*sign
;
166 struct definition_integer
*mantissa
;
167 struct definition_integer
*exp
;
168 /* Last values read */
173 * enum_val_equal assumes that signed and unsigned memory layout overlap.
179 } start
; /* lowest range value */
183 } end
; /* highest range value */
186 struct enum_range_to_quark
{
187 struct cds_list_head node
;
188 struct enum_range range
;
193 * We optimize the common case (range of size 1: single value) by creating a
194 * hash table mapping values to quark sets. We then lookup the ranges to
195 * complete the quark set.
197 * TODO: The proper structure to hold the range to quark set mapping would be an
198 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
199 * time. Using a simple O(n) list search for now for implementation speed and
200 * given that we can expect to have a _relatively_ small number of enumeration
201 * ranges. This might become untrue if we are fed with symbol tables often
202 * required to lookup function names from instruction pointer value.
205 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
206 struct cds_list_head range_to_quark
; /* (range, GQuark) */
207 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
210 struct declaration_enum
{
211 struct declaration p
;
212 struct declaration_integer
*integer_declaration
;
213 struct enum_table table
;
216 struct definition_enum
{
218 struct definition_integer
*integer
;
219 struct declaration_enum
*declaration
;
220 /* Last GQuark values read. Keeping a reference on the GQuark array. */
224 enum ctf_string_encoding
{
230 struct declaration_string
{
231 struct declaration p
;
232 enum ctf_string_encoding encoding
;
235 struct definition_string
{
237 struct declaration_string
*declaration
;
238 char *value
; /* freed at definition_string teardown */
239 size_t len
, alloc_len
;
242 struct declaration_field
{
244 struct declaration
*declaration
;
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 GPtrArray
*fields
; /* Array of pointers to struct definition */
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 GPtrArray
*fields
; /* Array of pointers to struct definition */
281 struct definition
*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 GPtrArray
*elems
; /* Array of pointers to struct definition */
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 GPtrArray
*elems
; /* Array of pointers to struct definition */
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 GQuark
new_definition_path(struct definition_scope
*parent_scope
, GQuark field_name
);
369 int compare_definition_path(struct definition
*definition
, GQuark path
)
371 return definition
->path
== path
;
374 void declaration_ref(struct declaration
*declaration
);
375 void declaration_unref(struct declaration
*declaration
);
377 void definition_ref(struct definition
*definition
);
378 void definition_unref(struct definition
*definition
);
380 struct declaration_integer
*integer_declaration_new(size_t len
, int byte_order
,
381 int signedness
, size_t alignment
);
384 * mantissa_len is the length of the number of bytes represented by the mantissa
385 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
387 struct declaration_float
*float_declaration_new(size_t mantissa_len
,
388 size_t exp_len
, int byte_order
,
392 * A GQuark can be translated to/from strings with g_quark_from_string() and
393 * g_quark_to_string().
397 * Returns a GArray of GQuark or NULL.
398 * Caller must release the GArray with g_array_unref().
400 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
404 * Returns a GArray of GQuark or NULL.
405 * Caller must release the GArray with g_array_unref().
407 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
411 * Returns a GArray of struct enum_range or NULL.
412 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
415 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
417 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
418 int64_t start
, int64_t end
, GQuark q
);
419 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
420 uint64_t start
, uint64_t end
, GQuark q
);
421 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
423 struct declaration_enum
*
424 enum_declaration_new(struct declaration_integer
*integer_declaration
);
426 struct declaration_string
*
427 string_declaration_new(enum ctf_string_encoding encoding
);
429 struct declaration_struct
*
430 struct_declaration_new(struct declaration_scope
*parent_scope
);
431 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
432 const char *field_name
,
433 struct declaration
*field_declaration
);
435 * Returns the index of a field within a structure.
437 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
440 * field returned only valid as long as the field structure is not appended to.
442 struct declaration_field
*
443 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
446 struct_definition_get_field_from_index(struct definition_struct
*struct_definition
,
448 int struct_rw(struct stream_pos
*pos
, struct definition
*definition
);
451 * The tag enumeration is validated to ensure that it contains only mappings
452 * from numeric values to a single tag. Overlapping tag value ranges are
453 * therefore forbidden.
455 struct declaration_untagged_variant
*untagged_variant_declaration_new(
456 struct declaration_scope
*parent_scope
);
457 struct declaration_variant
*variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
460 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
461 const char *field_name
,
462 struct declaration
*field_declaration
);
463 struct declaration_field
*
464 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
467 * Returns 0 on success, -EPERM on error.
469 int variant_definition_set_tag(struct definition_variant
*variant
,
470 struct definition
*enum_tag
);
472 * Returns the field selected by the current tag value.
473 * field returned only valid as long as the variant structure is not appended
476 struct definition
*variant_get_current_field(struct definition_variant
*variant
);
477 int variant_rw(struct stream_pos
*pos
, struct definition
*definition
);
480 * elem_declaration passed as parameter now belongs to the array. No
481 * need to free it explicitly. "len" is the number of elements in the
484 struct declaration_array
*
485 array_declaration_new(size_t len
, struct declaration
*elem_declaration
,
486 struct declaration_scope
*parent_scope
);
487 uint64_t array_len(struct definition_array
*array
);
488 struct definition
*array_index(struct definition_array
*array
, uint64_t i
);
489 int array_rw(struct stream_pos
*pos
, struct definition
*definition
);
492 * int_declaration and elem_declaration passed as parameter now belong
493 * to the sequence. No need to free them explicitly.
495 struct declaration_sequence
*
496 sequence_declaration_new(struct declaration_integer
*len_declaration
,
497 struct declaration
*elem_declaration
,
498 struct declaration_scope
*parent_scope
);
499 uint64_t sequence_len(struct definition_sequence
*sequence
);
500 struct definition
*sequence_index(struct definition_sequence
*sequence
, uint64_t i
);
501 int sequence_rw(struct stream_pos
*pos
, struct definition
*definition
);
504 * in: path (dot separated), out: q (GArray of GQuark)
506 void append_scope_path(const char *path
, GArray
*q
);
508 #endif /* _BABELTRACE_TYPES_H */