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>
26 #include <babeltrace/ctf/events.h>
34 /* Preallocate this many fields for structures */
35 #define DEFAULT_NR_STRUCT_FIELDS 8
44 struct declaration_scope
{
45 /* Hash table mapping type name GQuark to "struct declaration" */
46 /* Used for both typedef and typealias. */
47 GHashTable
*typedef_declarations
;
48 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
49 GHashTable
*struct_declarations
;
50 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
51 GHashTable
*variant_declarations
;
52 /* Hash table mapping enum name GQuark to "struct type_enum" */
53 GHashTable
*enum_declarations
;
54 struct declaration_scope
*parent_scope
;
57 /* definition scope */
58 struct definition_scope
{
59 /* Hash table mapping field name GQuark to "struct definition" */
60 GHashTable
*definitions
;
61 struct definition_scope
*parent_scope
;
63 * Complete "path" leading to this definition scope.
64 * Includes dynamic scope name '.' field name '.' field name '.' ....
65 * Array of GQuark elements (which are each separated by dots).
66 * The dynamic scope name can contain dots, and is encoded into
67 * a single GQuark. Thus, scope_path[0] returns the GQuark
68 * identifying the dynamic scope.
70 GArray
*scope_path
; /* array of GQuark */
75 size_t alignment
; /* type alignment, in bits */
76 int ref
; /* number of references to the type */
78 * declaration_free called with declaration ref is decremented to 0.
80 void (*declaration_free
)(struct declaration
*declaration
);
82 (*definition_new
)(struct declaration
*declaration
,
83 struct definition_scope
*parent_scope
,
84 GQuark field_name
, int index
,
85 const char *root_name
);
87 * definition_free called with definition ref is decremented to 0.
89 void (*definition_free
)(struct definition
*definition
);
93 struct declaration
*declaration
;
94 int index
; /* Position of the definition in its container */
95 GQuark name
; /* Field name in its container (or 0 if unset) */
96 int ref
; /* number of references to the definition */
98 struct definition_scope
*scope
;
101 typedef int (*rw_dispatch
)(struct stream_pos
*pos
,
102 struct definition
*definition
);
104 /* Parent of per-plugin positions */
106 /* read/write dispatch table. Specific to plugin used for stream. */
107 rw_dispatch
*rw_table
; /* rw dispatch table */
108 int (*event_cb
)(struct stream_pos
*pos
,
109 struct ctf_stream
*stream
);
113 int generic_rw(struct stream_pos
*pos
, struct definition
*definition
)
115 enum ctf_type_id dispatch_id
= definition
->declaration
->id
;
118 assert(pos
->rw_table
[dispatch_id
] != NULL
);
119 call
= pos
->rw_table
[dispatch_id
];
120 return call(pos
, definition
);
123 enum ctf_string_encoding
{
131 * Because we address in bits, bitfields end up being exactly the same as
132 * integers, except that their read/write functions must be able to deal with
133 * read/write non aligned on CHAR_BIT.
135 struct declaration_integer
{
136 struct declaration p
;
137 size_t len
; /* length, in bits. */
138 int byte_order
; /* byte order */
140 int base
; /* Base for pretty-printing: 2, 8, 10, 16 */
141 enum ctf_string_encoding encoding
;
142 struct ctf_clock
*clock
;
145 struct definition_integer
{
147 struct declaration_integer
*declaration
;
148 /* Last values read */
155 struct declaration_float
{
156 struct declaration p
;
157 struct declaration_integer
*sign
;
158 struct declaration_integer
*mantissa
;
159 struct declaration_integer
*exp
;
161 /* TODO: we might want to express more info about NaN, +inf and -inf */
164 struct definition_float
{
166 struct declaration_float
*declaration
;
167 struct definition_integer
*sign
;
168 struct definition_integer
*mantissa
;
169 struct definition_integer
*exp
;
170 /* Last values read */
175 * enum_val_equal assumes that signed and unsigned memory layout overlap.
181 } start
; /* lowest range value */
185 } end
; /* highest range value */
188 struct enum_range_to_quark
{
189 struct bt_list_head node
;
190 struct enum_range range
;
195 * We optimize the common case (range of size 1: single value) by creating a
196 * hash table mapping values to quark sets. We then lookup the ranges to
197 * complete the quark set.
199 * TODO: The proper structure to hold the range to quark set mapping would be an
200 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
201 * time. Using a simple O(n) list search for now for implementation speed and
202 * given that we can expect to have a _relatively_ small number of enumeration
203 * ranges. This might become untrue if we are fed with symbol tables often
204 * required to lookup function names from instruction pointer value.
207 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
208 struct bt_list_head range_to_quark
; /* (range, GQuark) */
209 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
212 struct declaration_enum
{
213 struct declaration p
;
214 struct declaration_integer
*integer_declaration
;
215 struct enum_table table
;
218 struct definition_enum
{
220 struct definition_integer
*integer
;
221 struct declaration_enum
*declaration
;
222 /* Last GQuark values read. Keeping a reference on the GQuark array. */
226 struct declaration_string
{
227 struct declaration p
;
228 enum ctf_string_encoding encoding
;
231 struct definition_string
{
233 struct declaration_string
*declaration
;
234 char *value
; /* freed at definition_string teardown */
235 size_t len
, alloc_len
;
238 struct declaration_field
{
240 struct declaration
*declaration
;
243 struct declaration_struct
{
244 struct declaration p
;
245 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
246 struct declaration_scope
*scope
;
247 GArray
*fields
; /* Array of declaration_field */
250 struct definition_struct
{
252 struct declaration_struct
*declaration
;
253 GPtrArray
*fields
; /* Array of pointers to struct definition */
256 struct declaration_untagged_variant
{
257 struct declaration p
;
258 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
259 struct declaration_scope
*scope
;
260 GArray
*fields
; /* Array of declaration_field */
263 struct declaration_variant
{
264 struct declaration p
;
265 struct declaration_untagged_variant
*untagged_variant
;
266 GArray
*tag_name
; /* Array of GQuark */
269 /* A variant needs to be tagged to be defined. */
270 struct definition_variant
{
272 struct declaration_variant
*declaration
;
273 struct definition
*enum_tag
;
274 GPtrArray
*fields
; /* Array of pointers to struct definition */
275 struct definition
*current_field
; /* Last field read */
278 struct declaration_array
{
279 struct declaration p
;
281 struct declaration
*elem
;
282 struct declaration_scope
*scope
;
285 struct definition_array
{
287 struct declaration_array
*declaration
;
288 GPtrArray
*elems
; /* Array of pointers to struct definition */
289 GString
*string
; /* String for encoded integer children */
292 struct declaration_sequence
{
293 struct declaration p
;
294 GArray
*length_name
; /* Array of GQuark */
295 struct declaration
*elem
;
296 struct declaration_scope
*scope
;
299 struct definition_sequence
{
301 struct declaration_sequence
*declaration
;
302 struct definition_integer
*length
;
303 GPtrArray
*elems
; /* Array of pointers to struct definition */
304 GString
*string
; /* String for encoded integer children */
307 int register_declaration(GQuark declaration_name
,
308 struct declaration
*declaration
,
309 struct declaration_scope
*scope
);
310 struct declaration
*lookup_declaration(GQuark declaration_name
,
311 struct declaration_scope
*scope
);
314 * Type scopes also contain a separate registry for struct, variant and
315 * enum types. Those register types rather than type definitions, so
316 * that a named variant can be declared without specifying its target
317 * "choice" tag field immediately.
319 int register_struct_declaration(GQuark struct_name
,
320 struct declaration_struct
*struct_declaration
,
321 struct declaration_scope
*scope
);
322 struct declaration_struct
*
323 lookup_struct_declaration(GQuark struct_name
,
324 struct declaration_scope
*scope
);
325 int register_variant_declaration(GQuark variant_name
,
326 struct declaration_untagged_variant
*untagged_variant_declaration
,
327 struct declaration_scope
*scope
);
328 struct declaration_untagged_variant
*lookup_variant_declaration(GQuark variant_name
,
329 struct declaration_scope
*scope
);
330 int register_enum_declaration(GQuark enum_name
,
331 struct declaration_enum
*enum_declaration
,
332 struct declaration_scope
*scope
);
333 struct declaration_enum
*
334 lookup_enum_declaration(GQuark enum_name
,
335 struct declaration_scope
*scope
);
337 struct declaration_scope
*
338 new_declaration_scope(struct declaration_scope
*parent_scope
);
339 void free_declaration_scope(struct declaration_scope
*scope
);
342 * field_definition is for field definitions. They are registered into
346 lookup_path_definition(GArray
*cur_path
, /* array of GQuark */
347 GArray
*lookup_path
, /* array of GQuark */
348 struct definition_scope
*scope
);
349 int register_field_definition(GQuark field_name
,
350 struct definition
*definition
,
351 struct definition_scope
*scope
);
352 struct definition_scope
*
353 new_definition_scope(struct definition_scope
*parent_scope
,
354 GQuark field_name
, const char *root_name
);
355 void free_definition_scope(struct definition_scope
*scope
);
357 GQuark
new_definition_path(struct definition_scope
*parent_scope
,
358 GQuark field_name
, const char *root_name
);
361 int compare_definition_path(struct definition
*definition
, GQuark path
)
363 return definition
->path
== path
;
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
,
374 int base
, enum ctf_string_encoding encoding
,
375 struct ctf_clock
*clock
);
376 uint64_t get_unsigned_int(struct definition
*field
);
377 int64_t get_signed_int(struct definition
*field
);
380 * mantissa_len is the length of the number of bytes represented by the mantissa
381 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
383 struct declaration_float
*float_declaration_new(size_t mantissa_len
,
384 size_t exp_len
, int byte_order
,
388 * A GQuark can be translated to/from strings with g_quark_from_string() and
389 * g_quark_to_string().
393 * Returns a GArray of GQuark or NULL.
394 * Caller must release the GArray with g_array_unref().
396 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
400 * Returns a GArray of GQuark or NULL.
401 * Caller must release the GArray with g_array_unref().
403 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
407 * Returns a GArray of struct enum_range or NULL.
408 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
411 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
413 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
414 int64_t start
, int64_t end
, GQuark q
);
415 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
416 uint64_t start
, uint64_t end
, GQuark q
);
417 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
419 struct declaration_enum
*
420 enum_declaration_new(struct declaration_integer
*integer_declaration
);
422 struct declaration_string
*
423 string_declaration_new(enum ctf_string_encoding encoding
);
424 char *get_string(struct definition
*field
);
426 struct declaration_struct
*
427 struct_declaration_new(struct declaration_scope
*parent_scope
,
429 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
430 const char *field_name
,
431 struct declaration
*field_declaration
);
433 * Returns the index of a field within a structure.
435 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
438 * field returned only valid as long as the field structure is not appended to.
440 struct declaration_field
*
441 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
444 struct_definition_get_field_from_index(struct definition_struct
*struct_definition
,
446 int struct_rw(struct stream_pos
*pos
, struct definition
*definition
);
447 uint64_t struct_declaration_len(struct declaration_struct
*struct_declaration
);
450 * The tag enumeration is validated to ensure that it contains only mappings
451 * from numeric values to a single tag. Overlapping tag value ranges are
452 * therefore forbidden.
454 struct declaration_untagged_variant
*untagged_variant_declaration_new(
455 struct declaration_scope
*parent_scope
);
456 struct declaration_variant
*variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
459 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
460 const char *field_name
,
461 struct declaration
*field_declaration
);
462 struct declaration_field
*
463 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
466 * Returns 0 on success, -EPERM on error.
468 int variant_definition_set_tag(struct definition_variant
*variant
,
469 struct definition
*enum_tag
);
471 * Returns the field selected by the current tag value.
472 * field returned only valid as long as the variant structure is not appended
475 struct definition
*variant_get_current_field(struct definition_variant
*variant
);
476 int variant_rw(struct stream_pos
*pos
, struct definition
*definition
);
479 * elem_declaration passed as parameter now belongs to the array. No
480 * need to free it explicitly. "len" is the number of elements in the
483 struct declaration_array
*
484 array_declaration_new(size_t len
, struct declaration
*elem_declaration
,
485 struct declaration_scope
*parent_scope
);
486 uint64_t array_len(struct definition_array
*array
);
487 struct definition
*array_index(struct definition_array
*array
, uint64_t i
);
488 int array_rw(struct stream_pos
*pos
, struct definition
*definition
);
489 GString
*get_char_array(struct definition
*field
);
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(const char *length_name
,
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
);
511 struct definition
*lookup_definition(struct definition
*definition
,
512 const char *field_name
);
513 struct definition_integer
*lookup_integer(struct definition
*definition
,
514 const char *field_name
,
516 struct definition_enum
*lookup_enum(struct definition
*definition
,
517 const char *field_name
,
519 struct definition
*lookup_variant(struct definition
*definition
,
520 const char *field_name
);
522 #endif /* _BABELTRACE_TYPES_H */