Add support for trace:hostname field
[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
24#include <babeltrace/align.h>
25#include <babeltrace/list.h>
26#include <babeltrace/ctf/events.h>
27#include <stdbool.h>
28#include <stdint.h>
29#include <limits.h>
30#include <string.h>
31#include <glib.h>
32#include <assert.h>
33
34/* Preallocate this many fields for structures */
35#define DEFAULT_NR_STRUCT_FIELDS 8
36
37struct ctf_stream_definition;
38struct stream_pos;
39struct format;
40struct definition;
41struct ctf_clock;
42
43/* type scope */
44struct 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;
55};
56
57/* definition scope */
58struct definition_scope {
59 /* Hash table mapping field name GQuark to "struct definition" */
60 GHashTable *definitions;
61 struct definition_scope *parent_scope;
62 /*
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.
69 */
70 GArray *scope_path; /* array of GQuark */
71};
72
73struct declaration {
74 enum ctf_type_id id;
75 size_t alignment; /* type alignment, in bits */
76 int ref; /* number of references to the type */
77 /*
78 * declaration_free called with declaration ref is decremented to 0.
79 */
80 void (*declaration_free)(struct declaration *declaration);
81 struct definition *
82 (*definition_new)(struct declaration *declaration,
83 struct definition_scope *parent_scope,
84 GQuark field_name, int index,
85 const char *root_name);
86 /*
87 * definition_free called with definition ref is decremented to 0.
88 */
89 void (*definition_free)(struct definition *definition);
90};
91
92struct 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 */
97 GQuark path;
98 struct definition_scope *scope;
99};
100
101typedef int (*rw_dispatch)(struct stream_pos *pos,
102 struct definition *definition);
103
104/* Parent of per-plugin positions */
105struct stream_pos {
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);
110};
111
112static inline
113int generic_rw(struct stream_pos *pos, struct definition *definition)
114{
115 enum ctf_type_id dispatch_id = definition->declaration->id;
116 rw_dispatch call;
117
118 assert(pos->rw_table[dispatch_id] != NULL);
119 call = pos->rw_table[dispatch_id];
120 return call(pos, definition);
121}
122
123/*
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.
127 */
128struct declaration_integer {
129 struct declaration p;
130 size_t len; /* length, in bits. */
131 int byte_order; /* byte order */
132 int signedness;
133 int base; /* Base for pretty-printing: 2, 8, 10, 16 */
134 enum ctf_string_encoding encoding;
135 struct ctf_clock *clock;
136};
137
138struct definition_integer {
139 struct definition p;
140 struct declaration_integer *declaration;
141 /* Last values read */
142 union {
143 uint64_t _unsigned;
144 int64_t _signed;
145 } value;
146};
147
148struct declaration_float {
149 struct declaration p;
150 struct declaration_integer *sign;
151 struct declaration_integer *mantissa;
152 struct declaration_integer *exp;
153 int byte_order;
154 /* TODO: we might want to express more info about NaN, +inf and -inf */
155};
156
157struct definition_float {
158 struct definition p;
159 struct declaration_float *declaration;
160 struct definition_integer *sign;
161 struct definition_integer *mantissa;
162 struct definition_integer *exp;
163 /* Last values read */
164 double value;
165};
166
167/*
168 * enum_val_equal assumes that signed and unsigned memory layout overlap.
169 */
170struct enum_range {
171 union {
172 int64_t _signed;
173 uint64_t _unsigned;
174 } start; /* lowest range value */
175 union {
176 int64_t _signed;
177 uint64_t _unsigned;
178 } end; /* highest range value */
179};
180
181struct enum_range_to_quark {
182 struct bt_list_head node;
183 struct enum_range range;
184 GQuark quark;
185};
186
187/*
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.
191 *
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.
198 */
199struct enum_table {
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) */
203};
204
205struct declaration_enum {
206 struct declaration p;
207 struct declaration_integer *integer_declaration;
208 struct enum_table table;
209};
210
211struct definition_enum {
212 struct definition p;
213 struct definition_integer *integer;
214 struct declaration_enum *declaration;
215 /* Last GQuark values read. Keeping a reference on the GQuark array. */
216 GArray *value;
217};
218
219struct declaration_string {
220 struct declaration p;
221 enum ctf_string_encoding encoding;
222};
223
224struct definition_string {
225 struct definition p;
226 struct declaration_string *declaration;
227 char *value; /* freed at definition_string teardown */
228 size_t len, alloc_len;
229};
230
231struct declaration_field {
232 GQuark name;
233 struct declaration *declaration;
234};
235
236struct 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 */
241};
242
243struct definition_struct {
244 struct definition p;
245 struct declaration_struct *declaration;
246 GPtrArray *fields; /* Array of pointers to struct definition */
247};
248
249struct 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 */
254};
255
256struct declaration_variant {
257 struct declaration p;
258 struct declaration_untagged_variant *untagged_variant;
259 GArray *tag_name; /* Array of GQuark */
260};
261
262/* A variant needs to be tagged to be defined. */
263struct definition_variant {
264 struct definition p;
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 */
269};
270
271struct declaration_array {
272 struct declaration p;
273 size_t len;
274 struct declaration *elem;
275 struct declaration_scope *scope;
276};
277
278struct definition_array {
279 struct definition p;
280 struct declaration_array *declaration;
281 GPtrArray *elems; /* Array of pointers to struct definition */
282 GString *string; /* String for encoded integer children */
283};
284
285struct declaration_sequence {
286 struct declaration p;
287 GArray *length_name; /* Array of GQuark */
288 struct declaration *elem;
289 struct declaration_scope *scope;
290};
291
292struct definition_sequence {
293 struct definition p;
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 */
298};
299
300int register_declaration(GQuark declaration_name,
301 struct declaration *declaration,
302 struct declaration_scope *scope);
303struct declaration *lookup_declaration(GQuark declaration_name,
304 struct declaration_scope *scope);
305
306/*
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.
311 */
312int register_struct_declaration(GQuark struct_name,
313 struct declaration_struct *struct_declaration,
314 struct declaration_scope *scope);
315struct declaration_struct *
316 lookup_struct_declaration(GQuark struct_name,
317 struct declaration_scope *scope);
318int register_variant_declaration(GQuark variant_name,
319 struct declaration_untagged_variant *untagged_variant_declaration,
320 struct declaration_scope *scope);
321struct declaration_untagged_variant *lookup_variant_declaration(GQuark variant_name,
322 struct declaration_scope *scope);
323int register_enum_declaration(GQuark enum_name,
324 struct declaration_enum *enum_declaration,
325 struct declaration_scope *scope);
326struct declaration_enum *
327 lookup_enum_declaration(GQuark enum_name,
328 struct declaration_scope *scope);
329
330struct declaration_scope *
331 new_declaration_scope(struct declaration_scope *parent_scope);
332void free_declaration_scope(struct declaration_scope *scope);
333
334/*
335 * field_definition is for field definitions. They are registered into
336 * definition scopes.
337 */
338struct definition *
339 lookup_path_definition(GArray *cur_path, /* array of GQuark */
340 GArray *lookup_path, /* array of GQuark */
341 struct definition_scope *scope);
342int register_field_definition(GQuark field_name,
343 struct definition *definition,
344 struct definition_scope *scope);
345struct definition_scope *
346 new_definition_scope(struct definition_scope *parent_scope,
347 GQuark field_name, const char *root_name);
348void free_definition_scope(struct definition_scope *scope);
349
350GQuark new_definition_path(struct definition_scope *parent_scope,
351 GQuark field_name, const char *root_name);
352
353static inline
354int compare_definition_path(struct definition *definition, GQuark path)
355{
356 return definition->path == path;
357}
358
359void declaration_ref(struct declaration *declaration);
360void declaration_unref(struct declaration *declaration);
361
362void definition_ref(struct definition *definition);
363void definition_unref(struct definition *definition);
364
365struct 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);
369uint64_t get_unsigned_int(const struct definition *field);
370int64_t get_signed_int(const struct definition *field);
371int get_int_signedness(const struct definition *field);
372int get_int_byte_order(const struct definition *field);
373int get_int_base(const struct definition *field);
374size_t get_int_len(const struct definition *field); /* in bits */
375enum ctf_string_encoding get_int_encoding(const struct definition *field);
376
377/*
378 * mantissa_len is the length of the number of bytes represented by the mantissa
379 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
380 */
381struct declaration_float *float_declaration_new(size_t mantissa_len,
382 size_t exp_len, int byte_order,
383 size_t alignment);
384
385/*
386 * A GQuark can be translated to/from strings with g_quark_from_string() and
387 * g_quark_to_string().
388 */
389
390/*
391 * Returns a GArray of GQuark or NULL.
392 * Caller must release the GArray with g_array_unref().
393 */
394GArray *enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
395 uint64_t v);
396
397/*
398 * Returns a GArray of GQuark or NULL.
399 * Caller must release the GArray with g_array_unref().
400 */
401GArray *enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
402 int64_t v);
403
404/*
405 * Returns a GArray of struct enum_range or NULL.
406 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
407 * release it).
408 */
409GArray *enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
410 GQuark q);
411void enum_signed_insert(struct declaration_enum *enum_declaration,
412 int64_t start, int64_t end, GQuark q);
413void enum_unsigned_insert(struct declaration_enum *enum_declaration,
414 uint64_t start, uint64_t end, GQuark q);
415size_t enum_get_nr_enumerators(struct declaration_enum *enum_declaration);
416
417struct declaration_enum *
418 enum_declaration_new(struct declaration_integer *integer_declaration);
419
420struct declaration_string *
421 string_declaration_new(enum ctf_string_encoding encoding);
422char *get_string(const struct definition *field);
423enum ctf_string_encoding get_string_encoding(const struct definition *field);
424
425struct declaration_struct *
426 struct_declaration_new(struct declaration_scope *parent_scope,
427 uint64_t min_align);
428void struct_declaration_add_field(struct declaration_struct *struct_declaration,
429 const char *field_name,
430 struct declaration *field_declaration);
431/*
432 * Returns the index of a field within a structure.
433 */
434int struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
435 GQuark field_name);
436/*
437 * field returned only valid as long as the field structure is not appended to.
438 */
439struct declaration_field *
440struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
441 int index);
442struct definition *
443struct_definition_get_field_from_index(struct definition_struct *struct_definition,
444 int index);
445int struct_rw(struct stream_pos *pos, struct definition *definition);
446uint64_t struct_declaration_len(struct declaration_struct *struct_declaration);
447
448/*
449 * The tag enumeration is validated to ensure that it contains only mappings
450 * from numeric values to a single tag. Overlapping tag value ranges are
451 * therefore forbidden.
452 */
453struct declaration_untagged_variant *untagged_variant_declaration_new(
454 struct declaration_scope *parent_scope);
455struct declaration_variant *variant_declaration_new(struct declaration_untagged_variant *untagged_variant,
456 const char *tag);
457
458void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
459 const char *field_name,
460 struct declaration *field_declaration);
461struct declaration_field *
462 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration,
463 GQuark tag);
464/*
465 * Returns 0 on success, -EPERM on error.
466 */
467int variant_definition_set_tag(struct definition_variant *variant,
468 struct definition *enum_tag);
469/*
470 * Returns the field selected by the current tag value.
471 * field returned only valid as long as the variant structure is not appended
472 * to.
473 */
474struct definition *variant_get_current_field(struct definition_variant *variant);
475int variant_rw(struct stream_pos *pos, struct definition *definition);
476
477/*
478 * elem_declaration passed as parameter now belongs to the array. No
479 * need to free it explicitly. "len" is the number of elements in the
480 * array.
481 */
482struct declaration_array *
483 array_declaration_new(size_t len, struct declaration *elem_declaration,
484 struct declaration_scope *parent_scope);
485uint64_t array_len(struct definition_array *array);
486struct definition *array_index(struct definition_array *array, uint64_t i);
487int array_rw(struct stream_pos *pos, struct definition *definition);
488GString *get_char_array(const struct definition *field);
489int get_array_len(const struct definition *field);
490
491/*
492 * int_declaration and elem_declaration passed as parameter now belong
493 * to the sequence. No need to free them explicitly.
494 */
495struct declaration_sequence *
496 sequence_declaration_new(const char *length_name,
497 struct declaration *elem_declaration,
498 struct declaration_scope *parent_scope);
499uint64_t sequence_len(struct definition_sequence *sequence);
500struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i);
501int sequence_rw(struct stream_pos *pos, struct definition *definition);
502
503/*
504 * in: path (dot separated), out: q (GArray of GQuark)
505 */
506void append_scope_path(const char *path, GArray *q);
507
508/*
509 * Lookup helpers.
510 */
511struct definition *lookup_definition(const struct definition *definition,
512 const char *field_name);
513struct definition_integer *lookup_integer(const struct definition *definition,
514 const char *field_name,
515 int signedness);
516struct definition_enum *lookup_enum(const struct definition *definition,
517 const char *field_name,
518 int signedness);
519struct definition *lookup_variant(const struct definition *definition,
520 const char *field_name);
521
522static inline
523const char *rem_(const char *str)
524{
525 if (str[0] == '_')
526 return &str[1];
527 else
528 return str;
529}
530
531#endif /* _BABELTRACE_TYPES_H */
This page took 0.023634 seconds and 4 git commands to generate.