Initial implementation of the debuginfo API
[babeltrace.git] / include / babeltrace / types.h
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
45 struct ctf_stream_definition;
46 struct bt_stream_pos;
47 struct bt_format;
48 struct bt_definition;
49 struct ctf_clock;
50
51 /* type scope */
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;
63 };
64
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;
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
81 struct 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
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 */
105 GQuark path;
106 struct definition_scope *scope;
107 };
108
109 typedef int (*rw_dispatch)(struct bt_stream_pos *pos,
110 struct bt_definition *definition);
111
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;
123 };
124
125 static inline
126 int generic_rw(struct bt_stream_pos *pos, struct bt_definition *definition)
127 {
128 enum ctf_type_id dispatch_id = definition->declaration->id;
129 rw_dispatch call;
130
131 assert(pos->rw_table[dispatch_id] != NULL);
132 call = pos->rw_table[dispatch_id];
133 return call(pos, definition);
134 }
135
136 /*
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.
140 */
141 struct declaration_integer {
142 struct bt_declaration p;
143 size_t len; /* length, in bits. */
144 int byte_order; /* byte order */
145 int signedness;
146 int base; /* Base for pretty-printing: 2, 8, 10, 16 */
147 enum ctf_string_encoding encoding;
148 struct ctf_clock *clock;
149 };
150
151 #ifdef ENABLE_DEBUGINFO
152 struct debug_info_source;
153 #endif
154
155 struct definition_integer {
156 struct bt_definition p;
157 struct declaration_integer *declaration;
158 /* Last values read */
159 union {
160 uint64_t _unsigned;
161 int64_t _signed;
162 } value;
163
164 #ifdef ENABLE_DEBUGINFO
165 /*
166 * Debug infos (NULL if not set).
167 *
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.
171 */
172 struct debug_info_source *debug_info_src;
173 #endif
174 };
175
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;
182 /* TODO: we might want to express more info about NaN, +inf and -inf */
183 };
184
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 */
192 double value;
193 };
194
195 /*
196 * enum_val_equal assumes that signed and unsigned memory layout overlap.
197 */
198 struct enum_range {
199 union {
200 int64_t _signed;
201 uint64_t _unsigned;
202 } start; /* lowest range value */
203 union {
204 int64_t _signed;
205 uint64_t _unsigned;
206 } end; /* highest range value */
207 };
208
209 struct enum_range_to_quark {
210 struct bt_list_head node;
211 struct enum_range range;
212 GQuark quark;
213 };
214
215 /*
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.
219 *
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.
226 */
227 struct enum_table {
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) */
231 };
232
233 struct declaration_enum {
234 struct bt_declaration p;
235 struct declaration_integer *integer_declaration;
236 struct enum_table table;
237 };
238
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. */
244 GArray *value;
245 };
246
247 struct declaration_string {
248 struct bt_declaration p;
249 enum ctf_string_encoding encoding;
250 };
251
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;
257 };
258
259 struct declaration_field {
260 GQuark name;
261 struct bt_declaration *declaration;
262 };
263
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 */
269 };
270
271 struct definition_struct {
272 struct bt_definition p;
273 struct declaration_struct *declaration;
274 GPtrArray *fields; /* Array of pointers to struct bt_definition */
275 };
276
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 */
282 };
283
284 struct declaration_variant {
285 struct bt_declaration p;
286 struct declaration_untagged_variant *untagged_variant;
287 GArray *tag_name; /* Array of GQuark */
288 };
289
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 */
297 };
298
299 struct declaration_array {
300 struct bt_declaration p;
301 size_t len;
302 struct bt_declaration *elem;
303 struct declaration_scope *scope;
304 };
305
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 */
311 };
312
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;
318 };
319
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 */
326 };
327
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);
333
334 /*
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.
339 */
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);
357
358 struct declaration_scope *
359 bt_new_declaration_scope(struct declaration_scope *parent_scope);
360 void bt_free_declaration_scope(struct declaration_scope *scope);
361
362 /*
363 * field_definition is for field definitions. They are registered into
364 * definition scopes.
365 */
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);
377
378 GQuark bt_new_definition_path(struct definition_scope *parent_scope,
379 GQuark field_name, const char *root_name);
380
381 static inline
382 int compare_definition_path(struct bt_definition *definition, GQuark path)
383 {
384 return definition->path == path;
385 }
386
387 void bt_declaration_ref(struct bt_declaration *declaration);
388 void bt_declaration_unref(struct bt_declaration *declaration);
389
390 void bt_definition_ref(struct bt_definition *definition);
391 void bt_definition_unref(struct bt_definition *definition);
392
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);
404
405 /*
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.
408 */
409 struct declaration_float *bt_float_declaration_new(size_t mantissa_len,
410 size_t exp_len, int byte_order,
411 size_t alignment);
412
413 /*
414 * A GQuark can be translated to/from strings with g_quark_from_string() and
415 * g_quark_to_string().
416 */
417
418 /*
419 * Returns a GArray of GQuark or NULL.
420 * Caller must release the GArray with g_array_unref().
421 */
422 GArray *bt_enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
423 uint64_t v);
424
425 /*
426 * Returns a GArray of GQuark or NULL.
427 * Caller must release the GArray with g_array_unref().
428 */
429 GArray *bt_enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
430 int64_t v);
431
432 /*
433 * Returns a GArray of struct enum_range or NULL.
434 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
435 * release it).
436 */
437 GArray *bt_enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
438 GQuark q);
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);
444
445 struct declaration_enum *
446 bt_enum_declaration_new(struct declaration_integer *integer_declaration);
447
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);
452
453 double bt_get_float(const struct bt_definition *field);
454
455 const struct bt_definition *bt_get_variant_field(struct bt_definition *definition);
456
457 struct declaration_struct *
458 bt_struct_declaration_new(struct declaration_scope *parent_scope,
459 uint64_t min_align);
460 void bt_struct_declaration_add_field(struct declaration_struct *struct_declaration,
461 const char *field_name,
462 struct bt_declaration *field_declaration);
463 /*
464 * Returns the index of a field within a structure.
465 */
466 int bt_struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
467 GQuark field_name);
468 /*
469 * field returned only valid as long as the field structure is not appended to.
470 */
471 struct declaration_field *
472 bt_struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
473 int index);
474 struct bt_definition *
475 bt_struct_definition_get_field_from_index(const struct definition_struct *struct_definition,
476 int index);
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);
479
480 /*
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.
484 */
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,
488 const char *tag);
489
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,
495 GQuark tag);
496 /*
497 * Returns 0 on success, -EPERM on error.
498 */
499 int variant_definition_set_tag(struct definition_variant *variant,
500 struct bt_definition *enum_tag);
501 /*
502 * Returns the field selected by the current tag value.
503 * field returned only valid as long as the variant structure is not appended
504 * to.
505 */
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);
508
509 /*
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
512 * array.
513 */
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);
522
523 /*
524 * int_declaration and elem_declaration passed as parameter now belong
525 * to the sequence. No need to free them explicitly.
526 */
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);
534
535 /*
536 * in: path (dot separated), out: q (GArray of GQuark)
537 */
538 void bt_append_scope_path(const char *path, GArray *q);
539
540 /*
541 * Lookup helpers.
542 */
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,
546 GQuark quark);
547 struct definition_integer *bt_lookup_integer(const struct bt_definition *definition,
548 const char *field_name,
549 int signedness);
550 struct definition_enum *bt_lookup_enum(const struct bt_definition *definition,
551 const char *field_name,
552 int signedness);
553 struct bt_definition *bt_lookup_variant(const struct bt_definition *definition,
554 const char *field_name);
555
556 static inline
557 const char *rem_(const char *str)
558 {
559 if (str[0] == '_')
560 return &str[1];
561 else
562 return str;
563 }
564
565 #endif /* _BABELTRACE_TYPES_H */
This page took 0.040003 seconds and 4 git commands to generate.