Implement ctf-metadata output plugin
[babeltrace.git] / include / babeltrace / types.h
... / ...
CommitLineData
1#ifndef _BABELTRACE_TYPES_H
2#define _BABELTRACE_TYPES_H
3
4/*
5 * BabelTrace
6 *
7 * Type Header
8 *
9 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
10 *
11 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 *
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:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
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
29 * SOFTWARE.
30 */
31
32#include <babeltrace/align.h>
33#include <babeltrace/list.h>
34#include <babeltrace/ctf/events.h>
35#include <stdbool.h>
36#include <stdint.h>
37#include <limits.h>
38#include <string.h>
39#include <glib.h>
40#include <assert.h>
41
42/* Preallocate this many fields for structures */
43#define DEFAULT_NR_STRUCT_FIELDS 8
44
45struct ctf_stream_definition;
46struct bt_stream_pos;
47struct bt_format;
48struct bt_definition;
49struct ctf_clock;
50
51/* type scope */
52struct 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;
63};
64
65/* definition scope */
66struct definition_scope {
67 /* Hash table mapping field name GQuark to "struct definition" */
68 GHashTable *definitions;
69 struct definition_scope *parent_scope;
70 /*
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.
77 */
78 GArray *scope_path; /* array of GQuark */
79};
80
81struct bt_declaration {
82 enum ctf_type_id id;
83 size_t alignment; /* type alignment, in bits */
84 int ref; /* number of references to the type */
85 /*
86 * declaration_free called with declaration ref is decremented to 0.
87 */
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);
94 /*
95 * definition_free called with definition ref is decremented to 0.
96 */
97 void (*definition_free)(struct bt_definition *definition);
98};
99
100struct 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 */
105 GQuark path;
106 struct definition_scope *scope;
107};
108
109typedef int (*rw_dispatch)(struct bt_stream_pos *pos,
110 struct bt_definition *definition);
111
112/* Parent of per-plugin positions */
113struct 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};
123
124static inline
125int generic_rw(struct bt_stream_pos *pos, struct bt_definition *definition)
126{
127 enum ctf_type_id dispatch_id = definition->declaration->id;
128 rw_dispatch call;
129
130 assert(pos->rw_table[dispatch_id] != NULL);
131 call = pos->rw_table[dispatch_id];
132 return call(pos, definition);
133}
134
135/*
136 * Because we address in bits, bitfields end up being exactly the same as
137 * integers, except that their read/write functions must be able to deal with
138 * read/write non aligned on CHAR_BIT.
139 */
140struct declaration_integer {
141 struct bt_declaration p;
142 size_t len; /* length, in bits. */
143 int byte_order; /* byte order */
144 int signedness;
145 int base; /* Base for pretty-printing: 2, 8, 10, 16 */
146 enum ctf_string_encoding encoding;
147 struct ctf_clock *clock;
148};
149
150struct definition_integer {
151 struct bt_definition p;
152 struct declaration_integer *declaration;
153 /* Last values read */
154 union {
155 uint64_t _unsigned;
156 int64_t _signed;
157 } value;
158};
159
160struct declaration_float {
161 struct bt_declaration p;
162 struct declaration_integer *sign;
163 struct declaration_integer *mantissa;
164 struct declaration_integer *exp;
165 int byte_order;
166 /* TODO: we might want to express more info about NaN, +inf and -inf */
167};
168
169struct definition_float {
170 struct bt_definition p;
171 struct declaration_float *declaration;
172 struct definition_integer *sign;
173 struct definition_integer *mantissa;
174 struct definition_integer *exp;
175 /* Last values read */
176 double value;
177};
178
179/*
180 * enum_val_equal assumes that signed and unsigned memory layout overlap.
181 */
182struct enum_range {
183 union {
184 int64_t _signed;
185 uint64_t _unsigned;
186 } start; /* lowest range value */
187 union {
188 int64_t _signed;
189 uint64_t _unsigned;
190 } end; /* highest range value */
191};
192
193struct enum_range_to_quark {
194 struct bt_list_head node;
195 struct enum_range range;
196 GQuark quark;
197};
198
199/*
200 * We optimize the common case (range of size 1: single value) by creating a
201 * hash table mapping values to quark sets. We then lookup the ranges to
202 * complete the quark set.
203 *
204 * TODO: The proper structure to hold the range to quark set mapping would be an
205 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
206 * time. Using a simple O(n) list search for now for implementation speed and
207 * given that we can expect to have a _relatively_ small number of enumeration
208 * ranges. This might become untrue if we are fed with symbol tables often
209 * required to lookup function names from instruction pointer value.
210 */
211struct enum_table {
212 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
213 struct bt_list_head range_to_quark; /* (range, GQuark) */
214 GHashTable *quark_to_range_set; /* (GQuark, range GArray) */
215};
216
217struct declaration_enum {
218 struct bt_declaration p;
219 struct declaration_integer *integer_declaration;
220 struct enum_table table;
221};
222
223struct definition_enum {
224 struct bt_definition p;
225 struct definition_integer *integer;
226 struct declaration_enum *declaration;
227 /* Last GQuark values read. Keeping a reference on the GQuark array. */
228 GArray *value;
229};
230
231struct declaration_string {
232 struct bt_declaration p;
233 enum ctf_string_encoding encoding;
234};
235
236struct definition_string {
237 struct bt_definition p;
238 struct declaration_string *declaration;
239 char *value; /* freed at definition_string teardown */
240 size_t len, alloc_len;
241};
242
243struct declaration_field {
244 GQuark name;
245 struct bt_declaration *declaration;
246};
247
248struct declaration_struct {
249 struct bt_declaration p;
250 GHashTable *fields_by_name; /* Tuples (field name, field index) */
251 struct declaration_scope *scope;
252 GArray *fields; /* Array of declaration_field */
253};
254
255struct definition_struct {
256 struct bt_definition p;
257 struct declaration_struct *declaration;
258 GPtrArray *fields; /* Array of pointers to struct bt_definition */
259};
260
261struct declaration_untagged_variant {
262 struct bt_declaration p;
263 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
264 struct declaration_scope *scope;
265 GArray *fields; /* Array of declaration_field */
266};
267
268struct declaration_variant {
269 struct bt_declaration p;
270 struct declaration_untagged_variant *untagged_variant;
271 GArray *tag_name; /* Array of GQuark */
272};
273
274/* A variant needs to be tagged to be defined. */
275struct definition_variant {
276 struct bt_definition p;
277 struct declaration_variant *declaration;
278 struct bt_definition *enum_tag;
279 GPtrArray *fields; /* Array of pointers to struct bt_definition */
280 struct bt_definition *current_field; /* Last field read */
281};
282
283struct declaration_array {
284 struct bt_declaration p;
285 size_t len;
286 struct bt_declaration *elem;
287 struct declaration_scope *scope;
288};
289
290struct definition_array {
291 struct bt_definition p;
292 struct declaration_array *declaration;
293 GPtrArray *elems; /* Array of pointers to struct bt_definition */
294 GString *string; /* String for encoded integer children */
295};
296
297struct declaration_sequence {
298 struct bt_declaration p;
299 GArray *length_name; /* Array of GQuark */
300 struct bt_declaration *elem;
301 struct declaration_scope *scope;
302};
303
304struct definition_sequence {
305 struct bt_definition p;
306 struct declaration_sequence *declaration;
307 struct definition_integer *length;
308 GPtrArray *elems; /* Array of pointers to struct bt_definition */
309 GString *string; /* String for encoded integer children */
310};
311
312int bt_register_declaration(GQuark declaration_name,
313 struct bt_declaration *declaration,
314 struct declaration_scope *scope);
315struct bt_declaration *bt_lookup_declaration(GQuark declaration_name,
316 struct declaration_scope *scope);
317
318/*
319 * Type scopes also contain a separate registry for struct, variant and
320 * enum types. Those register types rather than type definitions, so
321 * that a named variant can be declared without specifying its target
322 * "choice" tag field immediately.
323 */
324int bt_register_struct_declaration(GQuark struct_name,
325 struct declaration_struct *struct_declaration,
326 struct declaration_scope *scope);
327struct declaration_struct *
328 bt_lookup_struct_declaration(GQuark struct_name,
329 struct declaration_scope *scope);
330int bt_register_variant_declaration(GQuark variant_name,
331 struct declaration_untagged_variant *untagged_variant_declaration,
332 struct declaration_scope *scope);
333struct declaration_untagged_variant *bt_lookup_variant_declaration(GQuark variant_name,
334 struct declaration_scope *scope);
335int bt_register_enum_declaration(GQuark enum_name,
336 struct declaration_enum *enum_declaration,
337 struct declaration_scope *scope);
338struct declaration_enum *
339 bt_lookup_enum_declaration(GQuark enum_name,
340 struct declaration_scope *scope);
341
342struct declaration_scope *
343 bt_new_declaration_scope(struct declaration_scope *parent_scope);
344void bt_free_declaration_scope(struct declaration_scope *scope);
345
346/*
347 * field_definition is for field definitions. They are registered into
348 * definition scopes.
349 */
350struct bt_definition *
351 bt_lookup_path_definition(GArray *cur_path, /* array of GQuark */
352 GArray *lookup_path, /* array of GQuark */
353 struct definition_scope *scope);
354int bt_register_field_definition(GQuark field_name,
355 struct bt_definition *definition,
356 struct definition_scope *scope);
357struct definition_scope *
358 bt_new_definition_scope(struct definition_scope *parent_scope,
359 GQuark field_name, const char *root_name);
360void bt_free_definition_scope(struct definition_scope *scope);
361
362GQuark bt_new_definition_path(struct definition_scope *parent_scope,
363 GQuark field_name, const char *root_name);
364
365static inline
366int compare_definition_path(struct bt_definition *definition, GQuark path)
367{
368 return definition->path == path;
369}
370
371void bt_declaration_ref(struct bt_declaration *declaration);
372void bt_declaration_unref(struct bt_declaration *declaration);
373
374void bt_definition_ref(struct bt_definition *definition);
375void bt_definition_unref(struct bt_definition *definition);
376
377struct declaration_integer *bt_integer_declaration_new(size_t len, int byte_order,
378 int signedness, size_t alignment,
379 int base, enum ctf_string_encoding encoding,
380 struct ctf_clock *clock);
381uint64_t bt_get_unsigned_int(const struct bt_definition *field);
382int64_t bt_get_signed_int(const struct bt_definition *field);
383int bt_get_int_signedness(const struct bt_definition *field);
384int bt_get_int_byte_order(const struct bt_definition *field);
385int bt_get_int_base(const struct bt_definition *field);
386size_t bt_get_int_len(const struct bt_definition *field); /* in bits */
387enum ctf_string_encoding bt_get_int_encoding(const struct bt_definition *field);
388
389/*
390 * mantissa_len is the length of the number of bytes represented by the mantissa
391 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
392 */
393struct declaration_float *bt_float_declaration_new(size_t mantissa_len,
394 size_t exp_len, int byte_order,
395 size_t alignment);
396
397/*
398 * A GQuark can be translated to/from strings with g_quark_from_string() and
399 * g_quark_to_string().
400 */
401
402/*
403 * Returns a GArray of GQuark or NULL.
404 * Caller must release the GArray with g_array_unref().
405 */
406GArray *bt_enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
407 uint64_t v);
408
409/*
410 * Returns a GArray of GQuark or NULL.
411 * Caller must release the GArray with g_array_unref().
412 */
413GArray *bt_enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
414 int64_t v);
415
416/*
417 * Returns a GArray of struct enum_range or NULL.
418 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
419 * release it).
420 */
421GArray *bt_enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
422 GQuark q);
423void bt_enum_signed_insert(struct declaration_enum *enum_declaration,
424 int64_t start, int64_t end, GQuark q);
425void bt_enum_unsigned_insert(struct declaration_enum *enum_declaration,
426 uint64_t start, uint64_t end, GQuark q);
427size_t bt_enum_get_nr_enumerators(struct declaration_enum *enum_declaration);
428
429struct declaration_enum *
430 bt_enum_declaration_new(struct declaration_integer *integer_declaration);
431
432struct declaration_string *
433 bt_string_declaration_new(enum ctf_string_encoding encoding);
434char *bt_get_string(const struct bt_definition *field);
435enum ctf_string_encoding bt_get_string_encoding(const struct bt_definition *field);
436
437struct declaration_struct *
438 bt_struct_declaration_new(struct declaration_scope *parent_scope,
439 uint64_t min_align);
440void bt_struct_declaration_add_field(struct declaration_struct *struct_declaration,
441 const char *field_name,
442 struct bt_declaration *field_declaration);
443/*
444 * Returns the index of a field within a structure.
445 */
446int bt_struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
447 GQuark field_name);
448/*
449 * field returned only valid as long as the field structure is not appended to.
450 */
451struct declaration_field *
452bt_struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
453 int index);
454struct bt_definition *
455bt_struct_definition_get_field_from_index(struct definition_struct *struct_definition,
456 int index);
457int bt_struct_rw(struct bt_stream_pos *pos, struct bt_definition *definition);
458uint64_t bt_struct_declaration_len(struct declaration_struct *struct_declaration);
459
460/*
461 * The tag enumeration is validated to ensure that it contains only mappings
462 * from numeric values to a single tag. Overlapping tag value ranges are
463 * therefore forbidden.
464 */
465struct declaration_untagged_variant *bt_untagged_bt_variant_declaration_new(
466 struct declaration_scope *parent_scope);
467struct declaration_variant *bt_variant_declaration_new(struct declaration_untagged_variant *untagged_variant,
468 const char *tag);
469
470void bt_untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
471 const char *field_name,
472 struct bt_declaration *field_declaration);
473struct declaration_field *
474 bt_untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration,
475 GQuark tag);
476/*
477 * Returns 0 on success, -EPERM on error.
478 */
479int variant_definition_set_tag(struct definition_variant *variant,
480 struct bt_definition *enum_tag);
481/*
482 * Returns the field selected by the current tag value.
483 * field returned only valid as long as the variant structure is not appended
484 * to.
485 */
486struct bt_definition *bt_variant_get_current_field(struct definition_variant *variant);
487int bt_variant_rw(struct bt_stream_pos *pos, struct bt_definition *definition);
488
489/*
490 * elem_declaration passed as parameter now belongs to the array. No
491 * need to free it explicitly. "len" is the number of elements in the
492 * array.
493 */
494struct declaration_array *
495 bt_array_declaration_new(size_t len, struct bt_declaration *elem_declaration,
496 struct declaration_scope *parent_scope);
497uint64_t bt_array_len(struct definition_array *array);
498struct bt_definition *bt_array_index(struct definition_array *array, uint64_t i);
499int bt_array_rw(struct bt_stream_pos *pos, struct bt_definition *definition);
500GString *bt_get_char_array(const struct bt_definition *field);
501int bt_get_array_len(const struct bt_definition *field);
502
503/*
504 * int_declaration and elem_declaration passed as parameter now belong
505 * to the sequence. No need to free them explicitly.
506 */
507struct declaration_sequence *
508 bt_sequence_declaration_new(const char *length_name,
509 struct bt_declaration *elem_declaration,
510 struct declaration_scope *parent_scope);
511uint64_t bt_sequence_len(struct definition_sequence *sequence);
512struct bt_definition *bt_sequence_index(struct definition_sequence *sequence, uint64_t i);
513int bt_sequence_rw(struct bt_stream_pos *pos, struct bt_definition *definition);
514
515/*
516 * in: path (dot separated), out: q (GArray of GQuark)
517 */
518void bt_append_scope_path(const char *path, GArray *q);
519
520/*
521 * Lookup helpers.
522 */
523struct bt_definition *bt_lookup_definition(const struct bt_definition *definition,
524 const char *field_name);
525struct definition_integer *bt_lookup_integer(const struct bt_definition *definition,
526 const char *field_name,
527 int signedness);
528struct definition_enum *bt_lookup_enum(const struct bt_definition *definition,
529 const char *field_name,
530 int signedness);
531struct bt_definition *bt_lookup_variant(const struct bt_definition *definition,
532 const char *field_name);
533
534static inline
535const char *rem_(const char *str)
536{
537 if (str[0] == '_')
538 return &str[1];
539 else
540 return str;
541}
542
543#endif /* _BABELTRACE_TYPES_H */
This page took 0.023983 seconds and 4 git commands to generate.