1 #ifndef _BABELTRACE_TYPES_H
2 #define _BABELTRACE_TYPES_H
9 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
11 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
24 #include <babeltrace/align.h>
25 #include <babeltrace/list.h>
33 /* Preallocate this many fields for structures */
34 #define DEFAULT_NR_STRUCT_FIELDS 8
42 struct declaration_scope
{
43 /* Hash table mapping type name GQuark to "struct declaration" */
44 /* Used for both typedef and typealias. */
45 GHashTable
*typedef_declarations
;
46 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
47 GHashTable
*struct_declarations
;
48 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
49 GHashTable
*variant_declarations
;
50 /* Hash table mapping enum name GQuark to "struct type_enum" */
51 GHashTable
*enum_declarations
;
52 struct declaration_scope
*parent_scope
;
55 /* definition scope */
56 struct definition_scope
{
57 /* Hash table mapping field name GQuark to "struct definition" */
58 GHashTable
*definitions
;
59 struct definition_scope
*parent_scope
;
61 * Complete "path" leading to this definition scope.
62 * Includes dynamic scope name '.' field name '.' field name '.' ....
63 * Array of GQuark elements (which are each separated by dots).
64 * The dynamic scope name can contain dots, and is encoded into
65 * a single GQuark. Thus, scope_path[0] returns the GQuark
66 * identifying the dynamic scope.
68 GArray
*scope_path
; /* array of GQuark */
78 CTF_TYPE_UNTAGGED_VARIANT
,
87 size_t alignment
; /* type alignment, in bits */
88 int ref
; /* number of references to the type */
90 * declaration_free called with declaration ref is decremented to 0.
92 void (*declaration_free
)(struct declaration
*declaration
);
94 (*definition_new
)(struct declaration
*declaration
,
95 struct definition_scope
*parent_scope
,
96 GQuark field_name
, int index
,
97 const char *root_name
);
99 * definition_free called with definition ref is decremented to 0.
101 void (*definition_free
)(struct definition
*definition
);
105 struct declaration
*declaration
;
106 int index
; /* Position of the definition in its container */
107 GQuark name
; /* Field name in its container (or 0 if unset) */
108 int ref
; /* number of references to the definition */
110 struct definition_scope
*scope
;
113 typedef int (*rw_dispatch
)(struct stream_pos
*pos
,
114 struct definition
*definition
);
116 /* Parent of per-plugin positions */
118 /* read/write dispatch table. Specific to plugin used for stream. */
119 rw_dispatch
*rw_table
; /* rw dispatch table */
120 int (*event_cb
)(struct stream_pos
*pos
,
121 struct ctf_stream
*stream
);
125 int generic_rw(struct stream_pos
*pos
, struct definition
*definition
)
127 enum ctf_type_id dispatch_id
= definition
->declaration
->id
;
130 assert(pos
->rw_table
[dispatch_id
] != NULL
);
131 call
= pos
->rw_table
[dispatch_id
];
132 return call(pos
, definition
);
135 enum ctf_string_encoding
{
143 * Because we address in bits, bitfields end up being exactly the same as
144 * integers, except that their read/write functions must be able to deal with
145 * read/write non aligned on CHAR_BIT.
147 struct declaration_integer
{
148 struct declaration p
;
149 size_t len
; /* length, in bits. */
150 int byte_order
; /* byte order */
152 int base
; /* Base for pretty-printing: 2, 8, 10, 16 */
153 enum ctf_string_encoding encoding
;
156 struct definition_integer
{
158 struct declaration_integer
*declaration
;
159 /* Last values read */
166 struct declaration_float
{
167 struct declaration p
;
168 struct declaration_integer
*sign
;
169 struct declaration_integer
*mantissa
;
170 struct declaration_integer
*exp
;
172 /* TODO: we might want to express more info about NaN, +inf and -inf */
175 struct definition_float
{
177 struct declaration_float
*declaration
;
178 struct definition_integer
*sign
;
179 struct definition_integer
*mantissa
;
180 struct definition_integer
*exp
;
181 /* Last values read */
186 * enum_val_equal assumes that signed and unsigned memory layout overlap.
192 } start
; /* lowest range value */
196 } end
; /* highest range value */
199 struct enum_range_to_quark
{
200 struct cds_list_head node
;
201 struct enum_range range
;
206 * We optimize the common case (range of size 1: single value) by creating a
207 * hash table mapping values to quark sets. We then lookup the ranges to
208 * complete the quark set.
210 * TODO: The proper structure to hold the range to quark set mapping would be an
211 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
212 * time. Using a simple O(n) list search for now for implementation speed and
213 * given that we can expect to have a _relatively_ small number of enumeration
214 * ranges. This might become untrue if we are fed with symbol tables often
215 * required to lookup function names from instruction pointer value.
218 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
219 struct cds_list_head range_to_quark
; /* (range, GQuark) */
220 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
223 struct declaration_enum
{
224 struct declaration p
;
225 struct declaration_integer
*integer_declaration
;
226 struct enum_table table
;
229 struct definition_enum
{
231 struct definition_integer
*integer
;
232 struct declaration_enum
*declaration
;
233 /* Last GQuark values read. Keeping a reference on the GQuark array. */
237 struct declaration_string
{
238 struct declaration p
;
239 enum ctf_string_encoding encoding
;
242 struct definition_string
{
244 struct declaration_string
*declaration
;
245 char *value
; /* freed at definition_string teardown */
246 size_t len
, alloc_len
;
249 struct declaration_field
{
251 struct declaration
*declaration
;
254 struct declaration_struct
{
255 struct declaration p
;
256 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
257 struct declaration_scope
*scope
;
258 GArray
*fields
; /* Array of declaration_field */
261 struct definition_struct
{
263 struct declaration_struct
*declaration
;
264 GPtrArray
*fields
; /* Array of pointers to struct definition */
267 struct declaration_untagged_variant
{
268 struct declaration p
;
269 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
270 struct declaration_scope
*scope
;
271 GArray
*fields
; /* Array of declaration_field */
274 struct declaration_variant
{
275 struct declaration p
;
276 struct declaration_untagged_variant
*untagged_variant
;
277 GArray
*tag_name
; /* Array of GQuark */
280 /* A variant needs to be tagged to be defined. */
281 struct definition_variant
{
283 struct declaration_variant
*declaration
;
284 struct definition
*enum_tag
;
285 GPtrArray
*fields
; /* Array of pointers to struct definition */
286 struct definition
*current_field
; /* Last field read */
289 struct declaration_array
{
290 struct declaration p
;
292 struct declaration
*elem
;
293 struct declaration_scope
*scope
;
296 struct definition_array
{
298 struct declaration_array
*declaration
;
299 GPtrArray
*elems
; /* Array of pointers to struct definition */
300 GString
*string
; /* String for encoded integer children */
303 struct declaration_sequence
{
304 struct declaration p
;
305 GArray
*length_name
; /* Array of GQuark */
306 struct declaration
*elem
;
307 struct declaration_scope
*scope
;
310 struct definition_sequence
{
312 struct declaration_sequence
*declaration
;
313 struct definition_integer
*length
;
314 GPtrArray
*elems
; /* Array of pointers to struct definition */
315 GString
*string
; /* String for encoded integer children */
318 int register_declaration(GQuark declaration_name
,
319 struct declaration
*declaration
,
320 struct declaration_scope
*scope
);
321 struct declaration
*lookup_declaration(GQuark declaration_name
,
322 struct declaration_scope
*scope
);
325 * Type scopes also contain a separate registry for struct, variant and
326 * enum types. Those register types rather than type definitions, so
327 * that a named variant can be declared without specifying its target
328 * "choice" tag field immediately.
330 int register_struct_declaration(GQuark struct_name
,
331 struct declaration_struct
*struct_declaration
,
332 struct declaration_scope
*scope
);
333 struct declaration_struct
*
334 lookup_struct_declaration(GQuark struct_name
,
335 struct declaration_scope
*scope
);
336 int register_variant_declaration(GQuark variant_name
,
337 struct declaration_untagged_variant
*untagged_variant_declaration
,
338 struct declaration_scope
*scope
);
339 struct declaration_untagged_variant
*lookup_variant_declaration(GQuark variant_name
,
340 struct declaration_scope
*scope
);
341 int register_enum_declaration(GQuark enum_name
,
342 struct declaration_enum
*enum_declaration
,
343 struct declaration_scope
*scope
);
344 struct declaration_enum
*
345 lookup_enum_declaration(GQuark enum_name
,
346 struct declaration_scope
*scope
);
348 struct declaration_scope
*
349 new_declaration_scope(struct declaration_scope
*parent_scope
);
350 void free_declaration_scope(struct declaration_scope
*scope
);
353 * field_definition is for field definitions. They are registered into
357 lookup_path_definition(GArray
*cur_path
, /* array of GQuark */
358 GArray
*lookup_path
, /* array of GQuark */
359 struct definition_scope
*scope
);
360 int register_field_definition(GQuark field_name
,
361 struct definition
*definition
,
362 struct definition_scope
*scope
);
363 struct definition_scope
*
364 new_definition_scope(struct definition_scope
*parent_scope
,
365 GQuark field_name
, const char *root_name
);
366 void free_definition_scope(struct definition_scope
*scope
);
368 GQuark
new_definition_path(struct definition_scope
*parent_scope
,
369 GQuark field_name
, const char *root_name
);
372 int compare_definition_path(struct definition
*definition
, GQuark path
)
374 return definition
->path
== path
;
377 void declaration_ref(struct declaration
*declaration
);
378 void declaration_unref(struct declaration
*declaration
);
380 void definition_ref(struct definition
*definition
);
381 void definition_unref(struct definition
*definition
);
383 struct declaration_integer
*integer_declaration_new(size_t len
, int byte_order
,
384 int signedness
, size_t alignment
,
385 int base
, enum ctf_string_encoding encoding
);
386 uint64_t get_unsigned_int(struct definition
*field
);
387 int64_t get_signed_int(struct definition
*field
);
390 * mantissa_len is the length of the number of bytes represented by the mantissa
391 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
393 struct declaration_float
*float_declaration_new(size_t mantissa_len
,
394 size_t exp_len
, int byte_order
,
398 * A GQuark can be translated to/from strings with g_quark_from_string() and
399 * g_quark_to_string().
403 * Returns a GArray of GQuark or NULL.
404 * Caller must release the GArray with g_array_unref().
406 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
410 * Returns a GArray of GQuark or NULL.
411 * Caller must release the GArray with g_array_unref().
413 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
417 * Returns a GArray of struct enum_range or NULL.
418 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
421 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
423 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
424 int64_t start
, int64_t end
, GQuark q
);
425 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
426 uint64_t start
, uint64_t end
, GQuark q
);
427 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
429 struct declaration_enum
*
430 enum_declaration_new(struct declaration_integer
*integer_declaration
);
432 struct declaration_string
*
433 string_declaration_new(enum ctf_string_encoding encoding
);
434 char *get_string(struct definition
*field
);
436 struct declaration_struct
*
437 struct_declaration_new(struct declaration_scope
*parent_scope
,
439 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
440 const char *field_name
,
441 struct declaration
*field_declaration
);
443 * Returns the index of a field within a structure.
445 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
448 * field returned only valid as long as the field structure is not appended to.
450 struct declaration_field
*
451 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
454 struct_definition_get_field_from_index(struct definition_struct
*struct_definition
,
456 int struct_rw(struct stream_pos
*pos
, struct definition
*definition
);
457 uint64_t struct_declaration_len(struct declaration_struct
*struct_declaration
);
460 * The tag enumeration is validated to ensure that it contains only mappings
461 * from numeric values to a single tag. Overlapping tag value ranges are
462 * therefore forbidden.
464 struct declaration_untagged_variant
*untagged_variant_declaration_new(
465 struct declaration_scope
*parent_scope
);
466 struct declaration_variant
*variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
469 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
470 const char *field_name
,
471 struct declaration
*field_declaration
);
472 struct declaration_field
*
473 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
476 * Returns 0 on success, -EPERM on error.
478 int variant_definition_set_tag(struct definition_variant
*variant
,
479 struct definition
*enum_tag
);
481 * Returns the field selected by the current tag value.
482 * field returned only valid as long as the variant structure is not appended
485 struct definition
*variant_get_current_field(struct definition_variant
*variant
);
486 int variant_rw(struct stream_pos
*pos
, struct definition
*definition
);
489 * elem_declaration passed as parameter now belongs to the array. No
490 * need to free it explicitly. "len" is the number of elements in the
493 struct declaration_array
*
494 array_declaration_new(size_t len
, struct declaration
*elem_declaration
,
495 struct declaration_scope
*parent_scope
);
496 uint64_t array_len(struct definition_array
*array
);
497 struct definition
*array_index(struct definition_array
*array
, uint64_t i
);
498 int array_rw(struct stream_pos
*pos
, struct definition
*definition
);
499 GString
*get_char_array(struct definition
*field
);
502 * int_declaration and elem_declaration passed as parameter now belong
503 * to the sequence. No need to free them explicitly.
505 struct declaration_sequence
*
506 sequence_declaration_new(const char *length_name
,
507 struct declaration
*elem_declaration
,
508 struct declaration_scope
*parent_scope
);
509 uint64_t sequence_len(struct definition_sequence
*sequence
);
510 struct definition
*sequence_index(struct definition_sequence
*sequence
, uint64_t i
);
511 int sequence_rw(struct stream_pos
*pos
, struct definition
*definition
);
514 * in: path (dot separated), out: q (GArray of GQuark)
516 void append_scope_path(const char *path
, GArray
*q
);
521 struct definition
*lookup_definition(struct definition
*definition
,
522 const char *field_name
);
523 struct definition_integer
*lookup_integer(struct definition
*definition
,
524 const char *field_name
,
526 struct definition_enum
*lookup_enum(struct definition
*definition
,
527 const char *field_name
,
529 struct definition
*lookup_variant(struct definition
*definition
,
530 const char *field_name
);
532 #endif /* _BABELTRACE_TYPES_H */