acknowledgements: add link to DRDC's document
[lttng-docs.git] / 2.6 / lttng-docs-2.6.txt
CommitLineData
f0287ae1
PP
1The LTTng Documentation
2=======================
3Philippe Proulx <pproulx@efficios.com>
4v2.6, May 26, 2016
5
6
7include::../common/copyright.txt[]
8
9
10include::../common/warning-not-maintained.txt[]
11
12
13include::../common/welcome.txt[]
14
15
16include::../common/audience.txt[]
17
18
19[[chapters]]
20=== Chapter descriptions
21
22What follows is a list of brief descriptions of this documentation's
23chapters. The latter are ordered in such a way as to make the reading
24as linear as possible.
25
26. <<nuts-and-bolts,Nuts and bolts>> explains the
27 rudiments of software tracing and the rationale behind the
28 LTTng project.
29. <<installing-lttng,Installing LTTng>> is divided into
30 sections describing the steps needed to get a working installation
31 of LTTng packages for common Linux distributions and from its
32 source.
33. <<getting-started,Getting started>> is a very concise guide to
34 get started quickly with LTTng kernel and user space tracing. This
35 chapter is recommended if you're new to LTTng or software tracing
36 in general.
37. <<understanding-lttng,Understanding LTTng>> deals with some
38 core concepts and components of the LTTng suite. Understanding
39 those is important since the next chapter assumes you're familiar
40 with them.
41. <<using-lttng,Using LTTng>> is a complete user guide of the
42 LTTng project. It shows in great details how to instrument user
43 applications and the Linux kernel, how to control tracing sessions
44 using the `lttng` command line tool and miscellaneous practical use
45 cases.
46. <<reference,Reference>> contains references of LTTng components,
47 like links to online manpages and various APIs.
48
49We recommend that you read the above chapters in this order, although
50some of them may be skipped depending on your situation. You may skip
51<<nuts-and-bolts,Nuts and bolts>> if you're familiar with tracing
52and LTTng. Also, you may jump over <<installing-lttng,Installing LTTng>>
53if LTTng is already properly installed on your target system.
54
55
56include::../common/convention.txt[]
57
58
59include::../common/acknowledgements.txt[]
60
61
62[[whats-new]]
63== What's new in LTTng {revision}?
64
65Most of the changes of LTTng {revision} are bug fixes, making the toolchain
66more stable than ever before. Still, LTTng {revision} adds some interesting
67features to the project.
68
69LTTng 2.5 already supported the instrumentation and tracing of
70<<java-application,Java applications>> through `java.util.logging`
71(JUL). LTTng {revision} goes one step further by supporting
72https://logging.apache.org/log4j/1.2/[Apache log4j 1.2].
73The new log4j domain is selected using the `--log4j` option in various
74commands of the `lttng` tool.
75
76LTTng-modules has supported system call tracing for a long time,
77but until now, it was only possible to record either all of them,
78or none of them. LTTng {revision} allows the user to record specific
79system call events, for example:
80
81[role="term"]
82----
83lttng enable-event --kernel --syscall open,fork,chdir,pipe
84----
85
86Finally, the `lttng` command line tool is not only able to communicate
87with humans as it used to do, but also with machines thanks to its new
88<<mi,machine interface>> feature.
89
90To learn more about the new features of LTTng {revision}, see the
91http://lttng.org/blog/2015/02/27/lttng-2.6-released/[release announcement].
92
93
94[[nuts-and-bolts]]
95== Nuts and bolts
96
97What is LTTng? As its name suggests, the _Linux Trace Toolkit: next
98generation_ is a modern toolkit for tracing Linux systems and
99applications. So your first question might rather be: **what is
100tracing?**
101
102
103[[what-is-tracing]]
104=== What is tracing?
105
106As the history of software engineering progressed and led to what
107we now take for granted--complex, numerous and
108interdependent software applications running in parallel on
109sophisticated operating systems like Linux--the authors of such
110components, or software developers, began feeling a natural
111urge of having tools to ensure the robustness and good performance
112of their masterpieces.
113
114One major achievement in this field is, inarguably, the
115https://www.gnu.org/software/gdb/[GNU debugger (GDB)],
116which is an essential tool for developers to find and fix
117bugs. But even the best debugger won't help make your software run
118faster, and nowadays, faster software means either more work done by
119the same hardware, or cheaper hardware for the same work.
120
121A _profiler_ is often the tool of choice to identify performance
122bottlenecks. Profiling is suitable to identify _where_ performance is
123lost in a given software; the profiler outputs a profile, a
124statistical summary of observed events, which you may use to discover
125which functions took the most time to execute. However, a profiler
126won't report _why_ some identified functions are the bottleneck.
127Bottlenecks might only occur when specific conditions are met, sometimes
128almost impossible to capture by a statistical profiler, or impossible to
129reproduce with an application altered by the overhead of an event-based
130profiler. For a thorough investigation of software performance issues,
131a history of execution, with the recorded values of chosen variables
132and context, is essential. This is where tracing comes in handy.
133
134_Tracing_ is a technique used to understand what goes on in a running
135software system. The software used for tracing is called a _tracer_,
136which is conceptually similar to a tape recorder. When recording,
137specific probes placed in the software source code generate events
138that are saved on a giant tape: a _trace_ file. Both user applications
139and the operating system may be traced at the same time, opening the
140possibility of resolving a wide range of problems that are otherwise
141extremely challenging.
142
143Tracing is often compared to _logging_. However, tracers and loggers
144are two different tools, serving two different purposes. Tracers are
145designed to record much lower-level events that occur much more
146frequently than log messages, often in the thousands per second range,
147with very little execution overhead. Logging is more appropriate for
148very high-level analysis of less frequent events: user accesses,
149exceptional conditions (errors and warnings, for example), database
150transactions, instant messaging communications, and such. More formally,
151logging is one of several use cases that can be accomplished with
152tracing.
153
154The list of recorded events inside a trace file may be read manually
155like a log file for the maximum level of detail, but it is generally
156much more interesting to perform application-specific analyses to
157produce reduced statistics and graphs that are useful to resolve a
158given problem. Trace viewers and analysers are specialized tools
159designed to do this.
160
161So, in the end, this is what LTTng is: a powerful, open source set of
162tools to trace the Linux kernel and user applications at the same time.
163LTTng is composed of several components actively maintained and
164developed by its link:/community/#where[community].
165
166
167[[lttng-alternatives]]
168=== Alternatives to LTTng
169
170Excluding proprietary solutions, a few competing software tracers
171exist for Linux:
172
173* https://www.kernel.org/doc/Documentation/trace/ftrace.txt[ftrace]
174 is the de facto function tracer of the Linux kernel. Its user
175 interface is a set of special files in sysfs.
176* https://perf.wiki.kernel.org/[perf] is
177 a performance analyzing tool for Linux which supports hardware
178 performance counters, tracepoints, as well as other counters and
179 types of probes. perf's controlling utility is the `perf` command
180 line/curses tool.
181* http://linux.die.net/man/1/strace[strace]
182 is a command line utility which records system calls made by a
183 user process, as well as signal deliveries and changes of process
184 state. strace makes use of https://en.wikipedia.org/wiki/Ptrace[ptrace]
185 to fulfill its function.
186* https://sourceware.org/systemtap/[SystemTap]
187 is a Linux kernel and user space tracer which uses custom user scripts
188 to produce plain text traces. Scripts are converted to the C language,
189 then compiled as Linux kernel modules which are loaded to produce
190 trace data. SystemTap's primary user interface is the `stap`
191 command line tool.
192* http://www.sysdig.org/[sysdig], like
193 SystemTap, uses scripts to analyze Linux kernel events. Scripts,
194 or _chisels_ in sysdig's jargon, are written in Lua and executed
195 while the system is being traced, or afterwards. sysdig's interface
196 is the `sysdig` command line tool as well as the curses-based
197 `csysdig` tool.
198
199The main distinctive features of LTTng is that it produces correlated
200kernel and user space traces, as well as doing so with the lowest
201overhead amongst other solutions. It produces trace files in the
202http://diamon.org/ctf[CTF] format, an optimized file format
203for production and analyses of multi-gigabyte data. LTTng is the
204result of close to 10 years of
205active development by a community of passionate developers. LTTng {revision}
206is currently available on some major desktop, server, and embedded Linux
207distributions.
208
209The main interface for tracing control is a single command line tool
210named `lttng`. The latter can create several tracing sessions,
211enable/disable events on the fly, filter them efficiently with custom
212user expressions, start/stop tracing, and do much more. Traces can be
213recorded on disk or sent over the network, kept totally or partially,
214and viewed once tracing becomes inactive or in real-time.
215
216<<installing-lttng,Install LTTng now>> and start tracing!
217
218
219[[installing-lttng]]
220== Installing LTTng
221
222**LTTng** is a set of software components which interact to allow
223instrumenting the Linux kernel and user applications as well as
224controlling tracing sessions (starting/stopping tracing,
225enabling/disabling events, and more). Those components are bundled into
226the following packages:
227
228LTTng-tools::
229 Libraries and command line interface to control tracing sessions.
230
231LTTng-modules::
232 Linux kernel modules for tracing the kernel.
233
234LTTng-UST::
235 User space tracing library.
236
237Most distributions mark the LTTng-modules and LTTng-UST packages as
238optional. In the following sections, the steps to install all three are
239always provided, but note that LTTng-modules is only required if
240you intend to trace the Linux kernel and LTTng-UST is only required if
241you intend to trace user space applications.
242
243This chapter shows how to install the above packages on a Linux system.
244The easiest way is to use the package manager of the system's
245distribution (<<desktop-distributions,desktop>> or
246<<embedded-distributions,embedded>>). Support is also available for
247<<enterprise-distributions,enterprise distributions>>, such as Red Hat
248Enterprise Linux (RHEL) and SUSE Linux Enterprise Server (SLES).
249Otherwise, you can
250<<building-from-source,build the LTTng packages from source>>.
251
252
253[[desktop-distributions]]
254=== Desktop distributions
255
256Official LTTng {revision} packages are available for
257<<ubuntu,Ubuntu>>, <<fedora,Fedora>>, and
258<<opensuse,openSUSE>> (and other RPM-based distributions).
259
260More recent versions of LTTng are available for Debian and Arch Linux.
261
262Should any issue arise when
263following the procedures below, please inform the
264link:/community[community] about it.
265
266
267[[ubuntu]]
268==== Ubuntu
269
270LTTng {revision} is packaged in Ubuntu 15.10 _Wily Werewolf_. For other
271releases of Ubuntu, you need to build and install LTTng {revision}
272<<building-from-source,from source>>. Ubuntu 15.04 _Vivid Vervet_
273ships with link:/docs/v2.5/[LTTng 2.5], whilst
274Ubuntu 16.04 _Xenial Xerus_ ships with
275link:/docs/v2.7/[LTTng 2.7].
276
277To install LTTng {revision} from the official Ubuntu repositories,
278simply use `apt-get`:
279
280[role="term"]
281----
282sudo apt-get install lttng-tools
283sudo apt-get install lttng-modules-dkms
284sudo apt-get install liblttng-ust-dev
285----
286
287If you need to trace
288<<java-application,Java applications>>,
289you need to install the LTTng-UST Java agent also:
290
291[role="term"]
292----
293sudo apt-get install liblttng-ust-agent-java
294----
295
296
297[[fedora]]
298==== Fedora
299
300Fedora 22 and Fedora 23 ship with official LTTng-tools {revision} and
301LTTng-UST {revision} packages. Simply use `yum`:
302
303[role="term"]
304----
305sudo yum install lttng-tools
306sudo yum install lttng-ust
307sudo yum install lttng-ust-devel
308----
309
310LTTng-modules {revision} still needs to be built and installed from
311source. For that, make sure that the `kernel-devel` package is
312already installed beforehand:
313
314[role="term"]
315----
316sudo yum install kernel-devel
317----
318
319Proceed on to fetch
320<<building-from-source,LTTng-modules {revision}'s source>>. Build and
321install it as follows:
322
323[role="term"]
324----
325KERNELDIR=/usr/src/kernels/$(uname -r) make
326sudo make modules_install
327----
328
329NOTE: If you need to trace <<java-application,Java applications>> on
330Fedora, you need to build and install LTTng-UST {revision}
331<<building-from-source,from source>> and use the
332`--enable-java-agent-jul`, `--enable-java-agent-log4j`, or
333`--enable-java-agent-all` options.
334
335
336[[opensuse]]
337==== openSUSE/RPM
338
339openSUSE 13.1 and openSUSE 13.2 have LTTng {revision} packages. To install
340LTTng {revision}, you first need to add an entry to your repository
341configuration. All LTTng repositories are available
342http://download.opensuse.org/repositories/devel:/tools:/lttng/[here].
343For example, the following commands adds the LTTng repository for
344openSUSE{nbsp}13.1:
345
346[role="term"]
347----
348sudo zypper addrepo http://download.opensuse.org/repositories/devel:/tools:/lttng/openSUSE_13.1/devel:tools:lttng.repo
349----
350
351Then, refresh the package database:
352
353[role="term"]
354----
355sudo zypper refresh
356----
357
358and install `lttng-tools`, `lttng-modules` and `lttng-ust-devel`:
359
360[role="term"]
361----
362sudo zypper install lttng-tools
363sudo zypper install lttng-modules
364sudo zypper install lttng-ust-devel
365----
366
367NOTE: If you need to trace <<java-application,Java applications>> on
368openSUSE, you need to build and install LTTng-UST {revision}
369<<building-from-source,from source>> and use the
370`--enable-java-agent-jul`, `--enable-java-agent-log4j`, or
371`--enable-java-agent-all` options.
372
373
374[[embedded-distributions]]
375=== Embedded distributions
376
377LTTng is packaged by two popular
378embedded Linux distributions: <<buildroot,Buildroot>> and
379<<oe-yocto,OpenEmbedded/Yocto>>.
380
381
382[[buildroot]]
383==== Buildroot
384
385LTTng {revision} is available in Buildroot since Buildroot 2015.05. The
386LTTng packages are named `lttng-tools`, `lttng-modules`, and `lttng-libust`.
387
388To enable them, start the Buildroot configuration menu as usual:
389
390[role="term"]
391----
392make menuconfig
393----
394
395In:
396
397* _Kernel_: make sure _Linux kernel_ is enabled
398* _Toolchain_: make sure the following options are enabled:
399** _Enable large file (files > 2GB) support_
400** _Enable WCHAR support_
401
402In _Target packages_/_Debugging, profiling and benchmark_, enable
403_lttng-modules_ and _lttng-tools_. In
404_Target packages_/_Libraries_/_Other_, enable _lttng-libust_.
405
406NOTE: If you need to trace <<java-application,Java applications>> on
407Buildroot, you need to build and install LTTng-UST {revision}
408<<building-from-source,from source>> and use the
409`--enable-java-agent-jul`, `--enable-java-agent-log4j`, or
410`--enable-java-agent-all` options.
411
412
413[[oe-yocto]]
414==== OpenEmbedded/Yocto
415
416LTTng {revision} recipes are available in the
417http://layers.openembedded.org/layerindex/branch/master/layer/openembedded-core/[`openembedded-core`]
418layer of OpenEmbedded since February 8th, 2015 under the following names:
419
420* `lttng-tools`
421* `lttng-modules`
422* `lttng-ust`
423
424Using BitBake, the simplest way to include LTTng recipes in your
425target image is to add them to `IMAGE_INSTALL_append` in
426path:{conf/local.conf}:
427
428----
429IMAGE_INSTALL_append = " lttng-tools lttng-modules lttng-ust"
430----
431
432If you're using Hob, click _Edit image recipe_ once you have selected
433a machine and an image recipe. Then, under the _All recipes_ tab, search
434for `lttng` and include the three LTTng recipes.
435
436NOTE: If you need to trace <<java-application,Java applications>> on
437OpenEmbedded/Yocto, you need to build and install LTTng-UST {revision}
438<<building-from-source,from source>> and use the
439`--enable-java-agent-jul`, `--enable-java-agent-log4j`, or
440`--enable-java-agent-all` options.
441
442
443[[enterprise-distributions]]
444=== Enterprise distributions (RHEL, SLES)
445
446To install LTTng on enterprise Linux distributions
447(such as RHEL and SLES), please see
448http://packages.efficios.com/[EfficiOS Enterprise Packages].
449
450
451[[building-from-source]]
452=== Building from source
453
454As <<installing-lttng,previously stated>>, LTTng is shipped as
455three packages: LTTng-tools, LTTng-modules, and LTTng-UST. LTTng-tools
456contains everything needed to control tracing sessions, while
457LTTng-modules is only needed for Linux kernel tracing and LTTng-UST is
458only needed for user space tracing.
459
460The tarballs are available in the
461http://lttng.org/download#build-from-source[Download section]
462of the LTTng website.
463
464Please refer to the path:{README.md} files provided by each package to
465properly build and install them.
466
467TIP: The aforementioned path:{README.md} files
468are rendered as rich text when https://github.com/lttng[viewed on GitHub].
469
470
471[[getting-started]]
472== Getting started with LTTng
473
474This is a small guide to get started quickly with LTTng kernel and user
475space tracing. For a more thorough understanding of LTTng and intermediate
476to advanced use cases and, see <<understanding-lttng,Understanding LTTng>>
477and <<using-lttng,Using LTTng>>.
478
479Before reading this guide, make sure LTTng
480<<installing-lttng,is installed>>. LTTng-tools is required. Also install
481LTTng-modules for
482<<tracing-the-linux-kernel,tracing the Linux kernel>> and LTTng-UST
483for
484<<tracing-your-own-user-application,tracing your own user space applications>>.
485When the traces are finally written and complete, the
486<<viewing-and-analyzing-your-traces,Viewing and analyzing your traces>>
487section of this chapter will help you analyze your tracepoint events
488to investigate.
489
490
491[[tracing-the-linux-kernel]]
492=== Tracing the Linux kernel
493
494Make sure LTTng-tools and LTTng-modules packages
495<<installing-lttng,are installed>>.
496
497Since you're about to trace the Linux kernel itself, let's look at the
498available kernel events using the `lttng` tool, which has a
499Git-like command line structure:
500
501[role="term"]
502----
503lttng list --kernel
504----
505
506Before tracing, you need to create a session:
507
508[role="term"]
509----
510sudo lttng create
511----
512
513TIP: You can avoid using `sudo` in the previous and following commands
514if your user is a member of the <<lttng-sessiond,tracing group>>.
515
516Let's now enable some events for this session:
517
518[role="term"]
519----
520sudo lttng enable-event --kernel sched_switch,sched_process_fork
521----
522
523Or you might want to simply enable all available kernel events (beware
524that trace files grow rapidly when doing this):
525
526[role="term"]
527----
528sudo lttng enable-event --kernel --all
529----
530
531Start tracing:
532
533[role="term"]
534----
535sudo lttng start
536----
537
538By default, traces are saved in
539+\~/lttng-traces/__name__-__date__-__time__+,
540where +__name__+ is the session name.
541
542When you're done tracing:
543
544[role="term"]
545----
546sudo lttng stop
547sudo lttng destroy
548----
549
550Although `destroy` looks scary here, it doesn't actually destroy the
551written trace files: it only destroys the tracing session.
552
553What's next? Have a look at
554<<viewing-and-analyzing-your-traces,Viewing and analyzing your traces>>
555to view and analyze the trace you just recorded.
556
557
558[[tracing-your-own-user-application]]
559=== Tracing your own user application
560
561The previous section helped you create a trace out of Linux kernel
562events. This section steps you through a simple example showing you how
563to trace a _Hello world_ program written in C.
564
565Make sure the LTTng-tools and LTTng-UST packages
566<<installing-lttng,are installed>>.
567
568Tracing is just like having `printf()` calls at specific locations of
569your source code, albeit LTTng is much faster and more flexible than
570`printf()`. In the LTTng realm, **`tracepoint()`** is analogous to
571`printf()`.
572
573Unlike `printf()`, though, `tracepoint()` does not use a format string to
574know the types of its arguments: the formats of all tracepoints must be
575defined before using them. So before even writing our _Hello world_ program,
576we need to define the format of our tracepoint. This is done by creating a
577**tracepoint provider**, which consists of a tracepoint provider header
578(`.h` file) and a tracepoint provider definition (`.c` file).
579
580The tracepoint provider header contains some boilerplate as well as a
581list of tracepoint definitions and other optional definition entries
582which we skip for this quickstart. Each tracepoint is defined using the
583`TRACEPOINT_EVENT()` macro. For each tracepoint, you must provide:
584
585* a **provider name**, which is the "scope" or namespace of this
586 tracepoint (this usually includes the company and project names)
587* a **tracepoint name**
588* a **list of arguments** for the eventual `tracepoint()` call, each
589 item being:
590** the argument C type
591** the argument name
592* a **list of fields**, which correspond to the actual fields of the
593 recorded events for this tracepoint
594
595Here's an example of a simple tracepoint provider header with two
596arguments: an integer and a string:
597
598[source,c]
599----
600#undef TRACEPOINT_PROVIDER
601#define TRACEPOINT_PROVIDER hello_world
602
603#undef TRACEPOINT_INCLUDE
604#define TRACEPOINT_INCLUDE "./hello-tp.h"
605
606#if !defined(_HELLO_TP_H) || defined(TRACEPOINT_HEADER_MULTI_READ)
607#define _HELLO_TP_H
608
609#include <lttng/tracepoint.h>
610
611TRACEPOINT_EVENT(
612 hello_world,
613 my_first_tracepoint,
614 TP_ARGS(
615 int, my_integer_arg,
616 char*, my_string_arg
617 ),
618 TP_FIELDS(
619 ctf_string(my_string_field, my_string_arg)
620 ctf_integer(int, my_integer_field, my_integer_arg)
621 )
622)
623
624#endif /* _HELLO_TP_H */
625
626#include <lttng/tracepoint-event.h>
627----
628
629The exact syntax is well explained in the
630<<c-application,C application>> instrumentation guide of the
631<<using-lttng,Using LTTng>> chapter, as well as in
632man:lttng-ust(3).
633
634Save the above snippet as path:{hello-tp.h}.
635
636Write the tracepoint provider definition as path:{hello-tp.c}:
637
638[source,c]
639----
640#define TRACEPOINT_CREATE_PROBES
641#define TRACEPOINT_DEFINE
642
643#include "hello-tp.h"
644----
645
646Create the tracepoint provider:
647
648[role="term"]
649----
650gcc -c -I. hello-tp.c
651----
652
653Now, by including path:{hello-tp.h} in your own application, you may use the
654tracepoint defined above by properly refering to it when calling
655`tracepoint()`:
656
657[source,c]
658----
659#include <stdio.h>
660#include "hello-tp.h"
661
662int main(int argc, char *argv[])
663{
664 int x;
665
666 puts("Hello, World!\nPress Enter to continue...");
667
668 /*
669 * The following getchar() call is only placed here for the purpose
670 * of this demonstration, for pausing the application in order for
671 * you to have time to list its events. It's not needed otherwise.
672 */
673 getchar();
674
675 /*
676 * A tracepoint() call. Arguments, as defined in hello-tp.h:
677 *
678 * 1st: provider name (always)
679 * 2nd: tracepoint name (always)
680 * 3rd: my_integer_arg (first user-defined argument)
681 * 4th: my_string_arg (second user-defined argument)
682 *
683 * Notice the provider and tracepoint names are NOT strings;
684 * they are in fact parts of variables created by macros in
685 * hello-tp.h.
686 */
687 tracepoint(hello_world, my_first_tracepoint, 23, "hi there!");
688
689 for (x = 0; x < argc; ++x) {
690 tracepoint(hello_world, my_first_tracepoint, x, argv[x]);
691 }
692
693 puts("Quitting now!");
694
695 tracepoint(hello_world, my_first_tracepoint, x * x, "x^2");
696
697 return 0;
698}
699----
700
701Save this as path:{hello.c}, next to path:{hello-tp.c}.
702
703Notice path:{hello-tp.h}, the tracepoint provider header, is included
704by path:{hello.c}.
705
706You are now ready to compile the application with LTTng-UST support:
707
708[role="term"]
709----
710gcc -c hello.c
711gcc -o hello hello.o hello-tp.o -llttng-ust -ldl
712----
713
714Here's the whole build process:
715
716[role="img-100"]
717.User space tracing's build process.
718image::ust-flow.png[]
719
720If you followed the
721<<tracing-the-linux-kernel,Tracing the Linux kernel>> tutorial, the
722following steps should look familiar.
723
724First, run the application with a few arguments:
725
726[role="term"]
727----
728./hello world and beyond
729----
730
731You should see
732
733----
734Hello, World!
735Press Enter to continue...
736----
737
738Use the `lttng` tool to list all available user space events:
739
740[role="term"]
741----
742lttng list --userspace
743----
744
745You should see the `hello_world:my_first_tracepoint` tracepoint listed
746under the `./hello` process.
747
748Create a tracing session:
749
750[role="term"]
751----
752lttng create
753----
754
755Enable the `hello_world:my_first_tracepoint` tracepoint:
756
757[role="term"]
758----
759lttng enable-event --userspace hello_world:my_first_tracepoint
760----
761
762Start tracing:
763
764[role="term"]
765----
766lttng start
767----
768
769Go back to the running `hello` application and press Enter. All `tracepoint()`
770calls are executed and the program finally exits.
771
772Stop tracing:
773
774[role="term"]
775----
776lttng stop
777----
778
779Done! You may use `lttng view` to list the recorded events. This command
780starts http://diamon.org/babeltrace[`babeltrace`]
781in the background, if it's installed:
782
783[role="term"]
784----
785lttng view
786----
787
788should output something like:
789
790----
791[18:10:27.684304496] (+?.?????????) hostname hello_world:my_first_tracepoint: { cpu_id = 0 }, { my_string_field = "hi there!", my_integer_field = 23 }
792[18:10:27.684338440] (+0.000033944) hostname hello_world:my_first_tracepoint: { cpu_id = 0 }, { my_string_field = "./hello", my_integer_field = 0 }
793[18:10:27.684340692] (+0.000002252) hostname hello_world:my_first_tracepoint: { cpu_id = 0 }, { my_string_field = "world", my_integer_field = 1 }
794[18:10:27.684342616] (+0.000001924) hostname hello_world:my_first_tracepoint: { cpu_id = 0 }, { my_string_field = "and", my_integer_field = 2 }
795[18:10:27.684343518] (+0.000000902) hostname hello_world:my_first_tracepoint: { cpu_id = 0 }, { my_string_field = "beyond", my_integer_field = 3 }
796[18:10:27.684357978] (+0.000014460) hostname hello_world:my_first_tracepoint: { cpu_id = 0 }, { my_string_field = "x^2", my_integer_field = 16 }
797----
798
799When you're done, you may destroy the tracing session, which does _not_
800destroy the generated trace files, leaving them available for further
801analysis:
802
803[role="term"]
804----
805lttng destroy
806----
807
808The next section presents other alternatives to view and analyze your
809LTTng traces.
810
811
812[[viewing-and-analyzing-your-traces]]
813=== Viewing and analyzing your traces
814
815This section describes how to visualize the data gathered after tracing
816the Linux kernel or a user space application.
817
818Many ways exist to read LTTng traces:
819
820* **`babeltrace`** is a command line utility which converts trace formats;
821 it supports the format used by LTTng,
822 CTF, as well as a basic
823 text output which may be ++grep++ed. The `babeltrace` command is
824 part of the
825 http://diamon.org/babeltrace[Babeltrace] project.
826* Babeltrace also includes **Python bindings** so that you may
827 easily open and read an LTTng trace with your own script, benefiting
828 from the power of Python.
829* **http://tracecompass.org/[Trace Compass]**
830 is an Eclipse plugin used to visualize and analyze various types of
831 traces, including LTTng's. It also comes as a standalone application.
832
833LTTng trace files are usually recorded in the dir:{~/lttng-traces} directory.
834Let's now view the trace and perform a basic analysis using
835`babeltrace`.
836
837The simplest way to list all the recorded events of a trace is to pass its
838path to `babeltrace` with no options:
839
840[role="term"]
841----
842babeltrace ~/lttng-traces/my-session
843----
844
845`babeltrace` finds all traces recursively within the given path and
846prints all their events, merging them in order of time.
847
848Listing all the system calls of a Linux kernel trace with their arguments is
849easy with `babeltrace` and `grep`:
850
851[role="term"]
852----
853babeltrace ~/lttng-traces/my-kernel-session | grep sys_
854----
855
856Counting events is also straightforward:
857
858[role="term"]
859----
860babeltrace ~/lttng-traces/my-kernel-session | grep sys_read | wc --lines
861----
862
863The text output of `babeltrace` is useful for isolating events by simple
864matching using `grep` and similar utilities. However, more elaborate filters
865such as keeping only events with a field value falling within a specific range
866are not trivial to write using a shell. Moreover, reductions and even the
867most basic computations involving multiple events are virtually impossible
868to implement.
869
870Fortunately, Babeltrace ships with Python 3 bindings which makes it
871really easy to read the events of an LTTng trace sequentially and compute
872the desired information.
873
874Here's a simple example using the Babeltrace Python bindings. The following
875script accepts an LTTng Linux kernel trace path as its first argument and
876prints the short names of the top 5 running processes on CPU 0 during the
877whole trace:
878
879[source,python]
880----
881import sys
882from collections import Counter
883import babeltrace
884
885
886def top5proc():
887 if len(sys.argv) != 2:
888 msg = 'Usage: python {} TRACEPATH'.format(sys.argv[0])
889 raise ValueError(msg)
890
891 # a trace collection holds one to many traces
892 col = babeltrace.TraceCollection()
893
894 # add the trace provided by the user
895 # (LTTng traces always have the 'ctf' format)
896 if col.add_trace(sys.argv[1], 'ctf') is None:
897 raise RuntimeError('Cannot add trace')
898
899 # this counter dict will hold execution times:
900 #
901 # task command name -> total execution time (ns)
902 exec_times = Counter()
903
904 # this holds the last `sched_switch` timestamp
905 last_ts = None
906
907 # iterate events
908 for event in col.events:
909 # keep only `sched_switch` events
910 if event.name != 'sched_switch':
911 continue
912
913 # keep only events which happened on CPU 0
914 if event['cpu_id'] != 0:
915 continue
916
917 # event timestamp
918 cur_ts = event.timestamp
919
920 if last_ts is None:
921 # we start here
922 last_ts = cur_ts
923
924 # previous task command (short) name
925 prev_comm = event['prev_comm']
926
927 # initialize entry in our dict if not yet done
928 if prev_comm not in exec_times:
929 exec_times[prev_comm] = 0
930
931 # compute previous command execution time
932 diff = cur_ts - last_ts
933
934 # update execution time of this command
935 exec_times[prev_comm] += diff
936
937 # update last timestamp
938 last_ts = cur_ts
939
940 # display top 10
941 for name, ns in exec_times.most_common(5):
942 s = ns / 1000000000
943 print('{:20}{} s'.format(name, s))
944
945
946if __name__ == '__main__':
947 top5proc()
948----
949
950Save this script as path:{top5proc.py} and run it with Python 3, providing the
951path to an LTTng Linux kernel trace as the first argument:
952
953[role="term"]
954----
955python3 top5proc.py ~/lttng-sessions/my-session-.../kernel
956----
957
958Make sure the path you provide is the directory containing actual trace
959files (`channel0_0`, `metadata`, and the rest): the `babeltrace` utility
960recurses directories, but the Python bindings do not.
961
962Here's an example of output:
963
964----
965swapper/0 48.607245889 s
966chromium 7.192738188 s
967pavucontrol 0.709894415 s
968Compositor 0.660867933 s
969Xorg.bin 0.616753786 s
970----
971
972Note that `swapper/0` is the "idle" process of CPU 0 on Linux; since we
973weren't using the CPU that much when tracing, its first position in the list
974makes sense.
975
976
977[[understanding-lttng]]
978== Understanding LTTng
979
980If you're going to use LTTng in any serious way, it is fundamental that
981you become familiar with its core concepts. Technical terms like
982_tracing sessions_, _domains_, _channels_ and _events_ are used over
983and over in the <<using-lttng,Using LTTng>> chapter,
984and it is assumed that you understand what they mean when reading it.
985
986LTTng, as you already know, is a _toolkit_. It would be wrong
987to call it a simple _tool_ since it is composed of multiple interacting
988components. This chapter also describes the latter, providing details
989about their respective roles and how they connect together to form
990the current LTTng ecosystem.
991
992
993[[core-concepts]]
994=== Core concepts
995
996This section explains the various elementary concepts a user has to deal
997with when using LTTng. They are:
998
999* <<tracing-session,tracing session>>
1000* <<domain,domain>>
1001* <<channel,channel>>
1002* <<event,event>>
1003
1004
1005[[tracing-session]]
1006==== Tracing session
1007
1008A _tracing session_ is--like any session--a container of
1009state. Anything that is done when tracing using LTTng happens in the
1010scope of a tracing session. In this regard, it is analogous to a bank
1011website's session: you can't interact online with your bank account
1012unless you are logged in a session, except for reading a few static
1013webpages (LTTng, too, can report some static information that does not
1014need a created tracing session).
1015
1016A tracing session holds the following attributes and objects (some of
1017which are described in the following sections):
1018
1019* a name
1020* the tracing state (tracing started or stopped)
1021* the trace data output path/URL (local path or sent over the network)
1022* a mode (normal, snapshot or live)
1023* the snapshot output paths/URLs (if applicable)
1024* for each <<domain,domain>>, a list of <<channel,channels>>
1025* for each channel:
1026** a name
1027** the channel state (enabled or disabled)
1028** its parameters (event loss mode, sub-buffers size and count,
1029 timer periods, output type, trace files size and count, and the rest)
1030** a list of added context information
1031** a list of <<event,events>>
1032* for each event:
1033** its state (enabled or disabled)
1034** a list of instrumentation points (tracepoints, system calls,
1035 dynamic probes, other types of probes)
1036** associated log levels
1037** a filter expression
1038
1039All this information is completely isolated between tracing sessions.
1040As you can see in the list above, even the tracing state
1041is a per-tracing session attribute, so that you may trace your target
1042system/application in a given tracing session with a specific
1043configuration while another one stays inactive.
1044
1045[role="img-100"]
1046.A _tracing session_ is a container of domains, channels, and events.
1047image::concepts.png[]
1048
1049Conceptually, a tracing session is a per-user object; the
1050<<plumbing,Plumbing>> section shows how this is actually
1051implemented. Any user may create as many concurrent tracing sessions
1052as desired.
1053
1054[role="img-100"]
1055.Each user may create as many tracing sessions as desired.
1056image::many-sessions.png[]
1057
1058The trace data generated in a tracing session may be either saved
1059to disk, sent over the network or not saved at all (in which case
1060snapshots may still be saved to disk or sent to a remote machine).
1061
1062
1063[[domain]]
1064==== Domain
1065
1066A tracing _domain_ is the official term the LTTng project uses to
1067designate a tracer category.
1068
1069There are currently four known domains:
1070
1071* Linux kernel
1072* user space
1073* `java.util.logging` (JUL)
1074* log4j
1075
1076Different tracers expose common features in their own interfaces, but,
1077from a user's perspective, you still need to target a specific type of
1078tracer to perform some actions. For example, since both kernel and user
1079space tracers support named tracepoints (probes manually inserted in
1080source code), you need to specify which one is concerned when enabling
1081an event because both domains could have existing events with the same
1082name.
1083
1084Some features are not available in all domains. Filtering enabled
1085events using custom expressions, for example, is currently not
1086supported in the kernel domain, but support could be added in the
1087future.
1088
1089
1090[[channel]]
1091==== Channel
1092
1093A _channel_ is a set of events with specific parameters and potential
1094added context information. Channels have unique names per domain within
1095a tracing session. A given event is always registered to at least one
1096channel; having the same enabled event in two channels makes
1097this event being recorded twice everytime it occurs.
1098
1099Channels may be individually enabled or disabled. Occurring events of
1100a disabled channel never make it to recorded events.
1101
1102The fundamental role of a channel is to keep a shared ring buffer, where
1103events are eventually recorded by the tracer and consumed by a consumer
1104daemon. This internal ring buffer is divided into many sub-buffers of
1105equal size.
1106
1107Channels, when created, may be fine-tuned thanks to a few parameters,
1108many of them related to sub-buffers. The following subsections explain
1109what those parameters are and in which situations you should manually
1110adjust them.
1111
1112
1113[[channel-overwrite-mode-vs-discard-mode]]
1114===== Overwrite and discard event loss modes
1115
1116As previously mentioned, a channel's ring buffer is divided into many
1117equally sized sub-buffers.
1118
1119As events occur, they are serialized as trace data into a specific
1120sub-buffer (yellow arc in the following animation) until it is full:
1121when this happens, the sub-buffer is marked as consumable (red) and
1122another, _empty_ (white) sub-buffer starts receiving the following
1123events. The marked sub-buffer is eventually consumed by a consumer
1124daemon (returns to white).
1125
1126[NOTE]
1127[role="docsvg-channel-subbuf-anim"]
1128====
1129{note-no-anim}
1130====
1131
1132In an ideal world, sub-buffers are consumed faster than filled, like it
1133is the case above. In the real world, however, all sub-buffers could be
1134full at some point, leaving no space to record the following events. By
1135design, LTTng is a _non-blocking_ tracer: when no empty sub-buffer
1136exists, losing events is acceptable when the alternative would be to
1137cause substantial delays in the instrumented application's execution.
1138LTTng privileges performance over integrity, aiming at perturbing the
1139traced system as little as possible in order to make tracing of subtle
1140race conditions and rare interrupt cascades possible.
1141
1142When it comes to losing events because no empty sub-buffer is available,
1143the channel's _event loss mode_ determines what to do amongst:
1144
1145Discard::
1146 Drop the newest events until a sub-buffer is released.
1147
1148Overwrite::
1149 Clear the sub-buffer containing the oldest recorded
1150 events and start recording the newest events there. This mode is
1151 sometimes called _flight recorder mode_ because it behaves like a
1152 flight recorder: always keep a fixed amount of the latest data.
1153
1154Which mechanism you should choose depends on your context: prioritize
1155the newest or the oldest events in the ring buffer?
1156
1157Beware that, in overwrite mode, a whole sub-buffer is abandoned as soon
1158as a new event doesn't find an empty sub-buffer, whereas in discard
1159mode, only the event that doesn't fit is discarded.
1160
1161Also note that a count of lost events is incremented and saved in
1162the trace itself when an event is lost in discard mode, whereas no
1163information is kept when a sub-buffer gets overwritten before being
1164committed.
1165
1166There are known ways to decrease your probability of losing events. The
1167next section shows how tuning the sub-buffers count and size can be
1168used to virtually stop losing events.
1169
1170
1171[[channel-subbuf-size-vs-subbuf-count]]
1172===== Sub-buffers count and size
1173
1174For each channel, an LTTng user may set its number of sub-buffers and
1175their size.
1176
1177Note that there is a noticeable tracer's CPU overhead introduced when
1178switching sub-buffers (marking a full one as consumable and switching
1179to an empty one for the following events to be recorded). Knowing this,
1180the following list presents a few practical situations along with how
1181to configure sub-buffers for them:
1182
1183High event throughput::
1184 In general, prefer bigger sub-buffers to
1185 lower the risk of losing events. Having bigger sub-buffers
1186 also ensures a lower sub-buffer switching frequency. The number of
1187 sub-buffers is only meaningful if the channel is enabled in
1188 overwrite mode: in this case, if a sub-buffer overwrite happens, the
1189 other sub-buffers are left unaltered.
1190
1191Low event throughput::
1192 In general, prefer smaller sub-buffers
1193 since the risk of losing events is already low. Since events
1194 happen less frequently, the sub-buffer switching frequency should
1195 remain low and thus the tracer's overhead should not be a problem.
1196
1197Low memory system::
1198 If your target system has a low memory
1199 limit, prefer fewer first, then smaller sub-buffers. Even if the
1200 system is limited in memory, you want to keep the sub-buffers as
1201 big as possible to avoid a high sub-buffer switching frequency.
1202
1203You should know that LTTng uses CTF as its trace format, which means
1204event data is very compact. For example, the average LTTng Linux kernel
1205event weights about 32{nbsp}bytes. A sub-buffer size of 1{nbsp}MiB is
1206thus considered big.
1207
1208The previous situations highlight the major trade-off between a few big
1209sub-buffers and more, smaller sub-buffers: sub-buffer switching
1210frequency vs. how much data is lost in overwrite mode. Assuming a
1211constant event throughput and using the overwrite mode, the two
1212following configurations have the same ring buffer total size:
1213
1214[NOTE]
1215[role="docsvg-channel-subbuf-size-vs-count-anim"]
1216====
1217{note-no-anim}
1218====
1219
1220* **2 sub-buffers of 4 MiB each** lead to a very low sub-buffer
1221 switching frequency, but if a sub-buffer overwrite happens, half of
1222 the recorded events so far (4{nbsp}MiB) are definitely lost.
1223* **8 sub-buffers of 1 MiB each** lead to 4{nbsp}times the tracer's
1224 overhead as the previous configuration, but if a sub-buffer
1225 overwrite happens, only the eighth of events recorded so far are
1226 definitely lost.
1227
1228In discard mode, the sub-buffers count parameter is pointless: use two
1229sub-buffers and set their size according to the requirements of your
1230situation.
1231
1232
1233[[channel-switch-timer]]
1234===== Switch timer
1235
1236The _switch timer_ period is another important configurable feature of
1237channels to ensure periodic sub-buffer flushing.
1238
1239When the _switch timer_ fires, a sub-buffer switch happens. This timer
1240may be used to ensure that event data is consumed and committed to
1241trace files periodically in case of a low event throughput:
1242
1243[NOTE]
1244[role="docsvg-channel-switch-timer"]
1245====
1246{note-no-anim}
1247====
1248
1249It's also convenient when big sub-buffers are used to cope with
1250sporadic high event throughput, even if the throughput is normally
1251lower.
1252
1253
1254[[channel-buffering-schemes]]
1255===== Buffering schemes
1256
1257In the user space tracing domain, two **buffering schemes** are
1258available when creating a channel:
1259
1260Per-PID buffering::
1261 Keep one ring buffer per process.
1262
1263Per-UID buffering::
1264 Keep one ring buffer for all processes of a single user.
1265
1266The per-PID buffering scheme consumes more memory than the per-UID
1267option if more than one process is instrumented for LTTng-UST. However,
1268per-PID buffering ensures that one process having a high event
1269throughput won't fill all the shared sub-buffers, only its own.
1270
1271The Linux kernel tracing domain only has one available buffering scheme
1272which is to use a single ring buffer for the whole system.
1273
1274
1275[[event]]
1276==== Event
1277
1278An _event_, in LTTng's realm, is a term often used metonymically,
1279having multiple definitions depending on the context:
1280
1281. When tracing, an event is a _point in space-time_. Space, in a
1282 tracing context, is the set of all executable positions of a
1283 compiled application by a logical processor. When a program is
1284 executed by a processor and some instrumentation point, or
1285 _probe_, is encountered, an event occurs. This event is accompanied
1286 by some contextual payload (values of specific variables at this
1287 point of execution) which may or may not be recorded.
1288. In the context of a recorded trace file, the term _event_ implies
1289 a _recorded event_.
1290. When configuring a tracing session, _enabled events_ refer to
1291 specific rules which could lead to the transfer of actual
1292 occurring events (1) to recorded events (2).
1293
1294The whole <<core-concepts,Core concepts>> section focuses on the
1295third definition. An event is always registered to _one or more_
1296channels and may be enabled or disabled at will per channel. A disabled
1297event never leads to a recorded event, even if its channel is enabled.
1298
1299An event (3) is enabled with a few conditions that must _all_ be met
1300when an event (1) happens in order to generate a recorded event (2):
1301
1302. A _probe_ or group of probes in the traced application must be
1303 executed.
1304. **Optionally**, the probe must have a log level matching a
1305 log level range specified when enabling the event.
1306. **Optionally**, the occurring event must satisfy a custom
1307 expression, or _filter_, specified when enabling the event.
1308
1309
1310[[plumbing]]
1311=== Plumbing
1312
1313The previous section described the concepts at the heart of LTTng.
1314This section summarizes LTTng's implementation: how those objects are
1315managed by different applications and libraries working together to
1316form the toolkit.
1317
1318
1319[[plumbing-overview]]
1320==== Overview
1321
1322As <<installing-lttng,mentioned previously>>, the whole LTTng suite
1323is made of the LTTng-tools, LTTng-UST, and
1324LTTng-modules packages. Together, they provide different daemons, libraries,
1325kernel modules and command line interfaces. The following tree shows
1326which usable component belongs to which package:
1327
1328* **LTTng-tools**:
1329** session daemon (`lttng-sessiond`)
1330** consumer daemon (`lttng-consumerd`)
1331** relay daemon (`lttng-relayd`)
1332** tracing control library (`liblttng-ctl`)
1333** tracing control command line tool (`lttng`)
1334* **LTTng-UST**:
1335** user space tracing library (`liblttng-ust`) and its headers
1336** preloadable user space tracing helpers
1337 (`liblttng-ust-libc-wrapper`, `liblttng-ust-pthread-wrapper`,
1338 `liblttng-ust-cyg-profile`, `liblttng-ust-cyg-profile-fast`
1339 and `liblttng-ust-dl`)
1340** user space tracepoint code generator command line tool
1341 (`lttng-gen-tp`)
1342** `java.util.logging`/log4j tracepoint providers
1343 (`liblttng-ust-jul-jni` and `liblttng-ust-log4j-jni`) and JAR
1344 file (path:{liblttng-ust-agent.jar})
1345* **LTTng-modules**:
1346** LTTng Linux kernel tracer module
1347** tracing ring buffer kernel modules
1348** many LTTng probe kernel modules
1349
1350The following diagram shows how the most important LTTng components
1351interact. Plain purple arrows represent trace data paths while dashed
1352red arrows indicate control communications. The LTTng relay daemon is
1353shown running on a remote system, although it could as well run on the
1354target (monitored) system.
1355
1356[role="img-100"]
1357.Control and data paths between LTTng components.
1358image::plumbing-26.png[]
1359
1360Each component is described in the following subsections.
1361
1362
1363[[lttng-sessiond]]
1364==== Session daemon
1365
1366At the heart of LTTng's plumbing is the _session daemon_, often called
1367by its command name, `lttng-sessiond`.
1368
1369The session daemon is responsible for managing tracing sessions and
1370what they logically contain (channel properties, enabled/disabled
1371events, and the rest). By communicating locally with instrumented
1372applications (using LTTng-UST) and with the LTTng Linux kernel modules
1373(LTTng-modules), it oversees all tracing activities.
1374
1375One of the many things that `lttng-sessiond` does is to keep
1376track of the available event types. User space applications and
1377libraries actively connect and register to the session daemon when they
1378start. By contrast, `lttng-sessiond` seeks out and loads the appropriate
1379LTTng kernel modules as part of its own initialization. Kernel event
1380types are _pulled_ by `lttng-sessiond`, whereas user space event types
1381are _pushed_ to it by the various user space tracepoint providers.
1382
1383Using a specific inter-process communication protocol with Linux kernel
1384and user space tracers, the session daemon can send channel information
1385so that they are initialized, enable/disable specific probes based on
1386enabled/disabled events by the user, send event filters information to
1387LTTng tracers so that filtering actually happens at the tracer site,
1388start/stop tracing a specific application or the Linux kernel, and more.
1389
1390The session daemon is not useful without some user controlling it,
1391because it's only a sophisticated control interchange and thus
1392doesn't make any decision on its own. `lttng-sessiond` opens a local
1393socket for controlling it, albeit the preferred way to control it is
1394using `liblttng-ctl`, an installed C library hiding the communication
1395protocol behind an easy-to-use API. The `lttng` tool makes use of
1396`liblttng-ctl` to implement a user-friendly command line interface.
1397
1398`lttng-sessiond` does not receive any trace data from instrumented
1399applications; the _consumer daemons_ are the programs responsible for
1400collecting trace data using shared ring buffers. However, the session
1401daemon is the one that must spawn a consumer daemon and establish
1402a control communication with it.
1403
1404Session daemons run on a per-user basis. Knowing this, multiple
1405instances of `lttng-sessiond` may run simultaneously, each belonging
1406to a different user and each operating independently of the others.
1407Only `root`'s session daemon, however, may control LTTng kernel modules
1408(that is, the kernel tracer). With that in mind, if a user has no root
1409access on the target system, he cannot trace the system's kernel, but
1410should still be able to trace its own instrumented applications.
1411
1412It has to be noted that, although only `root`'s session daemon may
1413control the kernel tracer, the `lttng-sessiond` command has a `--group`
1414option which may be used to specify the name of a special user group
1415allowed to communicate with `root`'s session daemon and thus record
1416kernel traces. By default, this group is named `tracing`.
1417
1418If not done yet, the `lttng` tool, by default, automatically starts a
1419session daemon. `lttng-sessiond` may also be started manually:
1420
1421[role="term"]
1422----
1423lttng-sessiond
1424----
1425
1426This starts the session daemon in foreground. Use
1427
1428[role="term"]
1429----
1430lttng-sessiond --daemonize
1431----
1432
1433to start it as a true daemon.
1434
1435To kill the current user's session daemon, `pkill` may be used:
1436
1437[role="term"]
1438----
1439pkill lttng-sessiond
1440----
1441
1442The default `SIGTERM` signal terminates it cleanly.
1443
1444Several other options are available and described in
1445man:lttng-sessiond(8) or by running `lttng-sessiond --help`.
1446
1447
1448[[lttng-consumerd]]
1449==== Consumer daemon
1450
1451The _consumer daemon_, or `lttng-consumerd`, is a program sharing some
1452ring buffers with user applications or the LTTng kernel modules to
1453collect trace data and output it at some place (on disk or sent over
1454the network to an LTTng relay daemon).
1455
1456Consumer daemons are created by a session daemon as soon as events are
1457enabled within a tracing session, well before tracing is activated
1458for the latter. Entirely managed by session daemons,
1459consumer daemons survive session destruction to be reused later,
1460should a new tracing session be created. Consumer daemons are always
1461owned by the same user as their session daemon. When its owner session
1462daemon is killed, the consumer daemon also exits. This is because
1463the consumer daemon is always the child process of a session daemon.
1464Consumer daemons should never be started manually. For this reason,
1465they are not installed in one of the usual locations listed in the
1466`PATH` environment variable. `lttng-sessiond` has, however, a
1467bunch of options (see man:lttng-sessiond(8)) to
1468specify custom consumer daemon paths if, for some reason, a consumer
1469daemon other than the default installed one is needed.
1470
1471There are up to two running consumer daemons per user, whereas only one
1472session daemon may run per user. This is because each process has
1473independent bitness: if the target system runs a mixture of 32-bit and
147464-bit processes, it is more efficient to have separate corresponding
147532-bit and 64-bit consumer daemons. The `root` user is an exception: it
1476may have up to _three_ running consumer daemons: 32-bit and 64-bit
1477instances for its user space applications and one more reserved for
1478collecting kernel trace data.
1479
1480As new tracing domains are added to LTTng, the development community's
1481intent is to minimize the need for additionnal consumer daemon instances
1482dedicated to them. For instance, the `java.util.logging` (JUL) domain
1483events are in fact mapped to the user space domain, thus tracing this
1484particular domain is handled by existing user space domain consumer
1485daemons.
1486
1487
1488[[lttng-relayd]]
1489==== Relay daemon
1490
1491When a tracing session is configured to send its trace data over the
1492network, an LTTng _relay daemon_ must be used at the other end to
1493receive trace packets and serialize them to trace files. This setup
1494makes it possible to trace a target system without ever committing trace
1495data to its local storage, a feature which is useful for embedded
1496systems, amongst others. The command implementing the relay daemon
1497is `lttng-relayd`.
1498
1499The basic use case of `lttng-relayd` is to transfer trace data received
1500over the network to trace files on the local file system. The relay
1501daemon must listen on two TCP ports to achieve this: one control port,
1502used by the target session daemon, and one data port, used by the
1503target consumer daemon. The relay and session daemons agree on common
1504default ports when custom ones are not specified.
1505
1506Since the communication transport protocol for both ports is standard
1507TCP, the relay daemon may be started either remotely or locally (on the
1508target system).
1509
1510While two instances of consumer daemons (32-bit and 64-bit) may run
1511concurrently for a given user, `lttng-relayd` needs only be of its
1512host operating system's bitness.
1513
1514The other important feature of LTTng's relay daemon is the support of
1515_LTTng live_. LTTng live is an application protocol to view events as
1516they arrive. The relay daemon still records events in trace files,
1517but a _tee_ allows to inspect incoming events.
1518
1519[role="img-100"]
1520.The relay daemon creates a _tee_, forwarding the trace data to both trace files and a live viewer.
1521image::lttng-live.png[]
1522
1523Using LTTng live locally thus requires to run a local relay daemon.
1524
1525
1526[[liblttng-ctl-lttng]]
1527==== [[lttng-cli]]Control library and command line interface
1528
1529The LTTng control library, `liblttng-ctl`, can be used to communicate
1530with the session daemon using a C API that hides the underlying
1531protocol's details. `liblttng-ctl` is part of LTTng-tools.
1532
1533`liblttng-ctl` may be used by including its "master" header:
1534
1535[source,c]
1536----
1537#include <lttng/lttng.h>
1538----
1539
1540Some objects are referred by name (C string), such as tracing sessions,
1541but most of them require creating a handle first using
1542`lttng_create_handle()`. The best available developer documentation for
1543`liblttng-ctl` is, for the moment, its installed header files as such.
1544Every function/structure is thoroughly documented.
1545
1546The `lttng` program is the _de facto_ standard user interface to
1547control LTTng tracing sessions. `lttng` uses `liblttng-ctl` to
1548communicate with session daemons behind the scenes.
1549Its man page, man:lttng(1), is exhaustive, as well as its command
1550line help (+lttng _cmd_ --help+, where +_cmd_+ is the command name).
1551
1552The <<controlling-tracing,Controlling tracing>> section is a feature
1553tour of the `lttng` tool.
1554
1555
1556[[lttng-ust]]
1557==== User space tracing library
1558
1559The user space tracing part of LTTng is possible thanks to the user
1560space tracing library, `liblttng-ust`, which is part of the LTTng-UST
1561package.
1562
1563`liblttng-ust` provides header files containing macros used to define
1564tracepoints and create tracepoint providers, as well as a shared object
1565that must be linked to individual applications to connect to and
1566communicate with a session daemon and a consumer daemon as soon as the
1567application starts.
1568
1569The exact mechanism by which an application is registered to the
1570session daemon is beyond the scope of this documentation. The only thing
1571you need to know is that, since the library constructor does this job
1572automatically, tracepoints may be safely inserted anywhere in the source
1573code without prior manual initialization of `liblttng-ust`.
1574
1575The `liblttng-ust`-session daemon collaboration also provides an
1576interesting feature: user space events may be enabled _before_
1577applications actually start. By doing this and starting tracing before
1578launching the instrumented application, you make sure that even the
1579earliest occurring events can be recorded.
1580
1581The <<c-application,C application>> instrumenting guide of the
1582<<using-lttng,Using LTTng>> chapter focuses on using `liblttng-ust`:
1583instrumenting, building/linking and running a user application.
1584
1585
1586[[lttng-modules]]
1587==== LTTng kernel modules
1588
1589The LTTng Linux kernel modules provide everything needed to trace the
1590Linux kernel: various probes, a ring buffer implementation for a
1591consumer daemon to read trace data and the tracer itself.
1592
1593Only in exceptional circumstances should you ever need to load the
1594LTTng kernel modules manually: it is normally the responsability of
1595`root`'s session daemon to do so. Even if you were to develop your
1596own LTTng probe module--for tracing a custom kernel or some kernel
1597module (this topic is covered in the
1598<<instrumenting-linux-kernel,Linux kernel>> instrumenting guide of
1599the <<using-lttng,Using LTTng>> chapter)&#8212;you
1600should use the `--extra-kmod-probes` option of the session daemon to
1601append your probe to the default list. The session and consumer daemons
1602of regular users do not interact with the LTTng kernel modules at all.
1603
1604LTTng kernel modules are installed, by default, in
1605+/usr/lib/modules/_release_/extra+, where +_release_+ is the
1606kernel release (see `uname --kernel-release`).
1607
1608
1609[[using-lttng]]
1610== Using LTTng
1611
1612Using LTTng involves two main activities: **instrumenting** and
1613**controlling tracing**.
1614
1615_<<instrumenting,Instrumenting>>_ is the process of inserting probes
1616into some source code. It can be done manually, by writing tracepoint
1617calls at specific locations in the source code of the program to trace,
1618or more automatically using dynamic probes (address in assembled code,
1619symbol name, function entry/return, and others).
1620
1621It has to be noted that, as an LTTng user, you may not have to worry
1622about the instrumentation process. Indeed, you may want to trace a
1623program already instrumented. As an example, the Linux kernel is
1624thoroughly instrumented, which is why you can trace it without caring
1625about adding probes.
1626
1627_<<controlling-tracing,Controlling tracing>>_ is everything
1628that can be done by the LTTng session daemon, which is controlled using
1629`liblttng-ctl` or its command line utility, `lttng`: creating tracing
1630sessions, listing tracing sessions and events, enabling/disabling
1631events, starting/stopping the tracers, taking snapshots, amongst many
1632other commands.
1633
1634This chapter is a complete user guide of both activities,
1635with common use cases of LTTng exposed throughout the text. It is
1636assumed that you are familiar with LTTng's concepts (events, channels,
1637domains, tracing sessions) and that you understand the roles of its
1638components (daemons, libraries, command line tools); if not, we invite
1639you to read the <<understanding-lttng,Understanding LTTng>> chapter
1640before you begin reading this one.
1641
1642If you're new to LTTng, we suggest that you rather start with the
1643<<getting-started,Getting started>> small guide first, then come
1644back here to broaden your knowledge.
1645
1646If you're only interested in tracing the Linux kernel with its current
1647instrumentation, you may skip the
1648<<instrumenting,Instrumenting>> section.
1649
1650
1651[[instrumenting]]
1652=== Instrumenting
1653
1654There are many examples of tracing and monitoring in our everyday life.
1655You have access to real-time and historical weather reports and forecasts
1656thanks to weather stations installed around the country. You know your
1657possibly hospitalized friends' and family's hearts are safe thanks to
1658electrocardiography. You make sure not to drive your car too fast
1659and have enough fuel to reach your destination thanks to gauges visible
1660on your dashboard.
1661
1662All the previous examples have something in common: they rely on
1663**probes**. Without electrodes attached to the surface of a body's
1664skin, cardiac monitoring would be futile.
1665
1666LTTng, as a tracer, is no different from the real life examples above.
1667If you're about to trace a software system or, put in other words, record its
1668history of execution, you better have probes in the subject you're
1669tracing: the actual software. Various ways were developed to do this.
1670The most straightforward one is to manually place probes, called
1671_tracepoints_, in the software's source code. The Linux kernel tracing
1672domain also allows probes added dynamically.
1673
1674If you're only interested in tracing the Linux kernel, it may very well
1675be that your tracing needs are already appropriately covered by LTTng's
1676built-in Linux kernel tracepoints and other probes. Or you may be in
1677possession of a user space application which has already been
1678instrumented. In such cases, the work resides entirely in the design
1679and execution of tracing sessions, allowing you to jump to
1680<<controlling-tracing,Controlling tracing>> right now.
1681
1682This chapter focuses on the following use cases of instrumentation:
1683
1684* <<c-application,C>> and <<cxx-application,$$C++$$>> applications
1685* <<prebuilt-ust-helpers,prebuilt user space tracing helpers>>
1686* <<java-application,Java application>>
1687* <<instrumenting-linux-kernel,Linux kernel>> module or the
1688 kernel itself
1689* the <<proc-lttng-logger-abi,path:{/proc/lttng-logger} ABI>>
1690
1691Some advanced techniques are also presented at the very end of this
1692chapter.
1693
1694
1695[[c-application]]
1696==== C application
1697
1698Instrumenting a C (or $$C++$$) application, be it an executable program
1699or a library, implies using LTTng-UST, the
1700user space tracing component of LTTng. For C/$$C++$$ applications, the
1701LTTng-UST package includes a dynamically loaded library
1702(`liblttng-ust`), C headers and the `lttng-gen-tp` command line utility.
1703
1704Since C and $$C++$$ are the base languages of virtually all other
1705programming languages
1706(Java virtual machine, Python, Perl, PHP and Node.js interpreters, to
1707name a few), implementing user space tracing for an unsupported language
1708is just a matter of using the LTTng-UST C API at the right places.
1709
1710The usual work flow to instrument a user space C application with
1711LTTng-UST is:
1712
1713. Define tracepoints (actual probes)
1714. Write tracepoint providers
1715. Insert tracepoints into target source code
1716. Package (build) tracepoint providers
1717. Build user application and link it with tracepoint providers
1718
1719The steps above are discussed in greater detail in the following
1720subsections.
1721
1722
1723[[tracepoint-provider]]
1724===== Tracepoint provider
1725
1726Before jumping into defining tracepoints and inserting
1727them into the application source code, you must understand what a
1728_tracepoint provider_ is.
1729
1730For the sake of this guide, consider the following two files:
1731
1732[source,c]
1733.path:{tp.h}
1734----
1735#undef TRACEPOINT_PROVIDER
1736#define TRACEPOINT_PROVIDER my_provider
1737
1738#undef TRACEPOINT_INCLUDE
1739#define TRACEPOINT_INCLUDE "./tp.h"
1740
1741#if !defined(_TP_H) || defined(TRACEPOINT_HEADER_MULTI_READ)
1742#define _TP_H
1743
1744#include <lttng/tracepoint.h>
1745
1746TRACEPOINT_EVENT(
1747 my_provider,
1748 my_first_tracepoint,
1749 TP_ARGS(
1750 int, my_integer_arg,
1751 char*, my_string_arg
1752 ),
1753 TP_FIELDS(
1754 ctf_string(my_string_field, my_string_arg)
1755 ctf_integer(int, my_integer_field, my_integer_arg)
1756 )
1757)
1758
1759TRACEPOINT_EVENT(
1760 my_provider,
1761 my_other_tracepoint,
1762 TP_ARGS(
1763 int, my_int
1764 ),
1765 TP_FIELDS(
1766 ctf_integer(int, some_field, my_int)
1767 )
1768)
1769
1770#endif /* _TP_H */
1771
1772#include <lttng/tracepoint-event.h>
1773----
1774
1775[source,c]
1776.path:{tp.c}
1777----
1778#define TRACEPOINT_CREATE_PROBES
1779
1780#include "tp.h"
1781----
1782
1783The two files above are defining a _tracepoint provider_. A tracepoint
1784provider is some sort of namespace for _tracepoint definitions_. Tracepoint
1785definitions are written above with the `TRACEPOINT_EVENT()` macro, and allow
1786eventual `tracepoint()` calls respecting their definitions to be inserted
1787into the user application's C source code (we explore this in a
1788later section).
1789
1790Many tracepoint definitions may be part of the same tracepoint provider
1791and many tracepoint providers may coexist in a user space application. A
1792tracepoint provider is packaged either:
1793
1794* directly into an existing user application's C source file
1795* as an object file
1796* as a static library
1797* as a shared library
1798
1799The two files above, path:{tp.h} and path:{tp.c}, show a typical template for
1800writing a tracepoint provider. LTTng-UST was designed so that two
1801tracepoint providers should not be defined in the same header file.
1802
1803We will now go through the various parts of the above files and
1804give them a meaning. As you may have noticed, the LTTng-UST API for
1805C/$$C++$$ applications is some preprocessor sorcery. The LTTng-UST macros
1806used in your application and those in the LTTng-UST headers are
1807combined to produce actual source code needed to make tracing possible
1808using LTTng.
1809
1810Let's start with the header file, path:{tp.h}. It begins with
1811
1812[source,c]
1813----
1814#undef TRACEPOINT_PROVIDER
1815#define TRACEPOINT_PROVIDER my_provider
1816----
1817
1818`TRACEPOINT_PROVIDER` defines the name of the provider to which the
1819following tracepoint definitions belong. It is used internally by
1820LTTng-UST headers and _must_ be defined. Since `TRACEPOINT_PROVIDER`
1821could have been defined by another header file also included by the same
1822C source file, the best practice is to undefine it first.
1823
1824NOTE: Names in LTTng-UST follow the C
1825_identifier_ syntax (starting with a letter and containing either
1826letters, numbers or underscores); they are _not_ C strings
1827(not surrounded by double quotes). This is because LTTng-UST macros
1828use those identifier-like strings to create symbols (named types and
1829variables).
1830
1831The tracepoint provider is a group of tracepoint definitions; its chosen
1832name should reflect this. A hierarchy like Java packages is recommended,
1833using underscores instead of dots, for example,
1834`org_company_project_component`.
1835
1836Next is `TRACEPOINT_INCLUDE`:
1837
1838[source,c]
1839----
1840#undef TRACEPOINT_INCLUDE
1841#define TRACEPOINT_INCLUDE "./tp.h"
1842----
1843
1844This little bit of instrospection is needed by LTTng-UST to include
1845your header at various predefined places.
1846
1847Include guard follows:
1848
1849[source,c]
1850----
1851#if !defined(_TP_H) || defined(TRACEPOINT_HEADER_MULTI_READ)
1852#define _TP_H
1853----
1854
1855Add these precompiler conditionals to ensure the tracepoint event
1856generation can include this file more than once.
1857
1858The `TRACEPOINT_EVENT()` macro is defined in a LTTng-UST header file which
1859must be included:
1860
1861[source,c]
1862----
1863#include <lttng/tracepoint.h>
1864----
1865
1866This also allows the application to use the `tracepoint()` macro.
1867
1868Next is a list of `TRACEPOINT_EVENT()` macro calls which create the
1869actual tracepoint definitions. We skip this for the moment and
1870come back to how to use `TRACEPOINT_EVENT()`
1871<<defining-tracepoints,in a later section>>. Just pay attention to
1872the first argument: it's always the name of the tracepoint provider
1873being defined in this header file.
1874
1875End of include guard:
1876
1877[source,c]
1878----
1879#endif /* _TP_H */
1880----
1881
1882Finally, include `<lttng/tracepoint-event.h>` to expand the macros:
1883
1884[source,c]
1885----
1886#include <lttng/tracepoint-event.h>
1887----
1888
1889That's it for path:{tp.h}. Of course, this is only a header file; it must be
1890included in some C source file to actually use it. This is the job of
1891path:{tp.c}:
1892
1893[source,c]
1894----
1895#define TRACEPOINT_CREATE_PROBES
1896
1897#include "tp.h"
1898----
1899
1900When `TRACEPOINT_CREATE_PROBES` is defined, the macros used in path:{tp.h},
1901which is included just after, actually create the source code for
1902LTTng-UST probes (global data structures and functions) out of your
1903tracepoint definitions. How exactly this is done is out of this text's scope.
1904`TRACEPOINT_CREATE_PROBES` is discussed further
1905in
1906<<building-tracepoint-providers-and-user-application,Building/linking
1907tracepoint providers and the user application>>.
1908
1909You could include other header files like path:{tp.h} here to create the probes
1910of different tracepoint providers, for example:
1911
1912[source,c]
1913----
1914#define TRACEPOINT_CREATE_PROBES
1915
1916#include "tp1.h"
1917#include "tp2.h"
1918----
1919
1920The rule is: probes of a given tracepoint provider
1921must be created in exactly one source file. This source file could be one
1922of your project's; it doesn't have to be on its own like
1923path:{tp.c}, although
1924<<building-tracepoint-providers-and-user-application,a later section>>
1925shows that doing so allows packaging the tracepoint providers
1926independently and keep them out of your application, also making it
1927possible to reuse them between projects.
1928
1929The following sections explain how to define tracepoints, how to use the
1930`tracepoint()` macro to instrument your user space C application and how
1931to build/link tracepoint providers and your application with LTTng-UST
1932support.
1933
1934
1935[[lttng-gen-tp]]
1936===== Using `lttng-gen-tp`
1937
1938LTTng-UST ships with `lttng-gen-tp`, a handy command line utility for
1939generating most of the stuff discussed above. It takes a _template file_,
1940with a name usually ending with the `.tp` extension, containing only
1941tracepoint definitions, and outputs a tracepoint provider (either a C
1942source file or a precompiled object file) with its header file.
1943
1944`lttng-gen-tp` should suffice in <<static-linking,static linking>>
1945situations. When using it, write a template file containing a list of
1946`TRACEPOINT_EVENT()` macro calls. The tool finds the provider names
1947used and generate the appropriate files which are going to look a lot
1948like path:{tp.h} and path:{tp.c} above.
1949
1950Just call `lttng-gen-tp` like this:
1951
1952[role="term"]
1953----
1954lttng-gen-tp my-template.tp
1955----
1956
1957path:{my-template.c}, path:{my-template.o} and path:{my-template.h}
1958are created in the same directory.
1959
1960You may specify custom C flags passed to the compiler invoked by
1961`lttng-gen-tp` using the `CFLAGS` environment variable:
1962
1963[role="term"]
1964----
1965CFLAGS=-I/custom/include/path lttng-gen-tp my-template.tp
1966----
1967
1968For more information on `lttng-gen-tp`, see man:lttng-gen-tp(1).
1969
1970
1971[[defining-tracepoints]]
1972===== Defining tracepoints
1973
1974As written in <<tracepoint-provider,Tracepoint provider>>,
1975tracepoints are defined using the
1976`TRACEPOINT_EVENT()` macro. Each tracepoint, when called using the
1977`tracepoint()` macro in the actual application's source code, generates
1978a specific event type with its own fields.
1979
1980Let's have another look at the example above, with a few added comments:
1981
1982[source,c]
1983----
1984TRACEPOINT_EVENT(
1985 /* tracepoint provider name */
1986 my_provider,
1987
1988 /* tracepoint/event name */
1989 my_first_tracepoint,
1990
1991 /* list of tracepoint arguments */
1992 TP_ARGS(
1993 int, my_integer_arg,
1994 char*, my_string_arg
1995 ),
1996
1997 /* list of fields of eventual event */
1998 TP_FIELDS(
1999 ctf_string(my_string_field, my_string_arg)
2000 ctf_integer(int, my_integer_field, my_integer_arg)
2001 )
2002)
2003----
2004
2005The tracepoint provider name must match the name of the tracepoint
2006provider in which this tracepoint is defined
2007(see <<tracepoint-provider,Tracepoint provider>>). In other words,
2008always use the same string as the value of `TRACEPOINT_PROVIDER` above.
2009
2010The tracepoint name becomes the event name once events are recorded
2011by the LTTng-UST tracer. It must follow the tracepoint provider name
2012syntax: start with a letter and contain either letters, numbers or
2013underscores. Two tracepoints under the same provider cannot have the
2014same name. In other words, you cannot overload a tracepoint like you
2015would overload functions and methods in $$C++$$/Java.
2016
2017NOTE: The concatenation of the tracepoint
2018provider name and the tracepoint name cannot exceed 254 characters. If
2019it does, the instrumented application compiles and runs, but LTTng
2020issues multiple warnings and you could experience serious problems.
2021
2022The list of tracepoint arguments gives this tracepoint its signature:
2023see it like the declaration of a C function. The format of `TP_ARGS()`
2024arguments is: C type, then argument name; repeat as needed, up to ten
2025times. For example, if we were to replicate the signature of C standard
2026library's `fseek()`, the `TP_ARGS()` part would look like:
2027
2028[source,c]
2029----
2030 TP_ARGS(
2031 FILE*, stream,
2032 long int, offset,
2033 int, origin
2034 ),
2035----
2036
2037Of course, you need to include appropriate header files before
2038the `TRACEPOINT_EVENT()` macro calls if any argument has a complex type.
2039
2040`TP_ARGS()` may not be omitted, but may be empty. `TP_ARGS(void)` is
2041also accepted.
2042
2043The list of fields is where the fun really begins. The fields defined
2044in this list are the fields of the events generated by the execution
2045of this tracepoint. Each tracepoint field definition has a C
2046_argument expression_ which is evaluated when the execution reaches
2047the tracepoint. Tracepoint arguments _may be_ used freely in those
2048argument expressions, but they _don't_ have to.
2049
2050There are several types of tracepoint fields available. The macros to
2051define them are given and explained in the
2052<<liblttng-ust-tp-fields,LTTng-UST library reference>> section.
2053
2054Field names must follow the standard C identifier syntax: letter, then
2055optional sequence of letters, numbers or underscores. Each field must have
2056a different name.
2057
2058Those `ctf_*()` macros are added to the `TP_FIELDS()` part of
2059`TRACEPOINT_EVENT()`. Note that they are not delimited by commas.
2060`TP_FIELDS()` may be empty, but the `TP_FIELDS(void)` form is _not_
2061accepted.
2062
2063The following snippet shows how argument expressions may be used in
2064tracepoint fields and how they may refer freely to tracepoint arguments.
2065
2066[source,c]
2067----
2068/* for struct stat */
2069#include <sys/types.h>
2070#include <sys/stat.h>
2071#include <unistd.h>
2072
2073TRACEPOINT_EVENT(
2074 my_provider,
2075 my_tracepoint,
2076 TP_ARGS(
2077 int, my_int_arg,
2078 char*, my_str_arg,
2079 struct stat*, st
2080 ),
2081 TP_FIELDS(
2082 /* simple integer field with constant value */
2083 ctf_integer(
2084 int, /* field C type */
2085 my_constant_field, /* field name */
2086 23 + 17 /* argument expression */
2087 )
2088
2089 /* my_int_arg tracepoint argument */
2090 ctf_integer(
2091 int,
2092 my_int_arg_field,
2093 my_int_arg
2094 )
2095
2096 /* my_int_arg squared */
2097 ctf_integer(
2098 int,
2099 my_int_arg_field2,
2100 my_int_arg * my_int_arg
2101 )
2102
2103 /* sum of first 4 characters of my_str_arg */
2104 ctf_integer(
2105 int,
2106 sum4,
2107 my_str_arg[0] + my_str_arg[1] +
2108 my_str_arg[2] + my_str_arg[3]
2109 )
2110
2111 /* my_str_arg as string field */
2112 ctf_string(
2113 my_str_arg_field, /* field name */
2114 my_str_arg /* argument expression */
2115 )
2116
2117 /* st_size member of st tracepoint argument, hexadecimal */
2118 ctf_integer_hex(
2119 off_t, /* field C type */
2120 size_field, /* field name */
2121 st->st_size /* argument expression */
2122 )
2123
2124 /* st_size member of st tracepoint argument, as double */
2125 ctf_float(
2126 double, /* field C type */
2127 size_dbl_field, /* field name */
2128 (double) st->st_size /* argument expression */
2129 )
2130
2131 /* half of my_str_arg string as text sequence */
2132 ctf_sequence_text(
2133 char, /* element C type */
2134 half_my_str_arg_field, /* field name */
2135 my_str_arg, /* argument expression */
2136 size_t, /* length expression C type */
2137 strlen(my_str_arg) / 2 /* length expression */
2138 )
2139 )
2140)
2141----
2142
2143As you can see, having a custom argument expression for each field
2144makes tracepoints very flexible for tracing a user space C application.
2145This tracepoint definition is reused later in this guide, when
2146actually using tracepoints in a user space application.
2147
2148
2149[[using-tracepoint-classes]]
2150===== Using tracepoint classes
2151
2152In LTTng-UST, a _tracepoint class_ is a class of tracepoints sharing the
2153same field types and names. A _tracepoint instance_ is one instance of
2154such a declared tracepoint class, with its own event name and tracepoint
2155provider name.
2156
2157What is documented in <<defining-tracepoints,Defining tracepoints>>
2158is actually how to declare a _tracepoint class_ and define a
2159_tracepoint instance_ at the same time. Without revealing the internals
2160of LTTng-UST too much, it has to be noted that one serialization
2161function is created for each tracepoint class. A serialization
2162function is responsible for serializing the fields of a tracepoint
2163into a sub-buffer when tracing. For various performance reasons, when
2164your situation requires multiple tracepoints with different names, but
2165with the same fields layout, the best practice is to manually create
2166a tracepoint class and instantiate as many tracepoint instances as
2167needed. One positive effect of such a design, amongst other advantages,
2168is that all tracepoint instances of the same tracepoint class
2169reuse the same serialization function, thus reducing cache pollution.
2170
2171As an example, here are three tracepoint definitions as we know them:
2172
2173[source,c]
2174----
2175TRACEPOINT_EVENT(
2176 my_app,
2177 get_account,
2178 TP_ARGS(
2179 int, userid,
2180 size_t, len
2181 ),
2182 TP_FIELDS(
2183 ctf_integer(int, userid, userid)
2184 ctf_integer(size_t, len, len)
2185 )
2186)
2187
2188TRACEPOINT_EVENT(
2189 my_app,
2190 get_settings,
2191 TP_ARGS(
2192 int, userid,
2193 size_t, len
2194 ),
2195 TP_FIELDS(
2196 ctf_integer(int, userid, userid)
2197 ctf_integer(size_t, len, len)
2198 )
2199)
2200
2201TRACEPOINT_EVENT(
2202 my_app,
2203 get_transaction,
2204 TP_ARGS(
2205 int, userid,
2206 size_t, len
2207 ),
2208 TP_FIELDS(
2209 ctf_integer(int, userid, userid)
2210 ctf_integer(size_t, len, len)
2211 )
2212)
2213----
2214
2215In this case, three tracepoint classes are created, with one tracepoint
2216instance for each of them: `get_account`, `get_settings` and
2217`get_transaction`. However, they all share the same field names and
2218types. Declaring one tracepoint class and three tracepoint instances of
2219the latter is a better design choice:
2220
2221[source,c]
2222----
2223/* the tracepoint class */
2224TRACEPOINT_EVENT_CLASS(
2225 /* tracepoint provider name */
2226 my_app,
2227
2228 /* tracepoint class name */
2229 my_class,
2230
2231 /* arguments */
2232 TP_ARGS(
2233 int, userid,
2234 size_t, len
2235 ),
2236
2237 /* fields */
2238 TP_FIELDS(
2239 ctf_integer(int, userid, userid)
2240 ctf_integer(size_t, len, len)
2241 )
2242)
2243
2244/* the tracepoint instances */
2245TRACEPOINT_EVENT_INSTANCE(
2246 /* tracepoint provider name */
2247 my_app,
2248
2249 /* tracepoint class name */
2250 my_class,
2251
2252 /* tracepoint/event name */
2253 get_account,
2254
2255 /* arguments */
2256 TP_ARGS(
2257 int, userid,
2258 size_t, len
2259 )
2260)
2261TRACEPOINT_EVENT_INSTANCE(
2262 my_app,
2263 my_class,
2264 get_settings,
2265 TP_ARGS(
2266 int, userid,
2267 size_t, len
2268 )
2269)
2270TRACEPOINT_EVENT_INSTANCE(
2271 my_app,
2272 my_class,
2273 get_transaction,
2274 TP_ARGS(
2275 int, userid,
2276 size_t, len
2277 )
2278)
2279----
2280
2281Of course, all those names and `TP_ARGS()` invocations are redundant,
2282but some C preprocessor magic can solve this:
2283
2284[source,c]
2285----
2286#define MY_TRACEPOINT_ARGS \
2287 TP_ARGS( \
2288 int, userid, \
2289 size_t, len \
2290 )
2291
2292TRACEPOINT_EVENT_CLASS(
2293 my_app,
2294 my_class,
2295 MY_TRACEPOINT_ARGS,
2296 TP_FIELDS(
2297 ctf_integer(int, userid, userid)
2298 ctf_integer(size_t, len, len)
2299 )
2300)
2301
2302#define MY_APP_TRACEPOINT_INSTANCE(name) \
2303 TRACEPOINT_EVENT_INSTANCE( \
2304 my_app, \
2305 my_class, \
2306 name, \
2307 MY_TRACEPOINT_ARGS \
2308 )
2309
2310MY_APP_TRACEPOINT_INSTANCE(get_account)
2311MY_APP_TRACEPOINT_INSTANCE(get_settings)
2312MY_APP_TRACEPOINT_INSTANCE(get_transaction)
2313----
2314
2315
2316[[assigning-log-levels]]
2317===== Assigning log levels to tracepoints
2318
2319Optionally, a log level can be assigned to a defined tracepoint.
2320Assigning different levels of importance to tracepoints can be useful;
2321when controlling tracing sessions,
2322<<controlling-tracing,you can choose>> to only enable tracepoints
2323falling into a specific log level range.
2324
2325Log levels are assigned to defined tracepoints using the
2326`TRACEPOINT_LOGLEVEL()` macro. The latter must be used _after_ having
2327used `TRACEPOINT_EVENT()` for a given tracepoint. The
2328`TRACEPOINT_LOGLEVEL()` macro has the following construct:
2329
2330[source,c]
2331----
2332TRACEPOINT_LOGLEVEL(PROVIDER_NAME, TRACEPOINT_NAME, LOG_LEVEL)
2333----
2334
2335where the first two arguments are the same as the first two arguments
2336of `TRACEPOINT_EVENT()` and `LOG_LEVEL` is one
2337of the values given in the
2338<<liblttng-ust-tracepoint-loglevel,LTTng-UST library reference>>
2339section.
2340
2341As an example, let's assign a `TRACE_DEBUG_UNIT` log level to our
2342previous tracepoint definition:
2343
2344[source,c]
2345----
2346TRACEPOINT_LOGLEVEL(my_provider, my_tracepoint, TRACE_DEBUG_UNIT)
2347----
2348
2349
2350[[probing-the-application-source-code]]
2351===== Probing the application's source code
2352
2353Once tracepoints are properly defined within a tracepoint provider,
2354they may be inserted into the user application to be instrumented
2355using the `tracepoint()` macro. Its first argument is the tracepoint
2356provider name and its second is the tracepoint name. The next, optional
2357arguments are defined by the `TP_ARGS()` part of the definition of
2358the tracepoint to use.
2359
2360As an example, let us again take the following tracepoint definition:
2361
2362[source,c]
2363----
2364TRACEPOINT_EVENT(
2365 /* tracepoint provider name */
2366 my_provider,
2367
2368 /* tracepoint/event name */
2369 my_first_tracepoint,
2370
2371 /* list of tracepoint arguments */
2372 TP_ARGS(
2373 int, my_integer_arg,
2374 char*, my_string_arg
2375 ),
2376
2377 /* list of fields of eventual event */
2378 TP_FIELDS(
2379 ctf_string(my_string_field, my_string_arg)
2380 ctf_integer(int, my_integer_field, my_integer_arg)
2381 )
2382)
2383----
2384
2385Assuming this is part of a file named path:{tp.h} which defines the tracepoint
2386provider and which is included by path:{tp.c}, here's a complete C application
2387calling this tracepoint (multiple times):
2388
2389[source,c]
2390----
2391#define TRACEPOINT_DEFINE
2392#include "tp.h"
2393
2394int main(int argc, char* argv[])
2395{
2396 int i;
2397
2398 tracepoint(my_provider, my_first_tracepoint, 23, "Hello, World!");
2399
2400 for (i = 0; i < argc; ++i) {
2401 tracepoint(my_provider, my_first_tracepoint, i, argv[i]);
2402 }
2403
2404 return 0;
2405}
2406----
2407
2408For each tracepoint provider, `TRACEPOINT_DEFINE` must be defined into
2409exactly one translation unit (C source file) of the user application,
2410before including the tracepoint provider header file. In other words,
2411for a given tracepoint provider, you cannot define `TRACEPOINT_DEFINE`,
2412and then include its header file in two separate C source files of
2413the same application. `TRACEPOINT_DEFINE` is discussed further in
2414<<building-tracepoint-providers-and-user-application,Building/linking
2415tracepoint providers and the user application>>.
2416
2417As another example, remember this definition we wrote in a previous
2418section (comments are stripped):
2419
2420[source,c]
2421----
2422/* for struct stat */
2423#include <sys/types.h>
2424#include <sys/stat.h>
2425#include <unistd.h>
2426
2427TRACEPOINT_EVENT(
2428 my_provider,
2429 my_tracepoint,
2430 TP_ARGS(
2431 int, my_int_arg,
2432 char*, my_str_arg,
2433 struct stat*, st
2434 ),
2435 TP_FIELDS(
2436 ctf_integer(int, my_constant_field, 23 + 17)
2437 ctf_integer(int, my_int_arg_field, my_int_arg)
2438 ctf_integer(int, my_int_arg_field2, my_int_arg * my_int_arg)
2439 ctf_integer(int, sum4_field, my_str_arg[0] + my_str_arg[1] +
2440 my_str_arg[2] + my_str_arg[3])
2441 ctf_string(my_str_arg_field, my_str_arg)
2442 ctf_integer_hex(off_t, size_field, st->st_size)
2443 ctf_float(double, size_dbl_field, (double) st->st_size)
2444 ctf_sequence_text(char, half_my_str_arg_field, my_str_arg,
2445 size_t, strlen(my_str_arg) / 2)
2446 )
2447)
2448----
2449
2450Here's an example of calling it:
2451
2452[source,c]
2453----
2454#define TRACEPOINT_DEFINE
2455#include "tp.h"
2456
2457int main(void)
2458{
2459 struct stat s;
2460
2461 stat("/etc/fstab", &s);
2462
2463 tracepoint(my_provider, my_tracepoint, 23, "Hello, World!", &s);
2464
2465 return 0;
2466}
2467----
2468
2469When viewing the trace, assuming the file size of path:{/etc/fstab} is
2470301{nbsp}bytes, the event generated by the execution of this tracepoint
2471should have the following fields, in this order:
2472
2473----
2474my_constant_field 40
2475my_int_arg_field 23
2476my_int_arg_field2 529
2477sum4_field 389
2478my_str_arg_field "Hello, World!"
2479size_field 0x12d
2480size_dbl_field 301.0
2481half_my_str_arg_field "Hello,"
2482----
2483
2484
2485[[building-tracepoint-providers-and-user-application]]
2486===== Building/linking tracepoint providers and the user application
2487
2488The final step of using LTTng-UST for tracing a user space C application
2489(beside running the application) is building and linking tracepoint
2490providers and the application itself.
2491
2492As discussed above, the macros used by the user-written tracepoint provider
2493header file are useless until actually used to create probes code
2494(global data structures and functions) in a translation unit (C source file).
2495This is accomplished by defining `TRACEPOINT_CREATE_PROBES` in a translation
2496unit and then including the tracepoint provider header file.
2497When `TRACEPOINT_CREATE_PROBES` is defined, macros used and included by
2498the tracepoint provider header produce actual source code needed by any
2499application using the defined tracepoints. Defining
2500`TRACEPOINT_CREATE_PROBES` produces code used when registering
2501tracepoint providers when the tracepoint provider package loads.
2502
2503The other important definition is `TRACEPOINT_DEFINE`. This one creates
2504global, per-tracepoint structures referencing the tracepoint providers
2505data. Those structures are required by the actual functions inserted
2506where `tracepoint()` macros are placed and need to be defined by the
2507instrumented application.
2508
2509Both `TRACEPOINT_CREATE_PROBES` and `TRACEPOINT_DEFINE` need to be defined
2510at some places in order to trace a user space C application using LTTng.
2511Although explaining their exact mechanism is beyond the scope of this
2512document, the reason they both exist separately is to allow the trace
2513providers to be packaged as a shared object (dynamically loaded library).
2514
2515There are two ways to compile and link the tracepoint providers
2516with the application: _<<static-linking,statically>>_ or
2517_<<dynamic-linking,dynamically>>_. Both methods are covered in the
2518following subsections.
2519
2520
2521[[static-linking]]
2522===== Static linking the tracepoint providers to the application
2523
2524With the static linking method, compiled tracepoint providers are copied
2525into the target application. There are three ways to do this:
2526
2527. Use one of your **existing C source files** to create probes.
2528. Create probes in a separate C source file and build it as an
2529 **object file** to be linked with the application (more decoupled).
2530. Create probes in a separate C source file, build it as an
2531 object file and archive it to create a **static library**
2532 (more decoupled, more portable).
2533
2534The first approach is to define `TRACEPOINT_CREATE_PROBES` and include
2535your tracepoint provider(s) header file(s) directly into an existing C
2536source file. Here's an example:
2537
2538[source,c]
2539----
2540#include <stdlib.h>
2541#include <stdio.h>
2542/* ... */
2543
2544#define TRACEPOINT_CREATE_PROBES
2545#define TRACEPOINT_DEFINE
2546#include "tp.h"
2547
2548/* ... */
2549
2550int my_func(int a, const char* b)
2551{
2552 /* ... */
2553
2554 tracepoint(my_provider, my_tracepoint, buf, sz, limit, &tt)
2555
2556 /* ... */
2557}
2558
2559/* ... */
2560----
2561
2562Again, before including a given tracepoint provider header file,
2563`TRACEPOINT_CREATE_PROBES` and `TRACEPOINT_DEFINE` must be defined in
2564one, **and only one**, translation unit. Other C source files of the
2565same application may include path:{tp.h} to use tracepoints with
2566the `tracepoint()` macro, but must not define
2567`TRACEPOINT_CREATE_PROBES`/`TRACEPOINT_DEFINE` again.
2568
2569This translation unit may be built as an object file by making sure to
2570add `.` to the include path:
2571
2572[role="term"]
2573----
2574gcc -c -I. file.c
2575----
2576
2577The second approach is to isolate the tracepoint provider code into a
2578separate object file by using a dedicated C source file to create probes:
2579
2580[source,c]
2581----
2582#define TRACEPOINT_CREATE_PROBES
2583
2584#include "tp.h"
2585----
2586
2587`TRACEPOINT_DEFINE` must be defined by a translation unit of the
2588application. Since we're talking about static linking here, it could as
2589well be defined directly in the file above, before `#include "tp.h"`:
2590
2591[source,c]
2592----
2593#define TRACEPOINT_CREATE_PROBES
2594#define TRACEPOINT_DEFINE
2595
2596#include "tp.h"
2597----
2598
2599This is actually what <<lttng-gen-tp,`lttng-gen-tp`>> does, and is
2600the recommended practice.
2601
2602Build the tracepoint provider:
2603
2604[role="term"]
2605----
2606gcc -c -I. tp.c
2607----
2608
2609Finally, the resulting object file may be archived to create a
2610more portable tracepoint provider static library:
2611
2612[role="term"]
2613----
2614ar rc tp.a tp.o
2615----
2616
2617Using a static library does have the advantage of centralising the
2618tracepoint providers objects so they can be shared between multiple
2619applications. This way, when the tracepoint provider is modified, the
2620source code changes don't have to be patched into each application's source
2621code tree. The applications need to be relinked after each change, but need
2622not to be otherwise recompiled (unless the tracepoint provider's API
2623changes).
2624
2625Regardless of which method you choose, you end up with an object file
2626(potentially archived) containing the trace providers assembled code.
2627To link this code with the rest of your application, you must also link
2628with `liblttng-ust` and `libdl`:
2629
2630[role="term"]
2631----
2632gcc -o app tp.o other.o files.o of.o your.o app.o -llttng-ust -ldl
2633----
2634
2635or
2636
2637[role="term"]
2638----
2639gcc -o app tp.a other.o files.o of.o your.o app.o -llttng-ust -ldl
2640----
2641
2642If you're using a BSD
2643system, replace `-ldl` with `-lc`:
2644
2645[role="term"]
2646----
2647gcc -o app tp.a other.o files.o of.o your.o app.o -llttng-ust -lc
2648----
2649
2650The application can be started as usual, for example:
2651
2652[role="term"]
2653----
2654./app
2655----
2656
2657The `lttng` command line tool can be used to
2658<<controlling-tracing,control tracing>>.
2659
2660
2661[[dynamic-linking]]
2662===== Dynamic linking the tracepoint providers to the application
2663
2664The second approach to package the tracepoint providers is to use
2665dynamic linking: the library and its member functions are explicitly
2666sought, loaded and unloaded at runtime using `libdl`.
2667
2668It has to be noted that, for a variety of reasons, the created shared
2669library is be dynamically _loaded_, as opposed to dynamically
2670_linked_. The tracepoint provider shared object is, however, linked
2671with `liblttng-ust`, so that `liblttng-ust` is guaranteed to be loaded
2672as soon as the tracepoint provider is. If the tracepoint provider is
2673not loaded, since the application itself is not linked with
2674`liblttng-ust`, the latter is not loaded at all and the tracepoint calls
2675become inert.
2676
2677The process to create the tracepoint provider shared object is pretty
2678much the same as the static library method, except that:
2679
2680* since the tracepoint provider is not part of the application
2681 anymore, `TRACEPOINT_DEFINE` _must_ be defined, for each tracepoint
2682 provider, in exactly one translation unit (C source file) of the
2683 _application_;
2684* `TRACEPOINT_PROBE_DYNAMIC_LINKAGE` must be defined next to
2685 `TRACEPOINT_DEFINE`.
2686
2687Regarding `TRACEPOINT_DEFINE` and `TRACEPOINT_PROBE_DYNAMIC_LINKAGE`,
2688the recommended practice is to use a separate C source file in your
2689application to define them, then include the tracepoint provider
2690header files afterwards. For example:
2691
2692[source,c]
2693----
2694#define TRACEPOINT_DEFINE
2695#define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
2696
2697/* include the header files of one or more tracepoint providers below */
2698#include "tp1.h"
2699#include "tp2.h"
2700#include "tp3.h"
2701----
2702
2703`TRACEPOINT_PROBE_DYNAMIC_LINKAGE` makes the macros included afterwards
2704(by including the tracepoint provider header, which itself includes
2705LTTng-UST headers) aware that the tracepoint provider is to be loaded
2706dynamically and not part of the application's executable.
2707
2708The tracepoint provider object file used to create the shared library
2709is built like it is using the static library method, only with the
2710`-fpic` option added:
2711
2712[role="term"]
2713----
2714gcc -c -fpic -I. tp.c
2715----
2716
2717It is then linked as a shared library like this:
2718
2719[role="term"]
2720----
2721gcc -shared -Wl,--no-as-needed -o tp.so -llttng-ust tp.o
2722----
2723
2724As previously stated, this tracepoint provider shared object isn't
2725linked with the user application: it's loaded manually. This is
2726why the application is built with no mention of this tracepoint
2727provider, but still needs `libdl`:
2728
2729[role="term"]
2730----
2731gcc -o app other.o files.o of.o your.o app.o -ldl
2732----
2733
2734Now, to make LTTng-UST tracing available to the application, the
2735`LD_PRELOAD` environment variable is used to preload the tracepoint
2736provider shared library _before_ the application actually starts:
2737
2738[role="term"]
2739----
2740LD_PRELOAD=/path/to/tp.so ./app
2741----
2742
2743[NOTE]
2744====
2745It is not safe to use
2746`dlclose()` on a tracepoint provider shared object that
2747is being actively used for tracing, due to a lack of reference
2748counting from LTTng-UST to the shared object.
2749
2750For example, statically linking a tracepoint provider to a
2751shared object which is to be dynamically loaded by an application
2752(a plugin, for example) is not safe: the shared object, which
2753contains the tracepoint provider, could be dynamically closed
2754(`dlclose()`) at any time by the application.
2755
2756To instrument a shared object, either:
2757
2758* Statically link the tracepoint provider to the _application_, or
2759* Build the tracepoint provider as a shared object (following
2760 the procedure shown in this section), and preload it when
2761 tracing is needed using the `LD_PRELOAD`
2762 environment variable.
2763====
2764
2765Your application will still work without this preloading, albeit without
2766LTTng-UST tracing support:
2767
2768[role="term"]
2769----
2770./app
2771----
2772
2773
2774[[using-lttng-ust-with-daemons]]
2775===== Using LTTng-UST with daemons
2776
2777Some extra care is needed when using `liblttng-ust` with daemon
2778applications that call `fork()`, `clone()` or BSD's `rfork()` without
2779a following `exec()` family system call. The `liblttng-ust-fork`
2780library must be preloaded for the application.
2781
2782Example:
2783
2784[role="term"]
2785----
2786LD_PRELOAD=liblttng-ust-fork.so ./app
2787----
2788
2789Or, if you're using a tracepoint provider shared library:
2790
2791[role="term"]
2792----
2793LD_PRELOAD="liblttng-ust-fork.so /path/to/tp.so" ./app
2794----
2795
2796
2797[[lttng-ust-pkg-config]]
2798===== Using pkg-config
2799
2800On some distributions, LTTng-UST is shipped with a pkg-config metadata
2801file, so that you may use the `pkg-config` tool:
2802
2803[role="term"]
2804----
2805pkg-config --libs lttng-ust
2806----
2807
2808This prints `-llttng-ust -ldl` on Linux systems.
2809
2810You may also check the LTTng-UST version using `pkg-config`:
2811
2812[role="term"]
2813----
2814pkg-config --modversion lttng-ust
2815----
2816
2817For more information about pkg-config, see
2818http://linux.die.net/man/1/pkg-config[its manpage].
2819
2820
2821[role="since-2.5"]
2822[[tracef]]
2823===== Using `tracef()`
2824
2825`tracef()` is a small LTTng-UST API to avoid defining your own
2826tracepoints and tracepoint providers. The signature of `tracef()` is
2827the same as `printf()`'s.
2828
2829The `tracef()` utility function was developed to make user space tracing
2830super simple, albeit with notable disadvantages compared to custom,
2831full-fledged tracepoint providers:
2832
2833* All generated events have the same provider/event names, respectively
2834 `lttng_ust_tracef` and `event`.
2835* There's no static type checking.
2836* The only event field you actually get, named `msg`, is a string
2837 potentially containing the values you passed to the function
2838 using your own format. This also means that you cannot use filtering
2839 using a custom expression at runtime because there are no isolated
2840 fields.
2841* Since `tracef()` uses C standard library's `vasprintf()` function
2842 in the background to format the strings at runtime, its
2843 expected performance is lower than using custom tracepoint providers
2844 with typed fields, which do not require a conversion to a string.
2845
2846Thus, `tracef()` is useful for quick prototyping and debugging, but
2847should not be considered for any permanent/serious application
2848instrumentation.
2849
2850To use `tracef()`, first include `<lttng/tracef.h>` in the C source file
2851where you need to insert probes:
2852
2853[source,c]
2854----
2855#include <lttng/tracef.h>
2856----
2857
2858Use `tracef()` like you would use `printf()` in your source code, for
2859example:
2860
2861[source,c]
2862----
2863 /* ... */
2864
2865 tracef("my message, my integer: %d", my_integer);
2866
2867 /* ... */
2868----
2869
2870Link your application with `liblttng-ust`:
2871
2872[role="term"]
2873----
2874gcc -o app app.c -llttng-ust
2875----
2876
2877Execute the application as usual:
2878
2879[role="term"]
2880----
2881./app
2882----
2883
2884Voilà! Use the `lttng` command line tool to
2885<<controlling-tracing,control tracing>>. You can enable `tracef()`
2886events like this:
2887
2888[role="term"]
2889----
2890lttng enable-event --userspace 'lttng_ust_tracef:*'
2891----
2892
2893
2894[[lttng-ust-environment-variables-compiler-flags]]
2895===== LTTng-UST environment variables and special compilation flags
2896
2897A few special environment variables and compile flags may affect the
2898behavior of LTTng-UST.
2899
2900LTTng-UST's debugging can be activated by setting the environment
2901variable `LTTNG_UST_DEBUG` to `1` when launching the application. It
2902can also be enabled at compile time by defining `LTTNG_UST_DEBUG` when
2903compiling LTTng-UST (using the `-DLTTNG_UST_DEBUG` compiler option).
2904
2905The environment variable `LTTNG_UST_REGISTER_TIMEOUT` can be used to
2906specify how long the application should wait for the
2907<<lttng-sessiond,session daemon>>'s _registration done_ command
2908before proceeding to execute the main program. The timeout value is
2909specified in milliseconds. 0 means _don't wait_. -1 means
2910_wait forever_. Setting this environment variable to 0 is recommended
2911for applications with time contraints on the process startup time.
2912
2913The default value of `LTTNG_UST_REGISTER_TIMEOUT` (when not defined)
2914is **3000{nbsp}ms**.
2915
2916The compilation definition `LTTNG_UST_DEBUG_VALGRIND` should be enabled
2917at build time (`-DLTTNG_UST_DEBUG_VALGRIND`) to allow `liblttng-ust`
2918to be used with http://valgrind.org/[Valgrind].
2919The side effect of defining `LTTNG_UST_DEBUG_VALGRIND` is that per-CPU
2920buffering is disabled.
2921
2922
2923[[cxx-application]]
2924==== $$C++$$ application
2925
2926Because of $$C++$$'s cross-compatibility with the C language, $$C++$$
2927applications can be readily instrumented with the LTTng-UST C API.
2928
2929Follow the <<c-application,C application>> user guide above. It
2930should be noted that, in this case, tracepoint providers should have
2931the typical `.cpp`, `.cxx` or `.cc` extension and be built with `g++`
2932instead of `gcc`. This is the easiest way of avoiding linking errors
2933due to symbol name mangling incompatibilities between both languages.
2934
2935
2936[[prebuilt-ust-helpers]]
2937==== Prebuilt user space tracing helpers
2938
2939The LTTng-UST package provides a few helpers that one may find
2940useful in some situations. They all work the same way: you must
2941preload the appropriate shared object before running the user
2942application (using the `LD_PRELOAD` environment variable).
2943
2944The shared objects are normally found in dir:{/usr/lib}.
2945
2946The current installed helpers are:
2947
2948path:{liblttng-ust-libc-wrapper.so} and path:{liblttng-ust-pthread-wrapper.so}::
2949 <<liblttng-ust-libc-pthread-wrapper,C{nbsp}standard library
2950 and POSIX threads tracing>>.
2951
2952path:{liblttng-ust-cyg-profile.so} and path:{liblttng-ust-cyg-profile-fast.so}::
2953 <<liblttng-ust-cyg-profile,Function tracing>>.
2954
2955path:{liblttng-ust-dl.so}::
2956 <<liblttng-ust-dl,Dynamic linker tracing>>.
2957
2958The following subsections document what helpers instrument exactly
2959and how to use them.
2960
2961
2962[role="since-2.3"]
2963[[liblttng-ust-libc-pthread-wrapper]]
2964===== C standard library and POSIX threads tracing
2965
2966path:{liblttng-ust-libc-wrapper.so} and path:{liblttng-ust-pthread-wrapper.so}
2967can add instrumentation to respectively some C standard library and
2968POSIX threads functions.
2969
2970The following functions are traceable by path:{liblttng-ust-libc-wrapper.so}:
2971
2972[role="growable"]
2973.Functions instrumented by path:{liblttng-ust-libc-wrapper.so}
2974|====
2975|TP provider name |TP name |Instrumented function
2976
2977.6+|`ust_libc` |`malloc` |`malloc()`
2978 |`calloc` |`calloc()`
2979 |`realloc` |`realloc()`
2980 |`free` |`free()`
2981 |`memalign` |`memalign()`
2982 |`posix_memalign` |`posix_memalign()`
2983|====
2984
2985The following functions are traceable by
2986path:{liblttng-ust-pthread-wrapper.so}:
2987
2988[role="growable"]
2989.Functions instrumented by path:{liblttng-ust-pthread-wrapper.so}
2990|====
2991|TP provider name |TP name |Instrumented function
2992
2993.4+|`ust_pthread` |`pthread_mutex_lock_req` |`pthread_mutex_lock()` (request time)
2994 |`pthread_mutex_lock_acq` |`pthread_mutex_lock()` (acquire time)
2995 |`pthread_mutex_trylock` |`pthread_mutex_trylock()`
2996 |`pthread_mutex_unlock` |`pthread_mutex_unlock()`
2997|====
2998
2999All tracepoints have fields corresponding to the arguments of the
3000function they instrument.
3001
3002To use one or the other with any user application, independently of
3003how the latter is built, do:
3004
3005[role="term"]
3006----
3007LD_PRELOAD=liblttng-ust-libc-wrapper.so my-app
3008----
3009
3010or
3011
3012[role="term"]
3013----
3014LD_PRELOAD=liblttng-ust-pthread-wrapper.so my-app
3015----
3016
3017To use both, do:
3018
3019[role="term"]
3020----
3021LD_PRELOAD="liblttng-ust-libc-wrapper.so liblttng-ust-pthread-wrapper.so" my-app
3022----
3023
3024When the shared object is preloaded, it effectively replaces the
3025functions listed in the above tables by wrappers which add tracepoints
3026and call the replaced functions.
3027
3028Of course, like any other tracepoint, the ones above need to be enabled
3029in order for LTTng-UST to generate events. This is done using the
3030`lttng` command line tool
3031(see <<controlling-tracing,Controlling tracing>>).
3032
3033
3034[[liblttng-ust-cyg-profile]]
3035===== Function tracing
3036
3037Function tracing is the recording of which functions are entered and
3038left during the execution of an application. Like with any LTTng event,
3039the precise time at which this happens is also kept.
3040
3041GCC and clang have an option named
3042https://gcc.gnu.org/onlinedocs/gcc-4.9.1/gcc/Code-Gen-Options.html[`-finstrument-functions`]
3043which generates instrumentation calls for entry and exit to functions.
3044The LTTng-UST function tracing helpers, path:{liblttng-ust-cyg-profile.so}
3045and path:{liblttng-ust-cyg-profile-fast.so}, take advantage of this feature
3046to add instrumentation to the two generated functions (which contain
3047`cyg_profile` in their names, hence the shared object's name).
3048
3049In order to use LTTng-UST function tracing, the translation units to
3050instrument must be built using the `-finstrument-functions` compiler
3051flag.
3052
3053LTTng-UST function tracing comes in two flavors, each providing
3054different trade-offs: path:{liblttng-ust-cyg-profile-fast.so} and
3055path:{liblttng-ust-cyg-profile.so}.
3056
3057**path:{liblttng-ust-cyg-profile-fast.so}** is a lightweight variant that
3058should only be used where it can be _guaranteed_ that the complete event
3059stream is recorded without any missing events. Any kind of duplicate
3060information is left out. This version registers the following
3061tracepoints:
3062
3063[role="growable",options="header,autowidth"]
3064.Functions instrumented by path:{liblttng-ust-cyg-profile-fast.so}
3065|====
3066|TP provider name |TP name |Instrumented function
3067
3068.2+|`lttng_ust_cyg_profile_fast`
3069
3070|`func_entry`
3071a|Function entry
3072
3073`addr`::
3074 Address of called function.
3075
3076|`func_exit`
3077|Function exit
3078|====
3079
3080Assuming no event is lost, having only the function addresses on entry
3081is enough for creating a call graph (remember that a recorded event
3082always contains the ID of the CPU that generated it). A tool like
3083https://sourceware.org/binutils/docs/binutils/addr2line.html[`addr2line`]
3084may be used to convert function addresses back to source files names
3085and line numbers.
3086
3087The other helper,
3088**path:{liblttng-ust-cyg-profile.so}**,
3089is a more robust variant which also works for use cases where
3090events might get discarded or not recorded from application startup.
3091In these cases, the trace analyzer needs extra information to be
3092able to reconstruct the program flow. This version registers the
3093following tracepoints:
3094
3095[role="growable",options="header,autowidth"]
3096.Functions instrumented by path:{liblttng-ust-cyg-profile.so}
3097|====
3098|TP provider name |TP name |Instrumented function
3099
3100.2+|`lttng_ust_cyg_profile`
3101
3102|`func_entry`
3103a|Function entry
3104
3105`addr`::
3106 Address of called function.
3107
3108`call_site`::
3109 Call site address.
3110
3111|`func_exit`
3112a|Function exit
3113
3114`addr`::
3115 Address of called function.
3116
3117`call_site`::
3118 Call site address.
3119|====
3120
3121To use one or the other variant with any user application, assuming at
3122least one translation unit of the latter is compiled with the
3123`-finstrument-functions` option, do:
3124
3125[role="term"]
3126----
3127LD_PRELOAD=liblttng-ust-cyg-profile-fast.so my-app
3128----
3129
3130or
3131
3132[role="term"]
3133----
3134LD_PRELOAD=liblttng-ust-cyg-profile.so my-app
3135----
3136
3137It might be necessary to limit the number of source files where
3138`-finstrument-functions` is used to prevent excessive amount of trace
3139data to be generated at runtime.
3140
3141TIP: When using GCC, at least, you can use
3142 the `-finstrument-functions-exclude-function-list`
3143 option to avoid instrumenting entries and exits of specific
3144 symbol names.
3145
3146All events generated from LTTng-UST function tracing are provided on
3147log level `TRACE_DEBUG_FUNCTION`, which is useful to easily enable
3148function tracing events in your tracing session using the
3149`--loglevel-only` option of `lttng enable-event`
3150(see <<controlling-tracing,Controlling tracing>>).
3151
3152
3153[role="since-2.4"]
3154[[liblttng-ust-dl]]
3155===== Dynamic linker tracing
3156
3157This LTTng-UST helper causes all calls to `dlopen()` and `dlclose()`
3158in the target application to be traced with LTTng.
3159
3160The helper's shared object, path:{liblttng-ust-dl.so}, registers the
3161following tracepoints when preloaded:
3162
3163[role="growable",options="header,autowidth"]
3164.Functions instrumented by path:{liblttng-ust-dl.so}
3165|====
3166|TP provider name |TP name |Instrumented function
3167
3168.2+|`ust_baddr`
3169
3170|`push`
3171a|`dlopen()` call
3172
3173`baddr`::
3174 Memory base address (where the dynamic linker placed the shared
3175 object).
3176
3177`sopath`::
3178 File system path to the loaded shared object.
3179
3180`size`::
3181 File size of the the loaded shared object.
3182
3183`mtime`::
3184 Last modification time (seconds since Epoch time) of the loaded shared
3185 object.
3186
3187|`pop`
3188a|Function exit
3189
3190`baddr`::
3191 Memory base address (where the dynamic linker placed the shared
3192 object).
3193|====
3194
3195To use this LTTng-UST helper with any user application, independently of
3196how the latter is built, do:
3197
3198[role="term"]
3199----
3200LD_PRELOAD=liblttng-ust-dl.so my-app
3201----
3202
3203Of course, like any other tracepoint, the ones above need to be enabled
3204in order for LTTng-UST to generate events. This is done using the
3205`lttng` command line tool
3206(see <<controlling-tracing,Controlling tracing>>).
3207
3208
3209[role="since-2.4"]
3210[[java-application]]
3211==== Java application
3212
3213LTTng-UST provides a _logging_ back-end for Java applications using either
3214http://docs.oracle.com/javase/7/docs/api/java/util/logging/Logger.html[`java.util.logging`]
3215(JUL) or
3216http://logging.apache.org/log4j/1.2/[Apache log4j 1.2]
3217This back-end is called the _LTTng-UST Java agent_, and it is responsible
3218for the communications with an LTTng session daemon.
3219
3220From the user's point of view, once the LTTng-UST Java agent has been
3221initialized, JUL and log4j loggers may be created and used as usual.
3222The agent adds its own handler to the _root logger_, so that all
3223loggers may generate LTTng events with no effort.
3224
3225Common JUL/log4j features are supported using the `lttng` tool
3226(see <<controlling-tracing,Controlling tracing>>):
3227
3228* listing all logger names
3229* enabling/disabling events per logger name
3230* JUL/log4j log levels
3231
3232
3233[role="since-2.1"]
3234[[jul]]
3235===== `java.util.logging`
3236
3237Here's an example of tracing a Java application which is using
3238**`java.util.logging`**:
3239
3240[source,java]
3241----
3242import java.util.logging.Logger;
3243import org.lttng.ust.agent.LTTngAgent;
3244
3245public class Test
3246{
3247 private static final int answer = 42;
3248
3249 public static void main(String[] argv) throws Exception
3250 {
3251 // create a logger
3252 Logger logger = Logger.getLogger("jello");
3253
3254 // call this as soon as possible (before logging)
3255 LTTngAgent lttngAgent = LTTngAgent.getLTTngAgent();
3256
3257 // log at will!
3258 logger.info("some info");
3259 logger.warning("some warning");
3260 Thread.sleep(500);
3261 logger.finer("finer information; the answer is " + answer);
3262 Thread.sleep(123);
3263 logger.severe("error!");
3264
3265 // not mandatory, but cleaner
3266 lttngAgent.dispose();
3267 }
3268}
3269----
3270
3271The LTTng-UST Java agent is packaged in a JAR file named
3272`liblttng-ust-agent.jar` It is typically located in
3273dir:{/usr/lib/lttng/java}. To compile the snippet above
3274(saved as `Test.java`), do:
3275
3276[role="term"]
3277----
3278javac -cp /usr/lib/lttng/java/liblttng-ust-agent.jar Test.java
3279----
3280
3281You can run the resulting compiled class like this:
3282
3283[role="term"]
3284----
3285java -cp /usr/lib/lttng/java/liblttng-ust-agent.jar:. Test
3286----
3287
3288NOTE: http://openjdk.java.net/[OpenJDK] 7 is used for development and
3289continuous integration, thus this version is directly supported.
3290However, the LTTng-UST Java agent has also been tested with OpenJDK 6.
3291
3292
3293[role="since-2.6"]
3294[[log4j]]
3295===== Apache log4j 1.2
3296
3297LTTng features an Apache log4j 1.2 agent, which means your existing
3298Java applications using log4j 1.2 for logging can record events to
3299LTTng traces with just a minor source code modification.
3300
3301NOTE: This version of LTTng does not support Log4j 2.
3302
3303Here's an example:
3304
3305[source,java]
3306----
3307import org.apache.log4j.Logger;
3308import org.apache.log4j.BasicConfigurator;
3309import org.lttng.ust.agent.LTTngAgent;
3310
3311public class Test
3312{
3313 private static final int answer = 42;
3314
3315 public static void main(String[] argv) throws Exception
3316 {
3317 // create and configure a logger
3318 Logger logger = Logger.getLogger(Test.class);
3319 BasicConfigurator.configure();
3320
3321 // call this as soon as possible (before logging)
3322 LTTngAgent lttngAgent = LTTngAgent.getLTTngAgent();
3323
3324 // log at will!
3325 logger.info("some info");
3326 logger.warn("some warning");
3327 Thread.sleep(500);
3328 logger.debug("debug information; the answer is " + answer);
3329 Thread.sleep(123);
3330 logger.error("error!");
3331 logger.fatal("fatal error!");
3332
3333 // not mandatory, but cleaner
3334 lttngAgent.dispose();
3335 }
3336}
3337----
3338
3339To compile the snippet above, do:
3340
3341[role="term"]
3342----
3343javac -cp /usr/lib/lttng/java/liblttng-ust-agent.jar:$LOG4JCP Test.java
3344----
3345
3346where `$LOG4JCP` is the log4j 1.2 JAR file path.
3347
3348You can run the resulting compiled class like this:
3349
3350[role="term"]
3351----
3352java -cp /usr/lib/lttng/java/liblttng-ust-agent.jar:$LOG4JCP:. Test
3353----
3354
3355
3356[[instrumenting-linux-kernel]]
3357==== Linux kernel
3358
3359The Linux kernel can be instrumented for LTTng tracing, either its core
3360source code or a kernel module. It has to be noted that Linux is
3361readily traceable using LTTng since many parts of its source code are
3362already instrumented: this is the job of the upstream
3363http://git.lttng.org/?p=lttng-modules.git[LTTng-modules]
3364package. This section presents how to add LTTng instrumentation where it
3365does not currently exist and how to instrument custom kernel modules.
3366
3367All LTTng instrumentation in the Linux kernel is based on an existing
3368infrastructure which bears the name of its main macro, `TRACE_EVENT()`.
3369This macro is used to define tracepoints,
3370each tracepoint having a name, usually with the
3371+__subsys__&#95;__name__+ format,
3372+_subsys_+ being the subsystem name and
3373+_name_+ the specific event name.
3374
3375Tracepoints defined with `TRACE_EVENT()` may be inserted anywhere in
3376the Linux kernel source code, after what callbacks, called _probes_,
3377may be registered to execute some action when a tracepoint is
3378executed. This mechanism is directly used by ftrace and perf,
3379but cannot be used as is by LTTng: an adaptation layer is added to
3380satisfy LTTng's specific needs.
3381
3382With that in mind, this documentation does not cover the `TRACE_EVENT()`
3383format and how to use it, but it is mandatory to understand it and use
3384it to instrument Linux for LTTng. A series of
3385LWN articles explain
3386`TRACE_EVENT()` in details:
3387http://lwn.net/Articles/379903/[part 1],
3388http://lwn.net/Articles/381064/[part 2], and
3389http://lwn.net/Articles/383362/[part 3].
3390Once you master `TRACE_EVENT()` enough for your use case, continue
3391reading this section so that you can add the LTTng adaptation layer of
3392instrumentation.
3393
3394This section first discusses the general method of instrumenting the
3395Linux kernel for LTTng. This method is then reused for the specific
3396case of instrumenting a kernel module.
3397
3398
3399[[instrumenting-linux-kernel-itself]]
3400===== Instrumenting the Linux kernel for LTTng
3401
3402The following subsections explain strictly how to add custom LTTng
3403instrumentation to the Linux kernel. They do not explain how the
3404macros actually work and the internal mechanics of the tracer.
3405
3406You should have a Linux kernel source code tree to work with.
3407Throughout this section, all file paths are relative to the root of
3408this tree unless otherwise stated.
3409
3410You need a copy of the LTTng-modules Git repository:
3411
3412[role="term"]
3413----
3414git clone git://git.lttng.org/lttng-modules.git
3415----
3416
3417The steps to add custom LTTng instrumentation to a Linux kernel
3418involves defining and using the mainline `TRACE_EVENT()` tracepoints
3419first, then writing and using the LTTng adaptation layer.
3420
3421
3422[[mainline-trace-event]]
3423===== Defining/using tracepoints with mainline `TRACE_EVENT()` infrastructure
3424
3425The first step is to define tracepoints using the mainline Linux
3426`TRACE_EVENT()` macro and insert tracepoints where you want them.
3427Your tracepoint definitions reside in a header file in
3428dir:{include/trace/events}. If you're adding tracepoints to an existing
3429subsystem, edit its appropriate header file.
3430
3431As an example, the following header file (let's call it
3432dir:{include/trace/events/hello.h}) defines one tracepoint using
3433`TRACE_EVENT()`:
3434
3435[source,c]
3436----
3437/* subsystem name is "hello" */
3438#undef TRACE_SYSTEM
3439#define TRACE_SYSTEM hello
3440
3441#if !defined(_TRACE_HELLO_H) || defined(TRACE_HEADER_MULTI_READ)
3442#define _TRACE_HELLO_H
3443
3444#include <linux/tracepoint.h>
3445
3446TRACE_EVENT(
3447 /* "hello" is the subsystem name, "world" is the event name */
3448 hello_world,
3449
3450 /* tracepoint function prototype */
3451 TP_PROTO(int foo, const char* bar),
3452
3453 /* arguments for this tracepoint */
3454 TP_ARGS(foo, bar),
3455
3456 /* LTTng doesn't need those */
3457 TP_STRUCT__entry(),
3458 TP_fast_assign(),
3459 TP_printk("", 0)
3460);
3461
3462#endif
3463
3464/* this part must be outside protection */
3465#include <trace/define_trace.h>
3466----
3467
3468Notice that we don't use any of the last three arguments: they
3469are left empty here because LTTng doesn't need them. You would only fill
3470`TP_STRUCT__entry()`, `TP_fast_assign()` and `TP_printk()` if you were
3471to also use this tracepoint for ftrace/perf.
3472
3473Once this is done, you may place calls to `trace_hello_world()`
3474wherever you want in the Linux source code. As an example, let us place
3475such a tracepoint in the `usb_probe_device()` static function
3476(path:{drivers/usb/core/driver.c}):
3477
3478[source,c]
3479----
3480/* called from driver core with dev locked */
3481static int usb_probe_device(struct device *dev)
3482{
3483 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
3484 struct usb_device *udev = to_usb_device(dev);
3485 int error = 0;
3486
3487 trace_hello_world(udev->devnum, udev->product);
3488
3489 /* ... */
3490}
3491----
3492
3493This tracepoint should fire every time a USB device is plugged in.
3494
3495At the top of path:{driver.c}, we need to include our actual tracepoint
3496definition and, in this case (one place per subsystem), define
3497`CREATE_TRACE_POINTS`, which creates our tracepoint:
3498
3499[source,c]
3500----
3501/* ... */
3502
3503#include "usb.h"
3504
3505#define CREATE_TRACE_POINTS
3506#include <trace/events/hello.h>
3507
3508/* ... */
3509----
3510
3511Build your custom Linux kernel. In order to use LTTng, make sure the
3512following kernel configuration options are enabled:
3513
3514* `CONFIG_MODULES` (loadable module support)
3515* `CONFIG_KALLSYMS` (load all symbols for debugging/kksymoops)
3516* `CONFIG_HIGH_RES_TIMERS` (high resolution timer support)
3517* `CONFIG_TRACEPOINTS` (kernel tracepoint instrumentation)
3518
3519Boot the custom kernel. The directory
3520dir:{/sys/kernel/debug/tracing/events/hello} should exist if everything
3521went right, with a dir:{hello_world} subdirectory.
3522
3523
3524[[lttng-adaptation-layer]]
3525===== Adding the LTTng adaptation layer
3526
3527The steps to write the LTTng adaptation layer are, in your
3528LTTng-modules copy's source code tree:
3529
3530. In dir:{instrumentation/events/lttng-module},
3531 add a header +__subsys__.h+ for your custom
3532 subsystem +__subsys__+ and write your
3533 tracepoint definitions using LTTng-modules macros in it.
3534 Those macros look like the mainline kernel equivalents,
3535 but they present subtle, yet important differences.
3536. In dir:{probes}, create the C source file of the LTTng probe kernel
3537 module for your subsystem. It should be named
3538 +lttng-probe-__subsys__.c+.
3539. Edit path:{probes/Makefile} so that the LTTng-modules project
3540 builds your custom LTTng probe kernel module.
3541. Build and install LTTng kernel modules.
3542
3543Following our `hello_world` event example, here's the content of
3544path:{instrumentation/events/lttng-module/hello.h}:
3545
3546[source,c]
3547----
3548#undef TRACE_SYSTEM
3549#define TRACE_SYSTEM hello
3550
3551#if !defined(_TRACE_HELLO_H) || defined(TRACE_HEADER_MULTI_READ)
3552#define _TRACE_HELLO_H
3553
3554#include "../../../probes/lttng-tracepoint-event.h"
3555#include <linux/tracepoint.h>
3556
3557LTTNG_TRACEPOINT_EVENT(
3558 /* format identical to mainline version for those */
3559 hello_world,
3560 TP_PROTO(int foo, const char* bar),
3561 TP_ARGS(foo, bar),
3562
3563 /* possible differences */
3564 TP_STRUCT__entry(
3565 __field(int, my_int)
3566 __field(char, char0)
3567 __field(char, char1)
3568 __string(product, bar)
3569 ),
3570
3571 /* notice the use of tp_assign()/tp_strcpy() and no semicolons */
3572 TP_fast_assign(
3573 tp_assign(my_int, foo)
3574 tp_assign(char0, bar[0])
3575 tp_assign(char1, bar[1])
3576 tp_strcpy(product, bar)
3577 ),
3578
3579 /* This one is actually not used by LTTng either, but must be
3580 * present for the moment.
3581 */
3582 TP_printk("", 0)
3583
3584/* no semicolon after this either */
3585)
3586
3587#endif
3588
3589/* other difference: do NOT include <trace/define_trace.h> */
3590#include "../../../probes/define_trace.h"
3591----
3592
3593Some possible entries for `TP_STRUCT__entry()` and `TP_fast_assign()`,
3594in the case of LTTng-modules, are shown in the
3595<<lttng-modules-ref,LTTng-modules reference>> section.
3596
3597The best way to learn how to use the above macros is to inspect
3598existing LTTng tracepoint definitions in
3599dir:{instrumentation/events/lttng-module} header files. Compare
3600them with the Linux kernel mainline versions in
3601dir:{include/trace/events}.
3602
3603The next step is writing the LTTng probe kernel module C source file.
3604This one is named +lttng-probe-__subsys__.c+
3605in dir:{probes}. You may always use the following template:
3606
3607[source,c]
3608----
3609#include <linux/module.h>
3610#include "../lttng-tracer.h"
3611
3612/* Build time verification of mismatch between mainline TRACE_EVENT()
3613 * arguments and LTTng adaptation layer LTTNG_TRACEPOINT_EVENT() arguments.
3614 */
3615#include <trace/events/hello.h>
3616
3617/* create LTTng tracepoint probes */
3618#define LTTNG_PACKAGE_BUILD
3619#define CREATE_TRACE_POINTS
3620#define TRACE_INCLUDE_PATH ../instrumentation/events/lttng-module
3621
3622#include "../instrumentation/events/lttng-module/hello.h"
3623
3624MODULE_LICENSE("GPL and additional rights");
3625MODULE_AUTHOR("Your name <your-email>");
3626MODULE_DESCRIPTION("LTTng hello probes");
3627MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
3628 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
3629 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
3630 LTTNG_MODULES_EXTRAVERSION);
3631----
3632
3633Just replace `hello` with your subsystem name. In this example,
3634`<trace/events/hello.h>`, which is the original mainline tracepoint
3635definition header, is included for verification purposes: the
3636LTTng-modules build system is able to emit an error at build time when
3637the arguments of the mainline `TRACE_EVENT()` definitions do not match
3638the ones of the LTTng-modules adaptation layer
3639(`LTTNG_TRACEPOINT_EVENT()`).
3640
3641Edit path:{probes/Makefile} and add your new kernel module object
3642next to existing ones:
3643
3644[source,make]
3645----
3646# ...
3647
3648obj-m += lttng-probe-module.o
3649obj-m += lttng-probe-power.o
3650
3651obj-m += lttng-probe-hello.o
3652
3653# ...
3654----
3655
3656Time to build! Point to your custom Linux kernel source tree using
3657the `KERNELDIR` variable:
3658
3659[role="term"]
3660----
3661make KERNELDIR=/path/to/custom/linux
3662----
3663
3664Finally, install modules:
3665
3666[role="term"]
3667----
3668sudo make modules_install
3669----
3670
3671
3672[[instrumenting-linux-kernel-tracing]]
3673===== Tracing
3674
3675The <<controlling-tracing,Controlling tracing>> section explains
3676how to use the `lttng` tool to create and control tracing sessions.
3677Although the `lttng` tool loads the appropriate _known_ LTTng kernel
3678modules when needed (by launching `root`'s session daemon), it won't
3679load your custom `lttng-probe-hello` module by default. You need to
3680manually start an LTTng session daemon as `root` and use the
3681`--extra-kmod-probes` option to append your custom probe module to the
3682default list:
3683
3684[role="term"]
3685----
3686sudo pkill -u root lttng-sessiond
3687sudo lttng-sessiond --extra-kmod-probes=hello
3688----
3689
3690The first command makes sure any existing instance is killed. If
3691you're not interested in using the default probes, or if you only
3692want to use a few of them, you could use `--kmod-probes` instead,
3693which specifies an absolute list:
3694
3695[role="term"]
3696----
3697sudo lttng-sessiond --kmod-probes=hello,ext4,net,block,signal,sched
3698----
3699
3700Confirm the custom probe module is loaded:
3701
3702[role="term"]
3703----
3704lsmod | grep lttng_probe_hello
3705----
3706
3707The `hello_world` event should appear in the list when doing
3708
3709[role="term"]
3710----
3711lttng list --kernel | grep hello
3712----
3713
3714You may now create an LTTng tracing session, enable the `hello_world`
3715kernel event (and others if you wish) and start tracing:
3716
3717[role="term"]
3718----
3719sudo lttng create my-session
3720sudo lttng enable-event --kernel hello_world
3721sudo lttng start
3722----
3723
3724Plug a few USB devices, then stop tracing and inspect the trace (if
3725http://diamon.org/babeltrace[Babeltrace]
3726is installed):
3727
3728[role="term"]
3729----
3730sudo lttng stop
3731sudo lttng view
3732----
3733
3734Here's a sample output:
3735
3736----
3737[15:30:34.835895035] (+?.?????????) hostname hello_world: { cpu_id = 1 }, { my_int = 8, char0 = 68, char1 = 97, product = "DataTraveler 2.0" }
3738[15:30:42.262781421] (+7.426886386) hostname hello_world: { cpu_id = 1 }, { my_int = 9, char0 = 80, char1 = 97, product = "Patriot Memory" }
3739[15:30:48.175621778] (+5.912840357) hostname hello_world: { cpu_id = 1 }, { my_int = 10, char0 = 68, char1 = 97, product = "DataTraveler 2.0" }
3740----
3741
3742Two USB flash drives were used for this test.
3743
3744You may change your LTTng custom probe, rebuild it and reload it at
3745any time when not tracing. Make sure you remove the old module
3746(either by killing the root LTTng session daemon which loaded the
3747module in the first place, or by using `modprobe --remove` directly)
3748before loading the updated one.
3749
3750
3751[[instrumenting-out-of-tree-linux-kernel]]
3752===== Advanced: Instrumenting an out-of-tree Linux kernel module for LTTng
3753
3754Instrumenting a custom Linux kernel module for LTTng follows the exact
3755same steps as
3756<<instrumenting-linux-kernel-itself,adding instrumentation
3757to the Linux kernel itself>>,
3758the only difference being that your mainline tracepoint definition
3759header doesn't reside in the mainline source tree, but in your
3760kernel module source tree.
3761
3762The only reference to this mainline header is in the LTTng custom
3763probe's source code (path:{probes/lttng-probe-hello.c} in our example),
3764for build time verification:
3765
3766[source,c]
3767----
3768/* ... */
3769
3770/* Build time verification of mismatch between mainline TRACE_EVENT()
3771 * arguments and LTTng adaptation layer LTTNG_TRACEPOINT_EVENT() arguments.
3772 */
3773#include <trace/events/hello.h>
3774
3775/* ... */
3776----
3777
3778The preferred, flexible way to include your module's mainline
3779tracepoint definition header is to put it in a specific directory
3780relative to your module's root (`tracepoints`, for example) and include it
3781relative to your module's root directory in the LTTng custom probe's
3782source:
3783
3784[source,c]
3785----
3786#include <tracepoints/hello.h>
3787----
3788
3789You may then build LTTng-modules by adding your module's root
3790directory as an include path to the extra C flags:
3791
3792[role="term"]
3793----
3794make ccflags-y=-I/path/to/kernel/module KERNELDIR=/path/to/custom/linux
3795----
3796
3797Using `ccflags-y` allows you to move your kernel module to another
3798directory and rebuild the LTTng-modules project with no change to
3799source files.
3800
3801
3802[role="since-2.5"]
3803[[proc-lttng-logger-abi]]
3804==== LTTng logger ABI
3805
3806The `lttng-tracer` Linux kernel module, installed by the LTTng-modules
3807package, creates a special LTTng logger ABI file path:{/proc/lttng-logger}
3808when loaded. Writing text data to this file generates an LTTng kernel
3809domain event named `lttng_logger`.
3810
3811Unlike other kernel domain events, `lttng_logger` may be enabled by
3812any user, not only root users or members of the tracing group.
3813
3814To use the LTTng logger ABI, simply write a string to
3815path:{/proc/lttng-logger}:
3816
3817[role="term"]
3818----
3819echo -n 'Hello, World!' > /proc/lttng-logger
3820----
3821
3822The `msg` field of the `lttng_logger` event contains the recorded
3823message.
3824
3825NOTE: Messages are split in chunks of 1024{nbsp}bytes.
3826
3827The LTTng logger ABI is a quick and easy way to trace some events from
3828user space through the kernel tracer. However, it is much more basic
3829than LTTng-UST: it's slower (involves system call round-trip to the
3830kernel and only supports logging strings). The LTTng logger ABI is
3831particularly useful for recording logs as LTTng traces from shell
3832scripts, potentially combining them with other Linux kernel/user space
3833events.
3834
3835
3836[[instrumenting-32-bit-app-on-64-bit-system]]
3837==== Advanced: Instrumenting a 32-bit application on a 64-bit system
3838
3839[[advanced-instrumenting-techniques]]In order to trace a 32-bit
3840application running on a 64-bit system,
3841LTTng must use a dedicated 32-bit
3842<<lttng-consumerd,consumer daemon>>. This section discusses how to
3843build that daemon (which is _not_ part of the default 64-bit LTTng
3844build) and the LTTng 32-bit tracing libraries, and how to instrument
3845a 32-bit application in that context.
3846
3847Make sure you install all 32-bit versions of LTTng dependencies.
3848Their names can be found in the `README.md` files of each LTTng package
3849source. How to find and install them depends on your target's
3850Linux distribution. `gcc-multilib` is a common package name for the
3851multilib version of GCC, which you also need.
3852
3853The following packages will be built for 32-bit support on a 64-bit
3854system: http://urcu.so/[Userspace RCU],
3855LTTng-UST and LTTng-tools.
3856
3857
3858[[building-32-bit-userspace-rcu]]
3859===== Building 32-bit Userspace RCU
3860
3861Follow this:
3862
3863[role="term"]
3864----
3865git clone git://git.urcu.so/urcu.git
3866cd urcu
3867./bootstrap
3868./configure --libdir=/usr/lib32 CFLAGS=-m32
3869make
3870sudo make install
3871sudo ldconfig
3872----
3873
3874The `-m32` C compiler flag creates 32-bit object files and `--libdir`
3875indicates where to install the resulting libraries.
3876
3877
3878[[building-32-bit-lttng-ust]]
3879===== Building 32-bit LTTng-UST
3880
3881Follow this:
3882
3883[role="term"]
3884----
3885git clone http://git.lttng.org/lttng-ust.git
3886cd lttng-ust
3887./bootstrap
3888./configure --prefix=/usr \
3889 --libdir=/usr/lib32 \
3890 CFLAGS=-m32 CXXFLAGS=-m32 \
3891 LDFLAGS=-L/usr/lib32
3892make
3893sudo make install
3894sudo ldconfig
3895----
3896
3897`-L/usr/lib32` is required for the build to find the 32-bit versions
3898of Userspace RCU and other dependencies.
3899
3900[NOTE]
3901====
3902Depending on your Linux distribution,
390332-bit libraries could be installed at a different location than
3904dir:{/usr/lib32}. For example, Debian is known to install
3905some 32-bit libraries in dir:{/usr/lib/i386-linux-gnu}.
3906
3907In this case, make sure to set `LDFLAGS` to all the
3908relevant 32-bit library paths, for example,
3909`LDFLAGS="-L/usr/lib32 -L/usr/lib/i386-linux-gnu"`.
3910====
3911
3912NOTE: You may add options to path:{./configure} if you need them, e.g., for
3913Java and SystemTap support. Look at `./configure --help` for more
3914information.
3915
3916
3917[[building-32-bit-lttng-tools]]
3918===== Building 32-bit LTTng-tools
3919
3920Since the host is a 64-bit system, most 32-bit binaries and libraries of
3921LTTng-tools are not needed; the host uses their 64-bit counterparts.
3922The required step here is building and installing a 32-bit consumer
3923daemon.
3924
3925Follow this:
3926
3927[role="term"]
3928----
3929git clone http://git.lttng.org/lttng-tools.git
3930cd lttng-ust
3931./bootstrap
3932./configure --prefix=/usr \
3933 --libdir=/usr/lib32 CFLAGS=-m32 CXXFLAGS=-m32 \
3934 LDFLAGS=-L/usr/lib32
3935make
3936cd src/bin/lttng-consumerd
3937sudo make install
3938sudo ldconfig
3939----
3940
3941The above commands build all the LTTng-tools project as 32-bit
3942applications, but only installs the 32-bit consumer daemon.
3943
3944
3945[[building-64-bit-lttng-tools]]
3946===== Building 64-bit LTTng-tools
3947
3948Finally, you need to build a 64-bit version of LTTng-tools which is
3949aware of the 32-bit consumer daemon previously built and installed:
3950
3951[role="term"]
3952----
3953make clean
3954./bootstrap
3955./configure --prefix=/usr \
3956 --with-consumerd32-libdir=/usr/lib32 \
3957 --with-consumerd32-bin=/usr/lib32/lttng/libexec/lttng-consumerd
3958make
3959sudo make install
3960sudo ldconfig
3961----
3962
3963Henceforth, the 64-bit session daemon automatically finds the
396432-bit consumer daemon if required.
3965
3966
3967[[building-instrumented-32-bit-c-application]]
3968===== Building an instrumented 32-bit C application
3969
3970Let us reuse the _Hello world_ example of
3971<<tracing-your-own-user-application,Tracing your own user application>>
3972(<<getting-started,Getting started>> chapter).
3973
3974The instrumentation process is unaltered.
3975
3976First, a typical 64-bit build (assuming you're running a 64-bit system):
3977
3978[role="term"]
3979----
3980gcc -o hello64 -I. hello.c hello-tp.c -ldl -llttng-ust
3981----
3982
3983Now, a 32-bit build:
3984
3985[role="term"]
3986----
3987gcc -o hello32 -I. -m32 hello.c hello-tp.c -L/usr/lib32 \
3988 -ldl -llttng-ust -Wl,-rpath,/usr/lib32
3989----
3990
3991The `-rpath` option, passed to the linker, makes the dynamic loader
3992check for libraries in dir:{/usr/lib32} before looking in its default paths,
3993where it should find the 32-bit version of `liblttng-ust`.
3994
3995
3996[[running-32-bit-and-64-bit-c-applications]]
3997===== Running 32-bit and 64-bit versions of an instrumented C application
3998
3999Now, both 32-bit and 64-bit versions of the _Hello world_ example above
4000can be traced in the same tracing session. Use the `lttng` tool as usual
4001to create a tracing session and start tracing:
4002
4003[role="term"]
4004----
4005lttng create session-3264
4006lttng enable-event -u -a
4007./hello32
4008./hello64
4009lttng stop
4010----
4011
4012Use `lttng view` to verify both processes were
4013successfully traced.
4014
4015
4016[[controlling-tracing]]
4017=== Controlling tracing
4018
4019Once you're in possession of a software that is properly
4020<<instrumenting,instrumented>> for LTTng tracing, be it thanks to
4021the built-in LTTng probes for the Linux kernel, a custom user
4022application or a custom Linux kernel, all that is left is actually
4023tracing it. As a user, you control LTTng tracing using a single command
4024line interface: the `lttng` tool. This tool uses `liblttng-ctl` behind
4025the scene to connect to and communicate with session daemons. LTTng
4026session daemons may either be started manually (`lttng-sessiond`) or
4027automatically by the `lttng` command when needed. Trace data may
4028be forwarded to the network and used elsewhere using an LTTng relay
4029daemon (`lttng-relayd`).
4030
4031The manpages of `lttng`, `lttng-sessiond` and `lttng-relayd` are pretty
4032complete, thus this section is not an online copy of the latter (we
4033leave this contents for the
4034<<online-lttng-manpages,Online LTTng manpages>> section).
4035This section is rather a tour of LTTng
4036features through practical examples and tips.
4037
4038If not already done, make sure you understand the core concepts
4039and how LTTng components connect together by reading the
4040<<understanding-lttng,Understanding LTTng>> chapter; this section
4041assumes you are familiar with them.
4042
4043
4044[[creating-destroying-tracing-sessions]]
4045==== Creating and destroying tracing sessions
4046
4047Whatever you want to do with `lttng`, it has to happen inside a
4048**tracing session**, created beforehand. A session, in general, is a
4049per-user container of state. A tracing session is no different; it
4050keeps a specific state of stuff like:
4051
4052* session name
4053* enabled/disabled channels with associated parameters
4054* enabled/disabled events with associated log levels and filters
4055* context information added to channels
4056* tracing activity (started or stopped)
4057
4058and more.
4059
4060A single user may have many active tracing sessions. LTTng session
4061daemons are the ultimate owners and managers of tracing sessions. For
4062user space tracing, each user has its own session daemon. Since Linux
4063kernel tracing requires root privileges, only `root`'s session daemon
4064may enable and trace kernel events. However, `lttng` has a `--group`
4065option (which is passed to `lttng-sessiond` when starting it) to
4066specify the name of a _tracing group_ which selected users may be part
4067of to be allowed to communicate with `root`'s session daemon. By
4068default, the tracing group name is `tracing`.
4069
4070To create a tracing session, do:
4071
4072[role="term"]
4073----
4074lttng create my-session
4075----
4076
4077This creates a new tracing session named `my-session` and make it
4078the current one. If you don't specify a name (running only
4079`lttng create`), your tracing session is named `auto` followed by the
4080current date and time. Traces
4081are written in +\~/lttng-traces/__session__-+ followed
4082by the tracing session's creation date/time by default, where
4083+__session__+ is the tracing session name. To save them
4084at a different location, use the `--output` option:
4085
4086[role="term"]
4087----
4088lttng create --output /tmp/some-directory my-session
4089----
4090
4091You may create as many tracing sessions as you wish:
4092
4093[role="term"]
4094----
4095lttng create other-session
4096lttng create yet-another-session
4097----
4098
4099You may view all existing tracing sessions using the `list` command:
4100
4101[role="term"]
4102----
4103lttng list
4104----
4105
4106The state of a _current tracing session_ is kept in path:{~/.lttngrc}. Each
4107invocation of `lttng` reads this file to set its current tracing
4108session name so that you don't have to specify a session name for each
4109command. You could edit this file manually, but the preferred way to
4110set the current tracing session is to use the `set-session` command:
4111
4112[role="term"]
4113----
4114lttng set-session other-session
4115----
4116
4117Most `lttng` commands accept a `--session` option to specify the name
4118of the target tracing session.
4119
4120Any existing tracing session may be destroyed using the `destroy`
4121command:
4122
4123[role="term"]
4124----
4125lttng destroy my-session
4126----
4127
4128Providing no argument to `lttng destroy` destroys the current
4129tracing session. Destroying a tracing session stops any tracing
4130running within the latter. Destroying a tracing session frees resources
4131acquired by the session daemon and tracer side, making sure to flush
4132all trace data.
4133
4134You can't do much with LTTng using only the `create`, `set-session`
4135and `destroy` commands of `lttng`, but it is essential to know them in
4136order to control LTTng tracing, which always happen within the scope of
4137a tracing session.
4138
4139
4140[[enabling-disabling-events]]
4141==== Enabling and disabling events
4142
4143Inside a tracing session, individual events may be enabled or disabled
4144so that tracing them may or may not generate trace data.
4145
4146We sometimes use the term _event_ metonymically throughout this text to
4147refer to a specific condition, or _rule_, that could lead, when
4148satisfied, to an actual occurring event (a point at a specific position
4149in source code/binary program, logical processor and time capturing
4150some payload) being recorded as trace data. This specific condition is
4151composed of:
4152
4153. A **domain** (kernel, user space, `java.util.logging`, or log4j)
4154 (required).
4155. One or many **instrumentation points** in source code or binary
4156 program (tracepoint name, address, symbol name, function name,
4157 logger name, amongst other types of probes) to be executed (required).
4158. A **log level** (each instrumentation point declares its own log
4159 level) or log level range to match (optional; only valid for user
4160 space domain).
4161. A **custom user expression**, or **filter**, that must evaluate to
4162 _true_ when a tracepoint is executed (optional; only valid for user
4163 space domain).
4164
4165All conditions are specified using arguments passed to the
4166`enable-event` command of the `lttng` tool.
4167
4168Condition 1 is specified using either `--kernel`/`-k` (kernel),
4169`--userspace`/`-u` (user space), `--jul`/`-j`
4170(JUL), or `--log4j`/`-l` (log4j).
4171Exactly one of those four arguments must be specified.
4172
4173Condition 2 is specified using one of:
4174
4175`--tracepoint`::
4176 Tracepoint.
4177
4178`--probe`::
4179 Dynamic probe (address, symbol name or combination
4180 of both in binary program; only valid for kernel domain).
4181
4182`--function`::
4183 function entry/exit (address, symbol name or
4184 combination of both in binary program; only valid for kernel domain).
4185
4186`--syscall`::
4187 System call entry/exit (only valid for kernel domain).
4188
4189When none of the above is specified, `enable-event` defaults to
4190using `--tracepoint`.
4191
4192Condition 3 is specified using one of:
4193
4194`--loglevel`::
4195 Log level range from the specified level to the most severe
4196 level.
4197
4198`--loglevel-only`::
4199 Specific log level.
4200
4201See `lttng enable-event --help` for the complete list of log level
4202names.
4203
4204Condition 4 is specified using the `--filter` option. This filter is
4205a C-like expression, potentially reading real-time values of event
4206fields, that has to evaluate to _true_ for the condition to be satisfied.
4207Event fields are read using plain identifiers while context fields
4208must be prefixed with `$ctx.`. See `lttng enable-event --help` for
4209all usage details.
4210
4211The aforementioned arguments are combined to create and enable events.
4212Each unique combination of arguments leads to a different
4213_enabled event_. The log level and filter arguments are optional, their
4214default values being respectively all log levels and a filter which
4215always returns _true_.
4216
4217Here are a few examples (you must
4218<<creating-destroying-tracing-sessions,create a tracing session>>
4219first):
4220
4221[role="term"]
4222----
4223lttng enable-event -u --tracepoint my_app:hello_world
4224lttng enable-event -u --tracepoint my_app:hello_you --loglevel TRACE_WARNING
4225lttng enable-event -u --tracepoint 'my_other_app:*'
4226lttng enable-event -u --tracepoint my_app:foo_bar \
4227 --filter 'some_field <= 23 && !other_field'
4228lttng enable-event -k --tracepoint sched_switch
4229lttng enable-event -k --tracepoint gpio_value
4230lttng enable-event -k --function usb_probe_device usb_probe_device
4231lttng enable-event -k --syscall --all
4232----
4233
4234The wildcard symbol, `*`, matches _anything_ and may only be used at
4235the end of the string when specifying a _tracepoint_. Make sure to
4236use it between single quotes in your favorite shell to avoid
4237undesired shell expansion.
4238
4239System call events can be enabled individually, too:
4240
4241[role="term"]
4242----
4243lttng enable-event -k --syscall open
4244lttng enable-event -k --syscall read
4245lttng enable-event -k --syscall fork,chdir,pipe
4246----
4247
4248The complete list of available system call events can be
4249obtained using
4250
4251[role="term"]
4252----
4253lttng list --kernel --syscall
4254----
4255
4256You can see a list of events (enabled or disabled) using
4257
4258[role="term"]
4259----
4260lttng list some-session
4261----
4262
4263where `some-session` is the name of the desired tracing session.
4264
4265What you're actually doing when enabling events with specific conditions
4266is creating a **whitelist** of traceable events for a given channel.
4267Thus, the following case presents redundancy:
4268
4269[role="term"]
4270----
4271lttng enable-event -u --tracepoint my_app:hello_you
4272lttng enable-event -u --tracepoint my_app:hello_you --loglevel TRACE_DEBUG
4273----
4274
4275The second command, matching a log level range, is useless since the first
4276command enables all tracepoints matching the same name,
4277`my_app:hello_you`.
4278
4279Disabling an event is simpler: you only need to provide the event
4280name to the `disable-event` command:
4281
4282[role="term"]
4283----
4284lttng disable-event --userspace my_app:hello_you
4285----
4286
4287This name has to match a name previously given to `enable-event` (it
4288has to be listed in the output of `lttng list some-session`).
4289The `*` wildcard is supported, as long as you also used it in a
4290previous `enable-event` invocation.
4291
4292Disabling an event does not add it to some blacklist: it simply removes
4293it from its channel's whitelist. This is why you cannot disable an event
4294which wasn't previously enabled.
4295
4296A disabled event doesn't generate any trace data, even if all its
4297specified conditions are met.
4298
4299Events may be enabled and disabled at will, either when LTTng tracers
4300are active or not. Events may be enabled before a user space application
4301is even started.
4302
4303
4304[[basic-tracing-session-control]]
4305==== Basic tracing session control
4306
4307Once you have
4308<<creating-destroying-tracing-sessions,created a tracing session>>
4309and <<enabling-disabling-events,enabled one or more events>>,
4310you may activate the LTTng tracers for the current tracing session at
4311any time:
4312
4313[role="term"]
4314----
4315lttng start
4316----
4317
4318Subsequently, you may stop the tracers:
4319
4320[role="term"]
4321----
4322lttng stop
4323----
4324
4325LTTng is very flexible: user space applications may be launched before
4326or after the tracers are started. Events are only recorded if they
4327are properly enabled and if they occur while tracers are active.
4328
4329A tracing session name may be passed to both the `start` and `stop`
4330commands to start/stop tracing a session other than the current one.
4331
4332
4333[[enabling-disabling-channels]]
4334==== Enabling and disabling channels
4335
4336<<event,As mentioned>> in the
4337<<understanding-lttng,Understanding LTTng>> chapter, enabled
4338events are contained in a specific channel, itself contained in a
4339specific tracing session. A channel is a group of events with
4340tunable parameters (event loss mode, sub-buffer size, number of
4341sub-buffers, trace file sizes and count, to name a few). A given channel
4342may only be responsible for enabled events belonging to one domain:
4343either kernel or user space.
4344
4345If you only used the `create`, `enable-event` and `start`/`stop`
4346commands of the `lttng` tool so far, one or two channels were
4347automatically created for you (one for the kernel domain and/or one
4348for the user space domain). The default channels are both named
4349`channel0`; channels from different domains may have the same name.
4350
4351The current channels of a given tracing session can be viewed with
4352
4353[role="term"]
4354----
4355lttng list some-session
4356----
4357
4358where `some-session` is the name of the desired tracing session.
4359
4360To create and enable a channel, use the `enable-channel` command:
4361
4362[role="term"]
4363----
4364lttng enable-channel --kernel my-channel
4365----
4366
4367This creates a kernel domain channel named `my-channel` with
4368default parameters in the current tracing session.
4369
4370[NOTE]
4371====
4372Because of a current limitation, all
4373channels must be _created_ prior to beginning tracing in a
4374given tracing session, that is before the first time you do
4375`lttng start`.
4376
4377Since a channel is automatically created by
4378`enable-event` only for the specified domain, you cannot,
4379for example, enable a kernel domain event, start tracing and then
4380enable a user space domain event because no user space channel
4381exists yet and it's too late to create one.
4382
4383For this reason, make sure to configure your channels properly
4384before starting the tracers for the first time!
4385====
4386
4387Here's another example:
4388
4389[role="term"]
4390----
4391lttng enable-channel --userspace --session other-session --overwrite \
4392 --tracefile-size 1048576 1mib-channel
4393----
4394
4395This creates a user space domain channel named `1mib-channel` in
4396the tracing session named `other-session` that loses new events by
4397overwriting previously recorded events (instead of the default mode of
4398discarding newer ones) and saves trace files with a maximum size of
43991{nbsp}MiB each.
4400
4401Note that channels may also be created using the `--channel` option of
4402the `enable-event` command when the provided channel name doesn't exist
4403for the specified domain:
4404
4405[role="term"]
4406----
4407lttng enable-event --kernel --channel some-channel sched_switch
4408----
4409
4410If no kernel domain channel named `some-channel` existed before calling
4411the above command, it would be created with default parameters.
4412
4413You may enable the same event in two different channels:
4414
4415[role="term"]
4416----
4417lttng enable-event --userspace --channel my-channel app:tp
4418lttng enable-event --userspace --channel other-channel app:tp
4419----
4420
4421If both channels are enabled, the occurring `app:tp` event
4422generates two recorded events, one for each channel.
4423
4424Disabling a channel is done with the `disable-event` command:
4425
4426[role="term"]
4427----
4428lttng disable-event --kernel some-channel
4429----
4430
4431The state of a channel precedes the individual states of events within
4432it: events belonging to a disabled channel, even if they are
4433enabled, won't be recorded.
4434
4435
4436
4437[[fine-tuning-channels]]
4438===== Fine-tuning channels
4439
4440There are various parameters that may be fine-tuned with the
4441`enable-channel` command. The latter are well documented in
4442man:lttng(1) and in the <<channel,Channel>> section of the
4443<<understanding-lttng,Understanding LTTng>> chapter. For basic
4444tracing needs, their default values should be just fine, but here are a
4445few examples to break the ice.
4446
4447As the frequency of recorded events increases--either because the
4448event throughput is actually higher or because you enabled more events
4449than usual&#8212;__event loss__ might be experienced. Since LTTng never
4450waits, by design, for sub-buffer space availability (non-blocking
4451tracer), when a sub-buffer is full and no empty sub-buffers are left,
4452there are two possible outcomes: either the new events that do not fit
4453are rejected, or they start replacing the oldest recorded events.
4454The choice of which algorithm to use is a per-channel parameter, the
4455default being discarding the newest events until there is some space
4456left. If your situation always needs the latest events at the expense
4457of writing over the oldest ones, create a channel with the `--overwrite`
4458option:
4459
4460[role="term"]
4461----
4462lttng enable-channel --kernel --overwrite my-channel
4463----
4464
4465When an event is lost, it means no space was available in any
4466sub-buffer to accommodate it. Thus, if you want to cope with sporadic
4467high event throughput situations and avoid losing events, you need to
4468allocate more room for storing them in memory. This can be done by
4469either increasing the size of sub-buffers or by adding sub-buffers.
4470The following example creates a user space domain channel with
447116{nbsp}sub-buffers of 512{nbsp}kiB each:
4472
4473[role="term"]
4474----
4475lttng enable-channel --userspace --num-subbuf 16 --subbuf-size 512k big-channel
4476----
4477
4478Both values need to be powers of two, otherwise they are rounded up
4479to the next one.
4480
4481Two other interesting available parameters of `enable-channel` are
4482`--tracefile-size` and `--tracefile-count`, which respectively limit
4483the size of each trace file and the their count for a given channel.
4484When the number of written trace files reaches its limit for a given
4485channel-CPU pair, the next trace file overwrites the very first
4486one. The following example creates a kernel domain channel with a
4487maximum of three trace files of 1{nbsp}MiB each:
4488
4489[role="term"]
4490----
4491lttng enable-channel --kernel --tracefile-size 1M --tracefile-count 3 my-channel
4492----
4493
4494An efficient way to make sure lots of events are generated is enabling
4495all kernel events in this channel and starting the tracer:
4496
4497[role="term"]
4498----
4499lttng enable-event --kernel --all --channel my-channel
4500lttng start
4501----
4502
4503After a few seconds, look at trace files in your tracing session
4504output directory. For two CPUs, it should look like:
4505
4506----
4507my-channel_0_0 my-channel_1_0
4508my-channel_0_1 my-channel_1_1
4509my-channel_0_2 my-channel_1_2
4510----
4511
4512Amongst the files above, you might see one in each group with a size
4513lower than 1{nbsp}MiB: they are the files currently being written.
4514
4515Since all those small files are valid LTTng trace files, LTTng trace
4516viewers may read them. It is the viewer's responsibility to properly
4517merge the streams so as to present an ordered list to the user.
4518http://diamon.org/babeltrace[Babeltrace]
4519merges LTTng trace files correctly and is fast at doing it.
4520
4521
4522[[adding-context]]
4523==== Adding some context to channels
4524
4525If you read all the sections of
4526<<controlling-tracing,Controlling tracing>> so far, you should be
4527able to create tracing sessions, create and enable channels and events
4528within them and start/stop the LTTng tracers. Event fields recorded in
4529trace files provide important information about occurring events, but
4530sometimes external context may help you solve a problem faster. This
4531section discusses how to add context information to events of a
4532specific channel using the `lttng` tool.
4533
4534There are various available context values which can accompany events
4535recorded by LTTng, for example:
4536
4537* **process information**:
4538** identifier (PID)
4539** name
4540** priority
4541** scheduling priority (niceness)
4542** thread identifier (TID)
4543* the **hostname** of the system on which the event occurred
4544* plenty of **performance counters** using perf, for example:
4545** CPU cycles, stalled cycles, idle cycles, and the other cycle types
4546** cache misses
4547** branch instructions, misses, loads
4548** CPU faults
4549
4550The full list is available in the output of `lttng add-context --help`.
4551Some of them are reserved for a specific domain (kernel or
4552user space) while others are available for both.
4553
4554To add context information to one or all channels of a given tracing
4555session, use the `add-context` command:
4556
4557[role="term"]
4558----
4559lttng add-context --userspace --type vpid --type perf:thread:cpu-cycles
4560----
4561
4562The above example adds the virtual process identifier and per-thread
4563CPU cycles count values to all recorded user space domain events of the
4564current tracing session. Use the `--channel` option to select a specific
4565channel:
4566
4567[role="term"]
4568----
4569lttng add-context --kernel --channel my-channel --type tid
4570----
4571
4572adds the thread identifier value to all recorded kernel domain events
4573in the channel `my-channel` of the current tracing session.
4574
4575Beware that context information cannot be removed from channels once
4576it's added for a given tracing session.
4577
4578
4579[role="since-2.5"]
4580[[saving-loading-tracing-session]]
4581==== Saving and loading tracing session configurations
4582
4583Configuring a tracing session may be long: creating and enabling
4584channels with specific parameters, enabling kernel and user space
4585domain events with specific log levels and filters, and adding context
4586to some channels are just a few of the many possible operations using
4587the `lttng` command line tool. If you're going to use LTTng to solve real
4588world problems, chances are you're going to have to record events using
4589the same tracing session setup over and over, modifying a few variables
4590each time in your instrumented program or environment. To avoid
4591constant tracing session reconfiguration, the `lttng` tool is able to
4592save and load tracing session configurations to/from XML files.
4593
4594To save a given tracing session configuration, do:
4595
4596[role="term"]
4597----
4598lttng save my-session
4599----
4600
4601where `my-session` is the name of the tracing session to save. Tracing
4602session configurations are saved to dir:{~/.lttng/sessions} by default;
4603use the `--output-path` option to change this destination directory.
4604
4605All configuration parameters are saved:
4606
4607* tracing session name
4608* trace data output path
4609* channels with their state and all their parameters
4610* context information added to channels
4611* events with their state, log level and filter
4612* tracing activity (started or stopped)
4613
4614To load a tracing session, simply do:
4615
4616[role="term"]
4617----
4618lttng load my-session
4619----
4620
4621or, if you used a custom path:
4622
4623[role="term"]
4624----
4625lttng load --input-path /path/to/my-session.lttng
4626----
4627
4628Your saved tracing session is restored as if you just configured
4629it manually.
4630
4631
4632[[sending-trace-data-over-the-network]]
4633==== Sending trace data over the network
4634
4635The possibility of sending trace data over the network comes as a
4636built-in feature of LTTng-tools. For this to be possible, an LTTng
4637_relay daemon_ must be executed and listening on the machine where
4638trace data is to be received, and the user must create a tracing
4639session using appropriate options to forward trace data to the remote
4640relay daemon.
4641
4642The relay daemon listens on two different TCP ports: one for control
4643information and the other for actual trace data.
4644
4645Starting the relay daemon on the remote machine is easy:
4646
4647[role="term"]
4648----
4649lttng-relayd
4650----
4651
4652This makes it listen to its default ports: 5342 for control and
46535343 for trace data. The `--control-port` and `--data-port` options may
4654be used to specify different ports.
4655
4656Traces written by `lttng-relayd` are written to
4657+\~/lttng-traces/__hostname__/__session__+ by
4658default, where +__hostname__+ is the host name of the
4659traced (monitored) system and +__session__+ is the
4660tracing session name. Use the `--output` option to write trace data
4661outside dir:{~/lttng-traces}.
4662
4663On the sending side, a tracing session must be created using the
4664`lttng` tool with the `--set-url` option to connect to the distant
4665relay daemon:
4666
4667[role="term"]
4668----
4669lttng create my-session --set-url net://distant-host
4670----
4671
4672The URL format is described in the output of `lttng create --help`.
4673The above example uses the default ports; the `--ctrl-url` and
4674`--data-url` options may be used to set the control and data URLs
4675individually.
4676
4677Once this basic setup is completed and the connection is established,
4678you may use the `lttng` tool on the target machine as usual; everything
4679you do is transparently forwarded to the remote machine if needed.
4680For example, a parameter changing the maximum size of trace files
4681only has an effect on the distant relay daemon actually writing
4682the trace.
4683
4684
4685[role="since-2.4"]
4686[[lttng-live]]
4687==== Viewing events as they arrive
4688
4689We have seen how trace files may be produced by LTTng out of generated
4690application and Linux kernel events. We have seen that those trace files
4691may be either recorded locally by consumer daemons or remotely using
4692a relay daemon. And we have seen that the maximum size and count of
4693trace files is configurable for each channel. With all those features,
4694it's still not possible to read a trace file as it is being written
4695because it could be incomplete and appear corrupted to the viewer.
4696There is a way to view events as they arrive, however: using
4697_LTTng live_.
4698
4699LTTng live is implemented, in LTTng, solely on the relay daemon side.
4700As trace data is sent over the network to a relay daemon by a (possibly
4701remote) consumer daemon, a _tee_ is created: trace data is recorded to
4702trace files _as well as_ being transmitted to a connected live viewer:
4703
4704[role="img-90"]
4705.The relay daemon creates a _tee_, forwarding the trace data to both trace files and a live viewer.
4706image::lttng-live.png[]
4707
4708In order to use this feature, a tracing session must created in live
4709mode on the target system:
4710
4711[role="term"]
4712----
4713lttng create --live
4714----
4715
4716An optional parameter may be passed to `--live` to set the period
4717(in microseconds) between flushes to the network
4718(1{nbsp}second is the default). With:
4719
4720[role="term"]
4721----
4722lttng create --live 100000
4723----
4724
4725the daemons flush their data every 100{nbsp}ms.
4726
4727If no network output is specified to the `create` command, a local
4728relay daemon is spawned. In this very common case, viewing a live
4729trace is easy: enable events and start tracing as usual, then use
4730`lttng view` to start the default live viewer:
4731
4732[role="term"]
4733----
4734lttng view
4735----
4736
4737The correct arguments are passed to the live viewer so that it
4738may connect to the local relay daemon and start reading live events.
4739
4740You may also wish to use a live viewer not running on the target
4741system. In this case, you should specify a network output when using
4742the `create` command (`--set-url` or `--ctrl-url`/`--data-url` options).
4743A distant LTTng relay daemon should also be started to receive control
4744and trace data. By default, `lttng-relayd` listens on 127.0.0.1:5344
4745for an LTTng live connection. Otherwise, the desired URL may be
4746specified using its `--live-port` option.
4747
4748The
4749http://diamon.org/babeltrace[`babeltrace`]
4750viewer supports LTTng live as one of its input formats. `babeltrace` is
4751the default viewer when using `lttng view`. To use it manually, first
4752list active tracing sessions by doing the following (assuming the relay
4753daemon to connect to runs on the same host):
4754
4755[role="term"]
4756----
4757babeltrace --input-format lttng-live net://localhost
4758----
4759
4760Then, choose a tracing session and start viewing events as they arrive
4761using LTTng live:
4762
4763[role="term"]
4764----
4765babeltrace --input-format lttng-live net://localhost/host/hostname/my-session
4766----
4767
4768
4769[role="since-2.3"]
4770[[taking-a-snapshot]]
4771==== Taking a snapshot
4772
4773The normal behavior of LTTng is to record trace data as trace files.
4774This is ideal for keeping a long history of events that occurred on
4775the target system and applications, but may be too much data in some
4776situations. For example, you may wish to trace your application
4777continuously until some critical situation happens, in which case you
4778would only need the latest few recorded events to perform the desired
4779analysis, not multi-gigabyte trace files.
4780
4781LTTng has an interesting feature called _snapshots_. When creating
4782a tracing session in snapshot mode, no trace files are written; the
4783tracers' sub-buffers are constantly overwriting the oldest recorded
4784events with the newest. At any time, either when the tracers are started
4785or stopped, you may take a snapshot of those sub-buffers.
4786
4787There is no difference between the format of a normal trace file and the
4788format of a snapshot: viewers of LTTng traces also support LTTng
4789snapshots. By default, snapshots are written to disk, but they may also
4790be sent over the network.
4791
4792To create a tracing session in snapshot mode, do:
4793
4794[role="term"]
4795----
4796lttng create --snapshot my-snapshot-session
4797----
4798
4799Next, enable channels, events and add context to channels as usual.
4800Once a tracing session is created in snapshot mode, channels are
4801forced to use the
4802<<channel-overwrite-mode-vs-discard-mode,overwrite>> mode
4803(`--overwrite` option of the `enable-channel` command; also called
4804_flight recorder mode_) and have an `mmap()` channel type
4805(`--output mmap`).
4806
4807Start tracing. When you're ready to take a snapshot, do:
4808
4809[role="term"]
4810----
4811lttng snapshot record --name my-snapshot
4812----
4813
4814This records a snapshot named `my-snapshot` of all channels of
4815all domains of the current tracing session. By default, snapshots files
4816are recorded in the path returned by `lttng snapshot list-output`. You
4817may change this path or decide to send snapshots over the network
4818using either:
4819
4820. an output path/URL specified when creating the tracing session
4821 (`lttng create`)
4822. an added snapshot output path/URL using
4823 `lttng snapshot add-output`
4824. an output path/URL provided directly to the
4825 `lttng snapshot record` command
4826
4827Method 3 overrides method 2 which overrides method 1. When specifying
4828a URL, a relay daemon must be listening on some machine (see
4829<<sending-trace-data-over-the-network,Sending trace data over the network>>).
4830
4831If you need to make absolutely sure that the output file won't be
4832larger than a certain limit, you can set a maximum snapshot size when
4833taking it with the `--max-size` option:
4834
4835[role="term"]
4836----
4837lttng snapshot record --name my-snapshot --max-size 2M
4838----
4839
4840Older recorded events are discarded in order to respect this
4841maximum size.
4842
4843
4844[role="since-2.6"]
4845[[mi]]
4846==== Machine interface
4847
4848The `lttng` tool aims at providing a command output as human-readable as
4849possible. While this output is easy to parse by a human being, machines
4850have a hard time.
4851
4852This is why the `lttng` tool provides the general `--mi` option, which
4853must specify a machine interface output format. As of the latest
4854LTTng stable release, only the `xml` format is supported. A schema
4855definition (XSD) is made
4856https://github.com/lttng/lttng-tools/blob/master/src/common/mi_lttng.xsd[available]
4857to ease the integration with external tools as much as possible.
4858
4859The `--mi` option can be used in conjunction with all `lttng` commands.
4860Here are some examples:
4861
4862[role="term"]
4863----
4864lttng --mi xml create some-session
4865lttng --mi xml list some-session
4866lttng --mi xml list --kernel
4867lttng --mi xml enable-event --kernel --syscall open
4868lttng --mi xml start
4869----
4870
4871
4872[[reference]]
4873== Reference
4874
4875This chapter presents various references for LTTng packages such as links
4876to online manpages, tables needed by the rest of the text, descriptions
4877of library functions, and more.
4878
4879
4880[[online-lttng-manpages]]
4881=== Online LTTng manpages
4882
4883LTTng packages currently install the following link:/man[man pages],
4884available online using the links below:
4885
4886* **LTTng-tools**
4887** man:lttng(1)
4888** man:lttng-sessiond(8)
4889** man:lttng-relayd(8)
4890* **LTTng-UST**
4891** man:lttng-gen-tp(1)
4892** man:lttng-ust(3)
4893** man:lttng-ust-cyg-profile(3)
4894** man:lttng-ust-dl(3)
4895
4896
4897[[lttng-ust-ref]]
4898=== LTTng-UST
4899
4900This section presents references of the LTTng-UST package.
4901
4902
4903[[liblttng-ust]]
4904==== LTTng-UST library (+liblttng&#8209;ust+)
4905
4906The LTTng-UST library, or `liblttng-ust`, is the main shared object
4907against which user applications are linked to make LTTng user space
4908tracing possible.
4909
4910The <<c-application,C application>> guide shows the complete
4911process to instrument, build and run a C/$$C++$$ application using
4912LTTng-UST, while this section contains a few important tables.
4913
4914
4915[[liblttng-ust-tp-fields]]
4916===== Tracepoint fields macros (for `TP_FIELDS()`)
4917
4918The available macros to define tracepoint fields, which should be listed
4919within `TP_FIELDS()` in `TRACEPOINT_EVENT()`, are:
4920
4921[role="growable func-desc",cols="asciidoc,asciidoc"]
4922.Available macros to define LTTng-UST tracepoint fields
4923|====
4924|Macro |Description and parameters
4925
4926|
4927+ctf_integer(__t__, __n__, __e__)+
4928
4929+ctf_integer_nowrite(__t__, __n__, __e__)+
4930|
4931Standard integer, displayed in base 10.
4932
4933+__t__+::
4934 Integer C type (`int`, `long`, `size_t`, ...).
4935
4936+__n__+::
4937 Field name.
4938
4939+__e__+::
4940 Argument expression.
4941
4942|+ctf_integer_hex(__t__, __n__, __e__)+
4943|
4944Standard integer, displayed in base 16.
4945
4946+__t__+::
4947 Integer C type.
4948
4949+__n__+::
4950 Field name.
4951
4952+__e__+::
4953 Argument expression.
4954
4955|+ctf_integer_network(__t__, __n__, __e__)+
4956|
4957Integer in network byte order (big endian), displayed in base 10.
4958
4959+__t__+::
4960 Integer C type.
4961
4962+__n__+::
4963 Field name.
4964
4965+__e__+::
4966 Argument expression.
4967
4968|+ctf_integer_network_hex(__t__, __n__, __e__)+
4969|
4970Integer in network byte order, displayed in base 16.
4971
4972+__t__+::
4973 Integer C type.
4974
4975+__n__+::
4976 Field name.
4977
4978+__e__+::
4979 Argument expression.
4980
4981|
4982+ctf_float(__t__, __n__, __e__)+
4983
4984+ctf_float_nowrite(__t__, __n__, __e__)+
4985|
4986Floating point number.
4987
4988+__t__+::
4989 Floating point number C type (`float` or `double`).
4990
4991+__n__+::
4992 Field name.
4993
4994+__e__+::
4995 Argument expression.
4996
4997|
4998+ctf_string(__n__, __e__)+
4999
5000+ctf_string_nowrite(__n__, __e__)+
5001|
5002Null-terminated string; undefined behavior if +__e__+ is `NULL`.
5003
5004+__n__+::
5005 Field name.
5006
5007+__e__+::
5008 Argument expression.
5009
5010|
5011+ctf_array(__t__, __n__, __e__, __s__)+
5012
5013+ctf_array_nowrite(__t__, __n__, __e__, __s__)+
5014|
5015Statically-sized array of integers
5016
5017+__t__+::
5018 Array element C type.
5019
5020+__n__+::
5021 Field name.
5022
5023+__e__+::
5024 Argument expression.
5025
5026+__s__+::
5027 Number of elements.
5028
5029|
5030+ctf_array_text(__t__, __n__, __e__, __s__)+
5031
5032+ctf_array_text_nowrite(__t__, __n__, __e__, __s__)+
5033|
5034Statically-sized array, printed as text.
5035
5036The string does not need to be null-terminated.
5037
5038+__t__+::
5039 Array element C type (always `char`).
5040
5041+__n__+::
5042 Field name.
5043
5044+__e__+::
5045 Argument expression.
5046
5047+__s__+::
5048 Number of elements.
5049
5050|
5051+ctf_sequence(__t__, __n__, __e__, __T__, __E__)+
5052
5053+ctf_sequence_nowrite(__t__, __n__, __e__, __T__, __E__)+
5054|
5055Dynamically-sized array of integers.
5056
5057The type of +__E__+ needs to be unsigned.
5058
5059+__t__+::
5060 Array element C type.
5061
5062+__n__+::
5063 Field name.
5064
5065+__e__+::
5066 Argument expression.
5067
5068+__T__+::
5069 Length expression C type.
5070
5071+__E__+::
5072 Length expression.
5073
5074|
5075+ctf_sequence_text(__t__, __n__, __e__, __T__, __E__)+
5076
5077+ctf_sequence_text_nowrite(__t__, __n__, __e__, __T__, __E__)+
5078|
5079Dynamically-sized array, displayed as text.
5080
5081The string does not need to be null-terminated.
5082
5083The type of +__E__+ needs to be unsigned.
5084
5085The behaviour is undefined if +__e__+ is `NULL`.
5086
5087+__t__+::
5088 Sequence element C type (always `char`).
5089
5090+__n__+::
5091 Field name.
5092
5093+__e__+::
5094 Argument expression.
5095
5096+__T__+::
5097 Length expression C type.
5098
5099+__E__+::
5100 Length expression.
5101|====
5102
5103The `_nowrite` versions omit themselves from the session trace, but are
5104otherwise identical. This means the `_nowrite` fields won't be written
5105in the recorded trace. Their primary purpose is to make some
5106of the event context available to the
5107<<enabling-disabling-events,event filters>> without having to
5108commit the data to sub-buffers.
5109
5110
5111[[liblttng-ust-tracepoint-loglevel]]
5112===== Tracepoint log levels (for `TRACEPOINT_LOGLEVEL()`)
5113
5114The following table shows the available log level values for the
5115`TRACEPOINT_LOGLEVEL()` macro:
5116
5117`TRACE_EMERG`::
5118 System is unusable.
5119
5120`TRACE_ALERT`::
5121 Action must be taken immediately.
5122
5123`TRACE_CRIT`::
5124 Critical conditions.
5125
5126`TRACE_ERR`::
5127 Error conditions.
5128
5129`TRACE_WARNING`::
5130 Warning conditions.
5131
5132`TRACE_NOTICE`::
5133 Normal, but significant, condition.
5134
5135`TRACE_INFO`::
5136 Informational message.
5137
5138`TRACE_DEBUG_SYSTEM`::
5139 Debug information with system-level scope (set of programs).
5140
5141`TRACE_DEBUG_PROGRAM`::
5142 Debug information with program-level scope (set of processes).
5143
5144`TRACE_DEBUG_PROCESS`::
5145 Debug information with process-level scope (set of modules).
5146
5147`TRACE_DEBUG_MODULE`::
5148 Debug information with module (executable/library) scope (set of units).
5149
5150`TRACE_DEBUG_UNIT`::
5151 Debug information with compilation unit scope (set of functions).
5152
5153`TRACE_DEBUG_FUNCTION`::
5154 Debug information with function-level scope.
5155
5156`TRACE_DEBUG_LINE`::
5157 Debug information with line-level scope (TRACEPOINT_EVENT default).
5158
5159`TRACE_DEBUG`::
5160 Debug-level message.
5161
5162Log levels `TRACE_EMERG` through `TRACE_INFO` and `TRACE_DEBUG` match
5163http://man7.org/linux/man-pages/man3/syslog.3.html[syslog]
5164level semantics. Log levels `TRACE_DEBUG_SYSTEM` through `TRACE_DEBUG`
5165offer more fine-grained selection of debug information.
5166
5167
5168[[lttng-modules-ref]]
5169=== LTTng-modules
5170
5171This section presents references of the LTTng-modules package.
5172
5173
5174[[lttng-modules-tp-struct-entry]]
5175==== Tracepoint fields macros (for `TP_STRUCT__entry()`)
5176
5177This table describes possible entries for the `TP_STRUCT__entry()` part
5178of `LTTNG_TRACEPOINT_EVENT()`:
5179
5180[role="growable func-desc",cols="asciidoc,asciidoc"]
5181.Available entries for `TP_STRUCT__entry()` (in `LTTNG_TRACEPOINT_EVENT()`)
5182|====
5183|Macro |Description and parameters
5184
5185|+\__field(__t__, __n__)+
5186|
5187Standard integer, displayed in base 10.
5188
5189+__t__+::
5190 Integer C type (`int`, `unsigned char`, `size_t`, ...).
5191
5192+__n__+::
5193 Field name.
5194
5195|+\__field_hex(__t__, __n__)+
5196|
5197Standard integer, displayed in base 16.
5198
5199+__t__+::
5200 Integer C type.
5201
5202+__n__+::
5203 Field name.
5204
5205|+\__field_oct(__t__, __n__)+
5206|
5207Standard integer, displayed in base 8.
5208
5209+__t__+::
5210 Integer C type.
5211
5212+__n__+::
5213 Field name.
5214
5215|+\__field_network(__t__, __n__)+
5216|
5217Integer in network byte order (big endian), displayed in base 10.
5218
5219+__t__+::
5220 Integer C type.
5221
5222+__n__+::
5223 Field name.
5224
5225|+\__field_network_hex(__t__, __n__)+
5226|
5227Integer in network byte order (big endian), displayed in base 16.
5228
5229+__t__+::
5230 Integer C type.
5231
5232+__n__+::
5233 Field name.
5234
5235|+\__array(__t__, __n__, __s__)+
5236|
5237Statically-sized array, elements displayed in base 10.
5238
5239+__t__+::
5240 Array element C type.
5241
5242+__n__+::
5243 Field name.
5244
5245+__s__+::
5246 Number of elements.
5247
5248|+\__array_hex(__t__, __n__, __s__)+
5249|
5250Statically-sized array, elements displayed in base 16.
5251
5252+__t__+::
5253 array element C type.
5254+__n__+::
5255 field name.
5256+__s__+::
5257 number of elements.
5258
5259|+\__array_text(__t__, __n__, __s__)+
5260|
5261Statically-sized array, displayed as text.
5262
5263+__t__+::
5264 Array element C type (always char).
5265
5266+__n__+::
5267 Field name.
5268
5269+__s__+::
5270 Number of elements.
5271
5272|+\__dynamic_array(__t__, __n__, __s__)+
5273|
5274Dynamically-sized array, displayed in base 10.
5275
5276+__t__+::
5277 Array element C type.
5278
5279+__n__+::
5280 Field name.
5281
5282+__s__+::
5283 Length C expression.
5284
5285|+\__dynamic_array_hex(__t__, __n__, __s__)+
5286|
5287Dynamically-sized array, displayed in base 16.
5288
5289+__t__+::
5290 Array element C type.
5291
5292+__n__+::
5293 Field name.
5294
5295+__s__+::
5296 Length C expression.
5297
5298|+\__dynamic_array_text(__t__, __n__, __s__)+
5299|
5300Dynamically-sized array, displayed as text.
5301
5302+__t__+::
5303 Array element C type (always char).
5304
5305+__n__+::
5306 Field name.
5307
5308+__s__+::
5309 Length C expression.
5310
5311|+\__string(n, __s__)+
5312|
5313Null-terminated string.
5314
5315The behaviour is undefined behavior if +__s__+ is `NULL`.
5316
5317+__n__+::
5318 Field name.
5319
5320+__s__+::
5321 String source (pointer).
5322|====
5323
5324The above macros should cover the majority of cases. For advanced items,
5325see path:{probes/lttng-events.h}.
5326
5327
5328[[lttng-modules-tp-fast-assign]]
5329==== Tracepoint assignment macros (for `TP_fast_assign()`)
5330
5331This table describes possible entries for the `TP_fast_assign()` part
5332of `LTTNG_TRACEPOINT_EVENT()`:
5333
5334[role="growable func-desc",cols="asciidoc,asciidoc"]
5335.Available entries for `TP_fast_assign()` (in `LTTNG_TRACEPOINT_EVENT()`)
5336|====
5337|Macro |Description and parameters
5338
5339|+tp_assign(__d__, __s__)+
5340|
5341Assignment of C expression +__s__+ to tracepoint field +__d__+.
5342
5343+__d__+::
5344 Name of destination tracepoint field.
5345
5346+__s__+::
5347 Source C expression (may refer to tracepoint arguments).
5348
5349|+tp_memcpy(__d__, __s__, __l__)+
5350|
5351Memory copy of +__l__+ bytes from +__s__+ to tracepoint field
5352+__d__+ (use with array fields).
5353
5354+__d__+::
5355 Name of destination tracepoint field.
5356
5357+__s__+::
5358 Source C expression (may refer to tracepoint arguments).
5359
5360+__l__+::
5361 Number of bytes to copy.
5362
5363|+tp_memcpy_from_user(__d__, __s__, __l__)+
5364|
5365Memory copy of +__l__+ bytes from user space +__s__+ to tracepoint
5366field +__d__+ (use with array fields).
5367
5368+__d__+::
5369 Name of destination tracepoint field.
5370
5371+__s__+::
5372 Source C expression (may refer to tracepoint arguments).
5373
5374+__l__+::
5375 Number of bytes to copy.
5376
5377|+tp_memcpy_dyn(__d__, __s__)+
5378|
5379Memory copy of dynamically-sized array from +__s__+ to tracepoint field
5380+__d__+.
5381
5382The number of bytes is known from the field's length expression
5383(use with dynamically-sized array fields).
5384
5385+__d__+::
5386 Name of destination tracepoint field.
5387
5388+__s__+::
5389 Source C expression (may refer to tracepoint arguments).
5390
5391+__l__+::
5392 Number of bytes to copy.
5393
5394|+tp_strcpy(__d__, __s__)+
5395|
5396String copy of +__s__+ to tracepoint field +__d__+ (use with string
5397fields).
5398
5399+__d__+::
5400 Name of destination tracepoint field.
5401
5402+__s__+::
5403 Source C expression (may refer to tracepoint arguments).
5404|====
This page took 0.214215 seconds and 4 git commands to generate.