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
37 struct ctf_stream_definition
;
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_definition
*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
);
124 * Because we address in bits, bitfields end up being exactly the same as
125 * integers, except that their read/write functions must be able to deal with
126 * read/write non aligned on CHAR_BIT.
128 struct declaration_integer
{
129 struct declaration p
;
130 size_t len
; /* length, in bits. */
131 int byte_order
; /* byte order */
133 int base
; /* Base for pretty-printing: 2, 8, 10, 16 */
134 enum ctf_string_encoding encoding
;
135 struct ctf_clock
*clock
;
138 struct definition_integer
{
140 struct declaration_integer
*declaration
;
141 /* Last values read */
148 struct declaration_float
{
149 struct declaration p
;
150 struct declaration_integer
*sign
;
151 struct declaration_integer
*mantissa
;
152 struct declaration_integer
*exp
;
154 /* TODO: we might want to express more info about NaN, +inf and -inf */
157 struct definition_float
{
159 struct declaration_float
*declaration
;
160 struct definition_integer
*sign
;
161 struct definition_integer
*mantissa
;
162 struct definition_integer
*exp
;
163 /* Last values read */
168 * enum_val_equal assumes that signed and unsigned memory layout overlap.
174 } start
; /* lowest range value */
178 } end
; /* highest range value */
181 struct enum_range_to_quark
{
182 struct bt_list_head node
;
183 struct enum_range range
;
188 * We optimize the common case (range of size 1: single value) by creating a
189 * hash table mapping values to quark sets. We then lookup the ranges to
190 * complete the quark set.
192 * TODO: The proper structure to hold the range to quark set mapping would be an
193 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
194 * time. Using a simple O(n) list search for now for implementation speed and
195 * given that we can expect to have a _relatively_ small number of enumeration
196 * ranges. This might become untrue if we are fed with symbol tables often
197 * required to lookup function names from instruction pointer value.
200 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
201 struct bt_list_head range_to_quark
; /* (range, GQuark) */
202 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
205 struct declaration_enum
{
206 struct declaration p
;
207 struct declaration_integer
*integer_declaration
;
208 struct enum_table table
;
211 struct definition_enum
{
213 struct definition_integer
*integer
;
214 struct declaration_enum
*declaration
;
215 /* Last GQuark values read. Keeping a reference on the GQuark array. */
219 struct declaration_string
{
220 struct declaration p
;
221 enum ctf_string_encoding encoding
;
224 struct definition_string
{
226 struct declaration_string
*declaration
;
227 char *value
; /* freed at definition_string teardown */
228 size_t len
, alloc_len
;
231 struct declaration_field
{
233 struct declaration
*declaration
;
236 struct declaration_struct
{
237 struct declaration p
;
238 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
239 struct declaration_scope
*scope
;
240 GArray
*fields
; /* Array of declaration_field */
243 struct definition_struct
{
245 struct declaration_struct
*declaration
;
246 GPtrArray
*fields
; /* Array of pointers to struct definition */
249 struct declaration_untagged_variant
{
250 struct declaration p
;
251 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
252 struct declaration_scope
*scope
;
253 GArray
*fields
; /* Array of declaration_field */
256 struct declaration_variant
{
257 struct declaration p
;
258 struct declaration_untagged_variant
*untagged_variant
;
259 GArray
*tag_name
; /* Array of GQuark */
262 /* A variant needs to be tagged to be defined. */
263 struct definition_variant
{
265 struct declaration_variant
*declaration
;
266 struct definition
*enum_tag
;
267 GPtrArray
*fields
; /* Array of pointers to struct definition */
268 struct definition
*current_field
; /* Last field read */
271 struct declaration_array
{
272 struct declaration p
;
274 struct declaration
*elem
;
275 struct declaration_scope
*scope
;
278 struct definition_array
{
280 struct declaration_array
*declaration
;
281 GPtrArray
*elems
; /* Array of pointers to struct definition */
282 GString
*string
; /* String for encoded integer children */
285 struct declaration_sequence
{
286 struct declaration p
;
287 GArray
*length_name
; /* Array of GQuark */
288 struct declaration
*elem
;
289 struct declaration_scope
*scope
;
292 struct definition_sequence
{
294 struct declaration_sequence
*declaration
;
295 struct definition_integer
*length
;
296 GPtrArray
*elems
; /* Array of pointers to struct definition */
297 GString
*string
; /* String for encoded integer children */
300 int register_declaration(GQuark declaration_name
,
301 struct declaration
*declaration
,
302 struct declaration_scope
*scope
);
303 struct declaration
*lookup_declaration(GQuark declaration_name
,
304 struct declaration_scope
*scope
);
307 * Type scopes also contain a separate registry for struct, variant and
308 * enum types. Those register types rather than type definitions, so
309 * that a named variant can be declared without specifying its target
310 * "choice" tag field immediately.
312 int register_struct_declaration(GQuark struct_name
,
313 struct declaration_struct
*struct_declaration
,
314 struct declaration_scope
*scope
);
315 struct declaration_struct
*
316 lookup_struct_declaration(GQuark struct_name
,
317 struct declaration_scope
*scope
);
318 int register_variant_declaration(GQuark variant_name
,
319 struct declaration_untagged_variant
*untagged_variant_declaration
,
320 struct declaration_scope
*scope
);
321 struct declaration_untagged_variant
*lookup_variant_declaration(GQuark variant_name
,
322 struct declaration_scope
*scope
);
323 int register_enum_declaration(GQuark enum_name
,
324 struct declaration_enum
*enum_declaration
,
325 struct declaration_scope
*scope
);
326 struct declaration_enum
*
327 lookup_enum_declaration(GQuark enum_name
,
328 struct declaration_scope
*scope
);
330 struct declaration_scope
*
331 new_declaration_scope(struct declaration_scope
*parent_scope
);
332 void free_declaration_scope(struct declaration_scope
*scope
);
335 * field_definition is for field definitions. They are registered into
339 lookup_path_definition(GArray
*cur_path
, /* array of GQuark */
340 GArray
*lookup_path
, /* array of GQuark */
341 struct definition_scope
*scope
);
342 int register_field_definition(GQuark field_name
,
343 struct definition
*definition
,
344 struct definition_scope
*scope
);
345 struct definition_scope
*
346 new_definition_scope(struct definition_scope
*parent_scope
,
347 GQuark field_name
, const char *root_name
);
348 void free_definition_scope(struct definition_scope
*scope
);
350 GQuark
new_definition_path(struct definition_scope
*parent_scope
,
351 GQuark field_name
, const char *root_name
);
354 int compare_definition_path(struct definition
*definition
, GQuark path
)
356 return definition
->path
== path
;
359 void declaration_ref(struct declaration
*declaration
);
360 void declaration_unref(struct declaration
*declaration
);
362 void definition_ref(struct definition
*definition
);
363 void definition_unref(struct definition
*definition
);
365 struct declaration_integer
*integer_declaration_new(size_t len
, int byte_order
,
366 int signedness
, size_t alignment
,
367 int base
, enum ctf_string_encoding encoding
,
368 struct ctf_clock
*clock
);
369 uint64_t get_unsigned_int(const struct definition
*field
);
370 int64_t get_signed_int(const struct definition
*field
);
371 int get_int_signedness(const struct definition
*field
);
372 int get_int_byte_order(const struct definition
*field
);
373 int get_int_base(const struct definition
*field
);
374 enum ctf_string_encoding
get_int_encoding(const struct definition
*field
);
377 * mantissa_len is the length of the number of bytes represented by the mantissa
378 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
380 struct declaration_float
*float_declaration_new(size_t mantissa_len
,
381 size_t exp_len
, int byte_order
,
385 * A GQuark can be translated to/from strings with g_quark_from_string() and
386 * g_quark_to_string().
390 * Returns a GArray of GQuark or NULL.
391 * Caller must release the GArray with g_array_unref().
393 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
397 * Returns a GArray of GQuark or NULL.
398 * Caller must release the GArray with g_array_unref().
400 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
404 * Returns a GArray of struct enum_range or NULL.
405 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
408 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
410 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
411 int64_t start
, int64_t end
, GQuark q
);
412 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
413 uint64_t start
, uint64_t end
, GQuark q
);
414 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
416 struct declaration_enum
*
417 enum_declaration_new(struct declaration_integer
*integer_declaration
);
419 struct declaration_string
*
420 string_declaration_new(enum ctf_string_encoding encoding
);
421 char *get_string(const struct definition
*field
);
422 enum ctf_string_encoding
get_string_encoding(const struct definition
*field
);
424 struct declaration_struct
*
425 struct_declaration_new(struct declaration_scope
*parent_scope
,
427 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
428 const char *field_name
,
429 struct declaration
*field_declaration
);
431 * Returns the index of a field within a structure.
433 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
436 * field returned only valid as long as the field structure is not appended to.
438 struct declaration_field
*
439 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
442 struct_definition_get_field_from_index(struct definition_struct
*struct_definition
,
444 int struct_rw(struct stream_pos
*pos
, struct definition
*definition
);
445 uint64_t struct_declaration_len(struct declaration_struct
*struct_declaration
);
448 * The tag enumeration is validated to ensure that it contains only mappings
449 * from numeric values to a single tag. Overlapping tag value ranges are
450 * therefore forbidden.
452 struct declaration_untagged_variant
*untagged_variant_declaration_new(
453 struct declaration_scope
*parent_scope
);
454 struct declaration_variant
*variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
457 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
458 const char *field_name
,
459 struct declaration
*field_declaration
);
460 struct declaration_field
*
461 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
464 * Returns 0 on success, -EPERM on error.
466 int variant_definition_set_tag(struct definition_variant
*variant
,
467 struct definition
*enum_tag
);
469 * Returns the field selected by the current tag value.
470 * field returned only valid as long as the variant structure is not appended
473 struct definition
*variant_get_current_field(struct definition_variant
*variant
);
474 int variant_rw(struct stream_pos
*pos
, struct definition
*definition
);
477 * elem_declaration passed as parameter now belongs to the array. No
478 * need to free it explicitly. "len" is the number of elements in the
481 struct declaration_array
*
482 array_declaration_new(size_t len
, struct declaration
*elem_declaration
,
483 struct declaration_scope
*parent_scope
);
484 uint64_t array_len(struct definition_array
*array
);
485 struct definition
*array_index(struct definition_array
*array
, uint64_t i
);
486 int array_rw(struct stream_pos
*pos
, struct definition
*definition
);
487 GString
*get_char_array(const struct definition
*field
);
488 int get_array_len(const struct definition
*field
);
491 * int_declaration and elem_declaration passed as parameter now belong
492 * to the sequence. No need to free them explicitly.
494 struct declaration_sequence
*
495 sequence_declaration_new(const char *length_name
,
496 struct declaration
*elem_declaration
,
497 struct declaration_scope
*parent_scope
);
498 uint64_t sequence_len(struct definition_sequence
*sequence
);
499 struct definition
*sequence_index(struct definition_sequence
*sequence
, uint64_t i
);
500 int sequence_rw(struct stream_pos
*pos
, struct definition
*definition
);
503 * in: path (dot separated), out: q (GArray of GQuark)
505 void append_scope_path(const char *path
, GArray
*q
);
510 struct definition
*lookup_definition(const struct definition
*definition
,
511 const char *field_name
);
512 struct definition_integer
*lookup_integer(const struct definition
*definition
,
513 const char *field_name
,
515 struct definition_enum
*lookup_enum(const struct definition
*definition
,
516 const char *field_name
,
518 struct definition
*lookup_variant(const struct definition
*definition
,
519 const char *field_name
);
522 const char *rem_(const char *str
)
530 #endif /* _BABELTRACE_TYPES_H */