Update dynamic scope explanation
[ctf.git] / common-trace-format-proposal.txt
1
2 RFC: Common Trace Format (CTF) Proposal (pre-v1.7)
3
4 Mathieu Desnoyers, EfficiOS Inc.
5
6 The goal of the present document is to propose a trace format that suits the
7 needs of the embedded, telecom, high-performance and kernel communities. It is
8 based on the Common Trace Format Requirements (v1.4) document. It is designed to
9 allow traces to be natively generated by the Linux kernel, Linux user-space
10 applications written in C/C++, and hardware components. One major element of
11 CTF is the Trace Stream Description Language (TSDL) which flexibility
12 enables description of various binary trace stream layouts.
13
14 The latest version of this document can be found at:
15
16 git tree: git://git.efficios.com/ctf.git
17 gitweb: http://git.efficios.com/?p=ctf.git
18
19 A reference implementation of a library to read and write this trace format is
20 being implemented within the BabelTrace project, a converter between trace
21 formats. The development tree is available at:
22
23 git tree: git://git.efficios.com/babeltrace.git
24 gitweb: http://git.efficios.com/?p=babeltrace.git
25
26
27 1. Preliminary definitions
28
29 - Event Trace: An ordered sequence of events.
30 - Event Stream: An ordered sequence of events, containing a subset of the
31 trace event types.
32 - Event Packet: A sequence of physically contiguous events within an event
33 stream.
34 - Event: This is the basic entry in a trace. (aka: a trace record).
35 - An event identifier (ID) relates to the class (a type) of event within
36 an event stream.
37 e.g. event: irq_entry.
38 - An event (or event record) relates to a specific instance of an event
39 class.
40 e.g. event: irq_entry, at time X, on CPU Y
41 - Source Architecture: Architecture writing the trace.
42 - Reader Architecture: Architecture reading the trace.
43
44
45 2. High-level representation of a trace
46
47 A trace is divided into multiple event streams. Each event stream contains a
48 subset of the trace event types.
49
50 The final output of the trace, after its generation and optional transport over
51 the network, is expected to be either on permanent or temporary storage in a
52 virtual file system. Because each event stream is appended to while a trace is
53 being recorded, each is associated with a separate file for output. Therefore,
54 a stored trace can be represented as a directory containing one file per stream.
55
56 Meta-data description associated with the trace contains information on
57 trace event types expressed in the Trace Stream Description Language
58 (TSDL). This language describes:
59
60 - Trace version.
61 - Types available.
62 - Per-trace event header description.
63 - Per-stream event header description.
64 - Per-stream event context description.
65 - Per-event
66 - Event type to stream mapping.
67 - Event type to name mapping.
68 - Event type to ID mapping.
69 - Event context description.
70 - Event fields description.
71
72
73 3. Event stream
74
75 An event stream can be divided into contiguous event packets of variable
76 size. These subdivisions have a variable size. An event packet can
77 contain a certain amount of padding at the end. The stream header is
78 repeated at the beginning of each event packet. The rationale for the
79 event stream design choices is explained in Appendix B. Stream Header
80 Rationale.
81
82 The event stream header will therefore be referred to as the "event packet
83 header" throughout the rest of this document.
84
85
86 4. Types
87
88 Types are organized as type classes. Each type class belong to either of two
89 kind of types: basic types or compound types.
90
91 4.1 Basic types
92
93 A basic type is a scalar type, as described in this section. It includes
94 integers, GNU/C bitfields, enumerations, and floating point values.
95
96 4.1.1 Type inheritance
97
98 Type specifications can be inherited to allow deriving types from a
99 type class. For example, see the uint32_t named type derived from the "integer"
100 type class below ("Integers" section). Types have a precise binary
101 representation in the trace. A type class has methods to read and write these
102 types, but must be derived into a type to be usable in an event field.
103
104 4.1.2 Alignment
105
106 We define "byte-packed" types as aligned on the byte size, namely 8-bit.
107 We define "bit-packed" types as following on the next bit, as defined by the
108 "Integers" section.
109
110 Each basic type must specify its alignment, in bits. Examples of
111 possible alignments are: bit-packed (align = 1), byte-packed (align =
112 8), or word-aligned (e.g. align = 32 or align = 64). The choice depends
113 on the architecture preference and compactness vs performance trade-offs
114 of the implementation. Architectures providing fast unaligned write
115 byte-packed basic types to save space, aligning each type on byte
116 boundaries (8-bit). Architectures with slow unaligned writes align types
117 on specific alignment values. If no specific alignment is declared for a
118 type, it is assumed to be bit-packed for integers with size not multiple
119 of 8 bits and for gcc bitfields. All other basic types are byte-packed
120 by default. It is however recommended to always specify the alignment
121 explicitly. Alignment values must be power of two. Compound types are
122 aligned as specified in their individual specification.
123
124 TSDL meta-data attribute representation of a specific alignment:
125
126 align = value; /* value in bits */
127
128 4.1.3 Byte order
129
130 By default, the native endianness of the source architecture the trace is used.
131 Byte order can be overridden for a basic type by specifying a "byte_order"
132 attribute. Typical use-case is to specify the network byte order (big endian:
133 "be") to save data captured from the network into the trace without conversion.
134 If not specified, the byte order is native.
135
136 TSDL meta-data representation:
137
138 byte_order = native OR network OR be OR le; /* network and be are aliases */
139
140 4.1.4 Size
141
142 Type size, in bits, for integers and floats is that returned by "sizeof()" in C
143 multiplied by CHAR_BIT.
144 We require the size of "char" and "unsigned char" types (CHAR_BIT) to be fixed
145 to 8 bits for cross-endianness compatibility.
146
147 TSDL meta-data representation:
148
149 size = value; (value is in bits)
150
151 4.1.5 Integers
152
153 Signed integers are represented in two-complement. Integer alignment,
154 size, signedness and byte ordering are defined in the TSDL meta-data.
155 Integers aligned on byte size (8-bit) and with length multiple of byte
156 size (8-bit) correspond to the C99 standard integers. In addition,
157 integers with alignment and/or size that are _not_ a multiple of the
158 byte size are permitted; these correspond to the C99 standard bitfields,
159 with the added specification that the CTF integer bitfields have a fixed
160 binary representation. A MIT-licensed reference implementation of the
161 CTF portable bitfields is available at:
162
163 http://git.efficios.com/?p=babeltrace.git;a=blob;f=include/babeltrace/bitfield.h
164
165 Binary representation of integers:
166
167 - On little and big endian:
168 - Within a byte, high bits correspond to an integer high bits, and low bits
169 correspond to low bits.
170 - On little endian:
171 - Integer across multiple bytes are placed from the less significant to the
172 most significant.
173 - Consecutive integers are placed from lower bits to higher bits (even within
174 a byte).
175 - On big endian:
176 - Integer across multiple bytes are placed from the most significant to the
177 less significant.
178 - Consecutive integers are placed from higher bits to lower bits (even within
179 a byte).
180
181 This binary representation is derived from the bitfield implementation in GCC
182 for little and big endian. However, contrary to what GCC does, integers can
183 cross units boundaries (no padding is required). Padding can be explicitly
184 added (see 4.1.6 GNU/C bitfields) to follow the GCC layout if needed.
185
186 TSDL meta-data representation:
187
188 integer {
189 signed = true OR false; /* default false */
190 byte_order = native OR network OR be OR le; /* default native */
191 size = value; /* value in bits, no default */
192 align = value; /* value in bits */
193 /* based used for pretty-printing output, default: decimal. */
194 base = decimal OR dec OR OR d OR i OR u OR 10 OR hexadecimal OR hex OR x OR X OR p OR 16
195 OR octal OR oct OR o OR 8 OR binary OR b OR 2;
196 }
197
198 Example of type inheritance (creation of a uint32_t named type):
199
200 typealias integer {
201 size = 32;
202 signed = false;
203 align = 32;
204 } := uint32_t;
205
206 Definition of a named 5-bit signed bitfield:
207
208 typealias integer {
209 size = 5;
210 signed = true;
211 align = 1;
212 } := int5_t;
213
214 4.1.6 GNU/C bitfields
215
216 The GNU/C bitfields follow closely the integer representation, with a
217 particularity on alignment: if a bitfield cannot fit in the current unit, the
218 unit is padded and the bitfield starts at the following unit. The unit size is
219 defined by the size of the type "unit_type".
220
221 TSDL meta-data representation:
222
223 unit_type name:size:
224
225 As an example, the following structure declared in C compiled by GCC:
226
227 struct example {
228 short a:12;
229 short b:5;
230 };
231
232 The example structure is aligned on the largest element (short). The second
233 bitfield would be aligned on the next unit boundary, because it would not fit in
234 the current unit.
235
236 4.1.7 Floating point
237
238 The floating point values byte ordering is defined in the TSDL meta-data.
239
240 Floating point values follow the IEEE 754-2008 standard interchange formats.
241 Description of the floating point values include the exponent and mantissa size
242 in bits. Some requirements are imposed on the floating point values:
243
244 - FLT_RADIX must be 2.
245 - mant_dig is the number of digits represented in the mantissa. It is specified
246 by the ISO C99 standard, section 5.2.4, as FLT_MANT_DIG, DBL_MANT_DIG and
247 LDBL_MANT_DIG as defined by <float.h>.
248 - exp_dig is the number of digits represented in the exponent. Given that
249 mant_dig is one bit more than its actual size in bits (leading 1 is not
250 needed) and also given that the sign bit always takes one bit, exp_dig can be
251 specified as:
252
253 - sizeof(float) * CHAR_BIT - FLT_MANT_DIG
254 - sizeof(double) * CHAR_BIT - DBL_MANT_DIG
255 - sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG
256
257 TSDL meta-data representation:
258
259 floating_point {
260 exp_dig = value;
261 mant_dig = value;
262 byte_order = native OR network OR be OR le;
263 align = value;
264 }
265
266 Example of type inheritance:
267
268 typealias floating_point {
269 exp_dig = 8; /* sizeof(float) * CHAR_BIT - FLT_MANT_DIG */
270 mant_dig = 24; /* FLT_MANT_DIG */
271 byte_order = native;
272 align = 32;
273 } := float;
274
275 TODO: define NaN, +inf, -inf behavior.
276
277 Bit-packed, byte-packed or larger alignments can be used for floating
278 point values, similarly to integers.
279
280 4.1.8 Enumerations
281
282 Enumerations are a mapping between an integer type and a table of strings. The
283 numerical representation of the enumeration follows the integer type specified
284 by the meta-data. The enumeration mapping table is detailed in the enumeration
285 description within the meta-data. The mapping table maps inclusive value
286 ranges (or single values) to strings. Instead of being limited to simple
287 "value -> string" mappings, these enumerations map
288 "[ start_value ... end_value ] -> string", which map inclusive ranges of
289 values to strings. An enumeration from the C language can be represented in
290 this format by having the same start_value and end_value for each element, which
291 is in fact a range of size 1. This single-value range is supported without
292 repeating the start and end values with the value = string declaration.
293
294 enum name : integer_type {
295 somestring = start_value1 ... end_value1,
296 "other string" = start_value2 ... end_value2,
297 yet_another_string, /* will be assigned to end_value2 + 1 */
298 "some other string" = value,
299 ...
300 };
301
302 If the values are omitted, the enumeration starts at 0 and increment of 1 for
303 each entry:
304
305 enum name : unsigned int {
306 ZERO,
307 ONE,
308 TWO,
309 TEN = 10,
310 ELEVEN,
311 };
312
313 Overlapping ranges within a single enumeration are implementation defined.
314
315 A nameless enumeration can be declared as a field type or as part of a typedef:
316
317 enum : integer_type {
318 ...
319 }
320
321 Enumerations omitting the container type ": integer_type" use the "int"
322 type (for compatibility with C99). The "int" type must be previously
323 declared. E.g.:
324
325 typealias integer { size = 32; align = 32; signed = true } := int;
326
327 enum {
328 ...
329 }
330
331
332 4.2 Compound types
333
334 Compound are aggregation of type declarations. Compound types include
335 structures, variant, arrays, sequences, and strings.
336
337 4.2.1 Structures
338
339 Structures are aligned on the largest alignment required by basic types
340 contained within the structure. (This follows the ISO/C standard for structures)
341
342 TSDL meta-data representation of a named structure:
343
344 struct name {
345 field_type field_name;
346 field_type field_name;
347 ...
348 };
349
350 Example:
351
352 struct example {
353 integer { /* Nameless type */
354 size = 16;
355 signed = true;
356 align = 16;
357 } first_field_name;
358 uint64_t second_field_name; /* Named type declared in the meta-data */
359 };
360
361 The fields are placed in a sequence next to each other. They each possess a
362 field name, which is a unique identifier within the structure.
363
364 A nameless structure can be declared as a field type or as part of a typedef:
365
366 struct {
367 ...
368 }
369
370 Alignment for a structure compound type can be forced to a minimum value
371 by adding an "align" specifier after the declaration of a structure
372 body. This attribute is read as: align(value). The value is specified in
373 bits. The structure will be aligned on the maximum value between this
374 attribute and the alignment required by the basic types contained within
375 the structure. e.g.
376
377 struct {
378 ...
379 } align(32)
380
381 4.2.2 Variants (Discriminated/Tagged Unions)
382
383 A CTF variant is a selection between different types. A CTF variant must
384 always be defined within the scope of a structure or within fields
385 contained within a structure (defined recursively). A "tag" enumeration
386 field must appear in either the same lexical scope, prior to the variant
387 field (in field declaration order), in an upper lexical scope (see
388 Section 7.3.1), or in an upper dynamic scope (see Section 7.3.2). The
389 type selection is indicated by the mapping from the enumeration value to
390 the string used as variant type selector. The field to use as tag is
391 specified by the "tag_field", specified between "< >" after the
392 "variant" keyword for unnamed variants, and after "variant name" for
393 named variants.
394
395 The alignment of the variant is the alignment of the type as selected by the tag
396 value for the specific instance of the variant. The alignment of the type
397 containing the variant is independent of the variant alignment. The size of the
398 variant is the size as selected by the tag value for the specific instance of
399 the variant.
400
401 A named variant declaration followed by its definition within a structure
402 declaration:
403
404 variant name {
405 field_type sel1;
406 field_type sel2;
407 field_type sel3;
408 ...
409 };
410
411 struct {
412 enum : integer_type { sel1, sel2, sel3, ... } tag_field;
413 ...
414 variant name <tag_field> v;
415 }
416
417 An unnamed variant definition within a structure is expressed by the following
418 TSDL meta-data:
419
420 struct {
421 enum : integer_type { sel1, sel2, sel3, ... } tag_field;
422 ...
423 variant <tag_field> {
424 field_type sel1;
425 field_type sel2;
426 field_type sel3;
427 ...
428 } v;
429 }
430
431 Example of a named variant within a sequence that refers to a single tag field:
432
433 variant example {
434 uint32_t a;
435 uint64_t b;
436 short c;
437 };
438
439 struct {
440 enum : uint2_t { a, b, c } choice;
441 unsigned int seqlen;
442 variant example <choice> v[seqlen];
443 }
444
445 Example of an unnamed variant:
446
447 struct {
448 enum : uint2_t { a, b, c, d } choice;
449 /* Unrelated fields can be added between the variant and its tag */
450 int32_t somevalue;
451 variant <choice> {
452 uint32_t a;
453 uint64_t b;
454 short c;
455 struct {
456 unsigned int field1;
457 uint64_t field2;
458 } d;
459 } s;
460 }
461
462 Example of an unnamed variant within an array:
463
464 struct {
465 enum : uint2_t { a, b, c } choice;
466 variant <choice> {
467 uint32_t a;
468 uint64_t b;
469 short c;
470 } v[10];
471 }
472
473 Example of a variant type definition within a structure, where the defined type
474 is then declared within an array of structures. This variant refers to a tag
475 located in an upper lexical scope. This example clearly shows that a variant
476 type definition referring to the tag "x" uses the closest preceding field from
477 the lexical scope of the type definition.
478
479 struct {
480 enum : uint2_t { a, b, c, d } x;
481
482 typedef variant <x> { /*
483 * "x" refers to the preceding "x" enumeration in the
484 * lexical scope of the type definition.
485 */
486 uint32_t a;
487 uint64_t b;
488 short c;
489 } example_variant;
490
491 struct {
492 enum : int { x, y, z } x; /* This enumeration is not used by "v". */
493 example_variant v; /*
494 * "v" uses the "enum : uint2_t { a, b, c, d }"
495 * tag.
496 */
497 } a[10];
498 }
499
500 4.2.3 Arrays
501
502 Arrays are fixed-length. Their length is declared in the type
503 declaration within the meta-data. They contain an array of "inner type"
504 elements, which can refer to any type not containing the type of the
505 array being declared (no circular dependency). The length is the number
506 of elements in an array.
507
508 TSDL meta-data representation of a named array:
509
510 typedef elem_type name[length];
511
512 A nameless array can be declared as a field type within a structure, e.g.:
513
514 uint8_t field_name[10];
515
516 Arrays are always aligned on their element alignment requirement.
517
518 4.2.4 Sequences
519
520 Sequences are dynamically-sized arrays. They refer to a a "length"
521 unsigned integer field, which must appear in either the same lexical scope,
522 prior to the sequence field (in field declaration order), in an upper
523 lexical scope (see Section 7.3.1), or in an upper dynamic scope (see
524 Section 7.3.2). This length field represents the number of elements in
525 the sequence. The sequence per se is an array of "inner type" elements.
526
527 TSDL meta-data representation for a sequence type definition:
528
529 struct {
530 unsigned int length_field;
531 typedef elem_type typename[length_field];
532 typename seq_field_name;
533 }
534
535 A sequence can also be declared as a field type, e.g.:
536
537 struct {
538 unsigned int length_field;
539 long seq_field_name[length_field];
540 }
541
542 Multiple sequences can refer to the same length field, and these length
543 fields can be in a different upper dynamic scope:
544
545 e.g., assuming the stream.event.header defines:
546
547 stream {
548 ...
549 id = 1;
550 event.header := struct {
551 uint16_t seq_len;
552 };
553 };
554
555 event {
556 ...
557 stream_id = 1;
558 fields := struct {
559 long seq_a[stream.event.header.seq_len];
560 char seq_b[stream.event.header.seq_len];
561 };
562 };
563
564 The sequence elements follow the "array" specifications.
565
566 4.2.5 Strings
567
568 Strings are an array of bytes of variable size and are terminated by a '\0'
569 "NULL" character. Their encoding is described in the TSDL meta-data. In
570 absence of encoding attribute information, the default encoding is
571 UTF-8.
572
573 TSDL meta-data representation of a named string type:
574
575 typealias string {
576 encoding = UTF8 OR ASCII;
577 } := name;
578
579 A nameless string type can be declared as a field type:
580
581 string field_name; /* Use default UTF8 encoding */
582
583 Strings are always aligned on byte size.
584
585 5. Event Packet Header
586
587 The event packet header consists of two parts: the "event packet header"
588 is the same for all streams of a trace. The second part, the "event
589 packet context", is described on a per-stream basis. Both are described
590 in the TSDL meta-data. The packets are aligned on architecture-page-sized
591 addresses.
592
593 Event packet header (all fields are optional, specified by TSDL meta-data):
594
595 - Magic number (CTF magic number: 0xC1FC1FC1) specifies that this is a
596 CTF packet. This magic number is optional, but when present, it should
597 come at the very beginning of the packet.
598 - Trace UUID, used to ensure the event packet match the meta-data used.
599 (note: we cannot use a meta-data checksum in every cases instead of a
600 UUID because meta-data can be appended to while tracing is active)
601 This field is optional.
602 - Stream ID, used as reference to stream description in meta-data.
603 This field is optional if there is only one stream description in the
604 meta-data, but becomes required if there are more than one stream in
605 the TSDL meta-data description.
606
607 Event packet context (all fields are optional, specified by TSDL meta-data):
608
609 - Event packet content size (in bytes).
610 - Event packet size (in bytes, includes padding).
611 - Event packet content checksum (optional). Checksum excludes the event packet
612 header.
613 - Per-stream event packet sequence count (to deal with UDP packet loss). The
614 number of significant sequence counter bits should also be present, so
615 wrap-arounds are dealt with correctly.
616 - Time-stamp at the beginning and time-stamp at the end of the event packet.
617 Both timestamps are written in the packet header, but sampled respectively
618 while (or before) writing the first event and while (or after) writing the
619 last event in the packet. The inclusive range between these timestamps should
620 include all event timestamps assigned to events contained within the packet.
621 - Events discarded count
622 - Snapshot of a per-stream free-running counter, counting the number of
623 events discarded that were supposed to be written in the stream prior to
624 the first event in the event packet.
625 * Note: producer-consumer buffer full condition should fill the current
626 event packet with padding so we know exactly where events have been
627 discarded.
628 - Lossless compression scheme used for the event packet content. Applied
629 directly to raw data. New types of compression can be added in following
630 versions of the format.
631 0: no compression scheme
632 1: bzip2
633 2: gzip
634 3: xz
635 - Cypher used for the event packet content. Applied after compression.
636 0: no encryption
637 1: AES
638 - Checksum scheme used for the event packet content. Applied after encryption.
639 0: no checksum
640 1: md5
641 2: sha1
642 3: crc32
643
644 5.1 Event Packet Header Description
645
646 The event packet header layout is indicated by the trace packet.header
647 field. Here is a recommended structure type for the packet header with
648 the fields typically expected (although these fields are each optional):
649
650 struct event_packet_header {
651 uint32_t magic;
652 uint8_t trace_uuid[16];
653 uint32_t stream_id;
654 };
655
656 trace {
657 ...
658 packet.header := struct event_packet_header;
659 };
660
661 If the magic number is not present, tools such as "file" will have no
662 mean to discover the file type.
663
664 If the trace_uuid is not present, no validation that the meta-data
665 actually corresponds to the stream is performed.
666
667 If the stream_id packet header field is missing, the trace can only
668 contain a single stream. Its "id" field can be left out, and its events
669 don't need to declare a "stream_id" field.
670
671
672 5.2 Event Packet Context Description
673
674 Event packet context example. These are declared within the stream declaration
675 in the meta-data. All these fields are optional. If the packet size field is
676 missing, the whole stream only contains a single packet. If the content
677 size field is missing, the packet is filled (no padding). The content
678 and packet sizes include all headers.
679
680 An example event packet context type:
681
682 struct event_packet_context {
683 uint64_t timestamp_begin;
684 uint64_t timestamp_end;
685 uint32_t checksum;
686 uint32_t stream_packet_count;
687 uint32_t events_discarded;
688 uint32_t cpu_id;
689 uint32_t/uint16_t content_size;
690 uint32_t/uint16_t packet_size;
691 uint8_t stream_packet_count_bits; /* Significant counter bits */
692 uint8_t compression_scheme;
693 uint8_t encryption_scheme;
694 uint8_t checksum_scheme;
695 };
696
697
698 6. Event Structure
699
700 The overall structure of an event is:
701
702 1 - Stream Packet Context (as specified by the stream meta-data)
703 2 - Event Header (as specified by the stream meta-data)
704 3 - Stream Event Context (as specified by the stream meta-data)
705 4 - Event Context (as specified by the event meta-data)
706 5 - Event Payload (as specified by the event meta-data)
707
708 This structure defines an implicit dynamic scoping, where variants
709 located in inner structures (those with a higher number in the listing
710 above) can refer to the fields of outer structures (with lower number in
711 the listing above). See Section 7.3 TSDL Scopes for more detail.
712
713 6.1 Event Header
714
715 Event headers can be described within the meta-data. We hereby propose, as an
716 example, two types of events headers. Type 1 accommodates streams with less than
717 31 event IDs. Type 2 accommodates streams with 31 or more event IDs.
718
719 One major factor can vary between streams: the number of event IDs assigned to
720 a stream. Luckily, this information tends to stay relatively constant (modulo
721 event registration while trace is being recorded), so we can specify different
722 representations for streams containing few event IDs and streams containing
723 many event IDs, so we end up representing the event ID and time-stamp as
724 densely as possible in each case.
725
726 The header is extended in the rare occasions where the information cannot be
727 represented in the ranges available in the standard event header. They are also
728 used in the rare occasions where the data required for a field could not be
729 collected: the flag corresponding to the missing field within the missing_fields
730 array is then set to 1.
731
732 Types uintX_t represent an X-bit unsigned integer, as declared with
733 either:
734
735 typealias integer { size = X; align = X; signed = false } := uintX_t;
736
737 or
738
739 typealias integer { size = X; align = 1; signed = false } := uintX_t;
740
741 6.1.1 Type 1 - Few event IDs
742
743 - Aligned on 32-bit (or 8-bit if byte-packed, depending on the architecture
744 preference).
745 - Native architecture byte ordering.
746 - For "compact" selection
747 - Fixed size: 32 bits.
748 - For "extended" selection
749 - Size depends on the architecture and variant alignment.
750
751 struct event_header_1 {
752 /*
753 * id: range: 0 - 30.
754 * id 31 is reserved to indicate an extended header.
755 */
756 enum : uint5_t { compact = 0 ... 30, extended = 31 } id;
757 variant <id> {
758 struct {
759 uint27_t timestamp;
760 } compact;
761 struct {
762 uint32_t id; /* 32-bit event IDs */
763 uint64_t timestamp; /* 64-bit timestamps */
764 } extended;
765 } v;
766 };
767
768
769 6.1.2 Type 2 - Many event IDs
770
771 - Aligned on 16-bit (or 8-bit if byte-packed, depending on the architecture
772 preference).
773 - Native architecture byte ordering.
774 - For "compact" selection
775 - Size depends on the architecture and variant alignment.
776 - For "extended" selection
777 - Size depends on the architecture and variant alignment.
778
779 struct event_header_2 {
780 /*
781 * id: range: 0 - 65534.
782 * id 65535 is reserved to indicate an extended header.
783 */
784 enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;
785 variant <id> {
786 struct {
787 uint32_t timestamp;
788 } compact;
789 struct {
790 uint32_t id; /* 32-bit event IDs */
791 uint64_t timestamp; /* 64-bit timestamps */
792 } extended;
793 } v;
794 };
795
796
797 6.2 Event Context
798
799 The event context contains information relative to the current event.
800 The choice and meaning of this information is specified by the TSDL
801 stream and event meta-data descriptions. The stream context is applied
802 to all events within the stream. The stream context structure follows
803 the event header. The event context is applied to specific events. Its
804 structure follows the stream context structure.
805
806 An example of stream-level event context is to save the event payload size with
807 each event, or to save the current PID with each event. These are declared
808 within the stream declaration within the meta-data:
809
810 stream {
811 ...
812 event.context := struct {
813 uint pid;
814 uint16_t payload_size;
815 };
816 };
817
818 An example of event-specific event context is to declare a bitmap of missing
819 fields, only appended after the stream event context if the extended event
820 header is selected. NR_FIELDS is the number of fields within the event (a
821 numeric value).
822
823 event {
824 context = struct {
825 variant <id> {
826 struct { } compact;
827 struct {
828 uint1_t missing_fields[NR_FIELDS]; /* missing event fields bitmap */
829 } extended;
830 } v;
831 };
832 ...
833 }
834
835 6.3 Event Payload
836
837 An event payload contains fields specific to a given event type. The fields
838 belonging to an event type are described in the event-specific meta-data
839 within a structure type.
840
841 6.3.1 Padding
842
843 No padding at the end of the event payload. This differs from the ISO/C standard
844 for structures, but follows the CTF standard for structures. In a trace, even
845 though it makes sense to align the beginning of a structure, it really makes no
846 sense to add padding at the end of the structure, because structures are usually
847 not followed by a structure of the same type.
848
849 This trick can be done by adding a zero-length "end" field at the end of the C
850 structures, and by using the offset of this field rather than using sizeof()
851 when calculating the size of a structure (see Appendix "A. Helper macros").
852
853 6.3.2 Alignment
854
855 The event payload is aligned on the largest alignment required by types
856 contained within the payload. (This follows the ISO/C standard for structures)
857
858
859 7. Trace Stream Description Language (TSDL)
860
861 The Trace Stream Description Language (TSDL) allows expression of the
862 binary trace streams layout in a C99-like Domain Specific Language
863 (DSL).
864
865
866 7.1 Meta-data
867
868 The trace stream layout description is located in the trace meta-data.
869 The meta-data is itself located in a stream identified by its name:
870 "metadata".
871
872 The meta-data description can be expressed in two different formats:
873 text-only and packet-based. The text-only description facilitates
874 generation of meta-data and provides a convenient way to enter the
875 meta-data information by hand. The packet-based meta-data provides the
876 CTF stream packet facilities (checksumming, compression, encryption,
877 network-readiness) for meta-data stream generated and transported by a
878 tracer.
879
880 The text-only meta-data file is a plain text TSDL description.
881
882 The packet-based meta-data is made of "meta-data packets", which each
883 start with a meta-data packet header. The packet-based meta-data
884 description is detected by reading the magic number "0x75D11D57" at the
885 beginning of the file. This magic number is also used to detect the
886 endianness of the architecture by trying to read the CTF magic number
887 and its counterpart in reversed endianness. The events within the
888 meta-data stream have no event header nor event context. Each event only
889 contains a "string" payload. Each meta-data packet start with a special
890 packet header, specific to the meta-data stream, which contains,
891 exactly:
892
893 struct metadata_packet_header {
894 uint32_t magic; /* 0x75D11D57 */
895 uint8_t trace_uuid[16]; /* Unique Universal Identifier */
896 uint32_t checksum; /* 0 if unused */
897 uint32_t content_size; /* in bits */
898 uint32_t packet_size; /* in bits */
899 uint8_t compression_scheme; /* 0 if unused */
900 uint8_t encryption_scheme; /* 0 if unused */
901 uint8_t checksum_scheme; /* 0 if unused */
902 };
903
904 The packet-based meta-data can be converted to a text-only meta-data by
905 concatenating all the strings in contains.
906
907 In the textual representation of the meta-data, the text contained
908 within "/*" and "*/", as well as within "//" and end of line, are
909 treated as comments. Boolean values can be represented as true, TRUE,
910 or 1 for true, and false, FALSE, or 0 for false. Within the string-based
911 meta-data description, the trace UUID is represented as a string of
912 hexadecimal digits and dashes "-". In the event packet header, the trace
913 UUID is represented as an array of bytes.
914
915
916 7.2 Declaration vs Definition
917
918 A declaration associates a layout to a type, without specifying where
919 this type is located in the event structure hierarchy (see Section 6).
920 This therefore includes typedef, typealias, as well as all type
921 specifiers. In certain circumstances (typedef, structure field and
922 variant field), a declaration is followed by a declarator, which specify
923 the newly defined type name (for typedef), or the field name (for
924 declarations located within structure and variants). Array and sequence,
925 declared with square brackets ("[" "]"), are part of the declarator,
926 similarly to C99. The enumeration base type is specified by
927 ": enum_base", which is part of the type specifier. The variant tag
928 name, specified between "<" ">", is also part of the type specifier.
929
930 A definition associates a type to a location in the event structure
931 hierarchy (see Section 6). This association is denoted by ":=", as shown
932 in Section 7.3.
933
934
935 7.3 TSDL Scopes
936
937 TSDL uses two different types of scoping: a lexical scope is used for
938 declarations and type definitions, and a dynamic scope is used for
939 variants references to tag fields and for sequence references to length
940 fields.
941
942 7.3.1 Lexical Scope
943
944 Each of "trace", "stream", "event", "struct" and "variant" have their own
945 nestable declaration scope, within which types can be declared using "typedef"
946 and "typealias". A root declaration scope also contains all declarations
947 located outside of any of the aforementioned declarations. An inner
948 declaration scope can refer to type declared within its container
949 lexical scope prior to the inner declaration scope. Redefinition of a
950 typedef or typealias is not valid, although hiding an upper scope
951 typedef or typealias is allowed within a sub-scope.
952
953 7.3.2 Dynamic Scope
954
955 A dynamic scope consists in the lexical scope augmented with the
956 implicit event structure definition hierarchy presented at Section 6.
957 The dynamic scope is used for variant tag and sequence length
958 definitions. It is used at definition time to look up the location of
959 the tag field associated with a variant, and to lookup up the location
960 of the length field associated with a sequence.
961
962 Therefore, variants (or sequences) in lower levels in the dynamic scope
963 (e.g. event context) can refer to a tag (or length) field located in
964 upper levels (e.g. in the event header) by specifying, in this case, the
965 associated tag with <header.field_name>. This allows, for instance, the
966 event context to define a variant referring to the "id" field of the
967 event header as selector.
968
969 The target dynamic scope must be specified explicitly when referring to
970 a field outside of the local static scope. The dynamic scope prefixes
971 are thus:
972
973 - Trace Packet Header: <trace.packet.header. >,
974 - Stream Packet Context: <stream.packet.context. >,
975 - Event Header: <stream.event.header. >,
976 - Stream Event Context: <stream.event.context. >,
977 - Event Context: <event.context. >,
978 - Event Payload: <event.fields. >.
979
980 Multiple declarations of the same field name within a single scope is
981 not valid. It is however valid to re-use the same field name in
982 different scopes. There is no possible conflict, because the dynamic
983 scope must be specified when a variant refers to a tag field located in
984 a different dynamic scope.
985
986 The information available in the dynamic scopes can be thought of as the
987 current tracing context. At trace production, information about the
988 current context is saved into the specified scope field levels. At trace
989 consumption, for each event, the current trace context is therefore
990 readable by accessing the upper dynamic scopes.
991
992
993 7.4 TSDL Examples
994
995 The grammar representing the TSDL meta-data is presented in Appendix C.
996 TSDL Grammar. This section presents a rather lighter reading that
997 consists in examples of TSDL meta-data, with template values.
998
999 The stream "id" can be left out if there is only one stream in the
1000 trace. The event "id" field can be left out if there is only one event
1001 in a stream.
1002
1003 trace {
1004 major = value; /* Trace format version */
1005 minor = value;
1006 uuid = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"; /* Trace UUID */
1007 byte_order = be OR le; /* Endianness (required) */
1008 packet.header := struct {
1009 uint32_t magic;
1010 uint8_t trace_uuid[16];
1011 uint32_t stream_id;
1012 };
1013 };
1014
1015 stream {
1016 id = stream_id;
1017 /* Type 1 - Few event IDs; Type 2 - Many event IDs. See section 6.1. */
1018 event.header := event_header_1 OR event_header_2;
1019 event.context := struct {
1020 ...
1021 };
1022 packet.context := struct {
1023 ...
1024 };
1025 };
1026
1027 event {
1028 name = event_name;
1029 id = value; /* Numeric identifier within the stream */
1030 stream_id = stream_id;
1031 context := struct {
1032 ...
1033 };
1034 fields := struct {
1035 ...
1036 };
1037 };
1038
1039 /* More detail on types in section 4. Types */
1040
1041 /*
1042 * Named types:
1043 *
1044 * Type declarations behave similarly to the C standard.
1045 */
1046
1047 typedef aliased_type_specifiers new_type_declarators;
1048
1049 /* e.g.: typedef struct example new_type_name[10]; */
1050
1051 /*
1052 * typealias
1053 *
1054 * The "typealias" declaration can be used to give a name (including
1055 * pointer declarator specifier) to a type. It should also be used to
1056 * map basic C types (float, int, unsigned long, ...) to a CTF type.
1057 * Typealias is a superset of "typedef": it also allows assignment of a
1058 * simple variable identifier to a type.
1059 */
1060
1061 typealias type_class {
1062 ...
1063 } := type_specifiers type_declarator;
1064
1065 /*
1066 * e.g.:
1067 * typealias integer {
1068 * size = 32;
1069 * align = 32;
1070 * signed = false;
1071 * } := struct page *;
1072 *
1073 * typealias integer {
1074 * size = 32;
1075 * align = 32;
1076 * signed = true;
1077 * } := int;
1078 */
1079
1080 struct name {
1081 ...
1082 };
1083
1084 variant name {
1085 ...
1086 };
1087
1088 enum name : integer_type {
1089 ...
1090 };
1091
1092
1093 /*
1094 * Unnamed types, contained within compound type fields, typedef or typealias.
1095 */
1096
1097 struct {
1098 ...
1099 }
1100
1101 struct {
1102 ...
1103 } align(value)
1104
1105 variant {
1106 ...
1107 }
1108
1109 enum : integer_type {
1110 ...
1111 }
1112
1113 typedef type new_type[length];
1114
1115 struct {
1116 type field_name[length];
1117 }
1118
1119 typedef type new_type[length_type];
1120
1121 struct {
1122 type field_name[length_type];
1123 }
1124
1125 integer {
1126 ...
1127 }
1128
1129 floating_point {
1130 ...
1131 }
1132
1133 struct {
1134 integer_type field_name:size; /* GNU/C bitfield */
1135 }
1136
1137 struct {
1138 string field_name;
1139 }
1140
1141
1142 A. Helper macros
1143
1144 The two following macros keep track of the size of a GNU/C structure without
1145 padding at the end by placing HEADER_END as the last field. A one byte end field
1146 is used for C90 compatibility (C99 flexible arrays could be used here). Note
1147 that this does not affect the effective structure size, which should always be
1148 calculated with the header_sizeof() helper.
1149
1150 #define HEADER_END char end_field
1151 #define header_sizeof(type) offsetof(typeof(type), end_field)
1152
1153
1154 B. Stream Header Rationale
1155
1156 An event stream is divided in contiguous event packets of variable size. These
1157 subdivisions allow the trace analyzer to perform a fast binary search by time
1158 within the stream (typically requiring to index only the event packet headers)
1159 without reading the whole stream. These subdivisions have a variable size to
1160 eliminate the need to transfer the event packet padding when partially filled
1161 event packets must be sent when streaming a trace for live viewing/analysis.
1162 An event packet can contain a certain amount of padding at the end. Dividing
1163 streams into event packets is also useful for network streaming over UDP and
1164 flight recorder mode tracing (a whole event packet can be swapped out of the
1165 buffer atomically for reading).
1166
1167 The stream header is repeated at the beginning of each event packet to allow
1168 flexibility in terms of:
1169
1170 - streaming support,
1171 - allowing arbitrary buffers to be discarded without making the trace
1172 unreadable,
1173 - allow UDP packet loss handling by either dealing with missing event packet
1174 or asking for re-transmission.
1175 - transparently support flight recorder mode,
1176 - transparently support crash dump.
1177
1178
1179 C. TSDL Grammar
1180
1181 /*
1182 * Common Trace Format (CTF) Trace Stream Description Language (TSDL) Grammar.
1183 *
1184 * Inspired from the C99 grammar:
1185 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf (Annex A)
1186 * and c++1x grammar (draft)
1187 * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3291.pdf (Annex A)
1188 *
1189 * Specialized for CTF needs by including only constant and declarations from
1190 * C99 (excluding function declarations), and by adding support for variants,
1191 * sequences and CTF-specific specifiers. Enumeration container types
1192 * semantic is inspired from c++1x enum-base.
1193 */
1194
1195 1) Lexical grammar
1196
1197 1.1) Lexical elements
1198
1199 token:
1200 keyword
1201 identifier
1202 constant
1203 string-literal
1204 punctuator
1205
1206 1.2) Keywords
1207
1208 keyword: is one of
1209
1210 align
1211 const
1212 char
1213 double
1214 enum
1215 event
1216 floating_point
1217 float
1218 integer
1219 int
1220 long
1221 short
1222 signed
1223 stream
1224 string
1225 struct
1226 trace
1227 typealias
1228 typedef
1229 unsigned
1230 variant
1231 void
1232 _Bool
1233 _Complex
1234 _Imaginary
1235
1236
1237 1.3) Identifiers
1238
1239 identifier:
1240 identifier-nondigit
1241 identifier identifier-nondigit
1242 identifier digit
1243
1244 identifier-nondigit:
1245 nondigit
1246 universal-character-name
1247 any other implementation-defined characters
1248
1249 nondigit:
1250 _
1251 [a-zA-Z] /* regular expression */
1252
1253 digit:
1254 [0-9] /* regular expression */
1255
1256 1.4) Universal character names
1257
1258 universal-character-name:
1259 \u hex-quad
1260 \U hex-quad hex-quad
1261
1262 hex-quad:
1263 hexadecimal-digit hexadecimal-digit hexadecimal-digit hexadecimal-digit
1264
1265 1.5) Constants
1266
1267 constant:
1268 integer-constant
1269 enumeration-constant
1270 character-constant
1271
1272 integer-constant:
1273 decimal-constant integer-suffix-opt
1274 octal-constant integer-suffix-opt
1275 hexadecimal-constant integer-suffix-opt
1276
1277 decimal-constant:
1278 nonzero-digit
1279 decimal-constant digit
1280
1281 octal-constant:
1282 0
1283 octal-constant octal-digit
1284
1285 hexadecimal-constant:
1286 hexadecimal-prefix hexadecimal-digit
1287 hexadecimal-constant hexadecimal-digit
1288
1289 hexadecimal-prefix:
1290 0x
1291 0X
1292
1293 nonzero-digit:
1294 [1-9]
1295
1296 integer-suffix:
1297 unsigned-suffix long-suffix-opt
1298 unsigned-suffix long-long-suffix
1299 long-suffix unsigned-suffix-opt
1300 long-long-suffix unsigned-suffix-opt
1301
1302 unsigned-suffix:
1303 u
1304 U
1305
1306 long-suffix:
1307 l
1308 L
1309
1310 long-long-suffix:
1311 ll
1312 LL
1313
1314 enumeration-constant:
1315 identifier
1316 string-literal
1317
1318 character-constant:
1319 ' c-char-sequence '
1320 L' c-char-sequence '
1321
1322 c-char-sequence:
1323 c-char
1324 c-char-sequence c-char
1325
1326 c-char:
1327 any member of source charset except single-quote ('), backslash
1328 (\), or new-line character.
1329 escape-sequence
1330
1331 escape-sequence:
1332 simple-escape-sequence
1333 octal-escape-sequence
1334 hexadecimal-escape-sequence
1335 universal-character-name
1336
1337 simple-escape-sequence: one of
1338 \' \" \? \\ \a \b \f \n \r \t \v
1339
1340 octal-escape-sequence:
1341 \ octal-digit
1342 \ octal-digit octal-digit
1343 \ octal-digit octal-digit octal-digit
1344
1345 hexadecimal-escape-sequence:
1346 \x hexadecimal-digit
1347 hexadecimal-escape-sequence hexadecimal-digit
1348
1349 1.6) String literals
1350
1351 string-literal:
1352 " s-char-sequence-opt "
1353 L" s-char-sequence-opt "
1354
1355 s-char-sequence:
1356 s-char
1357 s-char-sequence s-char
1358
1359 s-char:
1360 any member of source charset except double-quote ("), backslash
1361 (\), or new-line character.
1362 escape-sequence
1363
1364 1.7) Punctuators
1365
1366 punctuator: one of
1367 [ ] ( ) { } . -> * + - < > : ; ... = ,
1368
1369
1370 2) Phrase structure grammar
1371
1372 primary-expression:
1373 identifier
1374 constant
1375 string-literal
1376 ( unary-expression )
1377
1378 postfix-expression:
1379 primary-expression
1380 postfix-expression [ unary-expression ]
1381 postfix-expression . identifier
1382 postfix-expressoin -> identifier
1383
1384 unary-expression:
1385 postfix-expression
1386 unary-operator postfix-expression
1387
1388 unary-operator: one of
1389 + -
1390
1391 assignment-operator:
1392 =
1393
1394 type-assignment-operator:
1395 :=
1396
1397 constant-expression-range:
1398 unary-expression ... unary-expression
1399
1400 2.2) Declarations:
1401
1402 declaration:
1403 declaration-specifiers declarator-list-opt ;
1404 ctf-specifier ;
1405
1406 declaration-specifiers:
1407 storage-class-specifier declaration-specifiers-opt
1408 type-specifier declaration-specifiers-opt
1409 type-qualifier declaration-specifiers-opt
1410
1411 declarator-list:
1412 declarator
1413 declarator-list , declarator
1414
1415 abstract-declarator-list:
1416 abstract-declarator
1417 abstract-declarator-list , abstract-declarator
1418
1419 storage-class-specifier:
1420 typedef
1421
1422 type-specifier:
1423 void
1424 char
1425 short
1426 int
1427 long
1428 float
1429 double
1430 signed
1431 unsigned
1432 _Bool
1433 _Complex
1434 _Imaginary
1435 struct-specifier
1436 variant-specifier
1437 enum-specifier
1438 typedef-name
1439 ctf-type-specifier
1440
1441 align-attribute:
1442 align ( unary-expression )
1443
1444 struct-specifier:
1445 struct identifier-opt { struct-or-variant-declaration-list-opt } align-attribute-opt
1446 struct identifier align-attribute-opt
1447
1448 struct-or-variant-declaration-list:
1449 struct-or-variant-declaration
1450 struct-or-variant-declaration-list struct-or-variant-declaration
1451
1452 struct-or-variant-declaration:
1453 specifier-qualifier-list struct-or-variant-declarator-list ;
1454 declaration-specifiers storage-class-specifier declaration-specifiers declarator-list ;
1455 typealias declaration-specifiers abstract-declarator-list := declaration-specifiers abstract-declarator-list ;
1456 typealias declaration-specifiers abstract-declarator-list := declarator-list ;
1457
1458 specifier-qualifier-list:
1459 type-specifier specifier-qualifier-list-opt
1460 type-qualifier specifier-qualifier-list-opt
1461
1462 struct-or-variant-declarator-list:
1463 struct-or-variant-declarator
1464 struct-or-variant-declarator-list , struct-or-variant-declarator
1465
1466 struct-or-variant-declarator:
1467 declarator
1468 declarator-opt : unary-expression
1469
1470 variant-specifier:
1471 variant identifier-opt variant-tag-opt { struct-or-variant-declaration-list }
1472 variant identifier variant-tag
1473
1474 variant-tag:
1475 < identifier >
1476
1477 enum-specifier:
1478 enum identifier-opt { enumerator-list }
1479 enum identifier-opt { enumerator-list , }
1480 enum identifier
1481 enum identifier-opt : declaration-specifiers { enumerator-list }
1482 enum identifier-opt : declaration-specifiers { enumerator-list , }
1483
1484 enumerator-list:
1485 enumerator
1486 enumerator-list , enumerator
1487
1488 enumerator:
1489 enumeration-constant
1490 enumeration-constant = unary-expression
1491 enumeration-constant = constant-expression-range
1492
1493 type-qualifier:
1494 const
1495
1496 declarator:
1497 pointer-opt direct-declarator
1498
1499 direct-declarator:
1500 identifier
1501 ( declarator )
1502 direct-declarator [ unary-expression ]
1503
1504 abstract-declarator:
1505 pointer-opt direct-abstract-declarator
1506
1507 direct-abstract-declarator:
1508 identifier-opt
1509 ( abstract-declarator )
1510 direct-abstract-declarator [ unary-expression ]
1511 direct-abstract-declarator [ ]
1512
1513 pointer:
1514 * type-qualifier-list-opt
1515 * type-qualifier-list-opt pointer
1516
1517 type-qualifier-list:
1518 type-qualifier
1519 type-qualifier-list type-qualifier
1520
1521 typedef-name:
1522 identifier
1523
1524 2.3) CTF-specific declarations
1525
1526 ctf-specifier:
1527 event { ctf-assignment-expression-list-opt }
1528 stream { ctf-assignment-expression-list-opt }
1529 trace { ctf-assignment-expression-list-opt }
1530 typealias declaration-specifiers abstract-declarator-list := declaration-specifiers abstract-declarator-list ;
1531 typealias declaration-specifiers abstract-declarator-list := declarator-list ;
1532
1533 ctf-type-specifier:
1534 floating_point { ctf-assignment-expression-list-opt }
1535 integer { ctf-assignment-expression-list-opt }
1536 string { ctf-assignment-expression-list-opt }
1537
1538 ctf-assignment-expression-list:
1539 ctf-assignment-expression
1540 ctf-assignment-expression-list ; ctf-assignment-expression
1541
1542 ctf-assignment-expression:
1543 unary-expression assignment-operator unary-expression
1544 unary-expression type-assignment-operator type-specifier
1545 declaration-specifiers storage-class-specifier declaration-specifiers declarator-list
1546 typealias declaration-specifiers abstract-declarator-list := declaration-specifiers abstract-declarator-list
1547 typealias declaration-specifiers abstract-declarator-list := declarator-list
This page took 0.103164 seconds and 5 git commands to generate.