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.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 #include <babeltrace/align.h>
33 #include <babeltrace/list.h>
34 #include <babeltrace/ctf/events.h>
42 /* Preallocate this many fields for structures */
43 #define DEFAULT_NR_STRUCT_FIELDS 8
45 struct ctf_stream_definition
;
52 struct declaration_scope
{
53 /* Hash table mapping type name GQuark to "struct declaration" */
54 /* Used for both typedef and typealias. */
55 GHashTable
*typedef_declarations
;
56 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
57 GHashTable
*struct_declarations
;
58 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
59 GHashTable
*variant_declarations
;
60 /* Hash table mapping enum name GQuark to "struct type_enum" */
61 GHashTable
*enum_declarations
;
62 struct declaration_scope
*parent_scope
;
65 /* definition scope */
66 struct definition_scope
{
67 /* Hash table mapping field name GQuark to "struct definition" */
68 GHashTable
*definitions
;
69 struct definition_scope
*parent_scope
;
71 * Complete "path" leading to this definition scope.
72 * Includes dynamic scope name '.' field name '.' field name '.' ....
73 * Array of GQuark elements (which are each separated by dots).
74 * The dynamic scope name can contain dots, and is encoded into
75 * a single GQuark. Thus, scope_path[0] returns the GQuark
76 * identifying the dynamic scope.
78 GArray
*scope_path
; /* array of GQuark */
83 size_t alignment
; /* type alignment, in bits */
84 int ref
; /* number of references to the type */
86 * declaration_free called with declaration ref is decremented to 0.
88 void (*declaration_free
)(struct declaration
*declaration
);
90 (*definition_new
)(struct declaration
*declaration
,
91 struct definition_scope
*parent_scope
,
92 GQuark field_name
, int index
,
93 const char *root_name
);
95 * definition_free called with definition ref is decremented to 0.
97 void (*definition_free
)(struct definition
*definition
);
101 struct declaration
*declaration
;
102 int index
; /* Position of the definition in its container */
103 GQuark name
; /* Field name in its container (or 0 if unset) */
104 int ref
; /* number of references to the definition */
106 struct definition_scope
*scope
;
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_definition
*stream
);
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 */
141 int base
; /* Base for pretty-printing: 2, 8, 10, 16 */
142 enum ctf_string_encoding encoding
;
143 struct ctf_clock
*clock
;
146 struct definition_integer
{
148 struct declaration_integer
*declaration
;
149 /* Last values read */
156 struct declaration_float
{
157 struct declaration p
;
158 struct declaration_integer
*sign
;
159 struct declaration_integer
*mantissa
;
160 struct declaration_integer
*exp
;
162 /* TODO: we might want to express more info about NaN, +inf and -inf */
165 struct definition_float
{
167 struct declaration_float
*declaration
;
168 struct definition_integer
*sign
;
169 struct definition_integer
*mantissa
;
170 struct definition_integer
*exp
;
171 /* Last values read */
176 * enum_val_equal assumes that signed and unsigned memory layout overlap.
182 } start
; /* lowest range value */
186 } end
; /* highest range value */
189 struct enum_range_to_quark
{
190 struct bt_list_head node
;
191 struct enum_range range
;
196 * We optimize the common case (range of size 1: single value) by creating a
197 * hash table mapping values to quark sets. We then lookup the ranges to
198 * complete the quark set.
200 * TODO: The proper structure to hold the range to quark set mapping would be an
201 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
202 * time. Using a simple O(n) list search for now for implementation speed and
203 * given that we can expect to have a _relatively_ small number of enumeration
204 * ranges. This might become untrue if we are fed with symbol tables often
205 * required to lookup function names from instruction pointer value.
208 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
209 struct bt_list_head range_to_quark
; /* (range, GQuark) */
210 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
213 struct declaration_enum
{
214 struct declaration p
;
215 struct declaration_integer
*integer_declaration
;
216 struct enum_table table
;
219 struct definition_enum
{
221 struct definition_integer
*integer
;
222 struct declaration_enum
*declaration
;
223 /* Last GQuark values read. Keeping a reference on the GQuark array. */
227 struct declaration_string
{
228 struct declaration p
;
229 enum ctf_string_encoding encoding
;
232 struct definition_string
{
234 struct declaration_string
*declaration
;
235 char *value
; /* freed at definition_string teardown */
236 size_t len
, alloc_len
;
239 struct declaration_field
{
241 struct declaration
*declaration
;
244 struct declaration_struct
{
245 struct declaration p
;
246 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
247 struct declaration_scope
*scope
;
248 GArray
*fields
; /* Array of declaration_field */
251 struct definition_struct
{
253 struct declaration_struct
*declaration
;
254 GPtrArray
*fields
; /* Array of pointers to struct definition */
257 struct declaration_untagged_variant
{
258 struct declaration p
;
259 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
260 struct declaration_scope
*scope
;
261 GArray
*fields
; /* Array of declaration_field */
264 struct declaration_variant
{
265 struct declaration p
;
266 struct declaration_untagged_variant
*untagged_variant
;
267 GArray
*tag_name
; /* Array of GQuark */
270 /* A variant needs to be tagged to be defined. */
271 struct definition_variant
{
273 struct declaration_variant
*declaration
;
274 struct definition
*enum_tag
;
275 GPtrArray
*fields
; /* Array of pointers to struct definition */
276 struct definition
*current_field
; /* Last field read */
279 struct declaration_array
{
280 struct declaration p
;
282 struct declaration
*elem
;
283 struct declaration_scope
*scope
;
286 struct definition_array
{
288 struct declaration_array
*declaration
;
289 GPtrArray
*elems
; /* Array of pointers to struct definition */
290 GString
*string
; /* String for encoded integer children */
293 struct declaration_sequence
{
294 struct declaration p
;
295 GArray
*length_name
; /* Array of GQuark */
296 struct declaration
*elem
;
297 struct declaration_scope
*scope
;
300 struct definition_sequence
{
302 struct declaration_sequence
*declaration
;
303 struct definition_integer
*length
;
304 GPtrArray
*elems
; /* Array of pointers to struct definition */
305 GString
*string
; /* String for encoded integer children */
308 int bt_register_declaration(GQuark declaration_name
,
309 struct declaration
*declaration
,
310 struct declaration_scope
*scope
);
311 struct declaration
*bt_lookup_declaration(GQuark declaration_name
,
312 struct declaration_scope
*scope
);
315 * Type scopes also contain a separate registry for struct, variant and
316 * enum types. Those register types rather than type definitions, so
317 * that a named variant can be declared without specifying its target
318 * "choice" tag field immediately.
320 int bt_register_struct_declaration(GQuark struct_name
,
321 struct declaration_struct
*struct_declaration
,
322 struct declaration_scope
*scope
);
323 struct declaration_struct
*
324 bt_lookup_struct_declaration(GQuark struct_name
,
325 struct declaration_scope
*scope
);
326 int bt_register_variant_declaration(GQuark variant_name
,
327 struct declaration_untagged_variant
*untagged_variant_declaration
,
328 struct declaration_scope
*scope
);
329 struct declaration_untagged_variant
*bt_lookup_variant_declaration(GQuark variant_name
,
330 struct declaration_scope
*scope
);
331 int bt_register_enum_declaration(GQuark enum_name
,
332 struct declaration_enum
*enum_declaration
,
333 struct declaration_scope
*scope
);
334 struct declaration_enum
*
335 bt_lookup_enum_declaration(GQuark enum_name
,
336 struct declaration_scope
*scope
);
338 struct declaration_scope
*
339 bt_new_declaration_scope(struct declaration_scope
*parent_scope
);
340 void bt_free_declaration_scope(struct declaration_scope
*scope
);
343 * field_definition is for field definitions. They are registered into
347 lookup_path_definition(GArray
*cur_path
, /* array of GQuark */
348 GArray
*lookup_path
, /* array of GQuark */
349 struct definition_scope
*scope
);
350 int register_field_definition(GQuark field_name
,
351 struct definition
*definition
,
352 struct definition_scope
*scope
);
353 struct definition_scope
*
354 new_definition_scope(struct definition_scope
*parent_scope
,
355 GQuark field_name
, const char *root_name
);
356 void free_definition_scope(struct definition_scope
*scope
);
358 GQuark
new_definition_path(struct definition_scope
*parent_scope
,
359 GQuark field_name
, const char *root_name
);
362 int compare_definition_path(struct definition
*definition
, GQuark path
)
364 return definition
->path
== path
;
367 void bt_declaration_ref(struct declaration
*declaration
);
368 void bt_declaration_unref(struct declaration
*declaration
);
370 void bt_definition_ref(struct definition
*definition
);
371 void bt_definition_unref(struct definition
*definition
);
373 struct declaration_integer
*bt_integer_declaration_new(size_t len
, int byte_order
,
374 int signedness
, size_t alignment
,
375 int base
, enum ctf_string_encoding encoding
,
376 struct ctf_clock
*clock
);
377 uint64_t bt_get_unsigned_int(const struct definition
*field
);
378 int64_t bt_get_signed_int(const struct definition
*field
);
379 int bt_get_int_signedness(const struct definition
*field
);
380 int bt_get_int_byte_order(const struct definition
*field
);
381 int bt_get_int_base(const struct definition
*field
);
382 size_t bt_get_int_len(const struct definition
*field
); /* in bits */
383 enum ctf_string_encoding
bt_get_int_encoding(const struct definition
*field
);
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
*bt_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
*bt_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
*bt_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
*bt_enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
419 void bt_enum_signed_insert(struct declaration_enum
*enum_declaration
,
420 int64_t start
, int64_t end
, GQuark q
);
421 void bt_enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
422 uint64_t start
, uint64_t end
, GQuark q
);
423 size_t bt_enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
425 struct declaration_enum
*
426 bt_enum_declaration_new(struct declaration_integer
*integer_declaration
);
428 struct declaration_string
*
429 bt_string_declaration_new(enum ctf_string_encoding encoding
);
430 char *bt_get_string(const struct definition
*field
);
431 enum ctf_string_encoding
bt_get_string_encoding(const struct definition
*field
);
433 struct declaration_struct
*
434 bt_struct_declaration_new(struct declaration_scope
*parent_scope
,
436 void bt_struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
437 const char *field_name
,
438 struct declaration
*field_declaration
);
440 * Returns the index of a field within a structure.
442 int bt_struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
445 * field returned only valid as long as the field structure is not appended to.
447 struct declaration_field
*
448 bt_struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
451 bt_struct_definition_get_field_from_index(struct definition_struct
*struct_definition
,
453 int bt_struct_rw(struct stream_pos
*pos
, struct definition
*definition
);
454 uint64_t bt_struct_declaration_len(struct declaration_struct
*struct_declaration
);
457 * The tag enumeration is validated to ensure that it contains only mappings
458 * from numeric values to a single tag. Overlapping tag value ranges are
459 * therefore forbidden.
461 struct declaration_untagged_variant
*bt_untagged_bt_variant_declaration_new(
462 struct declaration_scope
*parent_scope
);
463 struct declaration_variant
*bt_variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
466 void bt_untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
467 const char *field_name
,
468 struct declaration
*field_declaration
);
469 struct declaration_field
*
470 bt_untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
473 * Returns 0 on success, -EPERM on error.
475 int variant_definition_set_tag(struct definition_variant
*variant
,
476 struct definition
*enum_tag
);
478 * Returns the field selected by the current tag value.
479 * field returned only valid as long as the variant structure is not appended
482 struct definition
*bt_variant_get_current_field(struct definition_variant
*variant
);
483 int bt_variant_rw(struct stream_pos
*pos
, struct definition
*definition
);
486 * elem_declaration passed as parameter now belongs to the array. No
487 * need to free it explicitly. "len" is the number of elements in the
490 struct declaration_array
*
491 bt_array_declaration_new(size_t len
, struct declaration
*elem_declaration
,
492 struct declaration_scope
*parent_scope
);
493 uint64_t bt_array_len(struct definition_array
*array
);
494 struct definition
*bt_array_index(struct definition_array
*array
, uint64_t i
);
495 int bt_array_rw(struct stream_pos
*pos
, struct definition
*definition
);
496 GString
*bt_get_char_array(const struct definition
*field
);
497 int bt_get_array_len(const struct definition
*field
);
500 * int_declaration and elem_declaration passed as parameter now belong
501 * to the sequence. No need to free them explicitly.
503 struct declaration_sequence
*
504 bt_sequence_declaration_new(const char *length_name
,
505 struct declaration
*elem_declaration
,
506 struct declaration_scope
*parent_scope
);
507 uint64_t bt_sequence_len(struct definition_sequence
*sequence
);
508 struct definition
*bt_sequence_index(struct definition_sequence
*sequence
, uint64_t i
);
509 int bt_sequence_rw(struct stream_pos
*pos
, struct definition
*definition
);
512 * in: path (dot separated), out: q (GArray of GQuark)
514 void bt_append_scope_path(const char *path
, GArray
*q
);
519 struct definition
*lookup_definition(const struct definition
*definition
,
520 const char *field_name
);
521 struct definition_integer
*lookup_integer(const struct definition
*definition
,
522 const char *field_name
,
524 struct definition_enum
*bt_lookup_enum(const struct definition
*definition
,
525 const char *field_name
,
527 struct definition
*bt_lookup_variant(const struct definition
*definition
,
528 const char *field_name
);
531 const char *rem_(const char *str
)
539 #endif /* _BABELTRACE_TYPES_H */