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 */
81 struct bt_declaration
{
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 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
; /* byte order */
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
;
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 bool bt_int_is_char(const struct bt_declaration
*field
);
404 enum ctf_string_encoding
bt_get_int_encoding(const struct bt_definition
*field
);
407 * mantissa_len is the length of the number of bytes represented by the mantissa
408 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
410 struct declaration_float
*bt_float_declaration_new(size_t mantissa_len
,
411 size_t exp_len
, int byte_order
,
415 * A GQuark can be translated to/from strings with g_quark_from_string() and
416 * g_quark_to_string().
420 * Returns a GArray of GQuark or NULL.
421 * Caller must release the GArray with g_array_unref().
423 GArray
*bt_enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
427 * Returns a GArray of GQuark or NULL.
428 * Caller must release the GArray with g_array_unref().
430 GArray
*bt_enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
434 * Returns a GArray of struct enum_range or NULL.
435 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
438 GArray
*bt_enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
440 void bt_enum_signed_insert(struct declaration_enum
*enum_declaration
,
441 int64_t start
, int64_t end
, GQuark q
);
442 void bt_enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
443 uint64_t start
, uint64_t end
, GQuark q
);
444 size_t bt_enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
446 struct declaration_enum
*
447 bt_enum_declaration_new(struct declaration_integer
*integer_declaration
);
449 struct declaration_string
*
450 bt_string_declaration_new(enum ctf_string_encoding encoding
);
451 char *bt_get_string(const struct bt_definition
*field
);
452 enum ctf_string_encoding
bt_get_string_encoding(const struct bt_definition
*field
);
454 double bt_get_float(const struct bt_definition
*field
);
456 const struct bt_definition
*bt_get_variant_field(struct bt_definition
*definition
);
458 struct declaration_struct
*
459 bt_struct_declaration_new(struct declaration_scope
*parent_scope
,
461 void bt_struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
462 const char *field_name
,
463 struct bt_declaration
*field_declaration
);
465 * Returns the index of a field within a structure.
467 int bt_struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
470 * field returned only valid as long as the field structure is not appended to.
472 struct declaration_field
*
473 bt_struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
475 struct bt_definition
*
476 bt_struct_definition_get_field_from_index(const struct definition_struct
*struct_definition
,
478 int bt_struct_rw(struct bt_stream_pos
*pos
, struct bt_definition
*definition
);
479 uint64_t bt_struct_declaration_len(const struct declaration_struct
*struct_declaration
);
482 * The tag enumeration is validated to ensure that it contains only mappings
483 * from numeric values to a single tag. Overlapping tag value ranges are
484 * therefore forbidden.
486 struct declaration_untagged_variant
*bt_untagged_bt_variant_declaration_new(
487 struct declaration_scope
*parent_scope
);
488 struct declaration_variant
*bt_variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
491 void bt_untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
492 const char *field_name
,
493 struct bt_declaration
*field_declaration
);
494 struct declaration_field
*
495 bt_untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
498 * Returns 0 on success, -EPERM on error.
500 int variant_definition_set_tag(struct definition_variant
*variant
,
501 struct bt_definition
*enum_tag
);
503 * Returns the field selected by the current tag value.
504 * field returned only valid as long as the variant structure is not appended
507 struct bt_definition
*bt_variant_get_current_field(struct definition_variant
*variant
);
508 int bt_variant_rw(struct bt_stream_pos
*pos
, struct bt_definition
*definition
);
511 * elem_declaration passed as parameter now belongs to the array. No
512 * need to free it explicitly. "len" is the number of elements in the
515 struct declaration_array
*
516 bt_array_declaration_new(size_t len
, struct bt_declaration
*elem_declaration
,
517 struct declaration_scope
*parent_scope
);
518 uint64_t bt_array_len(struct definition_array
*array
);
519 struct bt_definition
*bt_array_index(struct definition_array
*array
, uint64_t i
);
520 int bt_array_rw(struct bt_stream_pos
*pos
, struct bt_definition
*definition
);
521 GString
*bt_get_char_array(const struct bt_definition
*field
);
522 int bt_get_array_len(const struct bt_definition
*field
);
525 * int_declaration and elem_declaration passed as parameter now belong
526 * to the sequence. No need to free them explicitly.
528 struct declaration_sequence
*
529 bt_sequence_declaration_new(const char *length_name
,
530 struct bt_declaration
*elem_declaration
,
531 struct declaration_scope
*parent_scope
);
532 uint64_t bt_sequence_len(struct definition_sequence
*sequence
);
533 struct bt_definition
*bt_sequence_index(struct definition_sequence
*sequence
, uint64_t i
);
534 int bt_sequence_rw(struct bt_stream_pos
*pos
, struct bt_definition
*definition
);
537 * in: path (dot separated), out: q (GArray of GQuark)
539 void bt_append_scope_path(const char *path
, GArray
*q
);
544 struct bt_definition
*bt_lookup_definition(const struct bt_definition
*definition
,
545 const char *field_name
);
546 struct bt_definition
*bt_lookup_definition_by_quark(const struct bt_definition
*definition
,
548 struct definition_integer
*bt_lookup_integer(const struct bt_definition
*definition
,
549 const char *field_name
,
551 struct definition_enum
*bt_lookup_enum(const struct bt_definition
*definition
,
552 const char *field_name
,
554 struct bt_definition
*bt_lookup_variant(const struct bt_definition
*definition
,
555 const char *field_name
);
558 const char *rem_(const char *str
)
566 #endif /* _BABELTRACE_TYPES_H */