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>
37 #include <babeltrace/compat/limits.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 */
81 struct bt_declaration
{
82 enum bt_ctf_type_id id
;
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 bt_declaration
*declaration
);
89 struct bt_definition
*
90 (*definition_new
)(struct bt_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 bt_definition
*definition
);
100 struct bt_definition
{
101 struct bt_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 bt_stream_pos
*pos
,
110 struct bt_definition
*definition
);
112 /* Parent of per-plugin positions */
113 struct bt_stream_pos
{
114 /* read/write dispatch table. Specific to plugin used for stream. */
115 rw_dispatch
*rw_table
; /* rw dispatch table */
116 int (*event_cb
)(struct bt_stream_pos
*pos
,
117 struct ctf_stream_definition
*stream
);
118 int (*pre_trace_cb
)(struct bt_stream_pos
*pos
,
119 struct bt_trace_descriptor
*trace
);
120 int (*post_trace_cb
)(struct bt_stream_pos
*pos
,
121 struct bt_trace_descriptor
*trace
);
122 struct bt_trace_descriptor
*trace
;
126 int generic_rw(struct bt_stream_pos
*pos
, struct bt_definition
*definition
)
128 enum bt_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
);
137 * Because we address in bits, bitfields end up being exactly the same as
138 * integers, except that their read/write functions must be able to deal with
139 * read/write non aligned on CHAR_BIT.
141 struct declaration_integer
{
142 struct bt_declaration p
;
143 size_t len
; /* length, in bits. */
144 int byte_order
; /* LITTLE_ENDIAN/BIG_ENDIAN, 0 == "Native" */
146 int base
; /* Base for pretty-printing: 2, 8, 10, 16 */
147 enum ctf_string_encoding encoding
;
148 struct ctf_clock
*clock
;
151 #ifdef ENABLE_DEBUG_INFO
152 struct debug_info_source
;
155 struct definition_integer
{
156 struct bt_definition p
;
157 struct declaration_integer
*declaration
;
158 /* Last values read */
164 #ifdef ENABLE_DEBUG_INFO
166 * Debug infos (NULL if not set).
168 * This is extended debug informations set by the CTF input plugin
169 * itself when available. If it's set, then this integer definition
170 * is the "_ip" field of the stream event context.
172 struct debug_info_source
*debug_info_src
;
176 struct declaration_float
{
177 struct bt_declaration p
;
178 struct declaration_integer
*sign
;
179 struct declaration_integer
*mantissa
;
180 struct declaration_integer
*exp
;
181 int byte_order
; /* LITTLE_ENDIAN/BIG_ENDIAN, 0 == "Native" */
182 /* TODO: we might want to express more info about NaN, +inf and -inf */
185 struct definition_float
{
186 struct bt_definition p
;
187 struct declaration_float
*declaration
;
188 struct definition_integer
*sign
;
189 struct definition_integer
*mantissa
;
190 struct definition_integer
*exp
;
191 /* Last values read */
196 * enum_val_equal assumes that signed and unsigned memory layout overlap.
202 } start
; /* lowest range value */
206 } end
; /* highest range value */
209 struct enum_range_to_quark
{
210 struct bt_list_head node
;
211 struct enum_range range
;
216 * We optimize the common case (range of size 1: single value) by creating a
217 * hash table mapping values to quark sets. We then lookup the ranges to
218 * complete the quark set.
220 * TODO: The proper structure to hold the range to quark set mapping would be an
221 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
222 * time. Using a simple O(n) list search for now for implementation speed and
223 * given that we can expect to have a _relatively_ small number of enumeration
224 * ranges. This might become untrue if we are fed with symbol tables often
225 * required to lookup function names from instruction pointer value.
228 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
229 struct bt_list_head range_to_quark
; /* (range, GQuark) */
230 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
233 struct declaration_enum
{
234 struct bt_declaration p
;
235 struct declaration_integer
*integer_declaration
;
236 struct enum_table table
;
239 struct definition_enum
{
240 struct bt_definition p
;
241 struct definition_integer
*integer
;
242 struct declaration_enum
*declaration
;
243 /* Last GQuark values read. Keeping a reference on the GQuark array. */
247 struct declaration_string
{
248 struct bt_declaration p
;
249 enum ctf_string_encoding encoding
;
252 struct definition_string
{
253 struct bt_definition p
;
254 struct declaration_string
*declaration
;
255 char *value
; /* freed at definition_string teardown */
256 size_t len
, alloc_len
;
259 struct declaration_field
{
261 struct bt_declaration
*declaration
;
264 struct declaration_struct
{
265 struct bt_declaration p
;
266 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
267 struct declaration_scope
*scope
;
268 GArray
*fields
; /* Array of declaration_field */
271 struct definition_struct
{
272 struct bt_definition p
;
273 struct declaration_struct
*declaration
;
274 GPtrArray
*fields
; /* Array of pointers to struct bt_definition */
277 struct declaration_untagged_variant
{
278 struct bt_declaration p
;
279 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
280 struct declaration_scope
*scope
;
281 GArray
*fields
; /* Array of declaration_field */
284 struct declaration_variant
{
285 struct bt_declaration p
;
286 struct declaration_untagged_variant
*untagged_variant
;
287 GArray
*tag_name
; /* Array of GQuark */
290 /* A variant needs to be tagged to be defined. */
291 struct definition_variant
{
292 struct bt_definition p
;
293 struct declaration_variant
*declaration
;
294 struct bt_definition
*enum_tag
;
295 GPtrArray
*fields
; /* Array of pointers to struct bt_definition */
296 struct bt_definition
*current_field
; /* Last field read */
299 struct declaration_array
{
300 struct bt_declaration p
;
302 struct bt_declaration
*elem
;
303 struct declaration_scope
*scope
;
306 struct definition_array
{
307 struct bt_definition p
;
308 struct declaration_array
*declaration
;
309 GPtrArray
*elems
; /* Array of pointers to struct bt_definition */
310 GString
*string
; /* String for encoded integer children */
313 struct declaration_sequence
{
314 struct bt_declaration p
;
315 GArray
*length_name
; /* Array of GQuark */
316 struct bt_declaration
*elem
;
317 struct declaration_scope
*scope
;
320 struct definition_sequence
{
321 struct bt_definition p
;
322 struct declaration_sequence
*declaration
;
323 struct definition_integer
*length
;
324 GPtrArray
*elems
; /* Array of pointers to struct bt_definition */
325 GString
*string
; /* String for encoded integer children */
328 int bt_register_declaration(GQuark declaration_name
,
329 struct bt_declaration
*declaration
,
330 struct declaration_scope
*scope
);
331 struct bt_declaration
*bt_lookup_declaration(GQuark declaration_name
,
332 struct declaration_scope
*scope
);
335 * Type scopes also contain a separate registry for struct, variant and
336 * enum types. Those register types rather than type definitions, so
337 * that a named variant can be declared without specifying its target
338 * "choice" tag field immediately.
340 int bt_register_struct_declaration(GQuark struct_name
,
341 struct declaration_struct
*struct_declaration
,
342 struct declaration_scope
*scope
);
343 struct declaration_struct
*
344 bt_lookup_struct_declaration(GQuark struct_name
,
345 struct declaration_scope
*scope
);
346 int bt_register_variant_declaration(GQuark variant_name
,
347 struct declaration_untagged_variant
*untagged_variant_declaration
,
348 struct declaration_scope
*scope
);
349 struct declaration_untagged_variant
*bt_lookup_variant_declaration(GQuark variant_name
,
350 struct declaration_scope
*scope
);
351 int bt_register_enum_declaration(GQuark enum_name
,
352 struct declaration_enum
*enum_declaration
,
353 struct declaration_scope
*scope
);
354 struct declaration_enum
*
355 bt_lookup_enum_declaration(GQuark enum_name
,
356 struct declaration_scope
*scope
);
358 struct declaration_scope
*
359 bt_new_declaration_scope(struct declaration_scope
*parent_scope
);
360 void bt_free_declaration_scope(struct declaration_scope
*scope
);
363 * field_definition is for field definitions. They are registered into
366 struct bt_definition
*
367 bt_lookup_path_definition(GArray
*cur_path
, /* array of GQuark */
368 GArray
*lookup_path
, /* array of GQuark */
369 struct definition_scope
*scope
);
370 int bt_register_field_definition(GQuark field_name
,
371 struct bt_definition
*definition
,
372 struct definition_scope
*scope
);
373 struct definition_scope
*
374 bt_new_definition_scope(struct definition_scope
*parent_scope
,
375 GQuark field_name
, const char *root_name
);
376 void bt_free_definition_scope(struct definition_scope
*scope
);
378 GQuark
bt_new_definition_path(struct definition_scope
*parent_scope
,
379 GQuark field_name
, const char *root_name
);
382 int compare_definition_path(struct bt_definition
*definition
, GQuark path
)
384 return definition
->path
== path
;
387 void bt_declaration_ref(struct bt_declaration
*declaration
);
388 void bt_declaration_unref(struct bt_declaration
*declaration
);
390 void bt_definition_ref(struct bt_definition
*definition
);
391 void bt_definition_unref(struct bt_definition
*definition
);
393 struct declaration_integer
*bt_integer_declaration_new(size_t len
, int byte_order
,
394 int signedness
, size_t alignment
,
395 int base
, enum ctf_string_encoding encoding
,
396 struct ctf_clock
*clock
);
397 uint64_t bt_get_unsigned_int(const struct bt_definition
*field
);
398 int64_t bt_get_signed_int(const struct bt_definition
*field
);
399 int bt_get_int_signedness(const struct bt_definition
*field
);
400 int bt_get_int_byte_order(const struct bt_definition
*field
);
401 int bt_get_int_base(const struct bt_definition
*field
);
402 size_t bt_get_int_len(const struct bt_definition
*field
); /* in bits */
403 enum ctf_string_encoding
bt_get_int_encoding(const struct bt_definition
*field
);
406 * mantissa_len is the length of the number of bytes represented by the mantissa
407 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
409 struct declaration_float
*bt_float_declaration_new(size_t mantissa_len
,
410 size_t exp_len
, int byte_order
,
414 * A GQuark can be translated to/from strings with g_quark_from_string() and
415 * g_quark_to_string().
419 * Returns a GArray of GQuark or NULL.
420 * Caller must release the GArray with g_array_unref().
422 GArray
*bt_enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
426 * Returns a GArray of GQuark or NULL.
427 * Caller must release the GArray with g_array_unref().
429 GArray
*bt_enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
433 * Returns a GArray of struct enum_range or NULL.
434 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
437 GArray
*bt_enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
439 void bt_enum_signed_insert(struct declaration_enum
*enum_declaration
,
440 int64_t start
, int64_t end
, GQuark q
);
441 void bt_enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
442 uint64_t start
, uint64_t end
, GQuark q
);
443 size_t bt_enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
445 struct declaration_enum
*
446 bt_enum_declaration_new(struct declaration_integer
*integer_declaration
);
448 struct declaration_string
*
449 bt_string_declaration_new(enum ctf_string_encoding encoding
);
450 char *bt_get_string(const struct bt_definition
*field
);
451 enum ctf_string_encoding
bt_get_string_encoding(const struct bt_definition
*field
);
453 double bt_get_float(const struct bt_definition
*field
);
455 const struct bt_definition
*bt_get_variant_field(struct bt_definition
*definition
);
457 struct declaration_struct
*
458 bt_struct_declaration_new(struct declaration_scope
*parent_scope
,
460 void bt_struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
461 const char *field_name
,
462 struct bt_declaration
*field_declaration
);
464 * Returns the index of a field within a structure.
466 int bt_struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
469 * field returned only valid as long as the field structure is not appended to.
471 struct declaration_field
*
472 bt_struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
474 struct bt_definition
*
475 bt_struct_definition_get_field_from_index(const struct definition_struct
*struct_definition
,
477 int bt_struct_rw(struct bt_stream_pos
*pos
, struct bt_definition
*definition
);
478 uint64_t bt_struct_declaration_len(const struct declaration_struct
*struct_declaration
);
481 * The tag enumeration is validated to ensure that it contains only mappings
482 * from numeric values to a single tag. Overlapping tag value ranges are
483 * therefore forbidden.
485 struct declaration_untagged_variant
*bt_untagged_bt_variant_declaration_new(
486 struct declaration_scope
*parent_scope
);
487 struct declaration_variant
*bt_variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
490 void bt_untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
491 const char *field_name
,
492 struct bt_declaration
*field_declaration
);
493 struct declaration_field
*
494 bt_untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
497 * Returns 0 on success, -EPERM on error.
499 int variant_definition_set_tag(struct definition_variant
*variant
,
500 struct bt_definition
*enum_tag
);
502 * Returns the field selected by the current tag value.
503 * field returned only valid as long as the variant structure is not appended
506 struct bt_definition
*bt_variant_get_current_field(struct definition_variant
*variant
);
507 int bt_variant_rw(struct bt_stream_pos
*pos
, struct bt_definition
*definition
);
510 * elem_declaration passed as parameter now belongs to the array. No
511 * need to free it explicitly. "len" is the number of elements in the
514 struct declaration_array
*
515 bt_array_declaration_new(size_t len
, struct bt_declaration
*elem_declaration
,
516 struct declaration_scope
*parent_scope
);
517 uint64_t bt_array_len(struct definition_array
*array
);
518 struct bt_definition
*bt_array_index(struct definition_array
*array
, uint64_t i
);
519 int bt_array_rw(struct bt_stream_pos
*pos
, struct bt_definition
*definition
);
520 GString
*bt_get_char_array(const struct bt_definition
*field
);
521 int bt_get_array_len(const struct bt_definition
*field
);
524 * int_declaration and elem_declaration passed as parameter now belong
525 * to the sequence. No need to free them explicitly.
527 struct declaration_sequence
*
528 bt_sequence_declaration_new(const char *length_name
,
529 struct bt_declaration
*elem_declaration
,
530 struct declaration_scope
*parent_scope
);
531 uint64_t bt_sequence_len(struct definition_sequence
*sequence
);
532 struct bt_definition
*bt_sequence_index(struct definition_sequence
*sequence
, uint64_t i
);
533 int bt_sequence_rw(struct bt_stream_pos
*pos
, struct bt_definition
*definition
);
536 * in: path (dot separated), out: q (GArray of GQuark)
538 void bt_append_scope_path(const char *path
, GArray
*q
);
543 struct bt_definition
*bt_lookup_definition(const struct bt_definition
*definition
,
544 const char *field_name
);
545 struct bt_definition
*bt_lookup_definition_by_quark(const struct bt_definition
*definition
,
547 struct definition_integer
*bt_lookup_integer(const struct bt_definition
*definition
,
548 const char *field_name
,
550 struct definition_enum
*bt_lookup_enum(const struct bt_definition
*definition
,
551 const char *field_name
,
553 struct bt_definition
*bt_lookup_variant(const struct bt_definition
*definition
,
554 const char *field_name
);
557 const char *rem_(const char *str
)
565 #endif /* _BABELTRACE_TYPES_H */