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
43 struct declaration_scope
{
44 /* Hash table mapping type name GQuark to "struct declaration" */
45 /* Used for both typedef and typealias. */
46 GHashTable
*typedef_declarations
;
47 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
48 GHashTable
*struct_declarations
;
49 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
50 GHashTable
*variant_declarations
;
51 /* Hash table mapping enum name GQuark to "struct type_enum" */
52 GHashTable
*enum_declarations
;
53 struct declaration_scope
*parent_scope
;
56 /* definition scope */
57 struct definition_scope
{
58 /* Hash table mapping field name GQuark to "struct definition" */
59 GHashTable
*definitions
;
60 struct definition_scope
*parent_scope
;
62 * Complete "path" leading to this definition scope.
63 * Includes dynamic scope name '.' field name '.' field name '.' ....
64 * Array of GQuark elements (which are each separated by dots).
65 * The dynamic scope name can contain dots, and is encoded into
66 * a single GQuark. Thus, scope_path[0] returns the GQuark
67 * identifying the dynamic scope.
69 GArray
*scope_path
; /* array of GQuark */
79 CTF_TYPE_UNTAGGED_VARIANT
,
88 size_t alignment
; /* type alignment, in bits */
89 int ref
; /* number of references to the type */
91 * declaration_free called with declaration ref is decremented to 0.
93 void (*declaration_free
)(struct declaration
*declaration
);
95 (*definition_new
)(struct declaration
*declaration
,
96 struct definition_scope
*parent_scope
,
97 GQuark field_name
, int index
,
98 const char *root_name
);
100 * definition_free called with definition ref is decremented to 0.
102 void (*definition_free
)(struct definition
*definition
);
106 struct declaration
*declaration
;
107 int index
; /* Position of the definition in its container */
108 GQuark name
; /* Field name in its container (or 0 if unset) */
109 int ref
; /* number of references to the definition */
111 struct definition_scope
*scope
;
114 typedef int (*rw_dispatch
)(struct stream_pos
*pos
,
115 struct definition
*definition
);
117 /* Parent of per-plugin positions */
119 /* read/write dispatch table. Specific to plugin used for stream. */
120 rw_dispatch
*rw_table
; /* rw dispatch table */
121 int (*event_cb
)(struct stream_pos
*pos
,
122 struct ctf_stream
*stream
);
126 int generic_rw(struct stream_pos
*pos
, struct definition
*definition
)
128 enum ctf_type_id dispatch_id
= definition
->declaration
->id
;
131 assert(pos
->rw_table
[dispatch_id
] != NULL
);
132 call
= pos
->rw_table
[dispatch_id
];
133 return call(pos
, definition
);
136 enum ctf_string_encoding
{
144 * Because we address in bits, bitfields end up being exactly the same as
145 * integers, except that their read/write functions must be able to deal with
146 * read/write non aligned on CHAR_BIT.
148 struct declaration_integer
{
149 struct declaration p
;
150 size_t len
; /* length, in bits. */
151 int byte_order
; /* byte order */
153 int base
; /* Base for pretty-printing: 2, 8, 10, 16 */
154 enum ctf_string_encoding encoding
;
155 struct ctf_clock
*clock
;
158 struct definition_integer
{
160 struct declaration_integer
*declaration
;
161 /* Last values read */
168 struct declaration_float
{
169 struct declaration p
;
170 struct declaration_integer
*sign
;
171 struct declaration_integer
*mantissa
;
172 struct declaration_integer
*exp
;
174 /* TODO: we might want to express more info about NaN, +inf and -inf */
177 struct definition_float
{
179 struct declaration_float
*declaration
;
180 struct definition_integer
*sign
;
181 struct definition_integer
*mantissa
;
182 struct definition_integer
*exp
;
183 /* Last values read */
188 * enum_val_equal assumes that signed and unsigned memory layout overlap.
194 } start
; /* lowest range value */
198 } end
; /* highest range value */
201 struct enum_range_to_quark
{
202 struct cds_list_head node
;
203 struct enum_range range
;
208 * We optimize the common case (range of size 1: single value) by creating a
209 * hash table mapping values to quark sets. We then lookup the ranges to
210 * complete the quark set.
212 * TODO: The proper structure to hold the range to quark set mapping would be an
213 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
214 * time. Using a simple O(n) list search for now for implementation speed and
215 * given that we can expect to have a _relatively_ small number of enumeration
216 * ranges. This might become untrue if we are fed with symbol tables often
217 * required to lookup function names from instruction pointer value.
220 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
221 struct cds_list_head range_to_quark
; /* (range, GQuark) */
222 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
225 struct declaration_enum
{
226 struct declaration p
;
227 struct declaration_integer
*integer_declaration
;
228 struct enum_table table
;
231 struct definition_enum
{
233 struct definition_integer
*integer
;
234 struct declaration_enum
*declaration
;
235 /* Last GQuark values read. Keeping a reference on the GQuark array. */
239 struct declaration_string
{
240 struct declaration p
;
241 enum ctf_string_encoding encoding
;
244 struct definition_string
{
246 struct declaration_string
*declaration
;
247 char *value
; /* freed at definition_string teardown */
248 size_t len
, alloc_len
;
251 struct declaration_field
{
253 struct declaration
*declaration
;
256 struct declaration_struct
{
257 struct declaration p
;
258 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
259 struct declaration_scope
*scope
;
260 GArray
*fields
; /* Array of declaration_field */
263 struct definition_struct
{
265 struct declaration_struct
*declaration
;
266 GPtrArray
*fields
; /* Array of pointers to struct definition */
269 struct declaration_untagged_variant
{
270 struct declaration p
;
271 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
272 struct declaration_scope
*scope
;
273 GArray
*fields
; /* Array of declaration_field */
276 struct declaration_variant
{
277 struct declaration p
;
278 struct declaration_untagged_variant
*untagged_variant
;
279 GArray
*tag_name
; /* Array of GQuark */
282 /* A variant needs to be tagged to be defined. */
283 struct definition_variant
{
285 struct declaration_variant
*declaration
;
286 struct definition
*enum_tag
;
287 GPtrArray
*fields
; /* Array of pointers to struct definition */
288 struct definition
*current_field
; /* Last field read */
291 struct declaration_array
{
292 struct declaration p
;
294 struct declaration
*elem
;
295 struct declaration_scope
*scope
;
298 struct definition_array
{
300 struct declaration_array
*declaration
;
301 GPtrArray
*elems
; /* Array of pointers to struct definition */
302 GString
*string
; /* String for encoded integer children */
305 struct declaration_sequence
{
306 struct declaration p
;
307 GArray
*length_name
; /* Array of GQuark */
308 struct declaration
*elem
;
309 struct declaration_scope
*scope
;
312 struct definition_sequence
{
314 struct declaration_sequence
*declaration
;
315 struct definition_integer
*length
;
316 GPtrArray
*elems
; /* Array of pointers to struct definition */
317 GString
*string
; /* String for encoded integer children */
320 int register_declaration(GQuark declaration_name
,
321 struct declaration
*declaration
,
322 struct declaration_scope
*scope
);
323 struct declaration
*lookup_declaration(GQuark declaration_name
,
324 struct declaration_scope
*scope
);
327 * Type scopes also contain a separate registry for struct, variant and
328 * enum types. Those register types rather than type definitions, so
329 * that a named variant can be declared without specifying its target
330 * "choice" tag field immediately.
332 int register_struct_declaration(GQuark struct_name
,
333 struct declaration_struct
*struct_declaration
,
334 struct declaration_scope
*scope
);
335 struct declaration_struct
*
336 lookup_struct_declaration(GQuark struct_name
,
337 struct declaration_scope
*scope
);
338 int register_variant_declaration(GQuark variant_name
,
339 struct declaration_untagged_variant
*untagged_variant_declaration
,
340 struct declaration_scope
*scope
);
341 struct declaration_untagged_variant
*lookup_variant_declaration(GQuark variant_name
,
342 struct declaration_scope
*scope
);
343 int register_enum_declaration(GQuark enum_name
,
344 struct declaration_enum
*enum_declaration
,
345 struct declaration_scope
*scope
);
346 struct declaration_enum
*
347 lookup_enum_declaration(GQuark enum_name
,
348 struct declaration_scope
*scope
);
350 struct declaration_scope
*
351 new_declaration_scope(struct declaration_scope
*parent_scope
);
352 void free_declaration_scope(struct declaration_scope
*scope
);
355 * field_definition is for field definitions. They are registered into
359 lookup_path_definition(GArray
*cur_path
, /* array of GQuark */
360 GArray
*lookup_path
, /* array of GQuark */
361 struct definition_scope
*scope
);
362 int register_field_definition(GQuark field_name
,
363 struct definition
*definition
,
364 struct definition_scope
*scope
);
365 struct definition_scope
*
366 new_definition_scope(struct definition_scope
*parent_scope
,
367 GQuark field_name
, const char *root_name
);
368 void free_definition_scope(struct definition_scope
*scope
);
370 GQuark
new_definition_path(struct definition_scope
*parent_scope
,
371 GQuark field_name
, const char *root_name
);
374 int compare_definition_path(struct definition
*definition
, GQuark path
)
376 return definition
->path
== path
;
379 void declaration_ref(struct declaration
*declaration
);
380 void declaration_unref(struct declaration
*declaration
);
382 void definition_ref(struct definition
*definition
);
383 void definition_unref(struct definition
*definition
);
385 struct declaration_integer
*integer_declaration_new(size_t len
, int byte_order
,
386 int signedness
, size_t alignment
,
387 int base
, enum ctf_string_encoding encoding
,
388 struct ctf_clock
*clock
);
389 uint64_t get_unsigned_int(struct definition
*field
);
390 int64_t get_signed_int(struct definition
*field
);
393 * mantissa_len is the length of the number of bytes represented by the mantissa
394 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
396 struct declaration_float
*float_declaration_new(size_t mantissa_len
,
397 size_t exp_len
, int byte_order
,
401 * A GQuark can be translated to/from strings with g_quark_from_string() and
402 * g_quark_to_string().
406 * Returns a GArray of GQuark or NULL.
407 * Caller must release the GArray with g_array_unref().
409 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
413 * Returns a GArray of GQuark or NULL.
414 * Caller must release the GArray with g_array_unref().
416 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
420 * Returns a GArray of struct enum_range or NULL.
421 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
424 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
426 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
427 int64_t start
, int64_t end
, GQuark q
);
428 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
429 uint64_t start
, uint64_t end
, GQuark q
);
430 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
432 struct declaration_enum
*
433 enum_declaration_new(struct declaration_integer
*integer_declaration
);
435 struct declaration_string
*
436 string_declaration_new(enum ctf_string_encoding encoding
);
437 char *get_string(struct definition
*field
);
439 struct declaration_struct
*
440 struct_declaration_new(struct declaration_scope
*parent_scope
,
442 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
443 const char *field_name
,
444 struct declaration
*field_declaration
);
446 * Returns the index of a field within a structure.
448 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
451 * field returned only valid as long as the field structure is not appended to.
453 struct declaration_field
*
454 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
457 struct_definition_get_field_from_index(struct definition_struct
*struct_definition
,
459 int struct_rw(struct stream_pos
*pos
, struct definition
*definition
);
460 uint64_t struct_declaration_len(struct declaration_struct
*struct_declaration
);
463 * The tag enumeration is validated to ensure that it contains only mappings
464 * from numeric values to a single tag. Overlapping tag value ranges are
465 * therefore forbidden.
467 struct declaration_untagged_variant
*untagged_variant_declaration_new(
468 struct declaration_scope
*parent_scope
);
469 struct declaration_variant
*variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
472 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
473 const char *field_name
,
474 struct declaration
*field_declaration
);
475 struct declaration_field
*
476 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
479 * Returns 0 on success, -EPERM on error.
481 int variant_definition_set_tag(struct definition_variant
*variant
,
482 struct definition
*enum_tag
);
484 * Returns the field selected by the current tag value.
485 * field returned only valid as long as the variant structure is not appended
488 struct definition
*variant_get_current_field(struct definition_variant
*variant
);
489 int variant_rw(struct stream_pos
*pos
, struct definition
*definition
);
492 * elem_declaration passed as parameter now belongs to the array. No
493 * need to free it explicitly. "len" is the number of elements in the
496 struct declaration_array
*
497 array_declaration_new(size_t len
, struct declaration
*elem_declaration
,
498 struct declaration_scope
*parent_scope
);
499 uint64_t array_len(struct definition_array
*array
);
500 struct definition
*array_index(struct definition_array
*array
, uint64_t i
);
501 int array_rw(struct stream_pos
*pos
, struct definition
*definition
);
502 GString
*get_char_array(struct definition
*field
);
505 * int_declaration and elem_declaration passed as parameter now belong
506 * to the sequence. No need to free them explicitly.
508 struct declaration_sequence
*
509 sequence_declaration_new(const char *length_name
,
510 struct declaration
*elem_declaration
,
511 struct declaration_scope
*parent_scope
);
512 uint64_t sequence_len(struct definition_sequence
*sequence
);
513 struct definition
*sequence_index(struct definition_sequence
*sequence
, uint64_t i
);
514 int sequence_rw(struct stream_pos
*pos
, struct definition
*definition
);
517 * in: path (dot separated), out: q (GArray of GQuark)
519 void append_scope_path(const char *path
, GArray
*q
);
524 struct definition
*lookup_definition(struct definition
*definition
,
525 const char *field_name
);
526 struct definition_integer
*lookup_integer(struct definition
*definition
,
527 const char *field_name
,
529 struct definition_enum
*lookup_enum(struct definition
*definition
,
530 const char *field_name
,
532 struct definition
*lookup_variant(struct definition
*definition
,
533 const char *field_name
);
535 #endif /* _BABELTRACE_TYPES_H */