README.md: document events_discarded feature
[deliverable/barectf.git] / README.md
1 barectf
2 =======
3
4 **barectf** is a command-line utility which generates pure C99
5 code that is able to write native
6 [CTF](http://git.efficios.com/?p=ctf.git;a=blob_plain;f=common-trace-format-specification.txt;hb=master)
7 (the Common Trace Format) out of a pre-written CTF metadata file.
8
9 You will find barectf interesting if:
10
11 1. You need to trace a program.
12 2. You need tracing to be as fast as possible, but also very flexible:
13 record integers of custom sizes, custom floating point numbers,
14 enumerations mapped to a specific integer type, structure fields,
15 NULL-terminated strings, static and dynamic arrays, etc.
16 3. You need to be able to convert the recorded binary events to
17 human-readable text, as well as analyze them with Python scripts
18 ([Babeltrace](http://www.efficios.com/babeltrace) does all that,
19 given a CTF input).
20 4. You _cannot_ use [LTTng](http://lttng.org/), an efficient tracing
21 framework for the Linux kernel and Linux/BSD user applications, which
22 outputs CTF.
23
24 The target audience of barectf is developers who need to trace bare metal
25 systems (without an operating system). The code produced by barectf
26 is pure C99 and is lightweight enough to fit on a tiny microcontroller.
27 Each event described in the CTF metadata input becomes one C function with
28 one parameter mapped to one event field. CTF data is recorded in a buffer of
29 any size provided by the user. This buffer corresponds to one CTF packet.
30 The generated tracing functions report when the buffer is full. The user
31 is entirely responsible for the buffering scheme: leave the buffer in memory,
32 save it to some permanent storage, swap it with another empty buffer and
33 concatenate recorded packets, etc.
34
35 barectf is written in Python 3 and currently uses
36 [pytsdl](https://github.com/efficios/pytsdl) to parse the CTF metadata file
37 provided by the user.
38
39
40 installing
41 ----------
42
43 Make sure you have `pip` for Python 3. On the latest Ubuntu releases,
44 it is called `pip3`:
45
46 sudo apt-get install python3-pip
47
48 On Ubuntu 12.04, you need to install `setuptools` first, then use
49 `easy_install3` to install `pip3`:
50
51 sudo apt-get install python3-setuptools
52 sudo easy_install3 pip
53
54 Install barectf:
55
56 sudo pip3 install barectf
57
58
59 using
60 -----
61
62 Using barectf involves:
63
64 1. Writing the CTF metadata file describing the various headers,
65 contexts and event fields.
66 2. Running the `barectf` command to generate C99 files out of
67 the CTF metadata file.
68 3. Using the generated C code in your specific application.
69
70 The following subsections explain the three steps above.
71
72 Also, have a look at the [`doc/examples`](doc/examples) directory which
73 contains a few complete examples.
74
75
76 ### writing the CTF metadata
77
78 The **Common Trace Format** is a specialized file format for recording
79 trace data. CTF is designed to be very fast to write and very flexible.
80 All headers, contexts and event fields written in binary files are
81 described using a custom C-like, declarative language, TSDL (Trace
82 Stream Description Language). The file containing this description is
83 called the **CTF metadata**. The latter may be automatically generated
84 by a tracer, like it is the case of LTTng, or written by hand. This
85 metadata file is then used by CTF trace readers to know the layout of
86 CTF binary files containing actual event contexts and fields.
87
88 The CTF metadata file contains several blocks describing various CTF
89 binary layouts. A CTF trace file is a concatenation of several CTF
90 packets. Here's the anatomy of a CTF packet:
91
92 ![CTF packet anatomy](doc/ctf-packet.png)
93
94 A CTF packet belongs to a specific CTF stream. While the packet header
95 is the same for all streams of a given CTF trace, everything else is
96 specified per stream. Following this packet header is a packet context,
97 and then actual recorded events. Each event starts with a mandatory
98 header (same event header for all events of a given stream). The event
99 header is followed by an optional event context with a layout shared
100 by all events of a given stream. Then follows another optional event
101 context, although this one has a layout specific to the event type.
102 Finally, event fields are written.
103
104 barectf asks you to write the CTF metadata by hand. Although its official
105 [specification](http://git.efficios.com/?p=ctf.git;a=blob_plain;f=common-trace-format-specification.txt;hb=master)
106 is thorough, you will almost always start from this template:
107
108 ```
109 /* CTF 1.8 */
110
111 /* a few useful standard integer aliases */
112 typealias integer {size = 8; align = 8;} := uint8_t;
113 typealias integer {size = 16; align = 16;} := uint16_t;
114 typealias integer {size = 32; align = 32;} := uint32_t;
115 typealias integer {size = 64; align = 64;} := uint64_t;
116 typealias integer {size = 8; align = 8; signed = true;} := int8_t;
117 typealias integer {size = 16; align = 16; signed = true;} := int16_t;
118 typealias integer {size = 32; align = 32; signed = true;} := int32_t;
119 typealias integer {size = 64; align = 64; signed = true;} := int64_t;
120
121 /* IEEE 754 standard-precision floating point alias */
122 typealias floating_point {
123 exp_dig = 8;
124 mant_dig = 24;
125 align = 32;
126 } := float;
127
128 /* IEEE 754 double-precision floating point alias */
129 typealias floating_point {
130 exp_dig = 11;
131 mant_dig = 53;
132 align = 64;
133 } := double;
134
135 /* trace block */
136 trace {
137 /* CTF version 1.8; leave this as is */
138 major = 1;
139 minor = 8;
140
141 /*
142 * Native byte order (`le` or `be`). This is used by barectf to generate
143 * the appropriate code when writing data to the packet.
144 */
145 byte_order = le;
146
147 /*
148 * Packet header. All packets (buffers) will have the same header.
149 *
150 * Special fields recognized by barectf (must appear in this order):
151 *
152 * magic: will be set to CTF's magic number (must be the first field)
153 * (32-bit unsigned integer) (mandatory)
154 * stream_id: will be set to the ID of the stream associated with
155 * this packet (unsigned integer of your choice) (mandatory)
156 */
157 packet.header := struct {
158 uint32_t magic;
159 uint32_t stream_id;
160 };
161 };
162
163 /* environment variables; you may add custom entries */
164 env {
165 domain = "bare";
166 tracer_name = "barectf";
167 tracer_major = 0;
168 tracer_minor = 1;
169 tracer_patchlevel = 0;
170 };
171
172 /* clock descriptor */
173 clock {
174 /* clock name */
175 name = my_clock;
176
177 /* clock frequency (Hz) */
178 freq = 1000000000;
179
180 /* optional clock value offset; offset from Epoch is: offset * (1 / freq) */
181 offset = 0;
182 };
183
184 /* alias for integer used to hold clock cycles */
185 typealias integer {
186 size = 32;
187
188 /* map to the appropriate clock using its name */
189 map = clock.my_clock.value;
190 } := my_clock_int_t;
191
192 /*
193 * A stream. You may have as many streams as you want. Events are unique
194 * within their own stream. The main advantage of having multiple streams
195 * is having different event headers, stream event contexts and stream
196 * packet contexts for each one.
197 */
198 stream {
199 /*
200 * Mandatory stream ID (must fit the integer type of
201 * `trace.packet.header.stream_id`).
202 */
203 id = 0;
204
205 /*
206 * Mandatory packet context. This structure follows the packet header
207 * (see `trace.packet.header`) immediately in CTF binary streams.
208 *
209 * Special fields recognized by barectf:
210 *
211 * timestamp_begin: will be set to the current clock value when opening
212 * the packet (same integer type as the clock's value)
213 * timestamp_end: will be set to the current clock value when closing
214 * the packet (same integer type as the clock's value)
215 * content_size: will be set to the content size, in bits, of this
216 * stream (unsigned 32-bit or 64-bit integer) (mandatory)
217 * packet_size: will be set to the packet size, in bits, of this
218 * stream (unsigned 32-bit or 64-bit integer) (mandatory)
219 * events_discarded: if present, the barectf_close_packet() function of
220 * this stream will accept an additional parameter to
221 * specify the number of events that were discarded in
222 * this stream _so far_ (free-running counter for the
223 * whole stream)
224 * cpu_id: if present, the barectf_open_packet() function of
225 * this stream will accept an additional parameter to
226 * specify the ID of the CPU associated with this stream
227 * (a given stream should only be written to by a
228 * specific CPU) (unsigned integer of your choice)
229 *
230 * `timestamp_end` must be present if `timestamp_begin` exists.
231 */
232 packet.context := struct {
233 my_clock_int_t timestamp_begin;
234 my_clock_int_t timestamp_end;
235 uint64_t content_size;
236 uint64_t packet_size;
237 uint32_t cpu_id;
238 };
239
240 /*
241 * Mandatory event header. All events recorded in this stream will start
242 * with this structure.
243 *
244 * Special fields recognized by barectf:
245 *
246 * id: will be filled by the event ID corresponding to a tracing
247 * function (unsigned integer of your choice)
248 * timestamp: will be filled by the current clock's value (same integer
249 * type as the clock's value)
250 */
251 event.header := struct {
252 uint32_t id;
253 my_clock_int_t timestamp;
254 };
255
256 /*
257 * Optional stream event context (you may remove the whole block or leave
258 * the structure empty if you don't want any). This structure follows the
259 * event header (see `stream.event.header`) immediately in CTF binary
260 * streams.
261 */
262 event.context := struct {
263 int32_t _some_stream_event_context_field;
264 };
265 };
266
267 /*
268 * An event. Events have an ID, a name, an optional context and fields. An
269 * event is associated to a specific stream using its stream ID.
270 */
271 event {
272 /*
273 * Mandatory event name. This is used by barectf to generate the suffix
274 * of this event's corresponding tracing function, so make sure it follows
275 * the C identifier syntax even though it's a quoted string here.
276 */
277 name = "my_event";
278
279 /*
280 * Mandatory event ID (must fit the integer type of in
281 * `stream.event.header.id` of the associated stream).
282 */
283 id = 0;
284
285 /* ID of the stream in which this event will be recorded */
286 stream_id = 0;
287
288 /*
289 * Optional event context (you may remove the whole block or leave the
290 * structure empty if you don't want one). This structure follows the
291 * stream event context (if it exists) immediately in CTF binary streams.
292 */
293 context := struct {
294 int32_t _some_event_context_field;
295 };
296
297 /*
298 * Mandatory event fields (although the structure may be left empty if this
299 * event has no fields). This structure follows the event context (if it
300 * exists) immediately in CTF binary streams.
301 */
302 fields := struct {
303 uint32_t _a;
304 uint32_t _b;
305 uint16_t _c;
306 string _d;
307 };
308 };
309 ```
310
311 The top `/* CTF 1.8 */` is actually needed right there, and as is, since it
312 acts as a CTF metadata magic number for CTF readers.
313
314 Only one stream and one event (belonging to this single stream) are described
315 in this template, but you may add as many as you need.
316
317 The following subsections describe the features of CTF metadata supported
318 by barectf.
319
320
321 #### types
322
323 The supported structure field types are:
324
325 * **integers** of any size (64-bit and less), any alignment (power of two)
326 * **floating point numbers** of any total size (64-bit and less), any
327 alignment (power of two)
328 * NULL-terminated **strings** of bytes
329 * **enumerations** associated with a specific integer type
330 * **static** and **dynamic arrays** of any type
331 * **structures** containing only integers, floating point numbers,
332 enumerations and _static_ arrays
333
334 CTF also supports _variants_ (dynamic selection between different types),
335 but barectf **does not**. Any detected variant will throw an error when
336 running `barectf`.
337
338
339 ##### integers
340
341 CTF integers are defined like this:
342
343 ```
344 integer {
345 /* mandatory size in bits (64-bit and less) */
346 size = 16;
347
348 /*
349 * Optional alignment in bits (power of two). Default is 8 when the
350 * size is a multiple of 8, and 1 otherwise.
351 */
352 align = 16;
353
354 /* optional signedness (`true` or `false`); default is unsigned */
355 signed = true;
356
357 /*
358 * Optional byte order (`le`, `be`, `native` or `network`). `native`
359 * will use the byte order specified by the `trace.byte_order`.
360 * Default is `native`.
361 */
362 byte_order = le;
363
364 /*
365 * Optional display base, used to display the integer value when
366 * reading the trace. Valid values are 2 (or `binary`, `bin` and `b`),
367 * 8 (or `o`, `oct` or `octal`), 10 (or `u`, `i`, `d`, `dec` or
368 * `decimal`), and 16 (or `x`, `X`, `p`, `hex` or `hexadecimal`).
369 * Default is 10.
370 */
371 base = hex;
372
373 /*
374 * Encoding (if this integer represents a character). Valid values
375 * are `none`, `UTF8` and `ASCII`. Default is `none`.
376 */
377 encoding = UTF8;
378 }
379 ```
380
381 The size (the only mandatory property) does _not_ have to be a power of two:
382
383 ```
384 integer {size = 23;}
385 ```
386
387 is perfectly valid.
388
389 A CTF integer field will make barectf produce a corresponding C integer
390 function parameter with an appropriate size. For example, the 23-bit integer
391 above would produce an `uint32_t` parameter (of which only the first 23
392 least significant bits will be written to the trace), while the first
393 16-bit one will produce an `int16_t` parameter.
394
395 The `integer` block also accepts a `map` property which is only used
396 when defining the integer used to carry the value of a specified
397 clock. You may always follow the example above.
398
399
400 ##### floating point numbers
401
402 CTF floating point numbers are defined like this:
403
404 ```
405 floating_point {
406 /* exponent size in bits */
407 exp_dig = 8;
408
409 /* mantissa size in bits */
410 mant_dig = 24;
411
412 /*
413 * Optional alignment (power of two). Default is 8 when the total
414 * size (exponent + mantissa) is a multiple of 8, and 1 otherwise.
415 */
416 align = 32;
417
418 /*
419 * Optional byte order (`le`, `be`, `native` or `network`). `native`
420 * will use the byte order specified by the `trace.byte_order`.
421 * Default is `native`.
422 */
423 byte_order = le;
424 }
425 ```
426
427 If a CTF floating point number is defined with an 8-bit exponent, a 24-bit
428 mantissa and a 32-bit alignment, its barectf C function parameter type will
429 be `float`. It will be `double` for an 11-bit exponent, 53-bit mantissa
430 and 64-bit aligned CTF floating point number. Any other configuration
431 will produce a `uint64_t` function parameter (you will need to cast your
432 custom floating point number to this when calling the tracing function).
433
434
435 ##### strings
436
437 CTF strings are pretty simple to define:
438
439 ```
440 string
441 ```
442
443 They may also have an encoding property:
444
445 ```
446 string {
447 /* encoding: `none`, `UTF8` or `ASCII`; default is `none` */
448 encoding = UTF8;
449 }
450 ```
451
452 CTF strings are always byte-aligned.
453
454 A CTF string field will make barectf produce a corresponding C function
455 parameter of type `const char*`. Bytes will be copied from this pointer
456 until a byte of value 0 is found (which will also be written to the
457 buffer to mark the end of the recorded string).
458
459
460 ##### enumerations
461
462 CTF enumerations associate labels to ranges of integer values. They
463 are a great way to trace named states using an integer. Here's an
464 example:
465
466 ```
467 enum : uint32_t {
468 ZERO,
469 ONE,
470 TWO,
471 TEN = 10,
472 ELEVEN,
473 "label with spaces",
474 RANGE = 23 ... 193
475 }
476 ```
477
478 Unless the first entry specifies a value, CTF enumerations are
479 always started at 0. They work pretty much like their C counterpart,
480 although they support ranges and literal strings as labels.
481
482 CTF enumerations are associated with a CTF integer type (`uint32_t`
483 above). This identifier must be an existing integer type alias.
484
485 A CTF enumeration field will make barectf produce a corresponding C
486 integer function parameter compatible with the associated CTF integer type.
487
488
489 ##### static arrays
490
491 Structure field names may be followed by a subscripted constant to
492 define a static array of the field type:
493
494 ```
495 struct {
496 integer {size = 16;} _field[10];
497 }
498 ```
499
500 In the above structure, `_field` is a static array of ten 16-bit integers.
501
502 A CTF static array field will make barectf produce a `const void*` C function
503 parameter. Bytes will be copied from this pointer to match the total static
504 array size. In the example above, the integer size is 16-bit, thus its
505 default alignment is 8-bit, so 20 bytes would be copied.
506
507 The inner element of a CTF static array _must be at least byte-aligned_
508 (8-bit), either by forcing its alignment, or by ensuring it manually
509 when placing fields one after the other. This means the following static
510 array is valid for barectf:
511
512 ```
513 struct {
514 /* ... */
515 integer {size = 5;} _field[10];
516 }
517 ```
518
519 as long as the very first 5-bit, 1-bit aligned integer element starts
520 on an 8-bit boundary.
521
522
523 ##### dynamic arrays
524
525 Just like static arrays, dynamic arrays are defined using a subscripted
526 length, albeit in this case, this length refers to another field using
527 the dot notation. Dynamic arrays are called _sequences_ in the CTF
528 specification.
529
530 Here's an example:
531
532 ```
533 struct {
534 uint32_t _length;
535 integer {size = 16;} _field[_length];
536 }
537 ```
538
539 In the above structure, `_field` is a dynamic array of `_length`
540 16-bit integers.
541
542 There are various scopes to which a dynamic array may refer:
543
544 * no prefix: previous field in the same structure, or in parent
545 structures until found
546 * `event.fields.` prefix: field of the event fields
547 * `event.context.` prefix: field of the event context if it exists
548 * `stream.event.context.` prefix: field of the stream event context
549 if it exists
550 * `stream.event.header.` prefix: field of the event header
551 * `stream.packet.context.` prefix: field of the packet context
552 * `trace.packet.header.` prefix: field of the packet header
553 * `env.` prefix: static property of the environment block
554
555 Here's another, more complex example:
556
557 ```
558 struct {
559 uint32_t _length;
560 string _other_field[stream.event.context.length];
561 float _static_array_of_dynamic_arrays[10][_length];
562 }
563 ```
564
565 The above examples also demonstrates that dynamic arrays and static
566 arrays may contain eachother. `_other_field` is a dynamic array of
567 `stream.event.context.length` strings. `_static_array_of_dynamic_arrays`
568 is a static array of 10 dynamic arrays of `_length` floating point
569 numbers. This syntax follows the C language.
570
571 A CTF dynamic array field will make barectf produce a `const void*` C function
572 parameter. Bytes will be copied from this pointer to match the
573 total dynamic array size. The previously recorded length will be
574 found automatically (always an offset from the beginning of the
575 stream packet, or from the beginning of the current event).
576
577 barectf has a few limitations concerning dynamic arrays:
578
579 * The inner element of a CTF dynamic array _must be at least byte-aligned_
580 (8-bit), either by forcing its alignment, or by ensuring it manually
581 when placing fields one after the other.
582 * The length type must be a 32-bit, byte-aligned unsigned integer
583 with a native byte order.
584
585
586 ##### structures
587
588 Structures contain fields associating a name to a type. The fields
589 are recorded in the specified order within the CTF binary stream.
590
591 Here's an example:
592
593 ```
594 struct {
595 uint32_t _a;
596 int16_t _b;
597 string {encoding = ASCII;} _c;
598 }
599 ```
600
601 The default alignment of a structure is the largest alignment amongst
602 its fields. For example, the following structure has a 32-bit alignment:
603
604 ```
605 struct {
606 uint16_t _a; /* alignment: 16 */
607 struct { /* alignment: 32 */
608 uint32_t _a; /* alignment: 32 */
609 string; _b; /* alignment: 8 */
610 } _b;
611 integer {size = 64;} _c; /* alignment: 8 */
612 }
613 ```
614
615 This default alignment may be overridden using a special `align()`
616 option after the structure is closed:
617
618 ```
619 struct {
620 uint16_t _a;
621 struct {
622 uint32_t _a;
623 string; _b;
624 } _b;
625 integer {size = 64;} _c;
626 } align(16)
627 ```
628
629 You may use structures as field types, although they must have a
630 _known size_ when running barectf. This means they cannot contain
631 sequences or strings.
632
633 A CTF structure field will make barectf produce a `const void*` C function
634 parameter. The structure (of known size) will be copied as is to the
635 current buffer, respecting its alignment.
636
637 Note that barectf requires inner structures to be at least byte-aligned.
638
639 Be careful when using CTF structures for recording binary structures
640 declared in C. You need to make sure your C compiler aligns structure
641 fields and adds padding exactly in the way you define your equivalent
642 CTF structure. For example, using GCC on the x86 architecture, 3 bytes
643 are added after field `a` in the following C structure since `b` is
644 32-bit aligned:
645
646 ```c
647 struct my_struct {
648 char a;
649 unsigned int b;
650 };
651 ```
652
653 It would be wrong to use the following CTF structure:
654
655 ```
656 struct {
657 integer {size = 8; signed = true;} a;
658 integer {size = 32;} b;
659 }
660 ```
661
662 since field `b` is byte-aligned by default. This one would work fine:
663
664 ```
665 struct {
666 integer {size = 8; signed = true;} a;
667 integer {size = 32; align = 32;} b;
668 }
669 ```
670
671 CTF structures can prove very useful for recording protocols with named
672 fields when reading the trace. For example, here's the CTF structure
673 describing the IPv4 header (excluding options):
674
675 ```
676 struct ipv4_header {
677 integer {size = 4;} version;
678 integer {size = 4;} ihl;
679 integer {size = 6;} dscp;
680 integer {size = 2;} ecn;
681 integer {size = 16; byte_order = network;} total_length;
682 integer {size = 16; byte_order = network;} identification;
683 integer {size = 1;} flag_more_fragment;
684 integer {size = 1;} flag_dont_fragment;
685 integer {size = 1;} flag_reserved;
686 integer {size = 13; byte_order = network;} fragment_offset;
687 integer {size = 8;} ttl;
688 integer {size = 8;} protocol;
689 integer {size = 16; byte_order = network;} header_checksum;
690 integer {size = 8;} src_ip_addr[4];
691 integer {size = 8;} dst_ip_addr[4];
692 }
693 ```
694
695 Although this complex structure has more than ten independent fields,
696 the generated C function would only call a 20-byte `memcpy()`, making
697 it fast to record. Bits will be unpacked properly and values displayed
698 in a human-readable form by the CTF reader thanks to the CTF metadata.
699
700
701 #### type aliases
702
703 Type aliases associate a name with a type definition. Any type may have
704 any name. They are similar to C `typedef`s.
705
706 Examples:
707
708 ```
709 typealias integer {
710 size = 16;
711 align = 4;
712 signed = true;
713 byte_order = network;
714 base = hex;
715 encoding = UTF8;
716 } := my_int;
717 ```
718
719 ```
720 typealias floating_point {
721 exp_dig = 8;
722 mant_dig = 8;
723 align = 16;
724 byte_order = be;
725 } := my_float;
726 ```
727
728 ```
729 typealias string {
730 encoding = ASCII;
731 } := my_string;
732 ```
733
734 ```
735 typealias enum : uint32_t {
736 ZERO,
737 ONE,
738 TWO,
739 TEN = 10,
740 ELEVEN,
741 "label with spaces",
742 RANGE = 23 ... 193
743 } := my_enum;
744 ```
745
746 ```
747 typealias struct {
748 uint32_t _length;
749 string _other_field;
750 float _hello[10][_length];
751 } align(8) := my_struct;
752 ```
753
754
755 ### running the `barectf` command
756
757 Using the `barectf` command-line utility is easy. In its simplest form,
758 it outputs a few C99 files out of a CTF metadata file:
759
760 barectf metadata
761
762 will output in the current working directory:
763
764 * `barectf_bitfield.h`: macros used by tracing functions to pack bits
765 * `barectf.h`: other macros and prototypes of context/tracing functions
766 * `barectf.c`: context/tracing functions
767
768 You may also want to produce `static inline` functions if your target
769 system has enough memory to hold the extra code:
770
771 barectf --static-inline metadata
772
773 `barectf` is the default name of the files and the default prefix of
774 barectf C functions and structures. You may use a custom prefix:
775
776 barectf --prefix trace metadata
777
778 You may also output the files elsewhere:
779
780 barectf --output /custom/path metadata
781
782 ### using the generated C99 code
783
784 This section assumes you ran `barectf` with no options:
785
786 barectf metadata
787
788 The command generates C99 structures and functions to initialize
789 and finalize bare CTF contexts. It also generates as many tracing functions
790 as there are events described in the CTF metadata file.
791
792 Before starting the record events, you must initialize a barectf
793 context. This is done using `barectf_init()`.
794
795 The clock callback parameter (`clock_cb`) is used to get the clock whenever
796 a tracing function is called. Each platform has its own way of obtaining
797 the a clock value, so this is left to user implementation. The actual
798 return type of the clock callback depends on the clock value CTF integer
799 type defined in the CTF metadata.
800
801 The `barectf_init()` function name will contain the decimal stream
802 ID if you have more than one stream. You must allocate the context
803 structure yourself.
804
805 Example:
806
807 ```c
808 struct barectf_ctx* barectf_ctx = platform_alloc(sizeof(*barectf_ctx));
809
810 barectf_init(barectf_ctx, buf, 8192, platform_get_clock, NULL);
811 ```
812
813 This initializes a barectf context with a buffer of 8192 bytes.
814
815 After the barectf context is initialized, open a packet using
816 `barectf_open_packet()`. If you have any non-special fields in
817 your stream packet context, `barectf_open_packet()` accepts a
818 parameter for each of them since the packet context is written
819 at this moment:
820
821 ```
822 barectf_open_packet(barectf_ctx);
823 ```
824
825 Once the packet is opened, you may call any of the tracing functions to record
826 CTF events into the context's buffer.
827
828 As an example, let's take the following CTF event definition:
829
830 ```
831 event {
832 name = "my_event";
833 id = 0;
834 stream_id = 0;
835 fields := struct {
836 integer {size = 32;} _a;
837 integer {size = 14; signed = true;} _b;
838 floating_point {exp_dig = 8; mant_dig = 24; align = 32;} _c;
839 struct {
840 uint32_t _a;
841 uint32_t _b;
842 } _d;
843 string _e;
844 };
845 };
846 ```
847
848 In this example, we assume the stream event context and the event context
849 are not defined for this event. `barectf` generates the following tracing
850 function prototype:
851
852 ```c
853 int barectf_trace_my_event(
854 struct barectf_ctx* ctx,
855 uint32_t param_ef__a,
856 int16_t param_ef__b,
857 float param_ef__c,
858 const void* param_ef__d,
859 const char* param_ef__e
860 );
861 ```
862
863 When called, this function first calls the clock callback to get a clock
864 value as soon as possible. It then proceeds to record each field with
865 proper alignment and updates the barectf context. On success, 0 is returned.
866 Otherwise, one of the following negative errors is returned:
867
868 * `-EBARECTF_NOSPC`: no space left in the context's buffer; the event
869 was **not** recorded. You should call `barectf_close_packet()` to finalize the
870 CTF packet.
871
872 `barectf_close_packet()` may be called at any time.
873 When `barectf_close_packet()` returns, the packet is complete and ready
874 to be read by a CTF reader. CTF packets may be concatenated in a single
875 CTF stream file. You may reuse the same context and buffer to record another
876 CTF packet, as long as you call `barectf_open_packet()` before calling any
877 tracing function.
878
879
880 ### reading CTF traces
881
882 To form a complete CTF trace, put your CTF metadata file (it should be
883 named `metadata`) and your binary stream files (concatenations of CTF
884 packets written by C code generated by barectf) in the same directory.
885
886 To read a CTF trace, use [Babeltrace](http://www.efficios.com/babeltrace).
887 Babeltrace is packaged by most major distributions (`babeltrace`).
888 Babeltrace ships with a command-line utility that can convert a CTF trace
889 to human-readable text output. Also, it includes a Python binding so
890 that you may analyze a CTF trace using a custom script.
891
892 In its simplest form, the `babeltrace` command-line converter is quite
893 easy to use:
894
895 babeltrace /path/to/directory/containing/ctf/files
896
897 See `babeltrace --help` for more options.
898
899 You may also use the Python 3 binding of Babeltrace to create custom
900 analysis scripts.
This page took 0.049161 seconds and 5 git commands to generate.