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
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
,
95 const char *root_name
);
97 * definition_free called with definition ref is decremented to 0.
99 void (*definition_free
)(struct definition
*definition
);
103 struct declaration
*declaration
;
104 int index
; /* Position of the definition in its container */
105 GQuark name
; /* Field name in its container (or 0 if unset) */
106 int ref
; /* number of references to the definition */
108 struct definition_scope
*scope
;
111 typedef int (*rw_dispatch
)(struct stream_pos
*pos
,
112 struct definition
*definition
);
114 /* Parent of per-plugin positions */
116 /* read/write dispatch table. Specific to plugin used for stream. */
117 rw_dispatch
*rw_table
; /* rw dispatch table */
118 int (*event_cb
)(struct stream_pos
*pos
,
119 struct ctf_stream
*stream
);
123 int generic_rw(struct stream_pos
*pos
, struct definition
*definition
)
125 enum ctf_type_id dispatch_id
= definition
->declaration
->id
;
128 assert(pos
->rw_table
[dispatch_id
] != NULL
);
129 call
= pos
->rw_table
[dispatch_id
];
130 return call(pos
, definition
);
133 enum ctf_string_encoding
{
141 * Because we address in bits, bitfields end up being exactly the same as
142 * integers, except that their read/write functions must be able to deal with
143 * read/write non aligned on CHAR_BIT.
145 struct declaration_integer
{
146 struct declaration p
;
147 size_t len
; /* length, in bits. */
148 int byte_order
; /* byte order */
150 int base
; /* Base for pretty-printing: 2, 8, 10, 16 */
151 enum ctf_string_encoding encoding
;
154 struct definition_integer
{
156 struct declaration_integer
*declaration
;
157 /* Last values read */
164 struct declaration_float
{
165 struct declaration p
;
166 struct declaration_integer
*sign
;
167 struct declaration_integer
*mantissa
;
168 struct declaration_integer
*exp
;
170 /* TODO: we might want to express more info about NaN, +inf and -inf */
173 struct definition_float
{
175 struct declaration_float
*declaration
;
176 struct definition_integer
*sign
;
177 struct definition_integer
*mantissa
;
178 struct definition_integer
*exp
;
179 /* Last values read */
184 * enum_val_equal assumes that signed and unsigned memory layout overlap.
190 } start
; /* lowest range value */
194 } end
; /* highest range value */
197 struct enum_range_to_quark
{
198 struct cds_list_head node
;
199 struct enum_range range
;
204 * We optimize the common case (range of size 1: single value) by creating a
205 * hash table mapping values to quark sets. We then lookup the ranges to
206 * complete the quark set.
208 * TODO: The proper structure to hold the range to quark set mapping would be an
209 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
210 * time. Using a simple O(n) list search for now for implementation speed and
211 * given that we can expect to have a _relatively_ small number of enumeration
212 * ranges. This might become untrue if we are fed with symbol tables often
213 * required to lookup function names from instruction pointer value.
216 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
217 struct cds_list_head range_to_quark
; /* (range, GQuark) */
218 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
221 struct declaration_enum
{
222 struct declaration p
;
223 struct declaration_integer
*integer_declaration
;
224 struct enum_table table
;
227 struct definition_enum
{
229 struct definition_integer
*integer
;
230 struct declaration_enum
*declaration
;
231 /* Last GQuark values read. Keeping a reference on the GQuark array. */
235 struct declaration_string
{
236 struct declaration p
;
237 enum ctf_string_encoding encoding
;
240 struct definition_string
{
242 struct declaration_string
*declaration
;
243 char *value
; /* freed at definition_string teardown */
244 size_t len
, alloc_len
;
247 struct declaration_field
{
249 struct declaration
*declaration
;
252 struct declaration_struct
{
253 struct declaration p
;
254 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
255 struct declaration_scope
*scope
;
256 GArray
*fields
; /* Array of declaration_field */
259 struct definition_struct
{
261 struct declaration_struct
*declaration
;
262 GPtrArray
*fields
; /* Array of pointers to struct definition */
265 struct declaration_untagged_variant
{
266 struct declaration p
;
267 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
268 struct declaration_scope
*scope
;
269 GArray
*fields
; /* Array of declaration_field */
272 struct declaration_variant
{
273 struct declaration p
;
274 struct declaration_untagged_variant
*untagged_variant
;
275 GArray
*tag_name
; /* Array of GQuark */
278 /* A variant needs to be tagged to be defined. */
279 struct definition_variant
{
281 struct declaration_variant
*declaration
;
282 struct definition
*enum_tag
;
283 GPtrArray
*fields
; /* Array of pointers to struct definition */
284 struct definition
*current_field
; /* Last field read */
287 struct declaration_array
{
288 struct declaration p
;
290 struct declaration
*elem
;
291 struct declaration_scope
*scope
;
294 struct definition_array
{
296 struct declaration_array
*declaration
;
297 GPtrArray
*elems
; /* Array of pointers to struct definition */
298 GString
*string
; /* String for encoded integer children */
301 struct declaration_sequence
{
302 struct declaration p
;
303 GArray
*length_name
; /* Array of GQuark */
304 struct declaration
*elem
;
305 struct declaration_scope
*scope
;
308 struct definition_sequence
{
310 struct declaration_sequence
*declaration
;
311 struct definition_integer
*length
;
312 GPtrArray
*elems
; /* Array of pointers to struct definition */
313 GString
*string
; /* String for encoded integer children */
316 int register_declaration(GQuark declaration_name
,
317 struct declaration
*declaration
,
318 struct declaration_scope
*scope
);
319 struct declaration
*lookup_declaration(GQuark declaration_name
,
320 struct declaration_scope
*scope
);
323 * Type scopes also contain a separate registry for struct, variant and
324 * enum types. Those register types rather than type definitions, so
325 * that a named variant can be declared without specifying its target
326 * "choice" tag field immediately.
328 int register_struct_declaration(GQuark struct_name
,
329 struct declaration_struct
*struct_declaration
,
330 struct declaration_scope
*scope
);
331 struct declaration_struct
*
332 lookup_struct_declaration(GQuark struct_name
,
333 struct declaration_scope
*scope
);
334 int register_variant_declaration(GQuark variant_name
,
335 struct declaration_untagged_variant
*untagged_variant_declaration
,
336 struct declaration_scope
*scope
);
337 struct declaration_untagged_variant
*lookup_variant_declaration(GQuark variant_name
,
338 struct declaration_scope
*scope
);
339 int register_enum_declaration(GQuark enum_name
,
340 struct declaration_enum
*enum_declaration
,
341 struct declaration_scope
*scope
);
342 struct declaration_enum
*
343 lookup_enum_declaration(GQuark enum_name
,
344 struct declaration_scope
*scope
);
346 struct declaration_scope
*
347 new_declaration_scope(struct declaration_scope
*parent_scope
);
348 void free_declaration_scope(struct declaration_scope
*scope
);
351 * field_definition is for field definitions. They are registered into
355 lookup_path_definition(GArray
*cur_path
, /* array of GQuark */
356 GArray
*lookup_path
, /* array of GQuark */
357 struct definition_scope
*scope
);
358 int register_field_definition(GQuark field_name
,
359 struct definition
*definition
,
360 struct definition_scope
*scope
);
361 struct definition_scope
*
362 new_definition_scope(struct definition_scope
*parent_scope
,
363 GQuark field_name
, const char *root_name
);
364 void free_definition_scope(struct definition_scope
*scope
);
366 GQuark
new_definition_path(struct definition_scope
*parent_scope
,
367 GQuark field_name
, const char *root_name
);
370 int compare_definition_path(struct definition
*definition
, GQuark path
)
372 return definition
->path
== path
;
375 void declaration_ref(struct declaration
*declaration
);
376 void declaration_unref(struct declaration
*declaration
);
378 void definition_ref(struct definition
*definition
);
379 void definition_unref(struct definition
*definition
);
381 struct declaration_integer
*integer_declaration_new(size_t len
, int byte_order
,
382 int signedness
, size_t alignment
,
383 int base
, enum ctf_string_encoding encoding
);
386 * mantissa_len is the length of the number of bytes represented by the mantissa
387 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
389 struct declaration_float
*float_declaration_new(size_t mantissa_len
,
390 size_t exp_len
, int byte_order
,
394 * A GQuark can be translated to/from strings with g_quark_from_string() and
395 * g_quark_to_string().
399 * Returns a GArray of GQuark or NULL.
400 * Caller must release the GArray with g_array_unref().
402 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
406 * Returns a GArray of GQuark or NULL.
407 * Caller must release the GArray with g_array_unref().
409 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
413 * Returns a GArray of struct enum_range or NULL.
414 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
417 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
419 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
420 int64_t start
, int64_t end
, GQuark q
);
421 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
422 uint64_t start
, uint64_t end
, GQuark q
);
423 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
425 struct declaration_enum
*
426 enum_declaration_new(struct declaration_integer
*integer_declaration
);
428 struct declaration_string
*
429 string_declaration_new(enum ctf_string_encoding encoding
);
431 struct declaration_struct
*
432 struct_declaration_new(struct declaration_scope
*parent_scope
,
434 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
435 const char *field_name
,
436 struct declaration
*field_declaration
);
438 * Returns the index of a field within a structure.
440 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
443 * field returned only valid as long as the field structure is not appended to.
445 struct declaration_field
*
446 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
449 struct_definition_get_field_from_index(struct definition_struct
*struct_definition
,
451 int struct_rw(struct stream_pos
*pos
, struct definition
*definition
);
452 uint64_t struct_declaration_len(struct declaration_struct
*struct_declaration
);
455 * The tag enumeration is validated to ensure that it contains only mappings
456 * from numeric values to a single tag. Overlapping tag value ranges are
457 * therefore forbidden.
459 struct declaration_untagged_variant
*untagged_variant_declaration_new(
460 struct declaration_scope
*parent_scope
);
461 struct declaration_variant
*variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
464 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
465 const char *field_name
,
466 struct declaration
*field_declaration
);
467 struct declaration_field
*
468 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
471 * Returns 0 on success, -EPERM on error.
473 int variant_definition_set_tag(struct definition_variant
*variant
,
474 struct definition
*enum_tag
);
476 * Returns the field selected by the current tag value.
477 * field returned only valid as long as the variant structure is not appended
480 struct definition
*variant_get_current_field(struct definition_variant
*variant
);
481 int variant_rw(struct stream_pos
*pos
, struct definition
*definition
);
484 * elem_declaration passed as parameter now belongs to the array. No
485 * need to free it explicitly. "len" is the number of elements in the
488 struct declaration_array
*
489 array_declaration_new(size_t len
, struct declaration
*elem_declaration
,
490 struct declaration_scope
*parent_scope
);
491 uint64_t array_len(struct definition_array
*array
);
492 struct definition
*array_index(struct definition_array
*array
, uint64_t i
);
493 int array_rw(struct stream_pos
*pos
, struct definition
*definition
);
496 * int_declaration and elem_declaration passed as parameter now belong
497 * to the sequence. No need to free them explicitly.
499 struct declaration_sequence
*
500 sequence_declaration_new(const char *length_name
,
501 struct declaration
*elem_declaration
,
502 struct declaration_scope
*parent_scope
);
503 uint64_t sequence_len(struct definition_sequence
*sequence
);
504 struct definition
*sequence_index(struct definition_sequence
*sequence
, uint64_t i
);
505 int sequence_rw(struct stream_pos
*pos
, struct definition
*definition
);
508 * in: path (dot separated), out: q (GArray of GQuark)
510 void append_scope_path(const char *path
, GArray
*q
);
515 struct definition_integer
*lookup_integer(struct definition
*definition
,
516 const char *field_name
,
518 struct definition_enum
*lookup_enum(struct definition
*definition
,
519 const char *field_name
,
521 struct definition
*lookup_variant(struct definition
*definition
,
522 const char *field_name
);
524 #endif /* _BABELTRACE_TYPES_H */