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
generic_rw(struct bt_stream_pos * pos,struct bt_definition * definition)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_DEBUG_INFO
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_DEBUG_INFO
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
compare_definition_path(struct bt_definition * definition,GQuark path)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 bool bt_int_is_char(const struct bt_declaration *field);
404 enum ctf_string_encoding bt_get_int_encoding(const struct bt_definition *field);
405 
406 /*
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.
409  */
410 struct declaration_float *bt_float_declaration_new(size_t mantissa_len,
411 				  size_t exp_len, int byte_order,
412 				  size_t alignment);
413 
414 /*
415  * A GQuark can be translated to/from strings with g_quark_from_string() and
416  * g_quark_to_string().
417  */
418 
419 /*
420  * Returns a GArray of GQuark or NULL.
421  * Caller must release the GArray with g_array_unref().
422  */
423 GArray *bt_enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
424 			       uint64_t v);
425 
426 /*
427  * Returns a GArray of GQuark or NULL.
428  * Caller must release the GArray with g_array_unref().
429  */
430 GArray *bt_enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
431 			      int64_t v);
432 
433 /*
434  * Returns a GArray of struct enum_range or NULL.
435  * Callers do _not_ own the returned GArray (and therefore _don't_ need to
436  * release it).
437  */
438 GArray *bt_enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
439 				GQuark q);
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);
445 
446 struct declaration_enum *
447 	bt_enum_declaration_new(struct declaration_integer *integer_declaration);
448 
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);
453 
454 double bt_get_float(const struct bt_definition *field);
455 
456 const struct bt_definition *bt_get_variant_field(struct bt_definition *definition);
457 
458 struct declaration_struct *
459 	bt_struct_declaration_new(struct declaration_scope *parent_scope,
460 			       uint64_t min_align);
461 void bt_struct_declaration_add_field(struct declaration_struct *struct_declaration,
462 				  const char *field_name,
463 				  struct bt_declaration *field_declaration);
464 /*
465  * Returns the index of a field within a structure.
466  */
467 int bt_struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
468 						    GQuark field_name);
469 /*
470  * field returned only valid as long as the field structure is not appended to.
471  */
472 struct declaration_field *
473 bt_struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
474 					int index);
475 struct bt_definition *
476 bt_struct_definition_get_field_from_index(const struct definition_struct *struct_definition,
477 				       int index);
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);
480 
481 /*
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.
485  */
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,
489 		const char *tag);
490 
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,
496 		GQuark tag);
497 /*
498  * Returns 0 on success, -EPERM on error.
499  */
500 int variant_definition_set_tag(struct definition_variant *variant,
501 			       struct bt_definition *enum_tag);
502 /*
503  * Returns the field selected by the current tag value.
504  * field returned only valid as long as the variant structure is not appended
505  * to.
506  */
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);
509 
510 /*
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
513  * array.
514  */
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);
523 
524 /*
525  * int_declaration and elem_declaration passed as parameter now belong
526  * to the sequence. No need to free them explicitly.
527  */
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);
535 
536 /*
537  * in: path (dot separated), out: q (GArray of GQuark)
538  */
539 void bt_append_scope_path(const char *path, GArray *q);
540 
541 /*
542  * Lookup helpers.
543  */
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,
547 				     GQuark quark);
548 struct definition_integer *bt_lookup_integer(const struct bt_definition *definition,
549 					  const char *field_name,
550 					  int signedness);
551 struct definition_enum *bt_lookup_enum(const struct bt_definition *definition,
552 				    const char *field_name,
553 				    int signedness);
554 struct bt_definition *bt_lookup_variant(const struct bt_definition *definition,
555 				  const char *field_name);
556 
557 static inline
rem_(const char * str)558 const char *rem_(const char *str)
559 {
560 	if (str[0] == '_')
561 		return &str[1];
562 	else
563 		return str;
564 }
565 
566 #endif /* _BABELTRACE_TYPES_H */
567