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