Clarify that unlisted enumeration values are allowed
[ctf.git] / common-trace-format-specification.md
CommitLineData
941a19cf
PP
1# Common Trace Format (CTF) Specification (v1.8.2)
2
3**Author**: Mathieu Desnoyers, [EfficiOS Inc.](http://www.efficios.com/)
4
5The goal of the present document is to specify a trace format that suits
6the needs of the embedded, telecom, high-performance and kernel
7communities. It is based on the
8[Common Trace Format Requirements (v1.4)](http://git.efficios.com/?p=ctf.git;a=blob_plain;f=common-trace-format-reqs.txt;hb=master)
9document. It is designed to allow traces to be natively generated by the
10Linux kernel, Linux user space applications written in C/C++, and
11hardware components. One major element of CTF is the Trace Stream
12Description Language (TSDL) which flexibility enables description of
13various binary trace stream layouts.
14
15The latest version of this document can be found at:
16
17 * Git: `git clone git://git.efficios.com/ctf.git`
18 * [Gitweb](http://git.efficios.com/?p=ctf.git)
19
20A reference implementation of a library to read and write this trace
21format is being implemented within the
22[Babeltrace](http://www.efficios.com/babeltrace) project, a converter
23between trace formats. The development tree is available at:
24
25 * Git: `git clone git://git.efficios.com/babeltrace.git`
26 * [Gitweb](http://git.efficios.com/?p=babeltrace.git)
27
28The [CE Workgroup](http://www.linuxfoundation.org/collaborate/workgroups/celf)
29of the Linux Foundation, [Ericsson](http://www.ericsson.com/), and
30[EfficiOS](http://www.efficios.com/) have sponsored this work.
31
32**Contents**:
33
34 1. Preliminary definitions
35 2. High-level representation of a trace
36 3. Event stream
37 4. Types
38 4.1 Basic types
39 4.1.1 Type inheritance
40 4.1.2 Alignment
41 4.1.3 Byte order
42 4.1.4 Size
43 4.1.5 Integers
44 4.1.6 GNU/C bitfields
45 4.1.7 Floating point
46 4.1.8 Enumerations
47 4.2 Compound types
48 4.2.1 Structures
49 4.2.2 Variants (discriminated/tagged unions)
50 4.2.3 Arrays
51 4.2.4 Sequences
52 4.2.5 Strings
53 5. Event packet header
54 5.1 Event packet header description
55 5.2 Event packet context description
56 6. Event structure
57 6.1 Event header
58 6.1.1 Type 1: few event IDs
59 6.1.2 Type 2: many event IDs
60 6.2 Stream event context and event context
61 6.3 Event payload
62 6.3.1 Padding
63 6.3.2 Alignment
64 7. Trace Stream Description Language (TSDL)
65 7.1 Metadata
66 7.2 Declaration vs definition
67 7.3 TSDL scopes
68 7.3.1 Lexical scope
69 7.3.2 Static and dynamic scopes
70 7.4 TSDL examples
71 8. Clocks
72 A. Helper macros
73 B. Stream header rationale
74 C. TSDL Grammar
75 C.1 Lexical grammar
76 C.1.1 Lexical elements
77 C.1.2 Keywords
78 C.1.3 Identifiers
79 C.1.4 Universal character names
80 C.1.5 Constants
81 C.1.6 String literals
82 C.1.7 Punctuators
83 C.2 Phrase structure grammar
84 C.2.2 Declarations:
85 C.2.3 CTF-specific declarations
86
87
88## 1. Preliminary definitions
89
90 * **Event trace**: an ordered sequence of events.
91 * **Event stream**: an ordered sequence of events, containing a
92 subset of the trace event types.
93 * **Event packet**: a sequence of physically contiguous events within
94 an event stream.
95 * **Event**: this is the basic entry in a trace. Also known as
96 a _trace record_.
97 * An **event identifier** (ID) relates to the class (a type) of
98 event within an event stream, e.g. event `irq_entry`.
99 * An **event** (or event record) relates to a specific instance of
100 an event class, e.g. event `irq_entry`, at time _X_, on CPU _Y_.
101 * Source architecture: architecture writing the trace.
102 * Reader architecture: architecture reading the trace.
103
104
105## 2. High-level representation of a trace
106
107A _trace_ is divided into multiple event streams. Each event stream
108contains a subset of the trace event types.
109
110The final output of the trace, after its generation and optional
111transport over the network, is expected to be either on permanent or
112temporary storage in a virtual file system. Because each event stream
113is appended to while a trace is being recorded, each is associated with
114a distinct set of files for output. Therefore, a stored trace can be
115represented as a directory containing zero, one or more files
116per stream.
117
118Metadata description associated with the trace contains information on
119trace event types expressed in the _Trace Stream Description Language_
120(TSDL). This language describes:
121
122 * Trace version
123 * Types available
124 * Per-trace event header description
125 * Per-stream event header description
126 * Per-stream event context description
127 * Per-event
128 * Event type to stream mapping
129 * Event type to name mapping
130 * Event type to ID mapping
131 * Event context description
132 * Event fields description
133
134
135## 3. Event stream
136
137An _event stream_ can be divided into contiguous event packets of
138variable size. An event packet can contain a certain amount of padding
139at the end. The stream header is repeated at the beginning of each
140event packet. The rationale for the event stream design choices is
141explained in [Stream header rationale](#specB).
142
143The event stream header will therefore be referred to as the
144_event packet header_ throughout the rest of this document.
145
146
147## 4. Types
148
149Types are organized as type classes. Each type class belong to either
150of two kind of types: _basic types_ or _compound types_.
151
152
153### 4.1 Basic types
154
155A basic type is a scalar type, as described in this section. It
156includes integers, GNU/C bitfields, enumerations, and floating
157point values.
158
159
160#### 4.1.1 Type inheritance
161
162Type specifications can be inherited to allow deriving types from a
163type class. For example, see the uint32_t named type derived from the
164[_integer_ type](#spec4.1.5) class. Types have a precise binary
165representation in the trace. A type class has methods to read and write
166these types, but must be derived into a type to be usable in an event
167field.
168
169
170#### 4.1.2 Alignment
171
172We define _byte-packed_ types as aligned on the byte size, namely 8-bit.
173We define _bit-packed_ types as following on the next bit, as defined
174by the [Integers](#spec4.1.5) section.
175
176Each basic type must specify its alignment, in bits. Examples of
177possible alignments are: bit-packed (`align = 1`), byte-packed
178(`align = 8`), or word-aligned (e.g. `align = 32` or `align = 64`).
179The choice depends on the architecture preference and compactness vs
180performance trade-offs of the implementation. Architectures providing
181fast unaligned write byte-packed basic types to save space, aligning
182each type on byte boundaries (8-bit). Architectures with slow unaligned
183writes align types on specific alignment values. If no specific
184alignment is declared for a type, it is assumed to be bit-packed for
185integers with size not multiple of 8 bits and for gcc bitfields. All
186other basic types are byte-packed by default. It is however recommended
187to always specify the alignment explicitly. Alignment values must be
188power of two. Compound types are aligned as specified in their
189individual specification.
190
191The base offset used for field alignment is the start of the packet
192containing the field. For instance, a field aligned on 32-bit needs to
193be at an offset multiple of 32-bit from the start of the packet that
194contains it.
195
196TSDL metadata attribute representation of a specific alignment:
197
198~~~ tsdl
199align = /* value in bits */;
200~~~
201
202#### 4.1.3 Byte order
203
204By default, byte order of a basic type is the byte order described in
205the trace description. It can be overridden by specifying a
206`byte_order` attribute for a basic type. Typical use-case is to specify
207the network byte order (big endian: `be`) to save data captured from
208the network into the trace without conversion.
209
210TSDL metadata representation:
211
212~~~ tsdl
213/* network and be are aliases */
214byte_order = /* native OR network OR be OR le */;
215~~~
216
217The `native` keyword selects the byte order described in the trace
218description. The `network` byte order is an alias for big endian.
219
220Even though the trace description section is not per se a type, for
221sake of clarity, it should be noted that `native` and `network` byte
222orders are only allowed within type declaration. The `byte_order`
223specified in the trace description section only accepts `be` or `le`
224values.
225
226
227#### 4.1.4 Size
228
229Type size, in bits, for integers and floats is that returned by
230`sizeof()` in C multiplied by `CHAR_BIT`. We require the size of `char`
231and `unsigned char` types (`CHAR_BIT`) to be fixed to 8 bits for
232cross-endianness compatibility.
233
234TSDL metadata representation:
235
236~~~ tsdl
237size = /* value is in bits */;
238~~~
239
240
241#### 4.1.5 Integers
242
243Signed integers are represented in two-complement. Integer alignment,
244size, signedness and byte ordering are defined in the TSDL metadata.
245Integers aligned on byte size (8-bit) and with length multiple of byte
246size (8-bit) correspond to the C99 standard integers. In addition,
247integers with alignment and/or size that are _not_ a multiple of the
248byte size are permitted; these correspond to the C99 standard bitfields,
249with the added specification that the CTF integer bitfields have a fixed
250binary representation. Integer size needs to be a positive integer.
251Integers of size 0 are **forbidden**. An MIT-licensed reference
252implementation of the CTF portable bitfields is available
253[here](http://git.efficios.com/?p=babeltrace.git;a=blob;f=include/babeltrace/bitfield.h).
254
255Binary representation of integers:
256
257 * On little and big endian:
258 * Within a byte, high bits correspond to an integer high bits, and
259 low bits correspond to low bits
260 * On little endian:
261 * Integer across multiple bytes are placed from the less significant
262 to the most significant
263 * Consecutive integers are placed from lower bits to higher bits
264 (even within a byte)
265 * On big endian:
266 * Integer across multiple bytes are placed from the most significant
267 to the less significant
268 * Consecutive integers are placed from higher bits to lower bits
269 (even within a byte)
270
271This binary representation is derived from the bitfield implementation
272in GCC for little and big endian. However, contrary to what GCC does,
273integers can cross units boundaries (no padding is required). Padding
274can be [explicitly added](#spec4.1.6) to follow the GCC layout if needed.
275
276TSDL metadata representation:
277
278~~~ tsdl
279integer {
280 signed = /* true OR false */; /* default: false */
281 byte_order = /* native OR network OR be OR le */; /* default: native */
282 size = /* value in bits */; /* no default */
283 align = /* value in bits */;
284
285 /* base used for pretty-printing output; default: decimal */
286 base = /* decimal OR dec OR d OR i OR u OR 10 OR hexadecimal OR hex
287 OR x OR X OR p OR 16 OR octal OR oct OR o OR 8 OR binary
288 OR b OR 2 */;
289
290 /* character encoding */
291 encoding = /* none or UTF8 or ASCII */; /* default: none */
292}
293~~~
294
295Example of type inheritance (creation of a `uint32_t` named type):
296
297~~~ tsdl
298typealias integer {
299 size = 32;
300 signed = false;
301 align = 32;
302} := uint32_t;
303~~~
304
305Definition of a named 5-bit signed bitfield:
306
307~~~ tsdl
308typealias integer {
309 size = 5;
310 signed = true;
311 align = 1;
312} := int5_t;
313~~~
314
315The character encoding field can be used to specify that the integer
316must be printed as a text character when read. e.g.:
317
318~~~ tsdl
319typealias integer {
320 size = 8;
321 align = 8;
322 signed = false;
323 encoding = UTF8;
324} := utf_char;
325~~~
326
327#### 4.1.6 GNU/C bitfields
328
329The GNU/C bitfields follow closely the integer representation, with a
330particularity on alignment: if a bitfield cannot fit in the current
331unit, the unit is padded and the bitfield starts at the following unit.
332The unit size is defined by the size of the type `unit_type`.
333
334TSDL metadata representation:
335
336~~~ tsdl
337unit_type name:size;
338~~~
339
340As an example, the following structure declared in C compiled by GCC:
341
342~~~ tsdl
343struct example {
344 short a:12;
345 short b:5;
346};
347~~~
348
349The example structure is aligned on the largest element (short). The
350second bitfield would be aligned on the next unit boundary, because it
351would not fit in the current unit.
352
353
354#### 4.1.7 Floating point
355
356The floating point values byte ordering is defined in the TSDL metadata.
357
358Floating point values follow the IEEE 754-2008 standard interchange
359formats. Description of the floating point values include the exponent
360and mantissa size in bits. Some requirements are imposed on the
361floating point values:
362
363* `FLT_RADIX` must be 2.
364* `mant_dig` is the number of digits represented in the mantissa. It is
365 specified by the ISO C99 standard, section 5.2.4, as `FLT_MANT_DIG`,
366 `DBL_MANT_DIG` and `LDBL_MANT_DIG` as defined by `<float.h>`.
367* `exp_dig` is the number of digits represented in the exponent. Given
368 that `mant_dig` is one bit more than its actual size in bits (leading
369 1 is not needed) and also given that the sign bit always takes one
370 bit, `exp_dig` can be specified as:
371 * `sizeof(float) * CHAR_BIT - FLT_MANT_DIG`
372 * `sizeof(double) * CHAR_BIT - DBL_MANT_DIG`
373 * `sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG`
374
375TSDL metadata representation:
376
377~~~ tsdl
378floating_point {
379 exp_dig = /* value */;
380 mant_dig = /* value */;
381 byte_order = /* native OR network OR be OR le */;
382 align = /* value */;
383}
384~~~
385
386Example of type inheritance:
387
388~~~ tsdl
389typealias floating_point {
390 exp_dig = 8; /* sizeof(float) * CHAR_BIT - FLT_MANT_DIG */
391 mant_dig = 24; /* FLT_MANT_DIG */
392 byte_order = native;
393 align = 32;
394} := float;
395~~~
396
397TODO: define NaN, +inf, -inf behavior.
398
399Bit-packed, byte-packed or larger alignments can be used for floating
400point values, similarly to integers.
401
402
403#### 4.1.8 Enumerations
404
405Enumerations are a mapping between an integer type and a table of
406strings. The numerical representation of the enumeration follows the
407integer type specified by the metadata. The enumeration mapping table
408is detailed in the enumeration description within the metadata. The
409mapping table maps inclusive value ranges (or single values) to strings.
410Instead of being limited to simple `value -> string` mappings, these
411enumerations map `[ start_value ... end_value ] -> string`, which map
412inclusive ranges of values to strings. An enumeration from the C
413language can be represented in this format by having the same
414`start_value` and `end_value` for each mapping, which is in fact a
415range of size 1. This single-value range is supported without repeating
416the start and end values with the `value = string` declaration.
417Enumerations need to contain at least one entry.
418
419~~~ tsdl
420enum name : integer_type {
421 somestring = /* start_value1 */ ... /* end_value1 */,
422 "other string" = /* start_value2 */ ... /* end_value2 */,
423 yet_another_string, /* will be assigned to end_value2 + 1 */
424 "some other string" = /* value */,
425 /* ... */
426}
427~~~
428
429If the values are omitted, the enumeration starts at 0 and increment
430of 1 for each entry. An entry with omitted value that follows a range
431entry takes as value the `end_value` of the previous range + 1:
432
433~~~ tsdl
434enum name : unsigned int {
435 ZERO,
436 ONE,
437 TWO,
438 TEN = 10,
439 ELEVEN,
440}
441~~~
442
443Overlapping ranges within a single enumeration are implementation
444defined.
445
446A nameless enumeration can be declared as a field type or as part of
447a `typedef`:
448
449~~~ tsdl
450enum : integer_type {
451 /* ... */
452}
453~~~
454
455Enumerations omitting the container type `: integer_type` use the `int`
456type (for compatibility with C99). The `int` type _must be_ previously
457declared, e.g.:
458
459~~~ tsdl
460typealias integer { size = 32; align = 32; signed = true; } := int;
461
462enum {
463 /* ... */
464}
465~~~
466
bc21db73
MD
467An enumeration field can have an integral value for which the associated
468enumeration type does not map to a string.
469
941a19cf
PP
470### 4.2 Compound types
471
472Compound are aggregation of type declarations. Compound types include
473structures, variant, arrays, sequences, and strings.
474
475
476#### 4.2.1 Structures
477
478Structures are aligned on the largest alignment required by basic types
479contained within the structure. (This follows the ISO/C standard for
480structures)
481
482TSDL metadata representation of a named structure:
483
484~~~ tsdl
485struct name {
486 field_type field_name;
487 field_type field_name;
488 /* ... */
489};
490~~~
491
492Example:
493
494~~~ tsdl
495struct example {
496 integer { /* nameless type */
497 size = 16;
498 signed = true;
499 align = 16;
500 } first_field_name;
501 uint64_t second_field_name; /* named type declared in the metadata */
502};
503~~~
504
505The fields are placed in a sequence next to each other. They each
506possess a field name, which is a unique identifier within the structure.
507The identifier is not allowed to use any [reserved keyword](#specC.1.2).
508Replacing reserved keywords with underscore-prefixed field names is
509**recommended**. Fields starting with an underscore should have their
510leading underscore removed by the CTF trace readers.
511
512A nameless structure can be declared as a field type or as part of
513a `typedef`:
514
515~~~ tsdl
516struct {
517 /* ... */
518}
519~~~
520
521Alignment for a structure compound type can be forced to a minimum
522value by adding an `align` specifier after the declaration of a
523structure body. This attribute is read as: `align(value)`. The value is
524specified in bits. The structure will be aligned on the maximum value
525between this attribute and the alignment required by the basic types
526contained within the structure. e.g.
527
528~~~ tsdl
529struct {
530 /* ... */
531} align(32)
532~~~
533
534#### 4.2.2 Variants (discriminated/tagged unions)
535
536A CTF variant is a selection between different types. A CTF variant must
537always be defined within the scope of a structure or within fields
538contained within a structure (defined recursively). A _tag_ enumeration
539field must appear in either the same static scope, prior to the variant
540field (in field declaration order), in an upper static scope, or in an
541upper dynamic scope (see [Static and dynamic scopes](#spec7.3.2)).
542The type selection is indicated by the mapping from the enumeration
543value to the string used as variant type selector. The field to use as
544tag is specified by the `tag_field`, specified between `< >` after the
545`variant` keyword for unnamed variants, and after _variant name_ for
546named variants. It is not required that each enumeration mapping appears
547as variant type tag field. It is also not required that each variant
548type tag appears as enumeration mapping. However, it is required that
549any enumeration mapping encountered within a stream has a matching
550variant type tag field.
551
552The alignment of the variant is the alignment of the type as selected
553by the tag value for the specific instance of the variant. The size of
554the variant is the size as selected by the tag value for the specific
555instance of the variant.
556
557The alignment of the type containing the variant is independent of the
558variant alignment. For instance, if a structure contains two fields, a
55932-bit integer, aligned on 32 bits, and a variant, which contains two
560choices: either a 32-bit field, aligned on 32 bits, or a 64-bit field,
561aligned on 64 bits, the alignment of the outmost structure will be
56232-bit (the alignment of its largest field, disregarding the alignment
563of the variant). The alignment of the variant will depend on the
564selector: if the variant's 32-bit field is selected, its alignment will
565be 32-bit, or 64-bit otherwise. It is important to note that variants
566are specifically tailored for compactness in a stream. Therefore, the
567relative offsets of compound type fields can vary depending on the
568offset at which the compound type starts if it contains a variant
569that itself contains a type with alignment larger than the largest field
570contained within the compound type. This is caused by the fact that the
571compound type may contain the enumeration that select the variant's
572choice, and therefore the alignment to be applied to the compound type
573cannot be determined before encountering the enumeration.
574
575Each variant type selector possess a field name, which is a unique
576identifier within the variant. The identifier is not allowed to use any
577[reserved keyword](#C.1.2). Replacing reserved keywords with
578underscore-prefixed field names is recommended. Fields starting with an
579underscore should have their leading underscore removed by the CTF trace
580readers.
581
582A named variant declaration followed by its definition within a
583structure declaration:
584
585~~~ tsdl
586variant name {
587 field_type sel1;
588 field_type sel2;
589 field_type sel3;
590 /* ... */
591};
592
593struct {
594 enum : integer_type { sel1, sel2, sel3, /* ... */ } tag_field;
595 /* ... */
596 variant name <tag_field> v;
597}
598~~~
599
600An unnamed variant definition within a structure is expressed by the
601following TSDL metadata:
602
603~~~ tsdl
604struct {
605 enum : integer_type { sel1, sel2, sel3, /* ... */ } tag_field;
606 /* ... */
607 variant <tag_field> {
608 field_type sel1;
609 field_type sel2;
610 field_type sel3;
611 /* ... */
612 } v;
613}
614~~~
615
616Example of a named variant within a sequence that refers to a single
617tag field:
618
619~~~ tsdl
620variant example {
621 uint32_t a;
622 uint64_t b;
623 short c;
624};
625
626struct {
627 enum : uint2_t { a, b, c } choice;
628 unsigned int seqlen;
629 variant example <choice> v[seqlen];
630}
631~~~
632
633Example of an unnamed variant:
634
635~~~ tsdl
636struct {
637 enum : uint2_t { a, b, c, d } choice;
638
639 /* Unrelated fields can be added between the variant and its tag */
640 int32_t somevalue;
641 variant <choice> {
642 uint32_t a;
643 uint64_t b;
644 short c;
645 struct {
646 unsigned int field1;
647 uint64_t field2;
648 } d;
649 } s;
650}
651~~~
652
653Example of an unnamed variant within an array:
654
655~~~ tsdl
656struct {
657 enum : uint2_t { a, b, c } choice;
658 variant <choice> {
659 uint32_t a;
660 uint64_t b;
661 short c;
662 } v[10];
663}
664~~~
665
666Example of a variant type definition within a structure, where the
667defined type is then declared within an array of structures. This
668variant refers to a tag located in an upper static scope. This example
669clearly shows that a variant type definition referring to the tag `x`
670uses the closest preceding field from the static scope of the type
671definition.
672
673~~~ tsdl
674struct {
675 enum : uint2_t { a, b, c, d } x;
676
677 /*
678 * "x" refers to the preceding "x" enumeration in the
679 * static scope of the type definition.
680 */
681 typedef variant <x> {
682 uint32_t a;
683 uint64_t b;
684 short c;
685 } example_variant;
686
687 struct {
688 enum : int { x, y, z } x; /* This enumeration is not used by "v". */
689
690 /* "v" uses the "enum : uint2_t { a, b, c, d }" tag. */
691 example_variant v;
692 } a[10];
693}
694~~~
695
696
697#### 4.2.3 Arrays
698
699Arrays are fixed-length. Their length is declared in the type
700declaration within the metadata. They contain an array of _inner type_
701elements, which can refer to any type not containing the type of the
702array being declared (no circular dependency). The length is the number
703of elements in an array.
704
705TSDL metadata representation of a named array:
706
707~~~ tsdl
708typedef elem_type name[/* length */];
709~~~
710
711A nameless array can be declared as a field type within a
712structure, e.g.:
713
714~~~ tsdl
715uint8_t field_name[10];
716~~~
717
718Arrays are always aligned on their element alignment requirement.
719
720
721#### 4.2.4 Sequences
722
723Sequences are dynamically-sized arrays. They refer to a _length_
724unsigned integer field, which must appear in either the same static
725scope, prior to the sequence field (in field declaration order),
726in an upper static scope, or in an upper dynamic scope
727(see [Static and dynamic scopes](#spec7.3.2)). This length field represents
728the number of elements in the sequence. The sequence per se is an
729array of _inner type_ elements.
730
731TSDL metadata representation for a sequence type definition:
732
733~~~ tsdl
734struct {
735 unsigned int length_field;
736 typedef elem_type typename[length_field];
737 typename seq_field_name;
738}
739~~~
740
741A sequence can also be declared as a field type, e.g.:
742
743~~~ tsdl
744struct {
745 unsigned int length_field;
746 long seq_field_name[length_field];
747}
748~~~
749
750Multiple sequences can refer to the same length field, and these length
751fields can be in a different upper dynamic scope, e.g., assuming the
752`stream.event.header` defines:
753
754~~~ tsdl
755stream {
756 /* ... */
757 id = 1;
758 event.header := struct {
759 uint16_t seq_len;
760 };
761};
762
763event {
764 /* ... */
765 stream_id = 1;
766 fields := struct {
767 long seq_a[stream.event.header.seq_len];
768 char seq_b[stream.event.header.seq_len];
769 };
770};
771~~~
772
773The sequence elements follow the [array](#spec4.2.3) specifications.
774
775
776#### 4.2.5 Strings
777
778Strings are an array of _bytes_ of variable size and are terminated by
779a `'\0'` "NULL" character. Their encoding is described in the TSDL
780metadata. In absence of encoding attribute information, the default
781encoding is UTF-8.
782
783TSDL metadata representation of a named string type:
784
785~~~ tsdl
786typealias string {
787 encoding = /* UTF8 OR ASCII */;
788} := name;
789~~~
790
791A nameless string type can be declared as a field type:
792
793~~~ tsdl
794string field_name; /* use default UTF8 encoding */
795~~~
796
797Strings are always aligned on byte size.
798
799
800## 5. Event packet header
801
802The event packet header consists of two parts: the
803_event packet header_ is the same for all streams of a trace. The
804second part, the _event packet context_, is described on a per-stream
805basis. Both are described in the TSDL metadata.
806
807Event packet header (all fields are optional, specified by
808TSDL metadata):
809
810 * **Magic number** (CTF magic number: 0xC1FC1FC1) specifies that this is
811 a CTF packet. This magic number is optional, but when present, it
812 should come at the very beginning of the packet.
813 * **Trace UUID**, used to ensure the event packet match the metadata used.
814 Note: we cannot use a metadata checksum in every cases instead of a
815 UUID because metadata can be appended to while tracing is active.
816 This field is optional.
817 * **Stream ID**, used as reference to stream description in metadata.
818 This field is optional if there is only one stream description in
819 the metadata, but becomes required if there are more than one
820 stream in the TSDL metadata description.
821
822Event packet context (all fields are optional, specified by
823TSDL metadata):
824
825 * Event packet **content size** (in bits).
826 * Event packet **size** (in bits, includes padding).
827 * Event packet content checksum. Checksum excludes the event packet
828 header.
829 * Per-stream event **packet sequence count** (to deal with UDP packet
830 loss). The number of significant sequence counter bits should also
831 be present, so wrap-arounds are dealt with correctly.
832 * Time-stamp at the beginning and timestamp at the end of the event
833 packet. Both timestamps are written in the packet header, but
834 sampled respectively while (or before) writing the first event and
835 while (or after) writing the last event in the packet. The inclusive
836 range between these timestamps should include all event timestamps
837 assigned to events contained within the packet. The timestamp at the
838 beginning of an event packet is guaranteed to be below or equal the
839 timestamp at the end of that event packet. The timestamp at the end
840 of an event packet is guaranteed to be below or equal the
841 timestamps at the end of any following packet within the same stream.
842 See [Clocks](#spec8) for more detail.
843 * **Events discarded count**. Snapshot of a per-stream
844 free-running counter, counting the number of events discarded that
845 were supposed to be written in the stream after the last event in
846 the event packet. Note: producer-consumer buffer full condition can
847 fill the current event packet with padding so we know exactly where
848 events have been discarded. However, if the buffer full condition
849 chooses not to fill the current event packet with padding, all we
850 know about the timestamp range in which the events have been
851 discarded is that it is somewhere between the beginning and the end
852 of the packet.
853 * Lossless **compression scheme** used for the event packet content.
854 Applied directly to raw data. New types of compression can be added
855 in following versions of the format.
856 * 0: no compression scheme
857 * 1: bzip2
858 * 2: gzip
859 * 3: xz
860 * **Cypher** used for the event packet content. Applied after
861 compression.
862 * 0: no encryption
863 * 1: AES
864 * **Checksum scheme** used for the event packet content. Applied after
865 encryption.
866 * 0: no checksum
867 * 1: md5
868 * 2: sha1
869 * 3: crc32
870
871
872### 5.1 Event packet header description
873
874The event packet header layout is indicated by the
875`trace.packet.header` field. Here is a recommended structure type for
876the packet header with the fields typically expected (although these
877fields are each optional):
878
879~~~ tsdl
880struct event_packet_header {
881 uint32_t magic;
882 uint8_t uuid[16];
883 uint32_t stream_id;
884};
885
886trace {
887 /* ... */
888 packet.header := struct event_packet_header;
889};
890~~~
891
9ab09151
PP
892If the magic number (`magic` field) is not present,
893tools such as `file` will have no mean to discover the file type.
941a19cf 894
9ab09151
PP
895If the `uuid` field is not present, no validation that the metadata
896actually corresponds to the stream is performed.
941a19cf 897
9ab09151 898If the `stream_id` packet header field is missing, the trace can only
941a19cf
PP
899contain a single stream. Its `id` field can be left out, and its events
900don't need to declare a `stream_id` field.
901
902
903### 5.2 Event packet context description
904
905Event packet context example. These are declared within the stream
906declaration in the metadata. All these fields are optional. If the
907packet size field is missing, the whole stream only contains a single
908packet. If the content size field is missing, the packet is filled
909(no padding). The content and packet sizes include all headers.
910
911An example event packet context type:
912
913~~~ tsdl
914struct event_packet_context {
915 uint64_t timestamp_begin;
916 uint64_t timestamp_end;
917 uint32_t checksum;
918 uint32_t stream_packet_count;
919 uint32_t events_discarded;
920 uint32_t cpu_id;
921 uint64_t content_size;
922 uint64_t packet_size;
923 uint8_t compression_scheme;
924 uint8_t encryption_scheme;
925 uint8_t checksum_scheme;
926};
927~~~
928
929
930## 6. Event Structure
931
932The overall structure of an event is:
933
934 1. Event header (as specified by the stream metadata)
935 2. Stream event context (as specified by the stream metadata)
936 3. Event context (as specified by the event metadata)
937 4. Event payload (as specified by the event metadata)
938
939This structure defines an implicit dynamic scoping, where variants
940located in inner structures (those with a higher number in the listing
941above) can refer to the fields of outer structures (with lower number
942in the listing above). See [TSDL scopes](#spec7.3) for more detail.
943
944The total length of an event is defined as the difference between the
945end of its event payload and the end of the previous event's event
946payload. Therefore, it includes the event header alignment padding, and
947all its fields and their respective alignment padding. Events of length
9480 are forbidden.
949
950
951### 6.1 Event header
952
953Event headers can be described within the metadata. We hereby propose,
954as an example, two types of events headers. Type 1 accommodates streams
955with less than 31 event IDs. Type 2 accommodates streams with 31 or
956more event IDs.
957
958One major factor can vary between streams: the number of event IDs
959assigned to a stream. Luckily, this information tends to stay
960relatively constant (modulo event registration while trace is being
961recorded), so we can specify different representations for streams
962containing few event IDs and streams containing many event IDs, so we
963end up representing the event ID and timestamp as densely as possible
964in each case.
965
966The header is extended in the rare occasions where the information
967cannot be represented in the ranges available in the standard event
968header. They are also used in the rare occasions where the data
969required for a field could not be collected: the flag corresponding to
970the missing field within the `missing_fields` array is then set to 1.
971
972Types `uintX_t` represent an `X`-bit unsigned integer, as declared with
973either:
974
975~~~ tsdl
976typealias integer {
977 size = /* X */;
978 align = /* X */;
979 signed = false;
980} := uintX_t;
981~~~
982
983or
984
985~~~ tsdl
986typealias integer {
987 size = /* X */;
988 align = 1;
989 signed = false;
990} := uintX_t;
991~~~
992
993For more information about timestamp fields, see [Clocks](#spec8).
994
995
996#### 6.1.1 Type 1: few event IDs
997
998 * Aligned on 32-bit (or 8-bit if byte-packed, depending on the
999 architecture preference)
1000 * Native architecture byte ordering
1001 * For `compact` selection, fixed size of 32 bits
1002 * For "extended" selection, size depends on the architecture and
1003 variant alignment
1004
1005~~~ tsdl
1006struct event_header_1 {
1007 /*
1008 * id: range: 0 - 30.
1009 * id 31 is reserved to indicate an extended header.
1010 */
1011 enum : uint5_t { compact = 0 ... 30, extended = 31 } id;
1012 variant <id> {
1013 struct {
1014 uint27_t timestamp;
1015 } compact;
1016 struct {
1017 uint32_t id; /* 32-bit event IDs */
1018 uint64_t timestamp; /* 64-bit timestamps */
1019 } extended;
1020 } v;
1021} align(32); /* or align(8) */
1022~~~
1023
1024
1025#### 6.1.2 Type 2: many event IDs
1026
1027 * Aligned on 16-bit (or 8-bit if byte-packed, depending on the
1028 architecture preference)
1029 * Native architecture byte ordering
1030 * For `compact` selection, size depends on the architecture and
1031 variant alignment
1032 * For `extended` selection, size depends on the architecture and
1033 variant alignment
1034
1035~~~ tsdl
1036struct event_header_2 {
1037 /*
1038 * id: range: 0 - 65534.
1039 * id 65535 is reserved to indicate an extended header.
1040 */
1041 enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;
1042 variant <id> {
1043 struct {
1044 uint32_t timestamp;
1045 } compact;
1046 struct {
1047 uint32_t id; /* 32-bit event IDs */
1048 uint64_t timestamp; /* 64-bit timestamps */
1049 } extended;
1050 } v;
1051} align(16); /* or align(8) */
1052~~~
1053
1054
1055### 6.2 Stream event context and event context
1056
1057The event context contains information relative to the current event.
1058The choice and meaning of this information is specified by the TSDL
1059stream and event metadata descriptions. The stream context is applied
1060to all events within the stream. The stream context structure follows
1061the event header. The event context is applied to specific events. Its
1062structure follows the stream context structure.
1063
1064An example of stream-level event context is to save the event payload
1065size with each event, or to save the current PID with each event.
1066These are declared within the stream declaration within the metadata:
1067
1068~~~ tsdl
1069stream {
1070 /* ... */
1071 event.context := struct {
1072 uint pid;
1073 uint16_t payload_size;
1074 };
1075};
1076~~~
1077
1078An example of event-specific event context is to declare a bitmap of
1079missing fields, only appended after the stream event context if the
1080extended event header is selected. `NR_FIELDS` is the number of fields
1081within the event (a numeric value).
1082
1083~~~ tsdl
1084event {
1085 context := struct {
1086 variant <id> {
1087 struct { } compact;
1088 struct {
1089 /* missing event fields bitmap */
1090 uint1_t missing_fields[NR_FIELDS];
1091 } extended;
1092 } v;
1093 };
1094 /* ... */
1095}
1096~~~
1097
1098
1099### 6.3 Event payload
1100
1101An event payload contains fields specific to a given event type. The
1102fields belonging to an event type are described in the event-specific
1103metadata within a structure type.
1104
1105
1106#### 6.3.1 Padding
1107
1108No padding at the end of the event payload. This differs from the ISO/C
1109standard for structures, but follows the CTF standard for structures.
1110In a trace, even though it makes sense to align the beginning of a
1111structure, it really makes no sense to add padding at the end of the
1112structure, because structures are usually not followed by a structure
1113of the same type.
1114
1115This trick can be done by adding a zero-length `end` field at the end
1116of the C structures, and by using the offset of this field rather than
1117using `sizeof()` when calculating the size of a structure
1118(see [Helper macros](#specA)).
1119
1120
1121#### 6.3.2 Alignment
1122
1123The event payload is aligned on the largest alignment required by types
1124contained within the payload. This follows the ISO/C standard for
1125structures.
1126
1127
1128## 7. Trace Stream Description Language (TSDL)
1129
1130The Trace Stream Description Language (TSDL) allows expression of the
1131binary trace streams layout in a C99-like Domain Specific Language
1132(DSL).
1133
1134
1135### 7.1 Meta-data
1136
1137The trace stream layout description is located in the trace metadata.
1138The metadata is itself located in a stream identified by its name:
1139`metadata`.
1140
1141The metadata description can be expressed in two different formats:
1142text-only and packet-based. The text-only description facilitates
1143generation of metadata and provides a convenient way to enter the
1144metadata information by hand. The packet-based metadata provides the
1145CTF stream packet facilities (checksumming, compression, encryption,
1146network-readiness) for metadata stream generated and transported by a
1147tracer.
1148
1149The text-only metadata file is a plain-text TSDL description. This file
1150must begin with the following characters to identify the file as a CTF
1151TSDL text-based metadata file (without the double-quotes):
1152
1153~~~ text
1154"/* CTF"
1155~~~
1156
1157It must be followed by a space, and the version of the specification
1158followed by the CTF trace, e.g.:
1159
1160~~~ text
1161" 1.8"
1162~~~
1163
1164These characters allow automated discovery of file type and CTF
1165specification version. They are interpreted as a the beginning of a
1166comment by the TSDL metadata parser. The comment can be continued to
1167contain extra commented characters before it is closed.
1168
1169The packet-based metadata is made of _metadata packets_, which each
1170start with a metadata packet header. The packet-based metadata
1171description is detected by reading the magic number 0x75D11D57 at the
1172beginning of the file. This magic number is also used to detect the
1173endianness of the architecture by trying to read the CTF magic number
1174and its counterpart in reversed endianness. The events within the
1175metadata stream have no event header nor event context. Each event only
1176contains a special _sequence_ payload, which is a sequence of bits which
1177length is implicitly calculated by using the
1178`trace.packet.header.content_size` field, minus the packet header size.
1179The formatting of this sequence of bits is a plain-text representation
1180of the TSDL description. Each metadata packet start with a special
1181packet header, specific to the metadata stream, which contains,
1182exactly:
1183
1184~~~ tsdl
1185struct metadata_packet_header {
1186 uint32_t magic; /* 0x75D11D57 */
1187 uint8_t uuid[16]; /* Unique Universal Identifier */
1188 uint32_t checksum; /* 0 if unused */
1189 uint32_t content_size; /* in bits */
1190 uint32_t packet_size; /* in bits */
1191 uint8_t compression_scheme; /* 0 if unused */
1192 uint8_t encryption_scheme; /* 0 if unused */
1193 uint8_t checksum_scheme; /* 0 if unused */
1194 uint8_t major; /* CTF spec version major number */
1195 uint8_t minor; /* CTF spec version minor number */
1196};
1197~~~
1198
1199The packet-based metadata can be converted to a text-only metadata by
1200concatenating all the strings it contains.
1201
1202In the textual representation of the metadata, the text contained
1203within `/*` and `*/`, as well as within `//` and end of line, are
1204treated as comments. Boolean values can be represented as `true`,
1205`TRUE`, or `1` for true, and `false`, `FALSE`, or `0` for false. Within
1206the string-based metadata description, the trace UUID is represented as
1207a string of hexadecimal digits and dashes `-`. In the event packet
1208header, the trace UUID is represented as an array of bytes.
1209
1210
1211### 7.2 Declaration vs definition
1212
1213A declaration associates a layout to a type, without specifying where
1214this type is located in the event [structure hierarchy](#spec6).
1215This therefore includes `typedef`, `typealias`, as well as all type
1216specifiers. In certain circumstances (`typedef`, structure field and
1217variant field), a declaration is followed by a declarator, which specify
1218the newly defined type name (for `typedef`), or the field name (for
1219declarations located within structure and variants). Array and sequence,
1220declared with square brackets (`[` `]`), are part of the declarator,
1221similarly to C99. The enumeration base type is specified by
1222`: enum_base`, which is part of the type specifier. The variant tag
1223name, specified between `<` `>`, is also part of the type specifier.
1224
1225A definition associates a type to a location in the event
1226[structure hierarchy](#spec6). This association is denoted by `:=`,
1227as shown in [TSDL scopes](#spec7.3).
1228
1229
1230### 7.3 TSDL scopes
1231
1232TSDL uses three different types of scoping: a lexical scope is used for
1233declarations and type definitions, and static and dynamic scopes are
1234used for variants references to tag fields (with relative and absolute
1235path lookups) and for sequence references to length fields.
1236
1237
1238#### 7.3.1 Lexical Scope
1239
1240Each of `trace`, `env`, `stream`, `event`, `struct` and `variant` have
1241their own nestable declaration scope, within which types can be declared
1242using `typedef` and `typealias`. A root declaration scope also contains
1243all declarations located outside of any of the aforementioned
1244declarations. An inner declaration scope can refer to type declared
1245within its container lexical scope prior to the inner declaration scope.
1246Redefinition of a typedef or typealias is not valid, although hiding an
1247upper scope typedef or typealias is allowed within a sub-scope.
1248
1249
1250#### 7.3.2 Static and dynamic scopes
1251
1252A local static scope consists in the scope generated by the declaration
1253of fields within a compound type. A static scope is a local static scope
1254augmented with the nested sub-static-scopes it contains.
1255
1256A dynamic scope consists in the static scope augmented with the
1257implicit [event structure](#spec6) definition hierarchy.
1258
1259Multiple declarations of the same field name within a local static scope
1260is not valid. It is however valid to re-use the same field name in
1261different local scopes.
1262
1263Nested static and dynamic scopes form lookup paths. These are used for
1264variant tag and sequence length references. They are used at the variant
1265and sequence definition site to look up the location of the tag field
1266associated with a variant, and to lookup up the location of the length
1267field associated with a sequence.
1268
1269Variants and sequences can refer to a tag field either using a relative
1270path or an absolute path. The relative path is relative to the scope in
1271which the variant or sequence performing the lookup is located.
1272Relative paths are only allowed to lookup within the same static scope,
1273which includes its nested static scopes. Lookups targeting parent static
1274scopes need to be performed with an absolute path.
1275
1276Absolute path lookups use the full path including the dynamic scope
1277followed by a `.` and then the static scope. Therefore, variants (or
1278sequences) in lower levels in the dynamic scope (e.g., event context)
1279can refer to a tag (or length) field located in upper levels
1280(e.g., in the event header) by specifying, in this case, the associated
1281tag with `<stream.event.header.field_name>`. This allows, for instance,
1282the event context to define a variant referring to the `id` field of
1283the event header as selector.
1284
1285The dynamic scope prefixes are thus:
1286
1287 * Trace environment: `<env. >`
1288 * Trace packet header: `<trace.packet.header. >`
1289 * Stream packet context: `<stream.packet.context. >`
1290 * Event header: `<stream.event.header. >`
1291 * Stream event context: `<stream.event.context. >`
1292 * Event context: `<event.context. >`
1293 * Event payload: `<event.fields. >`
1294
1295The target dynamic scope must be specified explicitly when referring to
1296a field outside of the static scope (absolute scope reference). No
1297conflict can occur between relative and dynamic paths, because the
1298keywords `trace`, `stream`, and `event` are reserved, and thus not
1299permitted as field names. It is recommended that field names clashing
1300with CTF and C99 reserved keywords use an underscore prefix to
1301eliminate the risk of generating a description containing an invalid
1302field name. Consequently, fields starting with an underscore should have
1303their leading underscore removed by the CTF trace readers.
1304
1305The information available in the dynamic scopes can be thought of as the
1306current tracing context. At trace production, information about the
1307current context is saved into the specified scope field levels. At trace
1308consumption, for each event, the current trace context is therefore
1309readable by accessing the upper dynamic scopes.
1310
1311
1312### 7.4 TSDL examples
1313
1314The grammar representing the TSDL metadata is presented in
1315[TSDL grammar](#specC). This section presents a rather lighter reading that
1316consists in examples of TSDL metadata, with template values.
1317
1318The stream ID can be left out if there is only one stream in the
1319trace. The event `id` field can be left out if there is only one event
1320in a stream.
1321
1322~~~ tsdl
1323trace {
1324 major = /* value */; /* CTF spec version major number */
1325 minor = /* value */; /* CTF spec version minor number */
1326 uuid = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"; /* Trace UUID */
1327 byte_order = /* be OR le */; /* Endianness (required) */
1328 packet.header := struct {
1329 uint32_t magic;
1330 uint8_t uuid[16];
1331 uint32_t stream_id;
1332 };
1333};
1334
1335/*
1336 * The "env" (environment) scope contains assignment expressions. The
1337 * field names and content are implementation-defined.
1338 */
1339env {
1340 pid = /* value */; /* example */
1341 proc_name = "name"; /* example */
1342 /* ... */
1343};
1344
1345stream {
1346 id = /* stream_id */;
1347 /* Type 1 - Few event IDs; Type 2 - Many event IDs. See section 6.1. */
1348 event.header := /* event_header_1 OR event_header_2 */;
1349 event.context := struct {
1350 /* ... */
1351 };
1352 packet.context := struct {
1353 /* ... */
1354 };
1355};
1356
1357event {
1358 name = "event_name";
1359 id = /* value */; /* Numeric identifier within the stream */
1360 stream_id = /* stream_id */;
1361 loglevel = /* value */;
1362 model.emf.uri = "string";
1363 context := struct {
1364 /* ... */
1365 };
1366 fields := struct {
1367 /* ... */
1368 };
1369};
1370
1371callsite {
1372 name = "event_name";
1373 func = "func_name";
1374 file = "myfile.c";
1375 line = 39;
1376 ip = 0x40096c;
1377};
1378~~~
1379
1380More detail on [types](#spec4):
1381
1382~~~ tsdl
1383/*
1384 * Named types:
1385 *
1386 * Type declarations behave similarly to the C standard.
1387 */
1388
1389typedef aliased_type_specifiers new_type_declarators;
1390
1391/* e.g.: typedef struct example new_type_name[10]; */
1392
1393/*
1394 * typealias
1395 *
1396 * The "typealias" declaration can be used to give a name (including
1397 * pointer declarator specifier) to a type. It should also be used to
1398 * map basic C types (float, int, unsigned long, ...) to a CTF type.
1399 * Typealias is a superset of "typedef": it also allows assignment of a
1400 * simple variable identifier to a type.
1401 */
1402
1403typealias type_class {
1404 /* ... */
1405} := type_specifiers type_declarator;
1406
1407/*
1408 * e.g.:
1409 * typealias integer {
1410 * size = 32;
1411 * align = 32;
1412 * signed = false;
1413 * } := struct page *;
1414 *
1415 * typealias integer {
1416 * size = 32;
1417 * align = 32;
1418 * signed = true;
1419 * } := int;
1420 */
1421
1422struct name {
1423 /* ... */
1424};
1425
1426variant name {
1427 /* ... */
1428};
1429
1430enum name : integer_type {
1431 /* ... */
1432};
1433~~~
1434
1435Unnamed types, contained within compound type fields, `typedef` or
1436`typealias`:
1437
1438~~~ tsdl
1439struct {
1440 /* ... */
1441}
1442~~~
1443
1444~~~ tsdl
1445struct {
1446 /* ... */
1447} align(value)
1448~~~
1449
1450~~~ tsdl
1451variant {
1452 /* ... */
1453}
1454~~~
1455
1456~~~ tsdl
1457enum : integer_type {
1458 /* ... */
1459}
1460~~~
1461
1462~~~ tsdl
1463typedef type new_type[length];
1464
1465struct {
1466 type field_name[length];
1467}
1468~~~
1469
1470~~~ tsdl
1471typedef type new_type[length_type];
1472
1473struct {
1474 type field_name[length_type];
1475}
1476~~~
1477
1478~~~ tsdl
1479integer {
1480 /* ... */
1481}
1482~~~
1483
1484~~~ tsdl
1485floating_point {
1486 /* ... */
1487}
1488~~~
1489
1490~~~ tsdl
1491struct {
1492 integer_type field_name:size; /* GNU/C bitfield */
1493}
1494~~~
1495
1496~~~ tsdl
1497struct {
1498 string field_name;
1499}
1500~~~
1501
1502
1503## 8. Clocks
1504
1505Clock metadata allows to describe the clock topology of the system, as
1506well as to detail each clock parameter. In absence of clock description,
1507it is assumed that all fields named `timestamp` use the same clock
1508source, which increments once per nanosecond.
1509
1510Describing a clock and how it is used by streams is threefold: first,
1511the clock and clock topology should be described in a `clock`
1512description block, e.g.:
1513
1514~~~ tsdl
1515clock {
1516 name = cycle_counter_sync;
1517 uuid = "62189bee-96dc-11e0-91a8-cfa3d89f3923";
1518 description = "Cycle counter synchronized across CPUs";
1519 freq = 1000000000; /* frequency, in Hz */
1520 /* precision in seconds is: 1000 * (1/freq) */
1521 precision = 1000;
1522 /*
1523 * clock value offset from Epoch is:
1524 * offset_s + (offset * (1/freq))
1525 */
1526 offset_s = 1326476837;
1527 offset = 897235420;
1528 absolute = FALSE;
1529};
1530~~~
1531
1532The mandatory `name` field specifies the name of the clock identifier,
1533which can later be used as a reference. The optional field `uuid` is
1534the unique identifier of the clock. It can be used to correlate
1535different traces that use the same clock. An optional textual
1536description string can be added with the `description` field. The
1537`freq` field is the initial frequency of the clock, in Hz. If the
1538`freq` field is not present, the frequency is assumed to be 1000000000
1539(providing clock increment of 1 ns). The optional `precision` field
1540details the uncertainty on the clock measurements, in (1/freq) units.
1541The `offset_s` and `offset` fields indicate the offset from
1542POSIX.1 Epoch, 1970-01-01 00:00:00 +0000 (UTC), to the zero of value
1543of the clock. The `offset_s` field is in seconds. The `offset` field is
1544in (1/freq) units. If any of the `offset_s` or `offset` field is not
1545present, it is assigned the 0 value. The field `absolute` is `TRUE` if
1546the clock is a global reference across different clock UUID
1547(e.g. NTP time). Otherwise, `absolute` is `FALSE`, and the clock can
1548be considered as synchronized only with other clocks that have the same
1549UUID.
1550
1551Secondly, a reference to this clock should be added within an integer
1552type:
1553
1554~~~ tsdl
1555typealias integer {
1556 size = 64; align = 1; signed = false;
1557 map = clock.cycle_counter_sync.value;
1558} := uint64_ccnt_t;
1559~~~
1560
1561Thirdly, stream declarations can reference the clock they use as a
1562timestamp source:
1563
1564~~~ tsdl
1565struct packet_context {
1566 uint64_ccnt_t ccnt_begin;
1567 uint64_ccnt_t ccnt_end;
1568 /* ... */
1569};
1570
1571stream {
1572 /* ... */
1573 event.header := struct {
1574 uint64_ccnt_t timestamp;
1575 /* ... */
1576 };
1577 packet.context := struct packet_context;
1578};
1579~~~
1580
1581For a N-bit integer type referring to a clock, if the integer overflows
1582compared to the N low order bits of the clock prior value found in the
1583same stream, then it is assumed that one, and only one, overflow
1584occurred. It is therefore important that events encoding time on a small
1585number of bits happen frequently enough to detect when more than one
1586N-bit overflow occurs.
1587
1588In a packet context, clock field names ending with `_begin` and `_end`
1589have a special meaning: this refers to the timestamps at, respectively,
1590the beginning and the end of each packet.
1591
1592
1593## A. Helper macros
1594
1595The two following macros keep track of the size of a GNU/C structure
1596without padding at the end by placing HEADER_END as the last field.
1597A one byte end field is used for C90 compatibility (C99 flexible arrays
1598could be used here). Note that this does not affect the effective
1599structure size, which should always be calculated with the
1600`header_sizeof()` helper.
1601
1602~~~ c
1603#define HEADER_END char end_field
1604#define header_sizeof(type) offsetof(typeof(type), end_field)
1605~~~
1606
1607## B. Stream header rationale
1608
1609An event stream is divided in contiguous event packets of variable
1610size. These subdivisions allow the trace analyzer to perform a fast
1611binary search by time within the stream (typically requiring to index
1612only the event packet headers) without reading the whole stream. These
1613subdivisions have a variable size to eliminate the need to transfer the
1614event packet padding when partially filled event packets must be sent
1615when streaming a trace for live viewing/analysis. An event packet can
1616contain a certain amount of padding at the end. Dividing streams into
1617event packets is also useful for network streaming over UDP and flight
1618recorder mode tracing (a whole event packet can be swapped out of the
1619buffer atomically for reading).
1620
1621The stream header is repeated at the beginning of each event packet to
1622allow flexibility in terms of:
1623
1624 * streaming support
1625 * allowing arbitrary buffers to be discarded without making the trace
1626 unreadable
1627 * allow UDP packet loss handling by either dealing with missing event packet
1628 or asking for re-transmission
1629 * transparently support flight recorder mode
1630 * transparently support crash dump
1631
1632
1633## C. TSDL Grammar
1634
1635~~~ c
1636/*
1637 * Common Trace Format (CTF) Trace Stream Description Language (TSDL) Grammar.
1638 *
1639 * Inspired from the C99 grammar:
1640 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf (Annex A)
1641 * and c++1x grammar (draft)
1642 * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3291.pdf (Annex A)
1643 *
1644 * Specialized for CTF needs by including only constant and declarations from
1645 * C99 (excluding function declarations), and by adding support for variants,
1646 * sequences and CTF-specific specifiers. Enumeration container types
1647 * semantic is inspired from c++1x enum-base.
1648 */
1649~~~
1650
1651
1652### C.1 Lexical grammar
1653
1654
1655#### C.1.1 Lexical elements
1656
1657~~~ text
1658token:
1659 keyword
1660 identifier
1661 constant
1662 string-literal
1663 punctuator
1664~~~
1665
1666#### C.1.2 Keywords
1667
1668~~~ text
1669keyword: is one of
1670
1671align
1672callsite
1673const
1674char
1675clock
1676double
1677enum
1678env
1679event
1680floating_point
1681float
1682integer
1683int
1684long
1685short
1686signed
1687stream
1688string
1689struct
1690trace
1691typealias
1692typedef
1693unsigned
1694variant
1695void
1696_Bool
1697_Complex
1698_Imaginary
1699~~~
1700
1701
1702#### C.1.3 Identifiers
1703
1704~~~ text
1705identifier:
1706 identifier-nondigit
1707 identifier identifier-nondigit
1708 identifier digit
1709
1710identifier-nondigit:
1711 nondigit
1712 universal-character-name
1713 any other implementation-defined characters
1714
1715nondigit:
1716 _
1717 [a-zA-Z] /* regular expression */
1718
1719digit:
1720 [0-9] /* regular expression */
1721~~~
1722
1723
1724#### C.1.4 Universal character names
1725
1726~~~ text
1727universal-character-name:
1728 \u hex-quad
1729 \U hex-quad hex-quad
1730
1731hex-quad:
1732 hexadecimal-digit hexadecimal-digit hexadecimal-digit hexadecimal-digit
1733~~~
1734
1735
1736##### C.1.5 Constants
1737
1738~~~ text
1739constant:
1740 integer-constant
1741 enumeration-constant
1742 character-constant
1743
1744integer-constant:
1745 decimal-constant integer-suffix-opt
1746 octal-constant integer-suffix-opt
1747 hexadecimal-constant integer-suffix-opt
1748
1749decimal-constant:
1750 nonzero-digit
1751 decimal-constant digit
1752
1753octal-constant:
1754 0
1755 octal-constant octal-digit
1756
1757hexadecimal-constant:
1758 hexadecimal-prefix hexadecimal-digit
1759 hexadecimal-constant hexadecimal-digit
1760
1761hexadecimal-prefix:
1762 0x
1763 0X
1764
1765nonzero-digit:
1766 [1-9]
1767
1768integer-suffix:
1769 unsigned-suffix long-suffix-opt
1770 unsigned-suffix long-long-suffix
1771 long-suffix unsigned-suffix-opt
1772 long-long-suffix unsigned-suffix-opt
1773
1774unsigned-suffix:
1775 u
1776 U
1777
1778long-suffix:
1779 l
1780 L
1781
1782long-long-suffix:
1783 ll
1784 LL
1785
1786enumeration-constant:
1787 identifier
1788 string-literal
1789
1790character-constant:
1791 ' c-char-sequence '
1792 L' c-char-sequence '
1793
1794c-char-sequence:
1795 c-char
1796 c-char-sequence c-char
1797
1798c-char:
1799 any member of source charset except single-quote ('), backslash
1800 (\), or new-line character.
1801 escape-sequence
1802
1803escape-sequence:
1804 simple-escape-sequence
1805 octal-escape-sequence
1806 hexadecimal-escape-sequence
1807 universal-character-name
1808
1809simple-escape-sequence: one of
1810 \' \" \? \\ \a \b \f \n \r \t \v
1811
1812octal-escape-sequence:
1813 \ octal-digit
1814 \ octal-digit octal-digit
1815 \ octal-digit octal-digit octal-digit
1816
1817hexadecimal-escape-sequence:
1818 \x hexadecimal-digit
1819 hexadecimal-escape-sequence hexadecimal-digit
1820~~~
1821
1822
1823#### C.1.6 String literals
1824
1825~~~ text
1826string-literal:
1827 " s-char-sequence-opt "
1828 L" s-char-sequence-opt "
1829
1830s-char-sequence:
1831 s-char
1832 s-char-sequence s-char
1833
1834s-char:
1835 any member of source charset except double-quote ("), backslash
1836 (\), or new-line character.
1837 escape-sequence
1838~~~
1839
1840
1841#### C.1.7 Punctuators
1842
1843~~~ text
1844punctuator: one of
1845 [ ] ( ) { } . -> * + - < > : ; ... = ,
1846~~~
1847
1848
1849### C.2 Phrase structure grammar
1850
1851~~~ text
1852primary-expression:
1853 identifier
1854 constant
1855 string-literal
1856 ( unary-expression )
1857
1858postfix-expression:
1859 primary-expression
1860 postfix-expression [ unary-expression ]
1861 postfix-expression . identifier
1862 postfix-expressoin -> identifier
1863
1864unary-expression:
1865 postfix-expression
1866 unary-operator postfix-expression
1867
1868unary-operator: one of
1869 + -
1870
1871assignment-operator:
1872 =
1873
1874type-assignment-operator:
1875 :=
1876
1877constant-expression-range:
1878 unary-expression ... unary-expression
1879~~~
1880
1881
1882#### C.2.2 Declarations:
1883
1884~~~ text
1885declaration:
1886 declaration-specifiers declarator-list-opt ;
1887 ctf-specifier ;
1888
1889declaration-specifiers:
1890 storage-class-specifier declaration-specifiers-opt
1891 type-specifier declaration-specifiers-opt
1892 type-qualifier declaration-specifiers-opt
1893
1894declarator-list:
1895 declarator
1896 declarator-list , declarator
1897
1898abstract-declarator-list:
1899 abstract-declarator
1900 abstract-declarator-list , abstract-declarator
1901
1902storage-class-specifier:
1903 typedef
1904
1905type-specifier:
1906 void
1907 char
1908 short
1909 int
1910 long
1911 float
1912 double
1913 signed
1914 unsigned
1915 _Bool
1916 _Complex
1917 _Imaginary
1918 struct-specifier
1919 variant-specifier
1920 enum-specifier
1921 typedef-name
1922 ctf-type-specifier
1923
1924align-attribute:
1925 align ( unary-expression )
1926
1927struct-specifier:
1928 struct identifier-opt { struct-or-variant-declaration-list-opt } align-attribute-opt
1929 struct identifier align-attribute-opt
1930
1931struct-or-variant-declaration-list:
1932 struct-or-variant-declaration
1933 struct-or-variant-declaration-list struct-or-variant-declaration
1934
1935struct-or-variant-declaration:
1936 specifier-qualifier-list struct-or-variant-declarator-list ;
1937 declaration-specifiers-opt storage-class-specifier declaration-specifiers-opt declarator-list ;
1938 typealias declaration-specifiers abstract-declarator-list type-assignment-operator declaration-specifiers abstract-declarator-list ;
1939 typealias declaration-specifiers abstract-declarator-list type-assignment-operator declarator-list ;
1940
1941specifier-qualifier-list:
1942 type-specifier specifier-qualifier-list-opt
1943 type-qualifier specifier-qualifier-list-opt
1944
1945struct-or-variant-declarator-list:
1946 struct-or-variant-declarator
1947 struct-or-variant-declarator-list , struct-or-variant-declarator
1948
1949struct-or-variant-declarator:
1950 declarator
1951 declarator-opt : unary-expression
1952
1953variant-specifier:
1954 variant identifier-opt variant-tag-opt { struct-or-variant-declaration-list }
1955 variant identifier variant-tag
1956
1957variant-tag:
1958 < unary-expression >
1959
1960enum-specifier:
1961 enum identifier-opt { enumerator-list }
1962 enum identifier-opt { enumerator-list , }
1963 enum identifier
1964 enum identifier-opt : declaration-specifiers { enumerator-list }
1965 enum identifier-opt : declaration-specifiers { enumerator-list , }
1966
1967enumerator-list:
1968 enumerator
1969 enumerator-list , enumerator
1970
1971enumerator:
1972 enumeration-constant
1973 enumeration-constant assignment-operator unary-expression
1974 enumeration-constant assignment-operator constant-expression-range
1975
1976type-qualifier:
1977 const
1978
1979declarator:
1980 pointer-opt direct-declarator
1981
1982direct-declarator:
1983 identifier
1984 ( declarator )
1985 direct-declarator [ unary-expression ]
1986
1987abstract-declarator:
1988 pointer-opt direct-abstract-declarator
1989
1990direct-abstract-declarator:
1991 identifier-opt
1992 ( abstract-declarator )
1993 direct-abstract-declarator [ unary-expression ]
1994 direct-abstract-declarator [ ]
1995
1996pointer:
1997 * type-qualifier-list-opt
1998 * type-qualifier-list-opt pointer
1999
2000type-qualifier-list:
2001 type-qualifier
2002 type-qualifier-list type-qualifier
2003
2004typedef-name:
2005 identifier
2006~~~
2007
2008
2009#### C.2.3 CTF-specific declarations
2010
2011~~~ text
2012ctf-specifier:
2013 clock { ctf-assignment-expression-list-opt }
2014 event { ctf-assignment-expression-list-opt }
2015 stream { ctf-assignment-expression-list-opt }
2016 env { ctf-assignment-expression-list-opt }
2017 trace { ctf-assignment-expression-list-opt }
2018 callsite { ctf-assignment-expression-list-opt }
2019 typealias declaration-specifiers abstract-declarator-list type-assignment-operator declaration-specifiers abstract-declarator-list
2020 typealias declaration-specifiers abstract-declarator-list type-assignment-operator declarator-list
2021
2022ctf-type-specifier:
2023 floating_point { ctf-assignment-expression-list-opt }
2024 integer { ctf-assignment-expression-list-opt }
2025 string { ctf-assignment-expression-list-opt }
2026 string
2027
2028ctf-assignment-expression-list:
2029 ctf-assignment-expression ;
2030 ctf-assignment-expression-list ctf-assignment-expression ;
2031
2032ctf-assignment-expression:
2033 unary-expression assignment-operator unary-expression
2034 unary-expression type-assignment-operator type-specifier
2035 declaration-specifiers-opt storage-class-specifier declaration-specifiers-opt declarator-list
2036 typealias declaration-specifiers abstract-declarator-list type-assignment-operator declaration-specifiers abstract-declarator-list
2037 typealias declaration-specifiers abstract-declarator-list type-assignment-operator declarator-list
2038~~~
This page took 0.091314 seconds and 4 git commands to generate.