2.8, 2.9: use `$` and `#` prompts in terminal boxes
[lttng-docs.git] / 2.9 / lttng-docs-2.9.txt
1 The LTTng Documentation
2 =======================
3 Philippe Proulx <pproulx@efficios.com>
4 v2.9, 20 February 2017
5
6
7 include::../common/copyright.txt[]
8
9
10 include::../common/welcome.txt[]
11
12
13 include::../common/audience.txt[]
14
15
16 [[chapters]]
17 === What's in this documentation?
18
19 The LTTng Documentation is divided into the following sections:
20
21 * **<<nuts-and-bolts,Nuts and bolts>>** explains the
22 rudiments of software tracing and the rationale behind the
23 LTTng project.
24 +
25 You can skip this section if you’re familiar with software tracing and
26 with the LTTng project.
27
28 * **<<installing-lttng,Installation>>** describes the steps to
29 install the LTTng packages on common Linux distributions and from
30 their sources.
31 +
32 You can skip this section if you already properly installed LTTng on
33 your target system.
34
35 * **<<getting-started,Quick start>>** is a concise guide to
36 getting started quickly with LTTng kernel and user space tracing.
37 +
38 We recommend this section if you're new to LTTng or to software tracing
39 in general.
40 +
41 You can skip this section if you're not new to LTTng.
42
43 * **<<core-concepts,Core concepts>>** explains the concepts at
44 the heart of LTTng.
45 +
46 It's a good idea to become familiar with the core concepts
47 before attempting to use the toolkit.
48
49 * **<<plumbing,Components of LTTng>>** describes the various components
50 of the LTTng machinery, like the daemons, the libraries, and the
51 command-line interface.
52 * **<<instrumenting,Instrumentation>>** shows different ways to
53 instrument user applications and the Linux kernel.
54 +
55 Instrumenting source code is essential to provide a meaningful
56 source of events.
57 +
58 You can skip this section if you do not have a programming background.
59
60 * **<<controlling-tracing,Tracing control>>** is divided into topics
61 which demonstrate how to use the vast array of features that
62 LTTng{nbsp}{revision} offers.
63 * **<<reference,Reference>>** contains reference tables.
64 * **<<glossary,Glossary>>** is a specialized dictionary of terms related
65 to LTTng or to the field of software tracing.
66
67
68 include::../common/convention.txt[]
69
70
71 include::../common/acknowledgements.txt[]
72
73
74 [[whats-new]]
75 == What's new in LTTng {revision}?
76
77 LTTng{nbsp}{revision} bears the name _Joannès_. A Berliner Weisse style
78 beer from the http://letreflenoir.com/[Trèfle Noir] microbrewery in
79 https://en.wikipedia.org/wiki/Rouyn-Noranda[Rouyn-Noranda], the
80 https://www.beeradvocate.com/beer/profile/20537/238967/[_**Joannès**_]
81 is a tangy beer with a distinct pink dress and intense fruit flavor,
82 thanks to the presence of fresh blackcurrant grown in Témiscamingue.
83
84 New features and changes in LTTng{nbsp}{revision}:
85
86 * **Tracing control**:
87 ** You can override the name or the URL of a tracing session
88 configuration when you use man:lttng-load(1) thanks to the new
89 opt:lttng-load(1):--override-name and
90 opt:lttng-load(1):--override-url options.
91 ** The new `lttng regenerate` command replaces the now deprecated
92 `lttng metadata` command of LTTng 2.8. man:lttng-regenerate(1) can
93 also <<regenerate-statedump,generate the state dump event records>>
94 of a given tracing session on demand, a handy feature when
95 <<taking-a-snapshot,taking a snapshot>>.
96 ** You can add PMU counters by raw ID with man:lttng-add-context(1):
97 +
98 --
99 [role="term"]
100 ----
101 $ lttng add-context --kernel --type=perf:cpu:raw:r0013c:x86unhalted
102 ----
103 --
104 +
105 The format of the raw ID is the same as used with man:perf-record(1).
106 See <<adding-context,Add context fields to a channel>> for more
107 examples.
108
109 ** The LTTng <<lttng-relayd,relay daemon>> is now supported on
110 OS{nbsp}X and macOS for a smoother integration within a trace
111 analysis workflow, regardless of the platform used.
112
113 * **User space tracing**:
114 ** Improved performance (tested on x86-64 and ARMv7-A
115 (https://en.wikipedia.org/wiki/Cubieboard[Cubieboard])
116 architectures).
117 ** New helper library (`liblttng-ust-fd`) to help with
118 <<liblttng-ust-fd,applications which close file descriptors that
119 don't belong to them>>, for example, in a loop which closes file
120 descriptors after man:fork(2), or BSD's `closeall()`.
121 ** More accurate <<liblttng-ust-dl,dynamic linker instrumentation>> and
122 state dump event records, especially when a dynamically loaded
123 library manually loads its own dependencies.
124 ** New `ctf_*()` field definition macros (see man:lttng-ust(3)):
125 *** `ctf_array_hex()`
126 *** `ctf_array_network()`
127 *** `ctf_array_network_hex()`
128 *** `ctf_sequence_hex()`
129 *** `ctf_sequence_network()`
130 *** `ctf_sequence_network_hex()`
131 ** New `lttng_ust_loaded` weak symbol defined by `liblttng-ust` for
132 an application to know if the LTTng-UST shared library is loaded
133 or not:
134 +
135 --
136 [source,c]
137 ----
138 #include <stdio.h>
139
140 int lttng_ust_loaded __attribute__((weak));
141
142 int main(void)
143 {
144 if (lttng_ust_loaded) {
145 puts("LTTng-UST is loaded!");
146 } else {
147 puts("LTTng-UST is not loaded!");
148 }
149
150 return 0;
151 }
152 ----
153 --
154
155 ** LTTng-UST thread names have the `-ust` suffix.
156
157 * **Linux kernel tracing**:
158 ** Improved performance (tested on x86-64 and ARMv7-A
159 (https://en.wikipedia.org/wiki/Cubieboard[Cubieboard])
160 architectures).
161 ** New enumeration <<lttng-modules-tp-fields,field definition macros>>:
162 `ctf_enum()` and `ctf_user_enum()`.
163 ** IPv4, IPv6, and TCP header data is recorded in the event records
164 produced by tracepoints starting with `net_`.
165 ** Detailed system call event records: `select`, `pselect6`, `poll`,
166 `ppoll`, `epoll_wait`, `epoll_pwait`, and `epoll_ctl` on all
167 architectures supported by LTTng-modules, and `accept4` on x86-64.
168 ** New I²C instrumentation: the `extract_sensitive_payload` parameter
169 of the new `lttng-probe-i2c` LTTng module controls whether or not
170 the payloads of I²C messages are recorded in I²C event records, since
171 they may contain sensitive data (for example, keystrokes).
172 ** When the LTTng kernel modules are built into the Linux kernel image,
173 the `CONFIG_TRACEPOINTS` configuration option is automatically
174 selected.
175
176
177 [[nuts-and-bolts]]
178 == Nuts and bolts
179
180 What is LTTng? As its name suggests, the _Linux Trace Toolkit: next
181 generation_ is a modern toolkit for tracing Linux systems and
182 applications. So your first question might be:
183 **what is tracing?**
184
185
186 [[what-is-tracing]]
187 === What is tracing?
188
189 As the history of software engineering progressed and led to what
190 we now take for granted--complex, numerous and
191 interdependent software applications running in parallel on
192 sophisticated operating systems like Linux--the authors of such
193 components, software developers, began feeling a natural
194 urge to have tools that would ensure the robustness and good performance
195 of their masterpieces.
196
197 One major achievement in this field is, inarguably, the
198 https://www.gnu.org/software/gdb/[GNU debugger (GDB)],
199 an essential tool for developers to find and fix bugs. But even the best
200 debugger won't help make your software run faster, and nowadays, faster
201 software means either more work done by the same hardware, or cheaper
202 hardware for the same work.
203
204 A _profiler_ is often the tool of choice to identify performance
205 bottlenecks. Profiling is suitable to identify _where_ performance is
206 lost in a given software. The profiler outputs a profile, a statistical
207 summary of observed events, which you may use to discover which
208 functions took the most time to execute. However, a profiler won't
209 report _why_ some identified functions are the bottleneck. Bottlenecks
210 might only occur when specific conditions are met, conditions that are
211 sometimes impossible to capture by a statistical profiler, or impossible
212 to reproduce with an application altered by the overhead of an
213 event-based profiler. For a thorough investigation of software
214 performance issues, a history of execution is essential, with the
215 recorded values of variables and context fields you choose, and
216 with as little influence as possible on the instrumented software. This
217 is where tracing comes in handy.
218
219 _Tracing_ is a technique used to understand what goes on in a running
220 software system. The software used for tracing is called a _tracer_,
221 which is conceptually similar to a tape recorder. When recording,
222 specific instrumentation points placed in the software source code
223 generate events that are saved on a giant tape: a _trace_ file. You
224 can trace user applications and the operating system at the same time,
225 opening the possibility of resolving a wide range of problems that would
226 otherwise be extremely challenging.
227
228 Tracing is often compared to _logging_. However, tracers and loggers are
229 two different tools, serving two different purposes. Tracers are
230 designed to record much lower-level events that occur much more
231 frequently than log messages, often in the range of thousands per
232 second, with very little execution overhead. Logging is more appropriate
233 for a very high-level analysis of less frequent events: user accesses,
234 exceptional conditions (errors and warnings, for example), database
235 transactions, instant messaging communications, and such. Simply put,
236 logging is one of the many use cases that can be satisfied with tracing.
237
238 The list of recorded events inside a trace file can be read manually
239 like a log file for the maximum level of detail, but it is generally
240 much more interesting to perform application-specific analyses to
241 produce reduced statistics and graphs that are useful to resolve a
242 given problem. Trace viewers and analyzers are specialized tools
243 designed to do this.
244
245 In the end, this is what LTTng is: a powerful, open source set of
246 tools to trace the Linux kernel and user applications at the same time.
247 LTTng is composed of several components actively maintained and
248 developed by its link:/community/#where[community].
249
250
251 [[lttng-alternatives]]
252 === Alternatives to noch:{LTTng}
253
254 Excluding proprietary solutions, a few competing software tracers
255 exist for Linux:
256
257 * https://github.com/dtrace4linux/linux[dtrace4linux] is a port of
258 Sun Microsystems's DTrace to Linux. The cmd:dtrace tool interprets
259 user scripts and is responsible for loading code into the
260 Linux kernel for further execution and collecting the outputted data.
261 * https://en.wikipedia.org/wiki/Berkeley_Packet_Filter[eBPF] is a
262 subsystem in the Linux kernel in which a virtual machine can execute
263 programs passed from the user space to the kernel. You can attach
264 such programs to tracepoints and KProbes thanks to a system call, and
265 they can output data to the user space when executed thanks to
266 different mechanisms (pipe, VM register values, and eBPF maps, to name
267 a few).
268 * https://www.kernel.org/doc/Documentation/trace/ftrace.txt[ftrace]
269 is the de facto function tracer of the Linux kernel. Its user
270 interface is a set of special files in sysfs.
271 * https://perf.wiki.kernel.org/[perf] is
272 a performance analyzing tool for Linux which supports hardware
273 performance counters, tracepoints, as well as other counters and
274 types of probes. perf's controlling utility is the cmd:perf command
275 line/curses tool.
276 * http://linux.die.net/man/1/strace[strace]
277 is a command-line utility which records system calls made by a
278 user process, as well as signal deliveries and changes of process
279 state. strace makes use of https://en.wikipedia.org/wiki/Ptrace[ptrace]
280 to fulfill its function.
281 * http://www.sysdig.org/[sysdig], like SystemTap, uses scripts to
282 analyze Linux kernel events. You write scripts, or _chisels_ in
283 sysdig's jargon, in Lua and sysdig executes them while the system is
284 being traced or afterwards. sysdig's interface is the cmd:sysdig
285 command-line tool as well as the curses-based cmd:csysdig tool.
286 * https://sourceware.org/systemtap/[SystemTap] is a Linux kernel and
287 user space tracer which uses custom user scripts to produce plain text
288 traces. SystemTap converts the scripts to the C language, and then
289 compiles them as Linux kernel modules which are loaded to produce
290 trace data. SystemTap's primary user interface is the cmd:stap
291 command-line tool.
292
293 The main distinctive features of LTTng is that it produces correlated
294 kernel and user space traces, as well as doing so with the lowest
295 overhead amongst other solutions. It produces trace files in the
296 http://diamon.org/ctf[CTF] format, a file format optimized
297 for the production and analyses of multi-gigabyte data.
298
299 LTTng is the result of more than 10 years of active open source
300 development by a community of passionate developers.
301 LTTng{nbsp}{revision} is currently available on major desktop and server
302 Linux distributions.
303
304 The main interface for tracing control is a single command-line tool
305 named cmd:lttng. The latter can create several tracing sessions, enable
306 and disable events on the fly, filter events efficiently with custom
307 user expressions, start and stop tracing, and much more. LTTng can
308 record the traces on the file system or send them over the network, and
309 keep them totally or partially. You can view the traces once tracing
310 becomes inactive or in real-time.
311
312 <<installing-lttng,Install LTTng now>> and
313 <<getting-started,start tracing>>!
314
315
316 [[installing-lttng]]
317 == Installation
318
319 **LTTng** is a set of software <<plumbing,components>> which interact to
320 <<instrumenting,instrument>> the Linux kernel and user applications, and
321 to <<controlling-tracing,control tracing>> (start and stop
322 tracing, enable and disable event rules, and the rest). Those
323 components are bundled into the following packages:
324
325 * **LTTng-tools**: Libraries and command-line interface to
326 control tracing.
327 * **LTTng-modules**: Linux kernel modules to instrument and
328 trace the kernel.
329 * **LTTng-UST**: Libraries and Java/Python packages to instrument and
330 trace user applications.
331
332 Most distributions mark the LTTng-modules and LTTng-UST packages as
333 optional when installing LTTng-tools (which is always required). In the
334 following sections, we always provide the steps to install all three,
335 but note that:
336
337 * You only need to install LTTng-modules if you intend to trace the
338 Linux kernel.
339 * You only need to install LTTng-UST if you intend to trace user
340 applications.
341
342 [role="growable"]
343 .Availability of LTTng{nbsp}{revision} for major Linux distributions as of 2 December 2016.
344 |====
345 |Distribution |Available in releases |Alternatives
346
347 |https://www.ubuntu.com/[Ubuntu]
348 |Ubuntu{nbsp}14.04 _Trusty Tahr_ and Ubuntu{nbsp}16.04 _Xenial Xerus_:
349 <<ubuntu-ppa,use the LTTng Stable{nbsp}{revision} PPA>>.
350 |link:/docs/v2.8#doc-ubuntu[LTTng{nbsp}2.8 for Ubuntu{nbsp}16.10 _Yakkety Yak_].
351
352 <<building-from-source,Build LTTng{nbsp}{revision} from source>> for
353 other Ubuntu releases.
354
355 |https://getfedora.org/[Fedora]
356 |_Not available_
357 |<<building-from-source,Build LTTng-modules{nbsp}{revision} from
358 source>>.
359
360 |https://www.debian.org/[Debian]
361 |_Not available_
362 |<<building-from-source,Build LTTng-modules{nbsp}{revision} from
363 source>>.
364
365 |https://www.opensuse.org/[openSUSE]
366 |_Not available_
367 |<<building-from-source,Build LTTng-modules{nbsp}{revision} from
368 source>>.
369
370 |https://www.archlinux.org/[Arch Linux]
371 |<<arch-linux,Latest AUR packages>>.
372 |
373
374 |https://alpinelinux.org/[Alpine Linux]
375 |_Not available_
376 |link:/docs/v2.8#doc-alpine-linux[LTTng{nbsp}2.8 for Alpine Linux{nbsp}"edge"].
377
378 LTTng{nbsp}2.8 for Alpine Linux{nbsp}3.5 (not released yet).
379
380 <<building-from-source,Build LTTng{nbsp}{revision} from source>> for
381 other Alpine Linux releases.
382
383 |https://www.redhat.com/[RHEL] and https://www.suse.com/[SLES]
384 |See http://packages.efficios.com/[EfficiOS Enterprise Packages].
385 |
386
387 |https://buildroot.org/[Buildroot]
388 |_Not available_
389 |link:/docs/v2.8#doc-buildroot[LTTng{nbsp}2.8 for Buildroot{nbsp}2016.11].
390
391 <<building-from-source,Build LTTng{nbsp}{revision} from source>> for
392 other Buildroot releases.
393
394 |http://www.openembedded.org/wiki/Main_Page[OpenEmbedded] and
395 https://www.yoctoproject.org/[Yocto]
396 |_Not available_
397 |link:/docs/v2.8#doc-oe-yocto[LTTng{nbsp}2.8 for Yocto Project{nbsp}2.2 _Morty_]
398 (`openembedded-core` layer).
399
400 <<building-from-source,Build LTTng{nbsp}{revision} from source>> for
401 other OpenEmbedded releases.
402 |====
403
404
405 [[ubuntu]]
406 === [[ubuntu-official-repositories]]Ubuntu
407
408 [[ubuntu-ppa]]
409 ==== noch:{LTTng} Stable {revision} PPA
410
411 The https://launchpad.net/~lttng/+archive/ubuntu/stable-{revision}[LTTng
412 Stable{nbsp}{revision} PPA] offers the latest stable
413 LTTng{nbsp}{revision} packages for:
414
415 * Ubuntu{nbsp}14.04 _Trusty Tahr_
416 * Ubuntu{nbsp}16.04 _Xenial Xerus_
417
418 To install LTTng{nbsp}{revision} from the LTTng Stable{nbsp}{revision} PPA:
419
420 . Add the LTTng Stable{nbsp}{revision} PPA repository and update the
421 list of packages:
422 +
423 --
424 [role="term"]
425 ----
426 # apt-add-repository ppa:lttng/stable-2.9
427 # apt-get update
428 ----
429 --
430
431 . Install the main LTTng{nbsp}{revision} packages:
432 +
433 --
434 [role="term"]
435 ----
436 # apt-get install lttng-tools
437 # apt-get install lttng-modules-dkms
438 # apt-get install liblttng-ust-dev
439 ----
440 --
441
442 . **If you need to instrument and trace
443 <<java-application,Java applications>>**, install the LTTng-UST
444 Java agent:
445 +
446 --
447 [role="term"]
448 ----
449 # apt-get install liblttng-ust-agent-java
450 ----
451 --
452
453 . **If you need to instrument and trace
454 <<python-application,Python{nbsp}3 applications>>**, install the
455 LTTng-UST Python agent:
456 +
457 --
458 [role="term"]
459 ----
460 # apt-get install python3-lttngust
461 ----
462 --
463
464
465 [[arch-linux]]
466 === Arch Linux
467
468 To install LTTng{nbsp}{revision} on Arch Linux using
469 https://archlinux.fr/yaourt-en[Yaourt]:
470
471 . Install the main LTTng{nbsp}{revision} packages:
472 +
473 --
474 [role="term"]
475 ----
476 $ yaourt -S lttng-tools
477 $ yaourt -S lttng-ust
478 $ yaourt -S lttng-modules
479 ----
480 --
481
482 . **If you need to instrument and trace <<python-application,Python
483 applications>>**, install the LTTng-UST Python agent:
484 +
485 --
486 [role="term"]
487 ----
488 $ yaourt -S python-lttngust
489 $ yaourt -S python2-lttngust
490 ----
491 --
492
493
494 [[enterprise-distributions]]
495 === RHEL, SUSE, and other enterprise distributions
496
497 To install LTTng on enterprise Linux distributions, such as Red Hat
498 Enterprise Linux (RHEL) and SUSE Linux Enterprise Server (SUSE), please
499 see http://packages.efficios.com/[EfficiOS Enterprise Packages].
500
501
502 [[building-from-source]]
503 === Build from source
504
505 To build and install LTTng{nbsp}{revision} from source:
506
507 . Using your distribution's package manager, or from source, install
508 the following dependencies of LTTng-tools and LTTng-UST:
509 +
510 --
511 * https://sourceforge.net/projects/libuuid/[libuuid]
512 * http://directory.fsf.org/wiki/Popt[popt]
513 * http://liburcu.org/[Userspace RCU]
514 * http://www.xmlsoft.org/[libxml2]
515 --
516
517 . Download, build, and install the latest LTTng-modules{nbsp}{revision}:
518 +
519 --
520 [role="term"]
521 ----
522 $ cd $(mktemp -d) &&
523 wget http://lttng.org/files/lttng-modules/lttng-modules-latest-2.9.tar.bz2 &&
524 tar -xf lttng-modules-latest-2.9.tar.bz2 &&
525 cd lttng-modules-2.9.* &&
526 make &&
527 sudo make modules_install &&
528 sudo depmod -a
529 ----
530 --
531
532 . Download, build, and install the latest LTTng-UST{nbsp}{revision}:
533 +
534 --
535 [role="term"]
536 ----
537 $ cd $(mktemp -d) &&
538 wget http://lttng.org/files/lttng-ust/lttng-ust-latest-2.9.tar.bz2 &&
539 tar -xf lttng-ust-latest-2.9.tar.bz2 &&
540 cd lttng-ust-2.9.* &&
541 ./configure &&
542 make &&
543 sudo make install &&
544 sudo ldconfig
545 ----
546 --
547 +
548 --
549 [IMPORTANT]
550 .Java and Python application tracing
551 ====
552 If you need to instrument and trace <<java-application,Java
553 applications>>, pass the `--enable-java-agent-jul`,
554 `--enable-java-agent-log4j`, or `--enable-java-agent-all` options to the
555 `configure` script, depending on which Java logging framework you use.
556
557 If you need to instrument and trace <<python-application,Python
558 applications>>, pass the `--enable-python-agent` option to the
559 `configure` script. You can set the `PYTHON` environment variable to the
560 path to the Python interpreter for which to install the LTTng-UST Python
561 agent package.
562 ====
563 --
564 +
565 --
566 [NOTE]
567 ====
568 By default, LTTng-UST libraries are installed to
569 dir:{/usr/local/lib}, which is the de facto directory in which to
570 keep self-compiled and third-party libraries.
571
572 When <<building-tracepoint-providers-and-user-application,linking an
573 instrumented user application with `liblttng-ust`>>:
574
575 * Append `/usr/local/lib` to the env:LD_LIBRARY_PATH environment
576 variable.
577 * Pass the `-L/usr/local/lib` and `-Wl,-rpath,/usr/local/lib` options to
578 man:gcc(1), man:g++(1), or man:clang(1).
579 ====
580 --
581
582 . Download, build, and install the latest LTTng-tools{nbsp}{revision}:
583 +
584 --
585 [role="term"]
586 ----
587 $ cd $(mktemp -d) &&
588 wget http://lttng.org/files/lttng-tools/lttng-tools-latest-2.9.tar.bz2 &&
589 tar -xf lttng-tools-latest-2.9.tar.bz2 &&
590 cd lttng-tools-2.9.* &&
591 ./configure &&
592 make &&
593 sudo make install &&
594 sudo ldconfig
595 ----
596 --
597
598 TIP: The https://github.com/eepp/vlttng[vlttng tool] can do all the
599 previous steps automatically for a given version of LTTng and confine
600 the installed files in a specific directory. This can be useful to test
601 LTTng without installing it on your system.
602
603
604 [[getting-started]]
605 == Quick start
606
607 This is a short guide to get started quickly with LTTng kernel and user
608 space tracing.
609
610 Before you follow this guide, make sure to <<installing-lttng,install>>
611 LTTng.
612
613 This tutorial walks you through the steps to:
614
615 . <<tracing-the-linux-kernel,Trace the Linux kernel>>.
616 . <<tracing-your-own-user-application,Trace a user application>> written
617 in C.
618 . <<viewing-and-analyzing-your-traces,View and analyze the
619 recorded events>>.
620
621
622 [[tracing-the-linux-kernel]]
623 === Trace the Linux kernel
624
625 The following command lines start with the `#` prompt because you need
626 root privileges to trace the Linux kernel. You can also trace the kernel
627 as a regular user if your Unix user is a member of the
628 <<tracing-group,tracing group>>.
629
630 . Create a <<tracing-session,tracing session>> which writes its traces
631 to dir:{/tmp/my-kernel-trace}:
632 +
633 --
634 [role="term"]
635 ----
636 # lttng create my-kernel-session --output=/tmp/my-kernel-trace
637 ----
638 --
639
640 . List the available kernel tracepoints and system calls:
641 +
642 --
643 [role="term"]
644 ----
645 # lttng list --kernel
646 # lttng list --kernel --syscall
647 ----
648 --
649
650 . Create <<event,event rules>> which match the desired instrumentation
651 point names, for example the `sched_switch` and `sched_process_fork`
652 tracepoints, and the man:open(2) and man:close(2) system calls:
653 +
654 --
655 [role="term"]
656 ----
657 # lttng enable-event --kernel sched_switch,sched_process_fork
658 # lttng enable-event --kernel --syscall open,close
659 ----
660 --
661 +
662 You can also create an event rule which matches _all_ the Linux kernel
663 tracepoints (this will generate a lot of data when tracing):
664 +
665 --
666 [role="term"]
667 ----
668 # lttng enable-event --kernel --all
669 ----
670 --
671
672 . <<basic-tracing-session-control,Start tracing>>:
673 +
674 --
675 [role="term"]
676 ----
677 # lttng start
678 ----
679 --
680
681 . Do some operation on your system for a few seconds. For example,
682 load a website, or list the files of a directory.
683 . <<basic-tracing-session-control,Stop tracing>> and destroy the
684 tracing session:
685 +
686 --
687 [role="term"]
688 ----
689 # lttng stop
690 # lttng destroy
691 ----
692 --
693 +
694 The man:lttng-destroy(1) command does not destroy the trace data; it
695 only destroys the state of the tracing session.
696
697 . For the sake of this example, make the recorded trace accessible to
698 the non-root users:
699 +
700 --
701 [role="term"]
702 ----
703 # chown -R $(whoami) /tmp/my-kernel-trace
704 ----
705 --
706
707 See <<viewing-and-analyzing-your-traces,View and analyze the
708 recorded events>> to view the recorded events.
709
710
711 [[tracing-your-own-user-application]]
712 === Trace a user application
713
714 This section steps you through a simple example to trace a
715 _Hello world_ program written in C.
716
717 To create the traceable user application:
718
719 . Create the tracepoint provider header file, which defines the
720 tracepoints and the events they can generate:
721 +
722 --
723 [source,c]
724 .path:{hello-tp.h}
725 ----
726 #undef TRACEPOINT_PROVIDER
727 #define TRACEPOINT_PROVIDER hello_world
728
729 #undef TRACEPOINT_INCLUDE
730 #define TRACEPOINT_INCLUDE "./hello-tp.h"
731
732 #if !defined(_HELLO_TP_H) || defined(TRACEPOINT_HEADER_MULTI_READ)
733 #define _HELLO_TP_H
734
735 #include <lttng/tracepoint.h>
736
737 TRACEPOINT_EVENT(
738 hello_world,
739 my_first_tracepoint,
740 TP_ARGS(
741 int, my_integer_arg,
742 char*, my_string_arg
743 ),
744 TP_FIELDS(
745 ctf_string(my_string_field, my_string_arg)
746 ctf_integer(int, my_integer_field, my_integer_arg)
747 )
748 )
749
750 #endif /* _HELLO_TP_H */
751
752 #include <lttng/tracepoint-event.h>
753 ----
754 --
755
756 . Create the tracepoint provider package source file:
757 +
758 --
759 [source,c]
760 .path:{hello-tp.c}
761 ----
762 #define TRACEPOINT_CREATE_PROBES
763 #define TRACEPOINT_DEFINE
764
765 #include "hello-tp.h"
766 ----
767 --
768
769 . Build the tracepoint provider package:
770 +
771 --
772 [role="term"]
773 ----
774 $ gcc -c -I. hello-tp.c
775 ----
776 --
777
778 . Create the _Hello World_ application source file:
779 +
780 --
781 [source,c]
782 .path:{hello.c}
783 ----
784 #include <stdio.h>
785 #include "hello-tp.h"
786
787 int main(int argc, char *argv[])
788 {
789 int x;
790
791 puts("Hello, World!\nPress Enter to continue...");
792
793 /*
794 * The following getchar() call is only placed here for the purpose
795 * of this demonstration, to pause the application in order for
796 * you to have time to list its tracepoints. It is not
797 * needed otherwise.
798 */
799 getchar();
800
801 /*
802 * A tracepoint() call.
803 *
804 * Arguments, as defined in hello-tp.h:
805 *
806 * 1. Tracepoint provider name (required)
807 * 2. Tracepoint name (required)
808 * 3. my_integer_arg (first user-defined argument)
809 * 4. my_string_arg (second user-defined argument)
810 *
811 * Notice the tracepoint provider and tracepoint names are
812 * NOT strings: they are in fact parts of variables that the
813 * macros in hello-tp.h create.
814 */
815 tracepoint(hello_world, my_first_tracepoint, 23, "hi there!");
816
817 for (x = 0; x < argc; ++x) {
818 tracepoint(hello_world, my_first_tracepoint, x, argv[x]);
819 }
820
821 puts("Quitting now!");
822 tracepoint(hello_world, my_first_tracepoint, x * x, "x^2");
823
824 return 0;
825 }
826 ----
827 --
828
829 . Build the application:
830 +
831 --
832 [role="term"]
833 ----
834 $ gcc -c hello.c
835 ----
836 --
837
838 . Link the application with the tracepoint provider package,
839 `liblttng-ust`, and `libdl`:
840 +
841 --
842 [role="term"]
843 ----
844 $ gcc -o hello hello.o hello-tp.o -llttng-ust -ldl
845 ----
846 --
847
848 Here's the whole build process:
849
850 [role="img-100"]
851 .User space tracing tutorial's build steps.
852 image::ust-flow.png[]
853
854 To trace the user application:
855
856 . Run the application with a few arguments:
857 +
858 --
859 [role="term"]
860 ----
861 $ ./hello world and beyond
862 ----
863 --
864 +
865 You see:
866 +
867 --
868 ----
869 Hello, World!
870 Press Enter to continue...
871 ----
872 --
873
874 . Start an LTTng <<lttng-sessiond,session daemon>>:
875 +
876 --
877 [role="term"]
878 ----
879 $ lttng-sessiond --daemonize
880 ----
881 --
882 +
883 Note that a session daemon might already be running, for example as
884 a service that the distribution's service manager started.
885
886 . List the available user space tracepoints:
887 +
888 --
889 [role="term"]
890 ----
891 $ lttng list --userspace
892 ----
893 --
894 +
895 You see the `hello_world:my_first_tracepoint` tracepoint listed
896 under the `./hello` process.
897
898 . Create a <<tracing-session,tracing session>>:
899 +
900 --
901 [role="term"]
902 ----
903 $ lttng create my-user-space-session
904 ----
905 --
906
907 . Create an <<event,event rule>> which matches the
908 `hello_world:my_first_tracepoint` event name:
909 +
910 --
911 [role="term"]
912 ----
913 $ lttng enable-event --userspace hello_world:my_first_tracepoint
914 ----
915 --
916
917 . <<basic-tracing-session-control,Start tracing>>:
918 +
919 --
920 [role="term"]
921 ----
922 $ lttng start
923 ----
924 --
925
926 . Go back to the running `hello` application and press Enter. The
927 program executes all `tracepoint()` instrumentation points and exits.
928 . <<basic-tracing-session-control,Stop tracing>> and destroy the
929 tracing session:
930 +
931 --
932 [role="term"]
933 ----
934 $ lttng stop
935 $ lttng destroy
936 ----
937 --
938 +
939 The man:lttng-destroy(1) command does not destroy the trace data; it
940 only destroys the state of the tracing session.
941
942 By default, LTTng saves the traces in
943 +$LTTNG_HOME/lttng-traces/__name__-__date__-__time__+,
944 where +__name__+ is the tracing session name. The
945 env:LTTNG_HOME environment variable defaults to `$HOME` if not set.
946
947 See <<viewing-and-analyzing-your-traces,View and analyze the
948 recorded events>> to view the recorded events.
949
950
951 [[viewing-and-analyzing-your-traces]]
952 === View and analyze the recorded events
953
954 Once you have completed the <<tracing-the-linux-kernel,Trace the Linux
955 kernel>> and <<tracing-your-own-user-application,Trace a user
956 application>> tutorials, you can inspect the recorded events.
957
958 Many tools are available to read LTTng traces:
959
960 * **cmd:babeltrace** is a command-line utility which converts trace
961 formats; it supports the format that LTTng produces, CTF, as well as a
962 basic text output which can be ++grep++ed. The cmd:babeltrace command
963 is part of the http://diamon.org/babeltrace[Babeltrace] project.
964 * Babeltrace also includes
965 **https://www.python.org/[Python] bindings** so
966 that you can easily open and read an LTTng trace with your own script,
967 benefiting from the power of Python.
968 * http://tracecompass.org/[**Trace Compass**]
969 is a graphical user interface for viewing and analyzing any type of
970 logs or traces, including LTTng's.
971 * https://github.com/lttng/lttng-analyses[**LTTng analyses**] is a
972 project which includes many high-level analyses of LTTng kernel
973 traces, like scheduling statistics, interrupt frequency distribution,
974 top CPU usage, and more.
975
976 NOTE: This section assumes that the traces recorded during the previous
977 tutorials were saved to their default location, in the
978 dir:{$LTTNG_HOME/lttng-traces} directory. The env:LTTNG_HOME
979 environment variable defaults to `$HOME` if not set.
980
981
982 [[viewing-and-analyzing-your-traces-bt]]
983 ==== Use the cmd:babeltrace command-line tool
984
985 The simplest way to list all the recorded events of a trace is to pass
986 its path to cmd:babeltrace with no options:
987
988 [role="term"]
989 ----
990 $ babeltrace ~/lttng-traces/my-user-space-session*
991 ----
992
993 cmd:babeltrace finds all traces recursively within the given path and
994 prints all their events, merging them in chronological order.
995
996 You can pipe the output of cmd:babeltrace into a tool like man:grep(1) for
997 further filtering:
998
999 [role="term"]
1000 ----
1001 $ babeltrace /tmp/my-kernel-trace | grep _switch
1002 ----
1003
1004 You can pipe the output of cmd:babeltrace into a tool like man:wc(1) to
1005 count the recorded events:
1006
1007 [role="term"]
1008 ----
1009 $ babeltrace /tmp/my-kernel-trace | grep _open | wc --lines
1010 ----
1011
1012
1013 [[viewing-and-analyzing-your-traces-bt-python]]
1014 ==== Use the Babeltrace Python bindings
1015
1016 The <<viewing-and-analyzing-your-traces-bt,text output of cmd:babeltrace>>
1017 is useful to isolate events by simple matching using man:grep(1) and
1018 similar utilities. However, more elaborate filters, such as keeping only
1019 event records with a field value falling within a specific range, are
1020 not trivial to write using a shell. Moreover, reductions and even the
1021 most basic computations involving multiple event records are virtually
1022 impossible to implement.
1023
1024 Fortunately, Babeltrace ships with Python 3 bindings which makes it easy
1025 to read the event records of an LTTng trace sequentially and compute the
1026 desired information.
1027
1028 The following script accepts an LTTng Linux kernel trace path as its
1029 first argument and prints the short names of the top 5 running processes
1030 on CPU 0 during the whole trace:
1031
1032 [source,python]
1033 .path:{top5proc.py}
1034 ----
1035 from collections import Counter
1036 import babeltrace
1037 import sys
1038
1039
1040 def top5proc():
1041 if len(sys.argv) != 2:
1042 msg = 'Usage: python3 {} TRACEPATH'.format(sys.argv[0])
1043 print(msg, file=sys.stderr)
1044 return False
1045
1046 # A trace collection contains one or more traces
1047 col = babeltrace.TraceCollection()
1048
1049 # Add the trace provided by the user (LTTng traces always have
1050 # the 'ctf' format)
1051 if col.add_trace(sys.argv[1], 'ctf') is None:
1052 raise RuntimeError('Cannot add trace')
1053
1054 # This counter dict contains execution times:
1055 #
1056 # task command name -> total execution time (ns)
1057 exec_times = Counter()
1058
1059 # This contains the last `sched_switch` timestamp
1060 last_ts = None
1061
1062 # Iterate on events
1063 for event in col.events:
1064 # Keep only `sched_switch` events
1065 if event.name != 'sched_switch':
1066 continue
1067
1068 # Keep only events which happened on CPU 0
1069 if event['cpu_id'] != 0:
1070 continue
1071
1072 # Event timestamp
1073 cur_ts = event.timestamp
1074
1075 if last_ts is None:
1076 # We start here
1077 last_ts = cur_ts
1078
1079 # Previous task command (short) name
1080 prev_comm = event['prev_comm']
1081
1082 # Initialize entry in our dict if not yet done
1083 if prev_comm not in exec_times:
1084 exec_times[prev_comm] = 0
1085
1086 # Compute previous command execution time
1087 diff = cur_ts - last_ts
1088
1089 # Update execution time of this command
1090 exec_times[prev_comm] += diff
1091
1092 # Update last timestamp
1093 last_ts = cur_ts
1094
1095 # Display top 5
1096 for name, ns in exec_times.most_common(5):
1097 s = ns / 1000000000
1098 print('{:20}{} s'.format(name, s))
1099
1100 return True
1101
1102
1103 if __name__ == '__main__':
1104 sys.exit(0 if top5proc() else 1)
1105 ----
1106
1107 Run this script:
1108
1109 [role="term"]
1110 ----
1111 $ python3 top5proc.py /tmp/my-kernel-trace/kernel
1112 ----
1113
1114 Output example:
1115
1116 ----
1117 swapper/0 48.607245889 s
1118 chromium 7.192738188 s
1119 pavucontrol 0.709894415 s
1120 Compositor 0.660867933 s
1121 Xorg.bin 0.616753786 s
1122 ----
1123
1124 Note that `swapper/0` is the "idle" process of CPU 0 on Linux; since we
1125 weren't using the CPU that much when tracing, its first position in the
1126 list makes sense.
1127
1128
1129 [[core-concepts]]
1130 == [[understanding-lttng]]Core concepts
1131
1132 From a user's perspective, the LTTng system is built on a few concepts,
1133 or objects, on which the <<lttng-cli,cmd:lttng command-line tool>>
1134 operates by sending commands to the <<lttng-sessiond,session daemon>>.
1135 Understanding how those objects relate to eachother is key in mastering
1136 the toolkit.
1137
1138 The core concepts are:
1139
1140 * <<tracing-session,Tracing session>>
1141 * <<domain,Tracing domain>>
1142 * <<channel,Channel and ring buffer>>
1143 * <<"event","Instrumentation point, event rule, event, and event record">>
1144
1145
1146 [[tracing-session]]
1147 === Tracing session
1148
1149 A _tracing session_ is a stateful dialogue between you and
1150 a <<lttng-sessiond,session daemon>>. You can
1151 <<creating-destroying-tracing-sessions,create a new tracing
1152 session>> with the `lttng create` command.
1153
1154 Anything that you do when you control LTTng tracers happens within a
1155 tracing session. In particular, a tracing session:
1156
1157 * Has its own name.
1158 * Has its own set of trace files.
1159 * Has its own state of activity (started or stopped).
1160 * Has its own <<tracing-session-mode,mode>> (local, network streaming,
1161 snapshot, or live).
1162 * Has its own <<channel,channels>> which have their own
1163 <<event,event rules>>.
1164
1165 [role="img-100"]
1166 .A _tracing session_ contains <<channel,channels>> that are members of <<domain,tracing domains>> and contain <<event,event rules>>.
1167 image::concepts.png[]
1168
1169 Those attributes and objects are completely isolated between different
1170 tracing sessions.
1171
1172 A tracing session is analogous to a cash machine session:
1173 the operations you do on the banking system through the cash machine do
1174 not alter the data of other users of the same system. In the case of
1175 the cash machine, a session lasts as long as your bank card is inside.
1176 In the case of LTTng, a tracing session lasts from the `lttng create`
1177 command to the `lttng destroy` command.
1178
1179 [role="img-100"]
1180 .Each Unix user has its own set of tracing sessions.
1181 image::many-sessions.png[]
1182
1183
1184 [[tracing-session-mode]]
1185 ==== Tracing session mode
1186
1187 LTTng can send the generated trace data to different locations. The
1188 _tracing session mode_ dictates where to send it. The following modes
1189 are available in LTTng{nbsp}{revision}:
1190
1191 Local mode::
1192 LTTng writes the traces to the file system of the machine being traced
1193 (target system).
1194
1195 Network streaming mode::
1196 LTTng sends the traces over the network to a
1197 <<lttng-relayd,relay daemon>> running on a remote system.
1198
1199 Snapshot mode::
1200 LTTng does not write the traces by default. Instead, you can request
1201 LTTng to <<taking-a-snapshot,take a snapshot>>, that is, a copy of the
1202 current tracing buffers, and to write it to the target's file system
1203 or to send it over the network to a <<lttng-relayd,relay daemon>>
1204 running on a remote system.
1205
1206 Live mode::
1207 This mode is similar to the network streaming mode, but a live
1208 trace viewer can connect to the distant relay daemon to
1209 <<lttng-live,view event records as LTTng generates them>> by
1210 the tracers.
1211
1212
1213 [[domain]]
1214 === Tracing domain
1215
1216 A _tracing domain_ is a namespace for event sources. A tracing domain
1217 has its own properties and features.
1218
1219 There are currently five available tracing domains:
1220
1221 * Linux kernel
1222 * User space
1223 * `java.util.logging` (JUL)
1224 * log4j
1225 * Python
1226
1227 You must specify a tracing domain when using some commands to avoid
1228 ambiguity. For example, since all the domains support named tracepoints
1229 as event sources (instrumentation points that you manually insert in the
1230 source code), you need to specify a tracing domain when
1231 <<enabling-disabling-events,creating an event rule>> because all the
1232 tracing domains could have tracepoints with the same names.
1233
1234 Some features are reserved to specific tracing domains. Dynamic function
1235 entry and return instrumentation points, for example, are currently only
1236 supported in the Linux kernel tracing domain, but support for other
1237 tracing domains could be added in the future.
1238
1239 You can create <<channel,channels>> in the Linux kernel and user space
1240 tracing domains. The other tracing domains have a single default
1241 channel.
1242
1243
1244 [[channel]]
1245 === Channel and ring buffer
1246
1247 A _channel_ is an object which is responsible for a set of ring buffers.
1248 Each ring buffer is divided into multiple sub-buffers. When an LTTng
1249 tracer emits an event, it can record it to one or more
1250 sub-buffers. The attributes of a channel determine what to do when
1251 there's no space left for a new event record because all sub-buffers
1252 are full, where to send a full sub-buffer, and other behaviours.
1253
1254 A channel is always associated to a <<domain,tracing domain>>. The
1255 `java.util.logging` (JUL), log4j, and Python tracing domains each have
1256 a default channel which you cannot configure.
1257
1258 A channel also owns <<event,event rules>>. When an LTTng tracer emits
1259 an event, it records it to the sub-buffers of all
1260 the enabled channels with a satisfied event rule, as long as those
1261 channels are part of active <<tracing-session,tracing sessions>>.
1262
1263
1264 [[channel-buffering-schemes]]
1265 ==== Per-user vs. per-process buffering schemes
1266
1267 A channel has at least one ring buffer _per CPU_. LTTng always
1268 records an event to the ring buffer associated to the CPU on which it
1269 occurred.
1270
1271 Two _buffering schemes_ are available when you
1272 <<enabling-disabling-channels,create a channel>> in the
1273 user space <<domain,tracing domain>>:
1274
1275 Per-user buffering::
1276 Allocate one set of ring buffers--one per CPU--shared by all the
1277 instrumented processes of each Unix user.
1278 +
1279 --
1280 [role="img-100"]
1281 .Per-user buffering scheme.
1282 image::per-user-buffering.png[]
1283 --
1284
1285 Per-process buffering::
1286 Allocate one set of ring buffers--one per CPU--for each
1287 instrumented process.
1288 +
1289 --
1290 [role="img-100"]
1291 .Per-process buffering scheme.
1292 image::per-process-buffering.png[]
1293 --
1294 +
1295 The per-process buffering scheme tends to consume more memory than the
1296 per-user option because systems generally have more instrumented
1297 processes than Unix users running instrumented processes. However, the
1298 per-process buffering scheme ensures that one process having a high
1299 event throughput won't fill all the shared sub-buffers of the same
1300 user, only its own.
1301
1302 The Linux kernel tracing domain has only one available buffering scheme
1303 which is to allocate a single set of ring buffers for the whole system.
1304 This scheme is similar to the per-user option, but with a single, global
1305 user "running" the kernel.
1306
1307
1308 [[channel-overwrite-mode-vs-discard-mode]]
1309 ==== Overwrite vs. discard event loss modes
1310
1311 When an event occurs, LTTng records it to a specific sub-buffer (yellow
1312 arc in the following animation) of a specific channel's ring buffer.
1313 When there's no space left in a sub-buffer, the tracer marks it as
1314 consumable (red) and another, empty sub-buffer starts receiving the
1315 following event records. A <<lttng-consumerd,consumer daemon>>
1316 eventually consumes the marked sub-buffer (returns to white).
1317
1318 [NOTE]
1319 [role="docsvg-channel-subbuf-anim"]
1320 ====
1321 {note-no-anim}
1322 ====
1323
1324 In an ideal world, sub-buffers are consumed faster than they are filled,
1325 as is the case in the previous animation. In the real world,
1326 however, all sub-buffers can be full at some point, leaving no space to
1327 record the following events.
1328
1329 By design, LTTng is a _non-blocking_ tracer: when no empty sub-buffer is
1330 available, it is acceptable to lose event records when the alternative
1331 would be to cause substantial delays in the instrumented application's
1332 execution. LTTng privileges performance over integrity; it aims at
1333 perturbing the traced system as little as possible in order to make
1334 tracing of subtle race conditions and rare interrupt cascades possible.
1335
1336 When it comes to losing event records because no empty sub-buffer is
1337 available, the channel's _event loss mode_ determines what to do. The
1338 available event loss modes are:
1339
1340 Discard mode::
1341 Drop the newest event records until a the tracer
1342 releases a sub-buffer.
1343
1344 Overwrite mode::
1345 Clear the sub-buffer containing the oldest event records and start
1346 writing the newest event records there.
1347 +
1348 This mode is sometimes called _flight recorder mode_ because it's
1349 similar to a
1350 https://en.wikipedia.org/wiki/Flight_recorder[flight recorder]:
1351 always keep a fixed amount of the latest data.
1352
1353 Which mechanism you should choose depends on your context: prioritize
1354 the newest or the oldest event records in the ring buffer?
1355
1356 Beware that, in overwrite mode, the tracer abandons a whole sub-buffer
1357 as soon as a there's no space left for a new event record, whereas in
1358 discard mode, the tracer only discards the event record that doesn't
1359 fit.
1360
1361 In discard mode, LTTng increments a count of lost event records when
1362 an event record is lost and saves this count to the trace. In
1363 overwrite mode, LTTng keeps no information when it overwrites a
1364 sub-buffer before consuming it.
1365
1366 There are a few ways to decrease your probability of losing event
1367 records.
1368 <<channel-subbuf-size-vs-subbuf-count,Sub-buffer count and size>> shows
1369 how you can fine-une the sub-buffer count and size of a channel to
1370 virtually stop losing event records, though at the cost of greater
1371 memory usage.
1372
1373
1374 [[channel-subbuf-size-vs-subbuf-count]]
1375 ==== Sub-buffer count and size
1376
1377 When you <<enabling-disabling-channels,create a channel>>, you can
1378 set its number of sub-buffers and their size.
1379
1380 Note that there is noticeable CPU overhead introduced when
1381 switching sub-buffers (marking a full one as consumable and switching
1382 to an empty one for the following events to be recorded). Knowing this,
1383 the following list presents a few practical situations along with how
1384 to configure the sub-buffer count and size for them:
1385
1386 * **High event throughput**: In general, prefer bigger sub-buffers to
1387 lower the risk of losing event records.
1388 +
1389 Having bigger sub-buffers also ensures a lower sub-buffer switching
1390 frequency.
1391 +
1392 The number of sub-buffers is only meaningful if you create the channel
1393 in overwrite mode: in this case, if a sub-buffer overwrite happens, the
1394 other sub-buffers are left unaltered.
1395
1396 * **Low event throughput**: In general, prefer smaller sub-buffers
1397 since the risk of losing event records is low.
1398 +
1399 Because events occur less frequently, the sub-buffer switching frequency
1400 should remain low and thus the tracer's overhead should not be a
1401 problem.
1402
1403 * **Low memory system**: If your target system has a low memory
1404 limit, prefer fewer first, then smaller sub-buffers.
1405 +
1406 Even if the system is limited in memory, you want to keep the
1407 sub-buffers as big as possible to avoid a high sub-buffer switching
1408 frequency.
1409
1410 Note that LTTng uses http://diamon.org/ctf/[CTF] as its trace format,
1411 which means event data is very compact. For example, the average
1412 LTTng kernel event record weights about 32{nbsp}bytes. Thus, a
1413 sub-buffer size of 1{nbsp}MiB is considered big.
1414
1415 The previous situations highlight the major trade-off between a few big
1416 sub-buffers and more, smaller sub-buffers: sub-buffer switching
1417 frequency vs. how much data is lost in overwrite mode. Assuming a
1418 constant event throughput and using the overwrite mode, the two
1419 following configurations have the same ring buffer total size:
1420
1421 [NOTE]
1422 [role="docsvg-channel-subbuf-size-vs-count-anim"]
1423 ====
1424 {note-no-anim}
1425 ====
1426
1427 * **2 sub-buffers of 4{nbsp}MiB each**: Expect a very low sub-buffer
1428 switching frequency, but if a sub-buffer overwrite happens, half of
1429 the event records so far (4{nbsp}MiB) are definitely lost.
1430 * **8 sub-buffers of 1{nbsp}MiB each**: Expect 4{nbsp}times the tracer's
1431 overhead as the previous configuration, but if a sub-buffer
1432 overwrite happens, only the eighth of event records so far are
1433 definitely lost.
1434
1435 In discard mode, the sub-buffers count parameter is pointless: use two
1436 sub-buffers and set their size according to the requirements of your
1437 situation.
1438
1439
1440 [[channel-switch-timer]]
1441 ==== Switch timer period
1442
1443 The _switch timer period_ is an important configurable attribute of
1444 a channel to ensure periodic sub-buffer flushing.
1445
1446 When the _switch timer_ expires, a sub-buffer switch happens. You can
1447 set the switch timer period attribute when you
1448 <<enabling-disabling-channels,create a channel>> to ensure that event
1449 data is consumed and committed to trace files or to a distant relay
1450 daemon periodically in case of a low event throughput.
1451
1452 [NOTE]
1453 [role="docsvg-channel-switch-timer"]
1454 ====
1455 {note-no-anim}
1456 ====
1457
1458 This attribute is also convenient when you use big sub-buffers to cope
1459 with a sporadic high event throughput, even if the throughput is
1460 normally low.
1461
1462
1463 [[channel-read-timer]]
1464 ==== Read timer period
1465
1466 By default, the LTTng tracers use a notification mechanism to signal a
1467 full sub-buffer so that a consumer daemon can consume it. When such
1468 notifications must be avoided, for example in real-time applications,
1469 you can use the channel's _read timer_ instead. When the read timer
1470 fires, the <<lttng-consumerd,consumer daemon>> checks for full,
1471 consumable sub-buffers.
1472
1473
1474 [[tracefile-rotation]]
1475 ==== Trace file count and size
1476
1477 By default, trace files can grow as large as needed. You can set the
1478 maximum size of each trace file that a channel writes when you
1479 <<enabling-disabling-channels,create a channel>>. When the size of
1480 a trace file reaches the channel's fixed maximum size, LTTng creates
1481 another file to contain the next event records. LTTng appends a file
1482 count to each trace file name in this case.
1483
1484 If you set the trace file size attribute when you create a channel, the
1485 maximum number of trace files that LTTng creates is _unlimited_ by
1486 default. To limit them, you can also set a maximum number of trace
1487 files. When the number of trace files reaches the channel's fixed
1488 maximum count, the oldest trace file is overwritten. This mechanism is
1489 called _trace file rotation_.
1490
1491
1492 [[event]]
1493 === Instrumentation point, event rule, event, and event record
1494
1495 An _event rule_ is a set of conditions which must be **all** satisfied
1496 for LTTng to record an occuring event.
1497
1498 You set the conditions when you <<enabling-disabling-events,create
1499 an event rule>>.
1500
1501 You always attach an event rule to <<channel,channel>> when you create
1502 it.
1503
1504 When an event passes the conditions of an event rule, LTTng records it
1505 in one of the attached channel's sub-buffers.
1506
1507 The available conditions, as of LTTng{nbsp}{revision}, are:
1508
1509 * The event rule _is enabled_.
1510 * The instrumentation point's type _is{nbsp}T_.
1511 * The instrumentation point's name (sometimes called _event name_)
1512 _matches{nbsp}N_, but _is not{nbsp}E_.
1513 * The instrumentation point's log level _is as severe as{nbsp}L_, or
1514 _is exactly{nbsp}L_.
1515 * The fields of the event's payload _satisfy_ a filter
1516 expression{nbsp}__F__.
1517
1518 As you can see, all the conditions but the dynamic filter are related to
1519 the event rule's status or to the instrumentation point, not to the
1520 occurring events. This is why, without a filter, checking if an event
1521 passes an event rule is not a dynamic task: when you create or modify an
1522 event rule, all the tracers of its tracing domain enable or disable the
1523 instrumentation points themselves once. This is possible because the
1524 attributes of an instrumentation point (type, name, and log level) are
1525 defined statically. In other words, without a dynamic filter, the tracer
1526 _does not evaluate_ the arguments of an instrumentation point unless it
1527 matches an enabled event rule.
1528
1529 Note that, for LTTng to record an event, the <<channel,channel>> to
1530 which a matching event rule is attached must also be enabled, and the
1531 tracing session owning this channel must be active.
1532
1533 [role="img-100"]
1534 .Logical path from an instrumentation point to an event record.
1535 image::event-rule.png[]
1536
1537 .Event, event record, or event rule?
1538 ****
1539 With so many similar terms, it's easy to get confused.
1540
1541 An **event** is the consequence of the execution of an _instrumentation
1542 point_, like a tracepoint that you manually place in some source code,
1543 or a Linux kernel KProbe. An event is said to _occur_ at a specific
1544 time. Different actions can be taken upon the occurance of an event,
1545 like record the event's payload to a buffer.
1546
1547 An **event record** is the representation of an event in a sub-buffer. A
1548 tracer is responsible for capturing the payload of an event, current
1549 context variables, the event's ID, and the event's timestamp. LTTng
1550 can append this sub-buffer to a trace file.
1551
1552 An **event rule** is a set of conditions which must all be satisfied for
1553 LTTng to record an occuring event. Events still occur without
1554 satisfying event rules, but LTTng does not record them.
1555 ****
1556
1557
1558 [[plumbing]]
1559 == Components of noch:{LTTng}
1560
1561 The second _T_ in _LTTng_ stands for _toolkit_: it would be wrong
1562 to call LTTng a simple _tool_ since it is composed of multiple
1563 interacting components. This section describes those components,
1564 explains their respective roles, and shows how they connect together to
1565 form the LTTng ecosystem.
1566
1567 The following diagram shows how the most important components of LTTng
1568 interact with user applications, the Linux kernel, and you:
1569
1570 [role="img-100"]
1571 .Control and trace data paths between LTTng components.
1572 image::plumbing.png[]
1573
1574 The LTTng project incorporates:
1575
1576 * **LTTng-tools**: Libraries and command-line interface to
1577 control tracing sessions.
1578 ** <<lttng-sessiond,Session daemon>> (man:lttng-sessiond(8)).
1579 ** <<lttng-consumerd,Consumer daemon>> (man:lttng-consumerd(8)).
1580 ** <<lttng-relayd,Relay daemon>> (man:lttng-relayd(8)).
1581 ** <<liblttng-ctl-lttng,Tracing control library>> (`liblttng-ctl`).
1582 ** <<lttng-cli,Tracing control command-line tool>> (man:lttng(1)).
1583 * **LTTng-UST**: Libraries and Java/Python packages to trace user
1584 applications.
1585 ** <<lttng-ust,User space tracing library>> (`liblttng-ust`) and its
1586 headers to instrument and trace any native user application.
1587 ** <<prebuilt-ust-helpers,Preloadable user space tracing helpers>>:
1588 *** `liblttng-ust-libc-wrapper`
1589 *** `liblttng-ust-pthread-wrapper`
1590 *** `liblttng-ust-cyg-profile`
1591 *** `liblttng-ust-cyg-profile-fast`
1592 *** `liblttng-ust-dl`
1593 ** User space tracepoint provider source files generator command-line
1594 tool (man:lttng-gen-tp(1)).
1595 ** <<lttng-ust-agents,LTTng-UST Java agent>> to instrument and trace
1596 Java applications using `java.util.logging` or
1597 Apache log4j 1.2 logging.
1598 ** <<lttng-ust-agents,LTTng-UST Python agent>> to instrument
1599 Python applications using the standard `logging` package.
1600 * **LTTng-modules**: <<lttng-modules,Linux kernel modules>> to trace
1601 the kernel.
1602 ** LTTng kernel tracer module.
1603 ** Tracing ring buffer kernel modules.
1604 ** Probe kernel modules.
1605 ** LTTng logger kernel module.
1606
1607
1608 [[lttng-cli]]
1609 === Tracing control command-line interface
1610
1611 [role="img-100"]
1612 .The tracing control command-line interface.
1613 image::plumbing-lttng-cli.png[]
1614
1615 The _man:lttng(1) command-line tool_ is the standard user interface to
1616 control LTTng <<tracing-session,tracing sessions>>. The cmd:lttng tool
1617 is part of LTTng-tools.
1618
1619 The cmd:lttng tool is linked with
1620 <<liblttng-ctl-lttng,`liblttng-ctl`>> to communicate with
1621 one or more <<lttng-sessiond,session daemons>> behind the scenes.
1622
1623 The cmd:lttng tool has a Git-like interface:
1624
1625 [role="term"]
1626 ----
1627 $ lttng <GENERAL OPTIONS> <COMMAND> <COMMAND OPTIONS>
1628 ----
1629
1630 The <<controlling-tracing,Tracing control>> section explores the
1631 available features of LTTng using the cmd:lttng tool.
1632
1633
1634 [[liblttng-ctl-lttng]]
1635 === Tracing control library
1636
1637 [role="img-100"]
1638 .The tracing control library.
1639 image::plumbing-liblttng-ctl.png[]
1640
1641 The _LTTng control library_, `liblttng-ctl`, is used to communicate
1642 with a <<lttng-sessiond,session daemon>> using a C API that hides the
1643 underlying protocol's details. `liblttng-ctl` is part of LTTng-tools.
1644
1645 The <<lttng-cli,cmd:lttng command-line tool>>
1646 is linked with `liblttng-ctl`.
1647
1648 You can use `liblttng-ctl` in C or $$C++$$ source code by including its
1649 "master" header:
1650
1651 [source,c]
1652 ----
1653 #include <lttng/lttng.h>
1654 ----
1655
1656 Some objects are referenced by name (C string), such as tracing
1657 sessions, but most of them require to create a handle first using
1658 `lttng_create_handle()`.
1659
1660 The best available developer documentation for `liblttng-ctl` is, as of
1661 LTTng{nbsp}{revision}, its installed header files. Every function and
1662 structure is thoroughly documented.
1663
1664
1665 [[lttng-ust]]
1666 === User space tracing library
1667
1668 [role="img-100"]
1669 .The user space tracing library.
1670 image::plumbing-liblttng-ust.png[]
1671
1672 The _user space tracing library_, `liblttng-ust` (see man:lttng-ust(3)),
1673 is the LTTng user space tracer. It receives commands from a
1674 <<lttng-sessiond,session daemon>>, for example to
1675 enable and disable specific instrumentation points, and writes event
1676 records to ring buffers shared with a
1677 <<lttng-consumerd,consumer daemon>>.
1678 `liblttng-ust` is part of LTTng-UST.
1679
1680 Public C header files are installed beside `liblttng-ust` to
1681 instrument any <<c-application,C or $$C++$$ application>>.
1682
1683 <<lttng-ust-agents,LTTng-UST agents>>, which are regular Java and Python
1684 packages, use their own library providing tracepoints which is
1685 linked with `liblttng-ust`.
1686
1687 An application or library does not have to initialize `liblttng-ust`
1688 manually: its constructor does the necessary tasks to properly register
1689 to a session daemon. The initialization phase also enables the
1690 instrumentation points matching the <<event,event rules>> that you
1691 already created.
1692
1693
1694 [[lttng-ust-agents]]
1695 === User space tracing agents
1696
1697 [role="img-100"]
1698 .The user space tracing agents.
1699 image::plumbing-lttng-ust-agents.png[]
1700
1701 The _LTTng-UST Java and Python agents_ are regular Java and Python
1702 packages which add LTTng tracing capabilities to the
1703 native logging frameworks. The LTTng-UST agents are part of LTTng-UST.
1704
1705 In the case of Java, the
1706 https://docs.oracle.com/javase/7/docs/api/java/util/logging/package-summary.html[`java.util.logging`
1707 core logging facilities] and
1708 https://logging.apache.org/log4j/1.2/[Apache log4j 1.2] are supported.
1709 Note that Apache Log4{nbsp}2 is not supported.
1710
1711 In the case of Python, the standard
1712 https://docs.python.org/3/library/logging.html[`logging`] package
1713 is supported. Both Python 2 and Python 3 modules can import the
1714 LTTng-UST Python agent package.
1715
1716 The applications using the LTTng-UST agents are in the
1717 `java.util.logging` (JUL),
1718 log4j, and Python <<domain,tracing domains>>.
1719
1720 Both agents use the same mechanism to trace the log statements. When an
1721 agent is initialized, it creates a log handler that attaches to the root
1722 logger. The agent also registers to a <<lttng-sessiond,session daemon>>.
1723 When the application executes a log statement, it is passed to the
1724 agent's log handler by the root logger. The agent's log handler calls a
1725 native function in a tracepoint provider package shared library linked
1726 with <<lttng-ust,`liblttng-ust`>>, passing the formatted log message and
1727 other fields, like its logger name and its log level. This native
1728 function contains a user space instrumentation point, hence tracing the
1729 log statement.
1730
1731 The log level condition of an
1732 <<event,event rule>> is considered when tracing
1733 a Java or a Python application, and it's compatible with the standard
1734 JUL, log4j, and Python log levels.
1735
1736
1737 [[lttng-modules]]
1738 === LTTng kernel modules
1739
1740 [role="img-100"]
1741 .The LTTng kernel modules.
1742 image::plumbing-lttng-modules.png[]
1743
1744 The _LTTng kernel modules_ are a set of Linux kernel modules
1745 which implement the kernel tracer of the LTTng project. The LTTng
1746 kernel modules are part of LTTng-modules.
1747
1748 The LTTng kernel modules include:
1749
1750 * A set of _probe_ modules.
1751 +
1752 Each module attaches to a specific subsystem
1753 of the Linux kernel using its tracepoint instrument points. There are
1754 also modules to attach to the entry and return points of the Linux
1755 system call functions.
1756
1757 * _Ring buffer_ modules.
1758 +
1759 A ring buffer implementation is provided as kernel modules. The LTTng
1760 kernel tracer writes to the ring buffer; a
1761 <<lttng-consumerd,consumer daemon>> reads from the ring buffer.
1762
1763 * The _LTTng kernel tracer_ module.
1764 * The _LTTng logger_ module.
1765 +
1766 The LTTng logger module implements the special path:{/proc/lttng-logger}
1767 file so that any executable can generate LTTng events by opening and
1768 writing to this file.
1769 +
1770 See <<proc-lttng-logger-abi,LTTng logger>>.
1771
1772 Generally, you do not have to load the LTTng kernel modules manually
1773 (using man:modprobe(8), for example): a root <<lttng-sessiond,session
1774 daemon>> loads the necessary modules when starting. If you have extra
1775 probe modules, you can specify to load them to the session daemon on
1776 the command line.
1777
1778 The LTTng kernel modules are installed in
1779 +/usr/lib/modules/__release__/extra+ by default, where +__release__+ is
1780 the kernel release (see `uname --kernel-release`).
1781
1782
1783 [[lttng-sessiond]]
1784 === Session daemon
1785
1786 [role="img-100"]
1787 .The session daemon.
1788 image::plumbing-sessiond.png[]
1789
1790 The _session daemon_, man:lttng-sessiond(8), is a daemon responsible for
1791 managing tracing sessions and for controlling the various components of
1792 LTTng. The session daemon is part of LTTng-tools.
1793
1794 The session daemon sends control requests to and receives control
1795 responses from:
1796
1797 * The <<lttng-ust,user space tracing library>>.
1798 +
1799 Any instance of the user space tracing library first registers to
1800 a session daemon. Then, the session daemon can send requests to
1801 this instance, such as:
1802 +
1803 --
1804 ** Get the list of tracepoints.
1805 ** Share an <<event,event rule>> so that the user space tracing library
1806 can enable or disable tracepoints. Amongst the possible conditions
1807 of an event rule is a filter expression which `liblttng-ust` evalutes
1808 when an event occurs.
1809 ** Share <<channel,channel>> attributes and ring buffer locations.
1810 --
1811 +
1812 The session daemon and the user space tracing library use a Unix
1813 domain socket for their communication.
1814
1815 * The <<lttng-ust-agents,user space tracing agents>>.
1816 +
1817 Any instance of a user space tracing agent first registers to
1818 a session daemon. Then, the session daemon can send requests to
1819 this instance, such as:
1820 +
1821 --
1822 ** Get the list of loggers.
1823 ** Enable or disable a specific logger.
1824 --
1825 +
1826 The session daemon and the user space tracing agent use a TCP connection
1827 for their communication.
1828
1829 * The <<lttng-modules,LTTng kernel tracer>>.
1830 * The <<lttng-consumerd,consumer daemon>>.
1831 +
1832 The session daemon sends requests to the consumer daemon to instruct
1833 it where to send the trace data streams, amongst other information.
1834
1835 * The <<lttng-relayd,relay daemon>>.
1836
1837 The session daemon receives commands from the
1838 <<liblttng-ctl-lttng,tracing control library>>.
1839
1840 The root session daemon loads the appropriate
1841 <<lttng-modules,LTTng kernel modules>> on startup. It also spawns
1842 a <<lttng-consumerd,consumer daemon>> as soon as you create
1843 an <<event,event rule>>.
1844
1845 The session daemon does not send and receive trace data: this is the
1846 role of the <<lttng-consumerd,consumer daemon>> and
1847 <<lttng-relayd,relay daemon>>. It does, however, generate the
1848 http://diamon.org/ctf/[CTF] metadata stream.
1849
1850 Each Unix user can have its own session daemon instance. The
1851 tracing sessions managed by different session daemons are completely
1852 independent.
1853
1854 The root user's session daemon is the only one which is
1855 allowed to control the LTTng kernel tracer, and its spawned consumer
1856 daemon is the only one which is allowed to consume trace data from the
1857 LTTng kernel tracer. Note, however, that any Unix user which is a member
1858 of the <<tracing-group,tracing group>> is allowed
1859 to create <<channel,channels>> in the
1860 Linux kernel <<domain,tracing domain>>, and thus to trace the Linux
1861 kernel.
1862
1863 The <<lttng-cli,cmd:lttng command-line tool>> automatically starts a
1864 session daemon when using its `create` command if none is currently
1865 running. You can also start the session daemon manually.
1866
1867
1868 [[lttng-consumerd]]
1869 === Consumer daemon
1870
1871 [role="img-100"]
1872 .The consumer daemon.
1873 image::plumbing-consumerd.png[]
1874
1875 The _consumer daemon_, man:lttng-consumerd(8), is a daemon which shares
1876 ring buffers with user applications or with the LTTng kernel modules to
1877 collect trace data and send it to some location (on disk or to a
1878 <<lttng-relayd,relay daemon>> over the network). The consumer daemon
1879 is part of LTTng-tools.
1880
1881 You do not start a consumer daemon manually: a consumer daemon is always
1882 spawned by a <<lttng-sessiond,session daemon>> as soon as you create an
1883 <<event,event rule>>, that is, before you start tracing. When you kill
1884 its owner session daemon, the consumer daemon also exits because it is
1885 the session daemon's child process. Command-line options of
1886 man:lttng-sessiond(8) target the consumer daemon process.
1887
1888 There are up to two running consumer daemons per Unix user, whereas only
1889 one session daemon can run per user. This is because each process can be
1890 either 32-bit or 64-bit: if the target system runs a mixture of 32-bit
1891 and 64-bit processes, it is more efficient to have separate
1892 corresponding 32-bit and 64-bit consumer daemons. The root user is an
1893 exception: it can have up to _three_ running consumer daemons: 32-bit
1894 and 64-bit instances for its user applications, and one more
1895 reserved for collecting kernel trace data.
1896
1897
1898 [[lttng-relayd]]
1899 === Relay daemon
1900
1901 [role="img-100"]
1902 .The relay daemon.
1903 image::plumbing-relayd.png[]
1904
1905 The _relay daemon_, man:lttng-relayd(8), is a daemon acting as a bridge
1906 between remote session and consumer daemons, local trace files, and a
1907 remote live trace viewer. The relay daemon is part of LTTng-tools.
1908
1909 The main purpose of the relay daemon is to implement a receiver of
1910 <<sending-trace-data-over-the-network,trace data over the network>>.
1911 This is useful when the target system does not have much file system
1912 space to record trace files locally.
1913
1914 The relay daemon is also a server to which a
1915 <<lttng-live,live trace viewer>> can
1916 connect. The live trace viewer sends requests to the relay daemon to
1917 receive trace data as the target system emits events. The
1918 communication protocol is named _LTTng live_; it is used over TCP
1919 connections.
1920
1921 Note that you can start the relay daemon on the target system directly.
1922 This is the setup of choice when the use case is to view events as
1923 the target system emits them without the need of a remote system.
1924
1925
1926 [[instrumenting]]
1927 == [[using-lttng]]Instrumentation
1928
1929 There are many examples of tracing and monitoring in our everyday life:
1930
1931 * You have access to real-time and historical weather reports and
1932 forecasts thanks to weather stations installed around the country.
1933 * You know your heart is safe thanks to an electrocardiogram.
1934 * You make sure not to drive your car too fast and to have enough fuel
1935 to reach your destination thanks to gauges visible on your dashboard.
1936
1937 All the previous examples have something in common: they rely on
1938 **instruments**. Without the electrodes attached to the surface of your
1939 body's skin, cardiac monitoring is futile.
1940
1941 LTTng, as a tracer, is no different from those real life examples. If
1942 you're about to trace a software system or, in other words, record its
1943 history of execution, you better have **instrumentation points** in the
1944 subject you're tracing, that is, the actual software.
1945
1946 Various ways were developed to instrument a piece of software for LTTng
1947 tracing. The most straightforward one is to manually place
1948 instrumentation points, called _tracepoints_, in the software's source
1949 code. It is also possible to add instrumentation points dynamically in
1950 the Linux kernel <<domain,tracing domain>>.
1951
1952 If you're only interested in tracing the Linux kernel, your
1953 instrumentation needs are probably already covered by LTTng's built-in
1954 <<lttng-modules,Linux kernel tracepoints>>. You may also wish to trace a
1955 user application which is already instrumented for LTTng tracing.
1956 In such cases, you can skip this whole section and read the topics of
1957 the <<controlling-tracing,Tracing control>> section.
1958
1959 Many methods are available to instrument a piece of software for LTTng
1960 tracing. They are:
1961
1962 * <<c-application,User space instrumentation for C and $$C++$$
1963 applications>>.
1964 * <<prebuilt-ust-helpers,Prebuilt user space tracing helpers>>.
1965 * <<java-application,User space Java agent>>.
1966 * <<python-application,User space Python agent>>.
1967 * <<proc-lttng-logger-abi,LTTng logger>>.
1968 * <<instrumenting-linux-kernel,LTTng kernel tracepoints>>.
1969
1970
1971 [[c-application]]
1972 === [[cxx-application]]User space instrumentation for C and $$C++$$ applications
1973
1974 The procedure to instrument a C or $$C++$$ user application with
1975 the <<lttng-ust,LTTng user space tracing library>>, `liblttng-ust`, is:
1976
1977 . <<tracepoint-provider,Create the source files of a tracepoint provider
1978 package>>.
1979 . <<probing-the-application-source-code,Add tracepoints to
1980 the application's source code>>.
1981 . <<building-tracepoint-providers-and-user-application,Build and link
1982 a tracepoint provider package and the user application>>.
1983
1984 If you need quick, man:printf(3)-like instrumentation, you can skip
1985 those steps and use <<tracef,`tracef()`>> or <<tracelog,`tracelog()`>>
1986 instead.
1987
1988 IMPORTANT: You need to <<installing-lttng,install>> LTTng-UST to
1989 instrument a user application with `liblttng-ust`.
1990
1991
1992 [[tracepoint-provider]]
1993 ==== Create the source files of a tracepoint provider package
1994
1995 A _tracepoint provider_ is a set of compiled functions which provide
1996 **tracepoints** to an application, the type of instrumentation point
1997 supported by LTTng-UST. Those functions can emit events with
1998 user-defined fields and serialize those events as event records to one
1999 or more LTTng-UST <<channel,channel>> sub-buffers. The `tracepoint()`
2000 macro, which you <<probing-the-application-source-code,insert in a user
2001 application's source code>>, calls those functions.
2002
2003 A _tracepoint provider package_ is an object file (`.o`) or a shared
2004 library (`.so`) which contains one or more tracepoint providers.
2005 Its source files are:
2006
2007 * One or more <<tpp-header,tracepoint provider header>> (`.h`).
2008 * A <<tpp-source,tracepoint provider package source>> (`.c`).
2009
2010 A tracepoint provider package is dynamically linked with `liblttng-ust`,
2011 the LTTng user space tracer, at run time.
2012
2013 [role="img-100"]
2014 .User application linked with `liblttng-ust` and containing a tracepoint provider.
2015 image::ust-app.png[]
2016
2017 NOTE: If you need quick, man:printf(3)-like instrumentation, you can
2018 skip creating and using a tracepoint provider and use
2019 <<tracef,`tracef()`>> or <<tracelog,`tracelog()`>> instead.
2020
2021
2022 [[tpp-header]]
2023 ===== Create a tracepoint provider header file template
2024
2025 A _tracepoint provider header file_ contains the tracepoint
2026 definitions of a tracepoint provider.
2027
2028 To create a tracepoint provider header file:
2029
2030 . Start from this template:
2031 +
2032 --
2033 [source,c]
2034 .Tracepoint provider header file template (`.h` file extension).
2035 ----
2036 #undef TRACEPOINT_PROVIDER
2037 #define TRACEPOINT_PROVIDER provider_name
2038
2039 #undef TRACEPOINT_INCLUDE
2040 #define TRACEPOINT_INCLUDE "./tp.h"
2041
2042 #if !defined(_TP_H) || defined(TRACEPOINT_HEADER_MULTI_READ)
2043 #define _TP_H
2044
2045 #include <lttng/tracepoint.h>
2046
2047 /*
2048 * Use TRACEPOINT_EVENT(), TRACEPOINT_EVENT_CLASS(),
2049 * TRACEPOINT_EVENT_INSTANCE(), and TRACEPOINT_LOGLEVEL() here.
2050 */
2051
2052 #endif /* _TP_H */
2053
2054 #include <lttng/tracepoint-event.h>
2055 ----
2056 --
2057
2058 . Replace:
2059 +
2060 * `provider_name` with the name of your tracepoint provider.
2061 * `"tp.h"` with the name of your tracepoint provider header file.
2062
2063 . Below the `#include <lttng/tracepoint.h>` line, put your
2064 <<defining-tracepoints,tracepoint definitions>>.
2065
2066 Your tracepoint provider name must be unique amongst all the possible
2067 tracepoint provider names used on the same target system. We
2068 suggest to include the name of your project or company in the name,
2069 for example, `org_lttng_my_project_tpp`.
2070
2071 TIP: [[lttng-gen-tp]]You can use the man:lttng-gen-tp(1) tool to create
2072 this boilerplate for you. When using cmd:lttng-gen-tp, all you need to
2073 write are the <<defining-tracepoints,tracepoint definitions>>.
2074
2075
2076 [[defining-tracepoints]]
2077 ===== Create a tracepoint definition
2078
2079 A _tracepoint definition_ defines, for a given tracepoint:
2080
2081 * Its **input arguments**. They are the macro parameters that the
2082 `tracepoint()` macro accepts for this particular tracepoint
2083 in the user application's source code.
2084 * Its **output event fields**. They are the sources of event fields
2085 that form the payload of any event that the execution of the
2086 `tracepoint()` macro emits for this particular tracepoint.
2087
2088 You can create a tracepoint definition by using the
2089 `TRACEPOINT_EVENT()` macro below the `#include <lttng/tracepoint.h>`
2090 line in the
2091 <<tpp-header,tracepoint provider header file template>>.
2092
2093 The syntax of the `TRACEPOINT_EVENT()` macro is:
2094
2095 [source,c]
2096 .`TRACEPOINT_EVENT()` macro syntax.
2097 ----
2098 TRACEPOINT_EVENT(
2099 /* Tracepoint provider name */
2100 provider_name,
2101
2102 /* Tracepoint name */
2103 tracepoint_name,
2104
2105 /* Input arguments */
2106 TP_ARGS(
2107 arguments
2108 ),
2109
2110 /* Output event fields */
2111 TP_FIELDS(
2112 fields
2113 )
2114 )
2115 ----
2116
2117 Replace:
2118
2119 * `provider_name` with your tracepoint provider name.
2120 * `tracepoint_name` with your tracepoint name.
2121 * `arguments` with the <<tpp-def-input-args,input arguments>>.
2122 * `fields` with the <<tpp-def-output-fields,output event field>>
2123 definitions.
2124
2125 This tracepoint emits events named `provider_name:tracepoint_name`.
2126
2127 [IMPORTANT]
2128 .Event name's length limitation
2129 ====
2130 The concatenation of the tracepoint provider name and the
2131 tracepoint name must not exceed **254 characters**. If it does, the
2132 instrumented application compiles and runs, but LTTng throws multiple
2133 warnings and you could experience serious issues.
2134 ====
2135
2136 [[tpp-def-input-args]]The syntax of the `TP_ARGS()` macro is:
2137
2138 [source,c]
2139 .`TP_ARGS()` macro syntax.
2140 ----
2141 TP_ARGS(
2142 type, arg_name
2143 )
2144 ----
2145
2146 Replace:
2147
2148 * `type` with the C type of the argument.
2149 * `arg_name` with the argument name.
2150
2151 You can repeat `type` and `arg_name` up to 10 times to have
2152 more than one argument.
2153
2154 .`TP_ARGS()` usage with three arguments.
2155 ====
2156 [source,c]
2157 ----
2158 TP_ARGS(
2159 int, count,
2160 float, ratio,
2161 const char*, query
2162 )
2163 ----
2164 ====
2165
2166 The `TP_ARGS()` and `TP_ARGS(void)` forms are valid to create a
2167 tracepoint definition with no input arguments.
2168
2169 [[tpp-def-output-fields]]The `TP_FIELDS()` macro contains a list of
2170 `ctf_*()` macros. Each `ctf_*()` macro defines one event field. See
2171 man:lttng-ust(3) for a complete description of the available `ctf_*()`
2172 macros. A `ctf_*()` macro specifies the type, size, and byte order of
2173 one event field.
2174
2175 Each `ctf_*()` macro takes an _argument expression_ parameter. This is a
2176 C expression that the tracer evalutes at the `tracepoint()` macro site
2177 in the application's source code. This expression provides a field's
2178 source of data. The argument expression can include input argument names
2179 listed in the `TP_ARGS()` macro.
2180
2181 Each `ctf_*()` macro also takes a _field name_ parameter. Field names
2182 must be unique within a given tracepoint definition.
2183
2184 Here's a complete tracepoint definition example:
2185
2186 .Tracepoint definition.
2187 ====
2188 The following tracepoint definition defines a tracepoint which takes
2189 three input arguments and has four output event fields.
2190
2191 [source,c]
2192 ----
2193 #include "my-custom-structure.h"
2194
2195 TRACEPOINT_EVENT(
2196 my_provider,
2197 my_tracepoint,
2198 TP_ARGS(
2199 const struct my_custom_structure*, my_custom_structure,
2200 float, ratio,
2201 const char*, query
2202 ),
2203 TP_FIELDS(
2204 ctf_string(query_field, query)
2205 ctf_float(double, ratio_field, ratio)
2206 ctf_integer(int, recv_size, my_custom_structure->recv_size)
2207 ctf_integer(int, send_size, my_custom_structure->send_size)
2208 )
2209 )
2210 ----
2211
2212 You can refer to this tracepoint definition with the `tracepoint()`
2213 macro in your application's source code like this:
2214
2215 [source,c]
2216 ----
2217 tracepoint(my_provider, my_tracepoint,
2218 my_structure, some_ratio, the_query);
2219 ----
2220 ====
2221
2222 NOTE: The LTTng tracer only evaluates tracepoint arguments at run time
2223 if they satisfy an enabled <<event,event rule>>.
2224
2225
2226 [[using-tracepoint-classes]]
2227 ===== Use a tracepoint class
2228
2229 A _tracepoint class_ is a class of tracepoints which share the same
2230 output event field definitions. A _tracepoint instance_ is one
2231 instance of such a defined tracepoint class, with its own tracepoint
2232 name.
2233
2234 The <<defining-tracepoints,`TRACEPOINT_EVENT()` macro>> is actually a
2235 shorthand which defines both a tracepoint class and a tracepoint
2236 instance at the same time.
2237
2238 When you build a tracepoint provider package, the C or $$C++$$ compiler
2239 creates one serialization function for each **tracepoint class**. A
2240 serialization function is responsible for serializing the event fields
2241 of a tracepoint to a sub-buffer when tracing.
2242
2243 For various performance reasons, when your situation requires multiple
2244 tracepoint definitions with different names, but with the same event
2245 fields, we recommend that you manually create a tracepoint class
2246 and instantiate as many tracepoint instances as needed. One positive
2247 effect of such a design, amongst other advantages, is that all
2248 tracepoint instances of the same tracepoint class reuse the same
2249 serialization function, thus reducing
2250 https://en.wikipedia.org/wiki/Cache_pollution[cache pollution].
2251
2252 .Use a tracepoint class and tracepoint instances.
2253 ====
2254 Consider the following three tracepoint definitions:
2255
2256 [source,c]
2257 ----
2258 TRACEPOINT_EVENT(
2259 my_app,
2260 get_account,
2261 TP_ARGS(
2262 int, userid,
2263 size_t, len
2264 ),
2265 TP_FIELDS(
2266 ctf_integer(int, userid, userid)
2267 ctf_integer(size_t, len, len)
2268 )
2269 )
2270
2271 TRACEPOINT_EVENT(
2272 my_app,
2273 get_settings,
2274 TP_ARGS(
2275 int, userid,
2276 size_t, len
2277 ),
2278 TP_FIELDS(
2279 ctf_integer(int, userid, userid)
2280 ctf_integer(size_t, len, len)
2281 )
2282 )
2283
2284 TRACEPOINT_EVENT(
2285 my_app,
2286 get_transaction,
2287 TP_ARGS(
2288 int, userid,
2289 size_t, len
2290 ),
2291 TP_FIELDS(
2292 ctf_integer(int, userid, userid)
2293 ctf_integer(size_t, len, len)
2294 )
2295 )
2296 ----
2297
2298 In this case, we create three tracepoint classes, with one implicit
2299 tracepoint instance for each of them: `get_account`, `get_settings`, and
2300 `get_transaction`. However, they all share the same event field names
2301 and types. Hence three identical, yet independent serialization
2302 functions are created when you build the tracepoint provider package.
2303
2304 A better design choice is to define a single tracepoint class and three
2305 tracepoint instances:
2306
2307 [source,c]
2308 ----
2309 /* The tracepoint class */
2310 TRACEPOINT_EVENT_CLASS(
2311 /* Tracepoint provider name */
2312 my_app,
2313
2314 /* Tracepoint class name */
2315 my_class,
2316
2317 /* Input arguments */
2318 TP_ARGS(
2319 int, userid,
2320 size_t, len
2321 ),
2322
2323 /* Output event fields */
2324 TP_FIELDS(
2325 ctf_integer(int, userid, userid)
2326 ctf_integer(size_t, len, len)
2327 )
2328 )
2329
2330 /* The tracepoint instances */
2331 TRACEPOINT_EVENT_INSTANCE(
2332 /* Tracepoint provider name */
2333 my_app,
2334
2335 /* Tracepoint class name */
2336 my_class,
2337
2338 /* Tracepoint name */
2339 get_account,
2340
2341 /* Input arguments */
2342 TP_ARGS(
2343 int, userid,
2344 size_t, len
2345 )
2346 )
2347 TRACEPOINT_EVENT_INSTANCE(
2348 my_app,
2349 my_class,
2350 get_settings,
2351 TP_ARGS(
2352 int, userid,
2353 size_t, len
2354 )
2355 )
2356 TRACEPOINT_EVENT_INSTANCE(
2357 my_app,
2358 my_class,
2359 get_transaction,
2360 TP_ARGS(
2361 int, userid,
2362 size_t, len
2363 )
2364 )
2365 ----
2366 ====
2367
2368
2369 [[assigning-log-levels]]
2370 ===== Assign a log level to a tracepoint definition
2371
2372 You can assign an optional _log level_ to a
2373 <<defining-tracepoints,tracepoint definition>>.
2374
2375 Assigning different levels of severity to tracepoint definitions can
2376 be useful: when you <<enabling-disabling-events,create an event rule>>,
2377 you can target tracepoints having a log level as severe as a specific
2378 value.
2379
2380 The concept of LTTng-UST log levels is similar to the levels found
2381 in typical logging frameworks:
2382
2383 * In a logging framework, the log level is given by the function
2384 or method name you use at the log statement site: `debug()`,
2385 `info()`, `warn()`, `error()`, and so on.
2386 * In LTTng-UST, you statically assign the log level to a tracepoint
2387 definition; any `tracepoint()` macro invocation which refers to
2388 this definition has this log level.
2389
2390 You can assign a log level to a tracepoint definition with the
2391 `TRACEPOINT_LOGLEVEL()` macro. You must use this macro _after_ the
2392 <<defining-tracepoints,`TRACEPOINT_EVENT()`>> or
2393 <<using-tracepoint-classes,`TRACEPOINT_INSTANCE()`>> macro for a given
2394 tracepoint.
2395
2396 The syntax of the `TRACEPOINT_LOGLEVEL()` macro is:
2397
2398 [source,c]
2399 .`TRACEPOINT_LOGLEVEL()` macro syntax.
2400 ----
2401 TRACEPOINT_LOGLEVEL(provider_name, tracepoint_name, log_level)
2402 ----
2403
2404 Replace:
2405
2406 * `provider_name` with the tracepoint provider name.
2407 * `tracepoint_name` with the tracepoint name.
2408 * `log_level` with the log level to assign to the tracepoint
2409 definition named `tracepoint_name` in the `provider_name`
2410 tracepoint provider.
2411 +
2412 See man:lttng-ust(3) for a list of available log level names.
2413
2414 .Assign the `TRACE_DEBUG_UNIT` log level to a tracepoint definition.
2415 ====
2416 [source,c]
2417 ----
2418 /* Tracepoint definition */
2419 TRACEPOINT_EVENT(
2420 my_app,
2421 get_transaction,
2422 TP_ARGS(
2423 int, userid,
2424 size_t, len
2425 ),
2426 TP_FIELDS(
2427 ctf_integer(int, userid, userid)
2428 ctf_integer(size_t, len, len)
2429 )
2430 )
2431
2432 /* Log level assignment */
2433 TRACEPOINT_LOGLEVEL(my_app, get_transaction, TRACE_DEBUG_UNIT)
2434 ----
2435 ====
2436
2437
2438 [[tpp-source]]
2439 ===== Create a tracepoint provider package source file
2440
2441 A _tracepoint provider package source file_ is a C source file which
2442 includes a <<tpp-header,tracepoint provider header file>> to expand its
2443 macros into event serialization and other functions.
2444
2445 You can always use the following tracepoint provider package source
2446 file template:
2447
2448 [source,c]
2449 .Tracepoint provider package source file template.
2450 ----
2451 #define TRACEPOINT_CREATE_PROBES
2452
2453 #include "tp.h"
2454 ----
2455
2456 Replace `tp.h` with the name of your <<tpp-header,tracepoint provider
2457 header file>> name. You may also include more than one tracepoint
2458 provider header file here to create a tracepoint provider package
2459 holding more than one tracepoint providers.
2460
2461
2462 [[probing-the-application-source-code]]
2463 ==== Add tracepoints to an application's source code
2464
2465 Once you <<tpp-header,create a tracepoint provider header file>>, you
2466 can use the `tracepoint()` macro in your application's
2467 source code to insert the tracepoints that this header
2468 <<defining-tracepoints,defines>>.
2469
2470 The `tracepoint()` macro takes at least two parameters: the tracepoint
2471 provider name and the tracepoint name. The corresponding tracepoint
2472 definition defines the other parameters.
2473
2474 .`tracepoint()` usage.
2475 ====
2476 The following <<defining-tracepoints,tracepoint definition>> defines a
2477 tracepoint which takes two input arguments and has two output event
2478 fields.
2479
2480 [source,c]
2481 .Tracepoint provider header file.
2482 ----
2483 #include "my-custom-structure.h"
2484
2485 TRACEPOINT_EVENT(
2486 my_provider,
2487 my_tracepoint,
2488 TP_ARGS(
2489 int, argc,
2490 const char*, cmd_name
2491 ),
2492 TP_FIELDS(
2493 ctf_string(cmd_name, cmd_name)
2494 ctf_integer(int, number_of_args, argc)
2495 )
2496 )
2497 ----
2498
2499 You can refer to this tracepoint definition with the `tracepoint()`
2500 macro in your application's source code like this:
2501
2502 [source,c]
2503 .Application's source file.
2504 ----
2505 #include "tp.h"
2506
2507 int main(int argc, char* argv[])
2508 {
2509 tracepoint(my_provider, my_tracepoint, argc, argv[0]);
2510
2511 return 0;
2512 }
2513 ----
2514
2515 Note how the application's source code includes
2516 the tracepoint provider header file containing the tracepoint
2517 definitions to use, path:{tp.h}.
2518 ====
2519
2520 .`tracepoint()` usage with a complex tracepoint definition.
2521 ====
2522 Consider this complex tracepoint definition, where multiple event
2523 fields refer to the same input arguments in their argument expression
2524 parameter:
2525
2526 [source,c]
2527 .Tracepoint provider header file.
2528 ----
2529 /* For `struct stat` */
2530 #include <sys/types.h>
2531 #include <sys/stat.h>
2532 #include <unistd.h>
2533
2534 TRACEPOINT_EVENT(
2535 my_provider,
2536 my_tracepoint,
2537 TP_ARGS(
2538 int, my_int_arg,
2539 char*, my_str_arg,
2540 struct stat*, st
2541 ),
2542 TP_FIELDS(
2543 ctf_integer(int, my_constant_field, 23 + 17)
2544 ctf_integer(int, my_int_arg_field, my_int_arg)
2545 ctf_integer(int, my_int_arg_field2, my_int_arg * my_int_arg)
2546 ctf_integer(int, sum4_field, my_str_arg[0] + my_str_arg[1] +
2547 my_str_arg[2] + my_str_arg[3])
2548 ctf_string(my_str_arg_field, my_str_arg)
2549 ctf_integer_hex(off_t, size_field, st->st_size)
2550 ctf_float(double, size_dbl_field, (double) st->st_size)
2551 ctf_sequence_text(char, half_my_str_arg_field, my_str_arg,
2552 size_t, strlen(my_str_arg) / 2)
2553 )
2554 )
2555 ----
2556
2557 You can refer to this tracepoint definition with the `tracepoint()`
2558 macro in your application's source code like this:
2559
2560 [source,c]
2561 .Application's source file.
2562 ----
2563 #define TRACEPOINT_DEFINE
2564 #include "tp.h"
2565
2566 int main(void)
2567 {
2568 struct stat s;
2569
2570 stat("/etc/fstab", &s);
2571 tracepoint(my_provider, my_tracepoint, 23, "Hello, World!", &s);
2572
2573 return 0;
2574 }
2575 ----
2576
2577 If you look at the event record that LTTng writes when tracing this
2578 program, assuming the file size of path:{/etc/fstab} is 301{nbsp}bytes,
2579 it should look like this:
2580
2581 .Event record fields
2582 |====
2583 |Field's name |Field's value
2584 |`my_constant_field` |40
2585 |`my_int_arg_field` |23
2586 |`my_int_arg_field2` |529
2587 |`sum4_field` |389
2588 |`my_str_arg_field` |`Hello, World!`
2589 |`size_field` |0x12d
2590 |`size_dbl_field` |301.0
2591 |`half_my_str_arg_field` |`Hello,`
2592 |====
2593 ====
2594
2595 Sometimes, the arguments you pass to `tracepoint()` are expensive to
2596 compute--they use the call stack, for example. To avoid this
2597 computation when the tracepoint is disabled, you can use the
2598 `tracepoint_enabled()` and `do_tracepoint()` macros.
2599
2600 The syntax of the `tracepoint_enabled()` and `do_tracepoint()` macros
2601 is:
2602
2603 [source,c]
2604 .`tracepoint_enabled()` and `do_tracepoint()` macros syntax.
2605 ----
2606 tracepoint_enabled(provider_name, tracepoint_name)
2607 do_tracepoint(provider_name, tracepoint_name, ...)
2608 ----
2609
2610 Replace:
2611
2612 * `provider_name` with the tracepoint provider name.
2613 * `tracepoint_name` with the tracepoint name.
2614
2615 `tracepoint_enabled()` returns a non-zero value if the tracepoint named
2616 `tracepoint_name` from the provider named `provider_name` is enabled
2617 **at run time**.
2618
2619 `do_tracepoint()` is like `tracepoint()`, except that it doesn't check
2620 if the tracepoint is enabled. Using `tracepoint()` with
2621 `tracepoint_enabled()` is dangerous since `tracepoint()` also contains
2622 the `tracepoint_enabled()` check, thus a race condition is
2623 possible in this situation:
2624
2625 [source,c]
2626 .Possible race condition when using `tracepoint_enabled()` with `tracepoint()`.
2627 ----
2628 if (tracepoint_enabled(my_provider, my_tracepoint)) {
2629 stuff = prepare_stuff();
2630 }
2631
2632 tracepoint(my_provider, my_tracepoint, stuff);
2633 ----
2634
2635 If the tracepoint is enabled after the condition, then `stuff` is not
2636 prepared: the emitted event will either contain wrong data, or the whole
2637 application could crash (segmentation fault, for example).
2638
2639 NOTE: Neither `tracepoint_enabled()` nor `do_tracepoint()` have an
2640 `STAP_PROBEV()` call. If you need it, you must emit
2641 this call yourself.
2642
2643
2644 [[building-tracepoint-providers-and-user-application]]
2645 ==== Build and link a tracepoint provider package and an application
2646
2647 Once you have one or more <<tpp-header,tracepoint provider header
2648 files>> and a <<tpp-source,tracepoint provider package source file>>,
2649 you can create the tracepoint provider package by compiling its source
2650 file. From here, multiple build and run scenarios are possible. The
2651 following table shows common application and library configurations
2652 along with the required command lines to achieve them.
2653
2654 In the following diagrams, we use the following file names:
2655
2656 `app`::
2657 Executable application.
2658
2659 `app.o`::
2660 Application's object file.
2661
2662 `tpp.o`::
2663 Tracepoint provider package object file.
2664
2665 `tpp.a`::
2666 Tracepoint provider package archive file.
2667
2668 `libtpp.so`::
2669 Tracepoint provider package shared object file.
2670
2671 `emon.o`::
2672 User library object file.
2673
2674 `libemon.so`::
2675 User library shared object file.
2676
2677 We use the following symbols in the diagrams of table below:
2678
2679 [role="img-100"]
2680 .Symbols used in the build scenario diagrams.
2681 image::ust-sit-symbols.png[]
2682
2683 We assume that path:{.} is part of the env:LD_LIBRARY_PATH environment
2684 variable in the following instructions.
2685
2686 [role="growable ust-scenarios",cols="asciidoc,asciidoc"]
2687 .Common tracepoint provider package scenarios.
2688 |====
2689 |Scenario |Instructions
2690
2691 |
2692 The instrumented application is statically linked with
2693 the tracepoint provider package object.
2694
2695 image::ust-sit+app-linked-with-tp-o+app-instrumented.png[]
2696
2697 |
2698 include::../common/ust-sit-step-tp-o.txt[]
2699
2700 To build the instrumented application:
2701
2702 . In path:{app.c}, before including path:{tpp.h}, add the following line:
2703 +
2704 --
2705 [source,c]
2706 ----
2707 #define TRACEPOINT_DEFINE
2708 ----
2709 --
2710
2711 . Compile the application source file:
2712 +
2713 --
2714 [role="term"]
2715 ----
2716 $ gcc -c app.c
2717 ----
2718 --
2719
2720 . Build the application:
2721 +
2722 --
2723 [role="term"]
2724 ----
2725 $ gcc -o app app.o tpp.o -llttng-ust -ldl
2726 ----
2727 --
2728
2729 To run the instrumented application:
2730
2731 * Start the application:
2732 +
2733 --
2734 [role="term"]
2735 ----
2736 $ ./app
2737 ----
2738 --
2739
2740 |
2741 The instrumented application is statically linked with the
2742 tracepoint provider package archive file.
2743
2744 image::ust-sit+app-linked-with-tp-a+app-instrumented.png[]
2745
2746 |
2747 To create the tracepoint provider package archive file:
2748
2749 . Compile the <<tpp-source,tracepoint provider package source file>>:
2750 +
2751 --
2752 [role="term"]
2753 ----
2754 $ gcc -I. -c tpp.c
2755 ----
2756 --
2757
2758 . Create the tracepoint provider package archive file:
2759 +
2760 --
2761 [role="term"]
2762 ----
2763 $ ar rcs tpp.a tpp.o
2764 ----
2765 --
2766
2767 To build the instrumented application:
2768
2769 . In path:{app.c}, before including path:{tpp.h}, add the following line:
2770 +
2771 --
2772 [source,c]
2773 ----
2774 #define TRACEPOINT_DEFINE
2775 ----
2776 --
2777
2778 . Compile the application source file:
2779 +
2780 --
2781 [role="term"]
2782 ----
2783 $ gcc -c app.c
2784 ----
2785 --
2786
2787 . Build the application:
2788 +
2789 --
2790 [role="term"]
2791 ----
2792 $ gcc -o app app.o tpp.a -llttng-ust -ldl
2793 ----
2794 --
2795
2796 To run the instrumented application:
2797
2798 * Start the application:
2799 +
2800 --
2801 [role="term"]
2802 ----
2803 $ ./app
2804 ----
2805 --
2806
2807 |
2808 The instrumented application is linked with the tracepoint provider
2809 package shared object.
2810
2811 image::ust-sit+app-linked-with-tp-so+app-instrumented.png[]
2812
2813 |
2814 include::../common/ust-sit-step-tp-so.txt[]
2815
2816 To build the instrumented application:
2817
2818 . In path:{app.c}, before including path:{tpp.h}, add the following line:
2819 +
2820 --
2821 [source,c]
2822 ----
2823 #define TRACEPOINT_DEFINE
2824 ----
2825 --
2826
2827 . Compile the application source file:
2828 +
2829 --
2830 [role="term"]
2831 ----
2832 $ gcc -c app.c
2833 ----
2834 --
2835
2836 . Build the application:
2837 +
2838 --
2839 [role="term"]
2840 ----
2841 $ gcc -o app app.o -ldl -L. -ltpp
2842 ----
2843 --
2844
2845 To run the instrumented application:
2846
2847 * Start the application:
2848 +
2849 --
2850 [role="term"]
2851 ----
2852 $ ./app
2853 ----
2854 --
2855
2856 |
2857 The tracepoint provider package shared object is preloaded before the
2858 instrumented application starts.
2859
2860 image::ust-sit+tp-so-preloaded+app-instrumented.png[]
2861
2862 |
2863 include::../common/ust-sit-step-tp-so.txt[]
2864
2865 To build the instrumented application:
2866
2867 . In path:{app.c}, before including path:{tpp.h}, add the
2868 following lines:
2869 +
2870 --
2871 [source,c]
2872 ----
2873 #define TRACEPOINT_DEFINE
2874 #define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
2875 ----
2876 --
2877
2878 . Compile the application source file:
2879 +
2880 --
2881 [role="term"]
2882 ----
2883 $ gcc -c app.c
2884 ----
2885 --
2886
2887 . Build the application:
2888 +
2889 --
2890 [role="term"]
2891 ----
2892 $ gcc -o app app.o -ldl
2893 ----
2894 --
2895
2896 To run the instrumented application with tracing support:
2897
2898 * Preload the tracepoint provider package shared object and
2899 start the application:
2900 +
2901 --
2902 [role="term"]
2903 ----
2904 $ LD_PRELOAD=./libtpp.so ./app
2905 ----
2906 --
2907
2908 To run the instrumented application without tracing support:
2909
2910 * Start the application:
2911 +
2912 --
2913 [role="term"]
2914 ----
2915 $ ./app
2916 ----
2917 --
2918
2919 |
2920 The instrumented application dynamically loads the tracepoint provider
2921 package shared object.
2922
2923 See the <<dlclose-warning,warning about `dlclose()`>>.
2924
2925 image::ust-sit+app-dlopens-tp-so+app-instrumented.png[]
2926
2927 |
2928 include::../common/ust-sit-step-tp-so.txt[]
2929
2930 To build the instrumented application:
2931
2932 . In path:{app.c}, before including path:{tpp.h}, add the
2933 following lines:
2934 +
2935 --
2936 [source,c]
2937 ----
2938 #define TRACEPOINT_DEFINE
2939 #define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
2940 ----
2941 --
2942
2943 . Compile the application source file:
2944 +
2945 --
2946 [role="term"]
2947 ----
2948 $ gcc -c app.c
2949 ----
2950 --
2951
2952 . Build the application:
2953 +
2954 --
2955 [role="term"]
2956 ----
2957 $ gcc -o app app.o -ldl
2958 ----
2959 --
2960
2961 To run the instrumented application:
2962
2963 * Start the application:
2964 +
2965 --
2966 [role="term"]
2967 ----
2968 $ ./app
2969 ----
2970 --
2971
2972 |
2973 The application is linked with the instrumented user library.
2974
2975 The instrumented user library is statically linked with the tracepoint
2976 provider package object file.
2977
2978 image::ust-sit+app-linked-with-lib+lib-linked-with-tp-o+lib-instrumented.png[]
2979
2980 |
2981 include::../common/ust-sit-step-tp-o-fpic.txt[]
2982
2983 To build the instrumented user library:
2984
2985 . In path:{emon.c}, before including path:{tpp.h}, add the
2986 following line:
2987 +
2988 --
2989 [source,c]
2990 ----
2991 #define TRACEPOINT_DEFINE
2992 ----
2993 --
2994
2995 . Compile the user library source file:
2996 +
2997 --
2998 [role="term"]
2999 ----
3000 $ gcc -I. -fpic -c emon.c
3001 ----
3002 --
3003
3004 . Build the user library shared object:
3005 +
3006 --
3007 [role="term"]
3008 ----
3009 $ gcc -shared -o libemon.so emon.o tpp.o -llttng-ust -ldl
3010 ----
3011 --
3012
3013 To build the application:
3014
3015 . Compile the application source file:
3016 +
3017 --
3018 [role="term"]
3019 ----
3020 $ gcc -c app.c
3021 ----
3022 --
3023
3024 . Build the application:
3025 +
3026 --
3027 [role="term"]
3028 ----
3029 $ gcc -o app app.o -L. -lemon
3030 ----
3031 --
3032
3033 To run the application:
3034
3035 * Start the application:
3036 +
3037 --
3038 [role="term"]
3039 ----
3040 $ ./app
3041 ----
3042 --
3043
3044 |
3045 The application is linked with the instrumented user library.
3046
3047 The instrumented user library is linked with the tracepoint provider
3048 package shared object.
3049
3050 image::ust-sit+app-linked-with-lib+lib-linked-with-tp-so+lib-instrumented.png[]
3051
3052 |
3053 include::../common/ust-sit-step-tp-so.txt[]
3054
3055 To build the instrumented user library:
3056
3057 . In path:{emon.c}, before including path:{tpp.h}, add the
3058 following line:
3059 +
3060 --
3061 [source,c]
3062 ----
3063 #define TRACEPOINT_DEFINE
3064 ----
3065 --
3066
3067 . Compile the user library source file:
3068 +
3069 --
3070 [role="term"]
3071 ----
3072 $ gcc -I. -fpic -c emon.c
3073 ----
3074 --
3075
3076 . Build the user library shared object:
3077 +
3078 --
3079 [role="term"]
3080 ----
3081 $ gcc -shared -o libemon.so emon.o -ldl -L. -ltpp
3082 ----
3083 --
3084
3085 To build the application:
3086
3087 . Compile the application source file:
3088 +
3089 --
3090 [role="term"]
3091 ----
3092 $ gcc -c app.c
3093 ----
3094 --
3095
3096 . Build the application:
3097 +
3098 --
3099 [role="term"]
3100 ----
3101 $ gcc -o app app.o -L. -lemon
3102 ----
3103 --
3104
3105 To run the application:
3106
3107 * Start the application:
3108 +
3109 --
3110 [role="term"]
3111 ----
3112 $ ./app
3113 ----
3114 --
3115
3116 |
3117 The tracepoint provider package shared object is preloaded before the
3118 application starts.
3119
3120 The application is linked with the instrumented user library.
3121
3122 image::ust-sit+tp-so-preloaded+app-linked-with-lib+lib-instrumented.png[]
3123
3124 |
3125 include::../common/ust-sit-step-tp-so.txt[]
3126
3127 To build the instrumented user library:
3128
3129 . In path:{emon.c}, before including path:{tpp.h}, add the
3130 following lines:
3131 +
3132 --
3133 [source,c]
3134 ----
3135 #define TRACEPOINT_DEFINE
3136 #define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
3137 ----
3138 --
3139
3140 . Compile the user library source file:
3141 +
3142 --
3143 [role="term"]
3144 ----
3145 $ gcc -I. -fpic -c emon.c
3146 ----
3147 --
3148
3149 . Build the user library shared object:
3150 +
3151 --
3152 [role="term"]
3153 ----
3154 $ gcc -shared -o libemon.so emon.o -ldl
3155 ----
3156 --
3157
3158 To build the application:
3159
3160 . Compile the application source file:
3161 +
3162 --
3163 [role="term"]
3164 ----
3165 $ gcc -c app.c
3166 ----
3167 --
3168
3169 . Build the application:
3170 +
3171 --
3172 [role="term"]
3173 ----
3174 $ gcc -o app app.o -L. -lemon
3175 ----
3176 --
3177
3178 To run the application with tracing support:
3179
3180 * Preload the tracepoint provider package shared object and
3181 start the application:
3182 +
3183 --
3184 [role="term"]
3185 ----
3186 $ LD_PRELOAD=./libtpp.so ./app
3187 ----
3188 --
3189
3190 To run the application without tracing support:
3191
3192 * Start the application:
3193 +
3194 --
3195 [role="term"]
3196 ----
3197 $ ./app
3198 ----
3199 --
3200
3201 |
3202 The application is linked with the instrumented user library.
3203
3204 The instrumented user library dynamically loads the tracepoint provider
3205 package shared object.
3206
3207 See the <<dlclose-warning,warning about `dlclose()`>>.
3208
3209 image::ust-sit+app-linked-with-lib+lib-dlopens-tp-so+lib-instrumented.png[]
3210
3211 |
3212 include::../common/ust-sit-step-tp-so.txt[]
3213
3214 To build the instrumented user library:
3215
3216 . In path:{emon.c}, before including path:{tpp.h}, add the
3217 following lines:
3218 +
3219 --
3220 [source,c]
3221 ----
3222 #define TRACEPOINT_DEFINE
3223 #define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
3224 ----
3225 --
3226
3227 . Compile the user library source file:
3228 +
3229 --
3230 [role="term"]
3231 ----
3232 $ gcc -I. -fpic -c emon.c
3233 ----
3234 --
3235
3236 . Build the user library shared object:
3237 +
3238 --
3239 [role="term"]
3240 ----
3241 $ gcc -shared -o libemon.so emon.o -ldl
3242 ----
3243 --
3244
3245 To build the application:
3246
3247 . Compile the application source file:
3248 +
3249 --
3250 [role="term"]
3251 ----
3252 $ gcc -c app.c
3253 ----
3254 --
3255
3256 . Build the application:
3257 +
3258 --
3259 [role="term"]
3260 ----
3261 $ gcc -o app app.o -L. -lemon
3262 ----
3263 --
3264
3265 To run the application:
3266
3267 * Start the application:
3268 +
3269 --
3270 [role="term"]
3271 ----
3272 $ ./app
3273 ----
3274 --
3275
3276 |
3277 The application dynamically loads the instrumented user library.
3278
3279 The instrumented user library is linked with the tracepoint provider
3280 package shared object.
3281
3282 See the <<dlclose-warning,warning about `dlclose()`>>.
3283
3284 image::ust-sit+app-dlopens-lib+lib-linked-with-tp-so+lib-instrumented.png[]
3285
3286 |
3287 include::../common/ust-sit-step-tp-so.txt[]
3288
3289 To build the instrumented user library:
3290
3291 . In path:{emon.c}, before including path:{tpp.h}, add the
3292 following line:
3293 +
3294 --
3295 [source,c]
3296 ----
3297 #define TRACEPOINT_DEFINE
3298 ----
3299 --
3300
3301 . Compile the user library source file:
3302 +
3303 --
3304 [role="term"]
3305 ----
3306 $ gcc -I. -fpic -c emon.c
3307 ----
3308 --
3309
3310 . Build the user library shared object:
3311 +
3312 --
3313 [role="term"]
3314 ----
3315 $ gcc -shared -o libemon.so emon.o -ldl -L. -ltpp
3316 ----
3317 --
3318
3319 To build the application:
3320
3321 . Compile the application source file:
3322 +
3323 --
3324 [role="term"]
3325 ----
3326 $ gcc -c app.c
3327 ----
3328 --
3329
3330 . Build the application:
3331 +
3332 --
3333 [role="term"]
3334 ----
3335 $ gcc -o app app.o -ldl -L. -lemon
3336 ----
3337 --
3338
3339 To run the application:
3340
3341 * Start the application:
3342 +
3343 --
3344 [role="term"]
3345 ----
3346 $ ./app
3347 ----
3348 --
3349
3350 |
3351 The application dynamically loads the instrumented user library.
3352
3353 The instrumented user library dynamically loads the tracepoint provider
3354 package shared object.
3355
3356 See the <<dlclose-warning,warning about `dlclose()`>>.
3357
3358 image::ust-sit+app-dlopens-lib+lib-dlopens-tp-so+lib-instrumented.png[]
3359
3360 |
3361 include::../common/ust-sit-step-tp-so.txt[]
3362
3363 To build the instrumented user library:
3364
3365 . In path:{emon.c}, before including path:{tpp.h}, add the
3366 following lines:
3367 +
3368 --
3369 [source,c]
3370 ----
3371 #define TRACEPOINT_DEFINE
3372 #define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
3373 ----
3374 --
3375
3376 . Compile the user library source file:
3377 +
3378 --
3379 [role="term"]
3380 ----
3381 $ gcc -I. -fpic -c emon.c
3382 ----
3383 --
3384
3385 . Build the user library shared object:
3386 +
3387 --
3388 [role="term"]
3389 ----
3390 $ gcc -shared -o libemon.so emon.o -ldl
3391 ----
3392 --
3393
3394 To build the application:
3395
3396 . Compile the application source file:
3397 +
3398 --
3399 [role="term"]
3400 ----
3401 $ gcc -c app.c
3402 ----
3403 --
3404
3405 . Build the application:
3406 +
3407 --
3408 [role="term"]
3409 ----
3410 $ gcc -o app app.o -ldl -L. -lemon
3411 ----
3412 --
3413
3414 To run the application:
3415
3416 * Start the application:
3417 +
3418 --
3419 [role="term"]
3420 ----
3421 $ ./app
3422 ----
3423 --
3424
3425 |
3426 The tracepoint provider package shared object is preloaded before the
3427 application starts.
3428
3429 The application dynamically loads the instrumented user library.
3430
3431 image::ust-sit+tp-so-preloaded+app-dlopens-lib+lib-instrumented.png[]
3432
3433 |
3434 include::../common/ust-sit-step-tp-so.txt[]
3435
3436 To build the instrumented user library:
3437
3438 . In path:{emon.c}, before including path:{tpp.h}, add the
3439 following lines:
3440 +
3441 --
3442 [source,c]
3443 ----
3444 #define TRACEPOINT_DEFINE
3445 #define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
3446 ----
3447 --
3448
3449 . Compile the user library source file:
3450 +
3451 --
3452 [role="term"]
3453 ----
3454 $ gcc -I. -fpic -c emon.c
3455 ----
3456 --
3457
3458 . Build the user library shared object:
3459 +
3460 --
3461 [role="term"]
3462 ----
3463 $ gcc -shared -o libemon.so emon.o -ldl
3464 ----
3465 --
3466
3467 To build the application:
3468
3469 . Compile the application source file:
3470 +
3471 --
3472 [role="term"]
3473 ----
3474 $ gcc -c app.c
3475 ----
3476 --
3477
3478 . Build the application:
3479 +
3480 --
3481 [role="term"]
3482 ----
3483 $ gcc -o app app.o -L. -lemon
3484 ----
3485 --
3486
3487 To run the application with tracing support:
3488
3489 * Preload the tracepoint provider package shared object and
3490 start the application:
3491 +
3492 --
3493 [role="term"]
3494 ----
3495 $ LD_PRELOAD=./libtpp.so ./app
3496 ----
3497 --
3498
3499 To run the application without tracing support:
3500
3501 * Start the application:
3502 +
3503 --
3504 [role="term"]
3505 ----
3506 $ ./app
3507 ----
3508 --
3509
3510 |
3511 The application is statically linked with the tracepoint provider
3512 package object file.
3513
3514 The application is linked with the instrumented user library.
3515
3516 image::ust-sit+app-linked-with-tp-o+app-linked-with-lib+lib-instrumented.png[]
3517
3518 |
3519 include::../common/ust-sit-step-tp-o.txt[]
3520
3521 To build the instrumented user library:
3522
3523 . In path:{emon.c}, before including path:{tpp.h}, add the
3524 following line:
3525 +
3526 --
3527 [source,c]
3528 ----
3529 #define TRACEPOINT_DEFINE
3530 ----
3531 --
3532
3533 . Compile the user library source file:
3534 +
3535 --
3536 [role="term"]
3537 ----
3538 $ gcc -I. -fpic -c emon.c
3539 ----
3540 --
3541
3542 . Build the user library shared object:
3543 +
3544 --
3545 [role="term"]
3546 ----
3547 $ gcc -shared -o libemon.so emon.o
3548 ----
3549 --
3550
3551 To build the application:
3552
3553 . Compile the application source file:
3554 +
3555 --
3556 [role="term"]
3557 ----
3558 $ gcc -c app.c
3559 ----
3560 --
3561
3562 . Build the application:
3563 +
3564 --
3565 [role="term"]
3566 ----
3567 $ gcc -o app app.o tpp.o -llttng-ust -ldl -L. -lemon
3568 ----
3569 --
3570
3571 To run the instrumented application:
3572
3573 * Start the application:
3574 +
3575 --
3576 [role="term"]
3577 ----
3578 $ ./app
3579 ----
3580 --
3581
3582 |
3583 The application is statically linked with the tracepoint provider
3584 package object file.
3585
3586 The application dynamically loads the instrumented user library.
3587
3588 image::ust-sit+app-linked-with-tp-o+app-dlopens-lib+lib-instrumented.png[]
3589
3590 |
3591 include::../common/ust-sit-step-tp-o.txt[]
3592
3593 To build the application:
3594
3595 . In path:{app.c}, before including path:{tpp.h}, add the following line:
3596 +
3597 --
3598 [source,c]
3599 ----
3600 #define TRACEPOINT_DEFINE
3601 ----
3602 --
3603
3604 . Compile the application source file:
3605 +
3606 --
3607 [role="term"]
3608 ----
3609 $ gcc -c app.c
3610 ----
3611 --
3612
3613 . Build the application:
3614 +
3615 --
3616 [role="term"]
3617 ----
3618 $ gcc -Wl,--export-dynamic -o app app.o tpp.o \
3619 -llttng-ust -ldl
3620 ----
3621 --
3622 +
3623 The `--export-dynamic` option passed to the linker is necessary for the
3624 dynamically loaded library to ``see'' the tracepoint symbols defined in
3625 the application.
3626
3627 To build the instrumented user library:
3628
3629 . Compile the user library source file:
3630 +
3631 --
3632 [role="term"]
3633 ----
3634 $ gcc -I. -fpic -c emon.c
3635 ----
3636 --
3637
3638 . Build the user library shared object:
3639 +
3640 --
3641 [role="term"]
3642 ----
3643 $ gcc -shared -o libemon.so emon.o
3644 ----
3645 --
3646
3647 To run the application:
3648
3649 * Start the application:
3650 +
3651 --
3652 [role="term"]
3653 ----
3654 $ ./app
3655 ----
3656 --
3657 |====
3658
3659 [[dlclose-warning]]
3660 [IMPORTANT]
3661 .Do not use man:dlclose(3) on a tracepoint provider package
3662 ====
3663 Never use man:dlclose(3) on any shared object which:
3664
3665 * Is linked with, statically or dynamically, a tracepoint provider
3666 package.
3667 * Calls man:dlopen(3) itself to dynamically open a tracepoint provider
3668 package shared object.
3669
3670 This is currently considered **unsafe** due to a lack of reference
3671 counting from LTTng-UST to the shared object.
3672
3673 A known workaround (available since glibc 2.2) is to use the
3674 `RTLD_NODELETE` flag when calling man:dlopen(3) initially. This has the
3675 effect of not unloading the loaded shared object, even if man:dlclose(3)
3676 is called.
3677
3678 You can also preload the tracepoint provider package shared object with
3679 the env:LD_PRELOAD environment variable to overcome this limitation.
3680 ====
3681
3682
3683 [[using-lttng-ust-with-daemons]]
3684 ===== Use noch:{LTTng-UST} with daemons
3685
3686 If your instrumented application calls man:fork(2), man:clone(2),
3687 or BSD's man:rfork(2), without a following man:exec(3)-family
3688 system call, you must preload the path:{liblttng-ust-fork.so} shared
3689 object when you start the application.
3690
3691 [role="term"]
3692 ----
3693 $ LD_PRELOAD=liblttng-ust-fork.so ./my-app
3694 ----
3695
3696 If your tracepoint provider package is
3697 a shared library which you also preload, you must put both
3698 shared objects in env:LD_PRELOAD:
3699
3700 [role="term"]
3701 ----
3702 $ LD_PRELOAD=liblttng-ust-fork.so:/path/to/tp.so ./my-app
3703 ----
3704
3705
3706 [role="since-2.9"]
3707 [[liblttng-ust-fd]]
3708 ===== Use noch:{LTTng-UST} with applications which close file descriptors that don't belong to them
3709
3710 If your instrumented application closes one or more file descriptors
3711 which it did not open itself, you must preload the
3712 path:{liblttng-ust-fd.so} shared object when you start the application:
3713
3714 [role="term"]
3715 ----
3716 $ LD_PRELOAD=liblttng-ust-fd.so ./my-app
3717 ----
3718
3719 Typical use cases include closing all the file descriptors after
3720 man:fork(2) or man:rfork(2) and buggy applications doing
3721 ``double closes''.
3722
3723
3724 [[lttng-ust-pkg-config]]
3725 ===== Use noch:{pkg-config}
3726
3727 On some distributions, LTTng-UST ships with a
3728 https://www.freedesktop.org/wiki/Software/pkg-config/[pkg-config]
3729 metadata file. If this is your case, then you can use cmd:pkg-config to
3730 build an application on the command line:
3731
3732 [role="term"]
3733 ----
3734 $ gcc -o my-app my-app.o tp.o $(pkg-config --cflags --libs lttng-ust)
3735 ----
3736
3737
3738 [[instrumenting-32-bit-app-on-64-bit-system]]
3739 ===== [[advanced-instrumenting-techniques]]Build a 32-bit instrumented application for a 64-bit target system
3740
3741 In order to trace a 32-bit application running on a 64-bit system,
3742 LTTng must use a dedicated 32-bit
3743 <<lttng-consumerd,consumer daemon>>.
3744
3745 The following steps show how to build and install a 32-bit consumer
3746 daemon, which is _not_ part of the default 64-bit LTTng build, how to
3747 build and install the 32-bit LTTng-UST libraries, and how to build and
3748 link an instrumented 32-bit application in that context.
3749
3750 To build a 32-bit instrumented application for a 64-bit target system,
3751 assuming you have a fresh target system with no installed Userspace RCU
3752 or LTTng packages:
3753
3754 . Download, build, and install a 32-bit version of Userspace RCU:
3755 +
3756 --
3757 [role="term"]
3758 ----
3759 $ cd $(mktemp -d) &&
3760 wget http://lttng.org/files/urcu/userspace-rcu-latest-0.9.tar.bz2 &&
3761 tar -xf userspace-rcu-latest-0.9.tar.bz2 &&
3762 cd userspace-rcu-0.9.* &&
3763 ./configure --libdir=/usr/local/lib32 CFLAGS=-m32 &&
3764 make &&
3765 sudo make install &&
3766 sudo ldconfig
3767 ----
3768 --
3769
3770 . Using your distribution's package manager, or from source, install
3771 the following 32-bit versions of the following dependencies of
3772 LTTng-tools and LTTng-UST:
3773 +
3774 --
3775 * https://sourceforge.net/projects/libuuid/[libuuid]
3776 * http://directory.fsf.org/wiki/Popt[popt]
3777 * http://www.xmlsoft.org/[libxml2]
3778 --
3779
3780 . Download, build, and install a 32-bit version of the latest
3781 LTTng-UST{nbsp}{revision}:
3782 +
3783 --
3784 [role="term"]
3785 ----
3786 $ cd $(mktemp -d) &&
3787 wget http://lttng.org/files/lttng-ust/lttng-ust-latest-2.9.tar.bz2 &&
3788 tar -xf lttng-ust-latest-2.9.tar.bz2 &&
3789 cd lttng-ust-2.9.* &&
3790 ./configure --libdir=/usr/local/lib32 \
3791 CFLAGS=-m32 CXXFLAGS=-m32 \
3792 LDFLAGS='-L/usr/local/lib32 -L/usr/lib32' &&
3793 make &&
3794 sudo make install &&
3795 sudo ldconfig
3796 ----
3797 --
3798 +
3799 [NOTE]
3800 ====
3801 Depending on your distribution,
3802 32-bit libraries could be installed at a different location than
3803 `/usr/lib32`. For example, Debian is known to install
3804 some 32-bit libraries in `/usr/lib/i386-linux-gnu`.
3805
3806 In this case, make sure to set `LDFLAGS` to all the
3807 relevant 32-bit library paths, for example:
3808
3809 [role="term"]
3810 ----
3811 $ LDFLAGS='-L/usr/lib/i386-linux-gnu -L/usr/lib32'
3812 ----
3813 ====
3814
3815 . Download the latest LTTng-tools{nbsp}{revision}, build, and install
3816 the 32-bit consumer daemon:
3817 +
3818 --
3819 [role="term"]
3820 ----
3821 $ cd $(mktemp -d) &&
3822 wget http://lttng.org/files/lttng-tools/lttng-tools-latest-2.9.tar.bz2 &&
3823 tar -xf lttng-tools-latest-2.9.tar.bz2 &&
3824 cd lttng-tools-2.9.* &&
3825 ./configure --libdir=/usr/local/lib32 CFLAGS=-m32 CXXFLAGS=-m32 \
3826 LDFLAGS='-L/usr/local/lib32 -L/usr/lib32' \
3827 --disable-bin-lttng --disable-bin-lttng-crash \
3828 --disable-bin-lttng-relayd --disable-bin-lttng-sessiond &&
3829 make &&
3830 cd src/bin/lttng-consumerd &&
3831 sudo make install &&
3832 sudo ldconfig
3833 ----
3834 --
3835
3836 . From your distribution or from source,
3837 <<installing-lttng,install>> the 64-bit versions of
3838 LTTng-UST and Userspace RCU.
3839 . Download, build, and install the 64-bit version of the
3840 latest LTTng-tools{nbsp}{revision}:
3841 +
3842 --
3843 [role="term"]
3844 ----
3845 $ cd $(mktemp -d) &&
3846 wget http://lttng.org/files/lttng-tools/lttng-tools-latest-2.9.tar.bz2 &&
3847 tar -xf lttng-tools-latest-2.9.tar.bz2 &&
3848 cd lttng-tools-2.9.* &&
3849 ./configure --with-consumerd32-libdir=/usr/local/lib32 \
3850 --with-consumerd32-bin=/usr/local/lib32/lttng/libexec/lttng-consumerd &&
3851 make &&
3852 sudo make install &&
3853 sudo ldconfig
3854 ----
3855 --
3856
3857 . Pass the following options to man:gcc(1), man:g++(1), or man:clang(1)
3858 when linking your 32-bit application:
3859 +
3860 ----
3861 -m32 -L/usr/lib32 -L/usr/local/lib32 \
3862 -Wl,-rpath,/usr/lib32,-rpath,/usr/local/lib32
3863 ----
3864 +
3865 For example, let's rebuild the quick start example in
3866 <<tracing-your-own-user-application,Trace a user application>> as an
3867 instrumented 32-bit application:
3868 +
3869 --
3870 [role="term"]
3871 ----
3872 $ gcc -m32 -c -I. hello-tp.c
3873 $ gcc -m32 -c hello.c
3874 $ gcc -m32 -o hello hello.o hello-tp.o \
3875 -L/usr/lib32 -L/usr/local/lib32 \
3876 -Wl,-rpath,/usr/lib32,-rpath,/usr/local/lib32 \
3877 -llttng-ust -ldl
3878 ----
3879 --
3880
3881 No special action is required to execute the 32-bit application and
3882 to trace it: use the command-line man:lttng(1) tool as usual.
3883
3884
3885 [role="since-2.5"]
3886 [[tracef]]
3887 ==== Use `tracef()`
3888
3889 man:tracef(3) is a small LTTng-UST API designed for quick,
3890 man:printf(3)-like instrumentation without the burden of
3891 <<tracepoint-provider,creating>> and
3892 <<building-tracepoint-providers-and-user-application,building>>
3893 a tracepoint provider package.
3894
3895 To use `tracef()` in your application:
3896
3897 . In the C or C++ source files where you need to use `tracef()`,
3898 include `<lttng/tracef.h>`:
3899 +
3900 --
3901 [source,c]
3902 ----
3903 #include <lttng/tracef.h>
3904 ----
3905 --
3906
3907 . In the application's source code, use `tracef()` like you would use
3908 man:printf(3):
3909 +
3910 --
3911 [source,c]
3912 ----
3913 /* ... */
3914
3915 tracef("my message: %d (%s)", my_integer, my_string);
3916
3917 /* ... */
3918 ----
3919 --
3920
3921 . Link your application with `liblttng-ust`:
3922 +
3923 --
3924 [role="term"]
3925 ----
3926 $ gcc -o app app.c -llttng-ust
3927 ----
3928 --
3929
3930 To trace the events that `tracef()` calls emit:
3931
3932 * <<enabling-disabling-events,Create an event rule>> which matches the
3933 `lttng_ust_tracef:*` event name:
3934 +
3935 --
3936 [role="term"]
3937 ----
3938 $ lttng enable-event --userspace 'lttng_ust_tracef:*'
3939 ----
3940 --
3941
3942 [IMPORTANT]
3943 .Limitations of `tracef()`
3944 ====
3945 The `tracef()` utility function was developed to make user space tracing
3946 super simple, albeit with notable disadvantages compared to
3947 <<defining-tracepoints,user-defined tracepoints>>:
3948
3949 * All the emitted events have the same tracepoint provider and
3950 tracepoint names, respectively `lttng_ust_tracef` and `event`.
3951 * There is no static type checking.
3952 * The only event record field you actually get, named `msg`, is a string
3953 potentially containing the values you passed to `tracef()`
3954 using your own format string. This also means that you cannot filter
3955 events with a custom expression at run time because there are no
3956 isolated fields.
3957 * Since `tracef()` uses the C standard library's man:vasprintf(3)
3958 function behind the scenes to format the strings at run time, its
3959 expected performance is lower than with user-defined tracepoints,
3960 which do not require a conversion to a string.
3961
3962 Taking this into consideration, `tracef()` is useful for some quick
3963 prototyping and debugging, but you should not consider it for any
3964 permanent and serious applicative instrumentation.
3965 ====
3966
3967
3968 [role="since-2.7"]
3969 [[tracelog]]
3970 ==== Use `tracelog()`
3971
3972 The man:tracelog(3) API is very similar to <<tracef,`tracef()`>>, with
3973 the difference that it accepts an additional log level parameter.
3974
3975 The goal of `tracelog()` is to ease the migration from logging to
3976 tracing.
3977
3978 To use `tracelog()` in your application:
3979
3980 . In the C or C++ source files where you need to use `tracelog()`,
3981 include `<lttng/tracelog.h>`:
3982 +
3983 --
3984 [source,c]
3985 ----
3986 #include <lttng/tracelog.h>
3987 ----
3988 --
3989
3990 . In the application's source code, use `tracelog()` like you would use
3991 man:printf(3), except for the first parameter which is the log
3992 level:
3993 +
3994 --
3995 [source,c]
3996 ----
3997 /* ... */
3998
3999 tracelog(TRACE_WARNING, "my message: %d (%s)",
4000 my_integer, my_string);
4001
4002 /* ... */
4003 ----
4004 --
4005 +
4006 See man:lttng-ust(3) for a list of available log level names.
4007
4008 . Link your application with `liblttng-ust`:
4009 +
4010 --
4011 [role="term"]
4012 ----
4013 $ gcc -o app app.c -llttng-ust
4014 ----
4015 --
4016
4017 To trace the events that `tracelog()` calls emit with a log level
4018 _as severe as_ a specific log level:
4019
4020 * <<enabling-disabling-events,Create an event rule>> which matches the
4021 `lttng_ust_tracelog:*` event name and a minimum level
4022 of severity:
4023 +
4024 --
4025 [role="term"]
4026 ----
4027 $ lttng enable-event --userspace 'lttng_ust_tracelog:*'
4028 --loglevel=TRACE_WARNING
4029 ----
4030 --
4031
4032 To trace the events that `tracelog()` calls emit with a
4033 _specific log level_:
4034
4035 * Create an event rule which matches the `lttng_ust_tracelog:*`
4036 event name and a specific log level:
4037 +
4038 --
4039 [role="term"]
4040 ----
4041 $ lttng enable-event --userspace 'lttng_ust_tracelog:*'
4042 --loglevel-only=TRACE_INFO
4043 ----
4044 --
4045
4046
4047 [[prebuilt-ust-helpers]]
4048 === Prebuilt user space tracing helpers
4049
4050 The LTTng-UST package provides a few helpers in the form or preloadable
4051 shared objects which automatically instrument system functions and
4052 calls.
4053
4054 The helper shared objects are normally found in dir:{/usr/lib}. If you
4055 built LTTng-UST <<building-from-source,from source>>, they are probably
4056 located in dir:{/usr/local/lib}.
4057
4058 The installed user space tracing helpers in LTTng-UST{nbsp}{revision}
4059 are:
4060
4061 path:{liblttng-ust-libc-wrapper.so}::
4062 path:{liblttng-ust-pthread-wrapper.so}::
4063 <<liblttng-ust-libc-pthread-wrapper,C{nbsp}standard library
4064 memory and POSIX threads function tracing>>.
4065
4066 path:{liblttng-ust-cyg-profile.so}::
4067 path:{liblttng-ust-cyg-profile-fast.so}::
4068 <<liblttng-ust-cyg-profile,Function entry and exit tracing>>.
4069
4070 path:{liblttng-ust-dl.so}::
4071 <<liblttng-ust-dl,Dynamic linker tracing>>.
4072
4073 To use a user space tracing helper with any user application:
4074
4075 * Preload the helper shared object when you start the application:
4076 +
4077 --
4078 [role="term"]
4079 ----
4080 $ LD_PRELOAD=liblttng-ust-libc-wrapper.so my-app
4081 ----
4082 --
4083 +
4084 You can preload more than one helper:
4085 +
4086 --
4087 [role="term"]
4088 ----
4089 $ LD_PRELOAD=liblttng-ust-libc-wrapper.so:liblttng-ust-dl.so my-app
4090 ----
4091 --
4092
4093
4094 [role="since-2.3"]
4095 [[liblttng-ust-libc-pthread-wrapper]]
4096 ==== Instrument C standard library memory and POSIX threads functions
4097
4098 The path:{liblttng-ust-libc-wrapper.so} and
4099 path:{liblttng-ust-pthread-wrapper.so} helpers
4100 add instrumentation to some C standard library and POSIX
4101 threads functions.
4102
4103 [role="growable"]
4104 .Functions instrumented by preloading path:{liblttng-ust-libc-wrapper.so}.
4105 |====
4106 |TP provider name |TP name |Instrumented function
4107
4108 .6+|`lttng_ust_libc` |`malloc` |man:malloc(3)
4109 |`calloc` |man:calloc(3)
4110 |`realloc` |man:realloc(3)
4111 |`free` |man:free(3)
4112 |`memalign` |man:memalign(3)
4113 |`posix_memalign` |man:posix_memalign(3)
4114 |====
4115
4116 [role="growable"]
4117 .Functions instrumented by preloading path:{liblttng-ust-pthread-wrapper.so}.
4118 |====
4119 |TP provider name |TP name |Instrumented function
4120
4121 .4+|`lttng_ust_pthread` |`pthread_mutex_lock_req` |man:pthread_mutex_lock(3p) (request time)
4122 |`pthread_mutex_lock_acq` |man:pthread_mutex_lock(3p) (acquire time)
4123 |`pthread_mutex_trylock` |man:pthread_mutex_trylock(3p)
4124 |`pthread_mutex_unlock` |man:pthread_mutex_unlock(3p)
4125 |====
4126
4127 When you preload the shared object, it replaces the functions listed
4128 in the previous tables by wrappers which contain tracepoints and call
4129 the replaced functions.
4130
4131
4132 [[liblttng-ust-cyg-profile]]
4133 ==== Instrument function entry and exit
4134
4135 The path:{liblttng-ust-cyg-profile*.so} helpers can add instrumentation
4136 to the entry and exit points of functions.
4137
4138 man:gcc(1) and man:clang(1) have an option named
4139 https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html[`-finstrument-functions`]
4140 which generates instrumentation calls for entry and exit to functions.
4141 The LTTng-UST function tracing helpers,
4142 path:{liblttng-ust-cyg-profile.so} and
4143 path:{liblttng-ust-cyg-profile-fast.so}, take advantage of this feature
4144 to add tracepoints to the two generated functions (which contain
4145 `cyg_profile` in their names, hence the helper's name).
4146
4147 To use the LTTng-UST function tracing helper, the source files to
4148 instrument must be built using the `-finstrument-functions` compiler
4149 flag.
4150
4151 There are two versions of the LTTng-UST function tracing helper:
4152
4153 * **path:{liblttng-ust-cyg-profile-fast.so}** is a lightweight variant
4154 that you should only use when it can be _guaranteed_ that the
4155 complete event stream is recorded without any lost event record.
4156 Any kind of duplicate information is left out.
4157 +
4158 Assuming no event record is lost, having only the function addresses on
4159 entry is enough to create a call graph, since an event record always
4160 contains the ID of the CPU that generated it.
4161 +
4162 You can use a tool like man:addr2line(1) to convert function addresses
4163 back to source file names and line numbers.
4164
4165 * **path:{liblttng-ust-cyg-profile.so}** is a more robust variant
4166 which also works in use cases where event records might get discarded or
4167 not recorded from application startup.
4168 In these cases, the trace analyzer needs more information to be
4169 able to reconstruct the program flow.
4170
4171 See man:lttng-ust-cyg-profile(3) to learn more about the instrumentation
4172 points of this helper.
4173
4174 All the tracepoints that this helper provides have the
4175 log level `TRACE_DEBUG_FUNCTION` (see man:lttng-ust(3)).
4176
4177 TIP: It's sometimes a good idea to limit the number of source files that
4178 you compile with the `-finstrument-functions` option to prevent LTTng
4179 from writing an excessive amount of trace data at run time. When using
4180 man:gcc(1), you can use the
4181 `-finstrument-functions-exclude-function-list` option to avoid
4182 instrument entries and exits of specific function names.
4183
4184
4185 [role="since-2.4"]
4186 [[liblttng-ust-dl]]
4187 ==== Instrument the dynamic linker
4188
4189 The path:{liblttng-ust-dl.so} helper adds instrumentation to the
4190 man:dlopen(3) and man:dlclose(3) function calls.
4191
4192 See man:lttng-ust-dl(3) to learn more about the instrumentation points
4193 of this helper.
4194
4195
4196 [role="since-2.4"]
4197 [[java-application]]
4198 === User space Java agent
4199
4200 You can instrument any Java application which uses one of the following
4201 logging frameworks:
4202
4203 * The https://docs.oracle.com/javase/7/docs/api/java/util/logging/package-summary.html[**`java.util.logging`**]
4204 (JUL) core logging facilities.
4205 * http://logging.apache.org/log4j/1.2/[**Apache log4j 1.2**], since
4206 LTTng 2.6. Note that Apache Log4j{nbsp}2 is not supported.
4207
4208 [role="img-100"]
4209 .LTTng-UST Java agent imported by a Java application.
4210 image::java-app.png[]
4211
4212 Note that the methods described below are new in LTTng{nbsp}{revision}.
4213 Previous LTTng versions use another technique.
4214
4215 NOTE: We use http://openjdk.java.net/[OpenJDK]{nbsp}8 for development
4216 and https://ci.lttng.org/[continuous integration], thus this version is
4217 directly supported. However, the LTTng-UST Java agent is also tested
4218 with OpenJDK{nbsp}7.
4219
4220
4221 [role="since-2.8"]
4222 [[jul]]
4223 ==== Use the LTTng-UST Java agent for `java.util.logging`
4224
4225 To use the LTTng-UST Java agent in a Java application which uses
4226 `java.util.logging` (JUL):
4227
4228 . In the Java application's source code, import the LTTng-UST
4229 log handler package for `java.util.logging`:
4230 +
4231 --
4232 [source,java]
4233 ----
4234 import org.lttng.ust.agent.jul.LttngLogHandler;
4235 ----
4236 --
4237
4238 . Create an LTTng-UST JUL log handler:
4239 +
4240 --
4241 [source,java]
4242 ----
4243 Handler lttngUstLogHandler = new LttngLogHandler();
4244 ----
4245 --
4246
4247 . Add this handler to the JUL loggers which should emit LTTng events:
4248 +
4249 --
4250 [source,java]
4251 ----
4252 Logger myLogger = Logger.getLogger("some-logger");
4253
4254 myLogger.addHandler(lttngUstLogHandler);
4255 ----
4256 --
4257
4258 . Use `java.util.logging` log statements and configuration as usual.
4259 The loggers with an attached LTTng-UST log handler can emit
4260 LTTng events.
4261
4262 . Before exiting the application, remove the LTTng-UST log handler from
4263 the loggers attached to it and call its `close()` method:
4264 +
4265 --
4266 [source,java]
4267 ----
4268 myLogger.removeHandler(lttngUstLogHandler);
4269 lttngUstLogHandler.close();
4270 ----
4271 --
4272 +
4273 This is not strictly necessary, but it is recommended for a clean
4274 disposal of the handler's resources.
4275
4276 . Include the LTTng-UST Java agent's common and JUL-specific JAR files,
4277 path:{lttng-ust-agent-common.jar} and path:{lttng-ust-agent-jul.jar},
4278 in the
4279 https://docs.oracle.com/javase/tutorial/essential/environment/paths.html[class
4280 path] when you build the Java application.
4281 +
4282 The JAR files are typically located in dir:{/usr/share/java}.
4283 +
4284 IMPORTANT: The LTTng-UST Java agent must be
4285 <<installing-lttng,installed>> for the logging framework your
4286 application uses.
4287
4288 .Use the LTTng-UST Java agent for `java.util.logging`.
4289 ====
4290 [source,java]
4291 .path:{Test.java}
4292 ----
4293 import java.io.IOException;
4294 import java.util.logging.Handler;
4295 import java.util.logging.Logger;
4296 import org.lttng.ust.agent.jul.LttngLogHandler;
4297
4298 public class Test
4299 {
4300 private static final int answer = 42;
4301
4302 public static void main(String[] argv) throws Exception
4303 {
4304 // Create a logger
4305 Logger logger = Logger.getLogger("jello");
4306
4307 // Create an LTTng-UST log handler
4308 Handler lttngUstLogHandler = new LttngLogHandler();
4309
4310 // Add the LTTng-UST log handler to our logger
4311 logger.addHandler(lttngUstLogHandler);
4312
4313 // Log at will!
4314 logger.info("some info");
4315 logger.warning("some warning");
4316 Thread.sleep(500);
4317 logger.finer("finer information; the answer is " + answer);
4318 Thread.sleep(123);
4319 logger.severe("error!");
4320
4321 // Not mandatory, but cleaner
4322 logger.removeHandler(lttngUstLogHandler);
4323 lttngUstLogHandler.close();
4324 }
4325 }
4326 ----
4327
4328 Build this example:
4329
4330 [role="term"]
4331 ----
4332 $ javac -cp /usr/share/java/jarpath/lttng-ust-agent-common.jar:/usr/share/java/jarpath/lttng-ust-agent-jul.jar Test.java
4333 ----
4334
4335 <<creating-destroying-tracing-sessions,Create a tracing session>>,
4336 <<enabling-disabling-events,create an event rule>> matching the
4337 `jello` JUL logger, and <<basic-tracing-session-control,start tracing>>:
4338
4339 [role="term"]
4340 ----
4341 $ lttng create
4342 $ lttng enable-event --jul jello
4343 $ lttng start
4344 ----
4345
4346 Run the compiled class:
4347
4348 [role="term"]
4349 ----
4350 $ java -cp /usr/share/java/jarpath/lttng-ust-agent-common.jar:/usr/share/java/jarpath/lttng-ust-agent-jul.jar:. Test
4351 ----
4352
4353 <<basic-tracing-session-control,Stop tracing>> and inspect the
4354 recorded events:
4355
4356 [role="term"]
4357 ----
4358 $ lttng stop
4359 $ lttng view
4360 ----
4361 ====
4362
4363 In the resulting trace, an <<event,event record>> generated by a Java
4364 application using `java.util.logging` is named `lttng_jul:event` and
4365 has the following fields:
4366
4367 `msg`::
4368 Log record's message.
4369
4370 `logger_name`::
4371 Logger name.
4372
4373 `class_name`::
4374 Name of the class in which the log statement was executed.
4375
4376 `method_name`::
4377 Name of the method in which the log statement was executed.
4378
4379 `long_millis`::
4380 Logging time (timestamp in milliseconds).
4381
4382 `int_loglevel`::
4383 Log level integer value.
4384
4385 `int_threadid`::
4386 ID of the thread in which the log statement was executed.
4387
4388 You can use the opt:lttng-enable-event(1):--loglevel or
4389 opt:lttng-enable-event(1):--loglevel-only option of the
4390 man:lttng-enable-event(1) command to target a range of JUL log levels
4391 or a specific JUL log level.
4392
4393
4394 [role="since-2.8"]
4395 [[log4j]]
4396 ==== Use the LTTng-UST Java agent for Apache log4j
4397
4398 To use the LTTng-UST Java agent in a Java application which uses
4399 Apache log4j 1.2:
4400
4401 . In the Java application's source code, import the LTTng-UST
4402 log appender package for Apache log4j:
4403 +
4404 --
4405 [source,java]
4406 ----
4407 import org.lttng.ust.agent.log4j.LttngLogAppender;
4408 ----
4409 --
4410
4411 . Create an LTTng-UST log4j log appender:
4412 +
4413 --
4414 [source,java]
4415 ----
4416 Appender lttngUstLogAppender = new LttngLogAppender();
4417 ----
4418 --
4419
4420 . Add this appender to the log4j loggers which should emit LTTng events:
4421 +
4422 --
4423 [source,java]
4424 ----
4425 Logger myLogger = Logger.getLogger("some-logger");
4426
4427 myLogger.addAppender(lttngUstLogAppender);
4428 ----
4429 --
4430
4431 . Use Apache log4j log statements and configuration as usual. The
4432 loggers with an attached LTTng-UST log appender can emit LTTng events.
4433
4434 . Before exiting the application, remove the LTTng-UST log appender from
4435 the loggers attached to it and call its `close()` method:
4436 +
4437 --
4438 [source,java]
4439 ----
4440 myLogger.removeAppender(lttngUstLogAppender);
4441 lttngUstLogAppender.close();
4442 ----
4443 --
4444 +
4445 This is not strictly necessary, but it is recommended for a clean
4446 disposal of the appender's resources.
4447
4448 . Include the LTTng-UST Java agent's common and log4j-specific JAR
4449 files, path:{lttng-ust-agent-common.jar} and
4450 path:{lttng-ust-agent-log4j.jar}, in the
4451 https://docs.oracle.com/javase/tutorial/essential/environment/paths.html[class
4452 path] when you build the Java application.
4453 +
4454 The JAR files are typically located in dir:{/usr/share/java}.
4455 +
4456 IMPORTANT: The LTTng-UST Java agent must be
4457 <<installing-lttng,installed>> for the logging framework your
4458 application uses.
4459
4460 .Use the LTTng-UST Java agent for Apache log4j.
4461 ====
4462 [source,java]
4463 .path:{Test.java}
4464 ----
4465 import org.apache.log4j.Appender;
4466 import org.apache.log4j.Logger;
4467 import org.lttng.ust.agent.log4j.LttngLogAppender;
4468
4469 public class Test
4470 {
4471 private static final int answer = 42;
4472
4473 public static void main(String[] argv) throws Exception
4474 {
4475 // Create a logger
4476 Logger logger = Logger.getLogger("jello");
4477
4478 // Create an LTTng-UST log appender
4479 Appender lttngUstLogAppender = new LttngLogAppender();
4480
4481 // Add the LTTng-UST log appender to our logger
4482 logger.addAppender(lttngUstLogAppender);
4483
4484 // Log at will!
4485 logger.info("some info");
4486 logger.warn("some warning");
4487 Thread.sleep(500);
4488 logger.debug("debug information; the answer is " + answer);
4489 Thread.sleep(123);
4490 logger.fatal("error!");
4491
4492 // Not mandatory, but cleaner
4493 logger.removeAppender(lttngUstLogAppender);
4494 lttngUstLogAppender.close();
4495 }
4496 }
4497
4498 ----
4499
4500 Build this example (`$LOG4JPATH` is the path to the Apache log4j JAR
4501 file):
4502
4503 [role="term"]
4504 ----
4505 $ javac -cp /usr/share/java/jarpath/lttng-ust-agent-common.jar:/usr/share/java/jarpath/lttng-ust-agent-log4j.jar:$LOG4JPATH Test.java
4506 ----
4507
4508 <<creating-destroying-tracing-sessions,Create a tracing session>>,
4509 <<enabling-disabling-events,create an event rule>> matching the
4510 `jello` log4j logger, and <<basic-tracing-session-control,start tracing>>:
4511
4512 [role="term"]
4513 ----
4514 $ lttng create
4515 $ lttng enable-event --log4j jello
4516 $ lttng start
4517 ----
4518
4519 Run the compiled class:
4520
4521 [role="term"]
4522 ----
4523 $ java -cp /usr/share/java/jarpath/lttng-ust-agent-common.jar:/usr/share/java/jarpath/lttng-ust-agent-log4j.jar:$LOG4JPATH:. Test
4524 ----
4525
4526 <<basic-tracing-session-control,Stop tracing>> and inspect the
4527 recorded events:
4528
4529 [role="term"]
4530 ----
4531 $ lttng stop
4532 $ lttng view
4533 ----
4534 ====
4535
4536 In the resulting trace, an <<event,event record>> generated by a Java
4537 application using log4j is named `lttng_log4j:event` and
4538 has the following fields:
4539
4540 `msg`::
4541 Log record's message.
4542
4543 `logger_name`::
4544 Logger name.
4545
4546 `class_name`::
4547 Name of the class in which the log statement was executed.
4548
4549 `method_name`::
4550 Name of the method in which the log statement was executed.
4551
4552 `filename`::
4553 Name of the file in which the executed log statement is located.
4554
4555 `line_number`::
4556 Line number at which the log statement was executed.
4557
4558 `timestamp`::
4559 Logging timestamp.
4560
4561 `int_loglevel`::
4562 Log level integer value.
4563
4564 `thread_name`::
4565 Name of the Java thread in which the log statement was executed.
4566
4567 You can use the opt:lttng-enable-event(1):--loglevel or
4568 opt:lttng-enable-event(1):--loglevel-only option of the
4569 man:lttng-enable-event(1) command to target a range of Apache log4j log levels
4570 or a specific log4j log level.
4571
4572
4573 [role="since-2.8"]
4574 [[java-application-context]]
4575 ==== Provide application-specific context fields in a Java application
4576
4577 A Java application-specific context field is a piece of state provided
4578 by the application which <<adding-context,you can add>>, using the
4579 man:lttng-add-context(1) command, to each <<event,event record>>
4580 produced by the log statements of this application.
4581
4582 For example, a given object might have a current request ID variable.
4583 You can create a context information retriever for this object and
4584 assign a name to this current request ID. You can then, using the
4585 man:lttng-add-context(1) command, add this context field by name to
4586 the JUL or log4j <<channel,channel>>.
4587
4588 To provide application-specific context fields in a Java application:
4589
4590 . In the Java application's source code, import the LTTng-UST
4591 Java agent context classes and interfaces:
4592 +
4593 --
4594 [source,java]
4595 ----
4596 import org.lttng.ust.agent.context.ContextInfoManager;
4597 import org.lttng.ust.agent.context.IContextInfoRetriever;
4598 ----
4599 --
4600
4601 . Create a context information retriever class, that is, a class which
4602 implements the `IContextInfoRetriever` interface:
4603 +
4604 --
4605 [source,java]
4606 ----
4607 class MyContextInfoRetriever implements IContextInfoRetriever
4608 {
4609 @Override
4610 public Object retrieveContextInfo(String key)
4611 {
4612 if (key.equals("intCtx")) {
4613 return (short) 17;
4614 } else if (key.equals("strContext")) {
4615 return "context value!";
4616 } else {
4617 return null;
4618 }
4619 }
4620 }
4621 ----
4622 --
4623 +
4624 This `retrieveContextInfo()` method is the only member of the
4625 `IContextInfoRetriever` interface. Its role is to return the current
4626 value of a state by name to create a context field. The names of the
4627 context fields and which state variables they return depends on your
4628 specific scenario.
4629 +
4630 All primitive types and objects are supported as context fields.
4631 When `retrieveContextInfo()` returns an object, the context field
4632 serializer calls its `toString()` method to add a string field to
4633 event records. The method can also return `null`, which means that
4634 no context field is available for the required name.
4635
4636 . Register an instance of your context information retriever class to
4637 the context information manager singleton:
4638 +
4639 --
4640 [source,java]
4641 ----
4642 IContextInfoRetriever cir = new MyContextInfoRetriever();
4643 ContextInfoManager cim = ContextInfoManager.getInstance();
4644 cim.registerContextInfoRetriever("retrieverName", cir);
4645 ----
4646 --
4647
4648 . Before exiting the application, remove your context information
4649 retriever from the context information manager singleton:
4650 +
4651 --
4652 [source,java]
4653 ----
4654 ContextInfoManager cim = ContextInfoManager.getInstance();
4655 cim.unregisterContextInfoRetriever("retrieverName");
4656 ----
4657 --
4658 +
4659 This is not strictly necessary, but it is recommended for a clean
4660 disposal of some manager's resources.
4661
4662 . Build your Java application with LTTng-UST Java agent support as
4663 usual, following the procedure for either the <<jul,JUL>> or
4664 <<log4j,Apache log4j>> framework.
4665
4666
4667 .Provide application-specific context fields in a Java application.
4668 ====
4669 [source,java]
4670 .path:{Test.java}
4671 ----
4672 import java.util.logging.Handler;
4673 import java.util.logging.Logger;
4674 import org.lttng.ust.agent.jul.LttngLogHandler;
4675 import org.lttng.ust.agent.context.ContextInfoManager;
4676 import org.lttng.ust.agent.context.IContextInfoRetriever;
4677
4678 public class Test
4679 {
4680 // Our context information retriever class
4681 private static class MyContextInfoRetriever
4682 implements IContextInfoRetriever
4683 {
4684 @Override
4685 public Object retrieveContextInfo(String key) {
4686 if (key.equals("intCtx")) {
4687 return (short) 17;
4688 } else if (key.equals("strContext")) {
4689 return "context value!";
4690 } else {
4691 return null;
4692 }
4693 }
4694 }
4695
4696 private static final int answer = 42;
4697
4698 public static void main(String args[]) throws Exception
4699 {
4700 // Get the context information manager instance
4701 ContextInfoManager cim = ContextInfoManager.getInstance();
4702
4703 // Create and register our context information retriever
4704 IContextInfoRetriever cir = new MyContextInfoRetriever();
4705 cim.registerContextInfoRetriever("myRetriever", cir);
4706
4707 // Create a logger
4708 Logger logger = Logger.getLogger("jello");
4709
4710 // Create an LTTng-UST log handler
4711 Handler lttngUstLogHandler = new LttngLogHandler();
4712
4713 // Add the LTTng-UST log handler to our logger
4714 logger.addHandler(lttngUstLogHandler);
4715
4716 // Log at will!
4717 logger.info("some info");
4718 logger.warning("some warning");
4719 Thread.sleep(500);
4720 logger.finer("finer information; the answer is " + answer);
4721 Thread.sleep(123);
4722 logger.severe("error!");
4723
4724 // Not mandatory, but cleaner
4725 logger.removeHandler(lttngUstLogHandler);
4726 lttngUstLogHandler.close();
4727 cim.unregisterContextInfoRetriever("myRetriever");
4728 }
4729 }
4730 ----
4731
4732 Build this example:
4733
4734 [role="term"]
4735 ----
4736 $ javac -cp /usr/share/java/jarpath/lttng-ust-agent-common.jar:/usr/share/java/jarpath/lttng-ust-agent-jul.jar Test.java
4737 ----
4738
4739 <<creating-destroying-tracing-sessions,Create a tracing session>>
4740 and <<enabling-disabling-events,create an event rule>> matching the
4741 `jello` JUL logger:
4742
4743 [role="term"]
4744 ----
4745 $ lttng create
4746 $ lttng enable-event --jul jello
4747 ----
4748
4749 <<adding-context,Add the application-specific context fields>> to the
4750 JUL channel:
4751
4752 [role="term"]
4753 ----
4754 $ lttng add-context --jul --type='$app.myRetriever:intCtx'
4755 $ lttng add-context --jul --type='$app.myRetriever:strContext'
4756 ----
4757
4758 <<basic-tracing-session-control,Start tracing>>:
4759
4760 [role="term"]
4761 ----
4762 $ lttng start
4763 ----
4764
4765 Run the compiled class:
4766
4767 [role="term"]
4768 ----
4769 $ java -cp /usr/share/java/jarpath/lttng-ust-agent-common.jar:/usr/share/java/jarpath/lttng-ust-agent-jul.jar:. Test
4770 ----
4771
4772 <<basic-tracing-session-control,Stop tracing>> and inspect the
4773 recorded events:
4774
4775 [role="term"]
4776 ----
4777 $ lttng stop
4778 $ lttng view
4779 ----
4780 ====
4781
4782
4783 [role="since-2.7"]
4784 [[python-application]]
4785 === User space Python agent
4786
4787 You can instrument a Python 2 or Python 3 application which uses the
4788 standard https://docs.python.org/3/library/logging.html[`logging`]
4789 package.
4790
4791 Each log statement emits an LTTng event once the
4792 application module imports the
4793 <<lttng-ust-agents,LTTng-UST Python agent>> package.
4794
4795 [role="img-100"]
4796 .A Python application importing the LTTng-UST Python agent.
4797 image::python-app.png[]
4798
4799 To use the LTTng-UST Python agent:
4800
4801 . In the Python application's source code, import the LTTng-UST Python
4802 agent:
4803 +
4804 --
4805 [source,python]
4806 ----
4807 import lttngust
4808 ----
4809 --
4810 +
4811 The LTTng-UST Python agent automatically adds its logging handler to the
4812 root logger at import time.
4813 +
4814 Any log statement that the application executes before this import does
4815 not emit an LTTng event.
4816 +
4817 IMPORTANT: The LTTng-UST Python agent must be
4818 <<installing-lttng,installed>>.
4819
4820 . Use log statements and logging configuration as usual.
4821 Since the LTTng-UST Python agent adds a handler to the _root_
4822 logger, you can trace any log statement from any logger.
4823
4824 .Use the LTTng-UST Python agent.
4825 ====
4826 [source,python]
4827 .path:{test.py}
4828 ----
4829 import lttngust
4830 import logging
4831 import time
4832
4833
4834 def example():
4835 logging.basicConfig()
4836 logger = logging.getLogger('my-logger')
4837
4838 while True:
4839 logger.debug('debug message')
4840 logger.info('info message')
4841 logger.warn('warn message')
4842 logger.error('error message')
4843 logger.critical('critical message')
4844 time.sleep(1)
4845
4846
4847 if __name__ == '__main__':
4848 example()
4849 ----
4850
4851 NOTE: `logging.basicConfig()`, which adds to the root logger a basic
4852 logging handler which prints to the standard error stream, is not
4853 strictly required for LTTng-UST tracing to work, but in versions of
4854 Python preceding 3.2, you could see a warning message which indicates
4855 that no handler exists for the logger `my-logger`.
4856
4857 <<creating-destroying-tracing-sessions,Create a tracing session>>,
4858 <<enabling-disabling-events,create an event rule>> matching the
4859 `my-logger` Python logger, and <<basic-tracing-session-control,start
4860 tracing>>:
4861
4862 [role="term"]
4863 ----
4864 $ lttng create
4865 $ lttng enable-event --python my-logger
4866 $ lttng start
4867 ----
4868
4869 Run the Python script:
4870
4871 [role="term"]
4872 ----
4873 $ python test.py
4874 ----
4875
4876 <<basic-tracing-session-control,Stop tracing>> and inspect the recorded
4877 events:
4878
4879 [role="term"]
4880 ----
4881 $ lttng stop
4882 $ lttng view
4883 ----
4884 ====
4885
4886 In the resulting trace, an <<event,event record>> generated by a Python
4887 application is named `lttng_python:event` and has the following fields:
4888
4889 `asctime`::
4890 Logging time (string).
4891
4892 `msg`::
4893 Log record's message.
4894
4895 `logger_name`::
4896 Logger name.
4897
4898 `funcName`::
4899 Name of the function in which the log statement was executed.
4900
4901 `lineno`::
4902 Line number at which the log statement was executed.
4903
4904 `int_loglevel`::
4905 Log level integer value.
4906
4907 `thread`::
4908 ID of the Python thread in which the log statement was executed.
4909
4910 `threadName`::
4911 Name of the Python thread in which the log statement was executed.
4912
4913 You can use the opt:lttng-enable-event(1):--loglevel or
4914 opt:lttng-enable-event(1):--loglevel-only option of the
4915 man:lttng-enable-event(1) command to target a range of Python log levels
4916 or a specific Python log level.
4917
4918 When an application imports the LTTng-UST Python agent, the agent tries
4919 to register to a <<lttng-sessiond,session daemon>>. Note that you must
4920 <<start-sessiond,start the session daemon>> _before_ you run the Python
4921 application. If a session daemon is found, the agent tries to register
4922 to it during 5{nbsp}seconds, after which the application continues
4923 without LTTng tracing support. You can override this timeout value with
4924 the env:LTTNG_UST_PYTHON_REGISTER_TIMEOUT environment variable
4925 (milliseconds).
4926
4927 If the session daemon stops while a Python application with an imported
4928 LTTng-UST Python agent runs, the agent retries to connect and to
4929 register to a session daemon every 3{nbsp}seconds. You can override this
4930 delay with the env:LTTNG_UST_PYTHON_REGISTER_RETRY_DELAY environment
4931 variable.
4932
4933
4934 [role="since-2.5"]
4935 [[proc-lttng-logger-abi]]
4936 === LTTng logger
4937
4938 The `lttng-tracer` Linux kernel module, part of
4939 <<lttng-modules,LTTng-modules>>, creates the special LTTng logger file
4940 path:{/proc/lttng-logger} when it's loaded. Any application can write
4941 text data to this file to emit an LTTng event.
4942
4943 [role="img-100"]
4944 .An application writes to the LTTng logger file to emit an LTTng event.
4945 image::lttng-logger.png[]
4946
4947 The LTTng logger is the quickest method--not the most efficient,
4948 however--to add instrumentation to an application. It is designed
4949 mostly to instrument shell scripts:
4950
4951 [role="term"]
4952 ----
4953 $ echo "Some message, some $variable" > /proc/lttng-logger
4954 ----
4955
4956 Any event that the LTTng logger emits is named `lttng_logger` and
4957 belongs to the Linux kernel <<domain,tracing domain>>. However, unlike
4958 other instrumentation points in the kernel tracing domain, **any Unix
4959 user** can <<enabling-disabling-events,create an event rule>> which
4960 matches its event name, not only the root user or users in the
4961 <<tracing-group,tracing group>>.
4962
4963 To use the LTTng logger:
4964
4965 * From any application, write text data to the path:{/proc/lttng-logger}
4966 file.
4967
4968 The `msg` field of `lttng_logger` event records contains the
4969 recorded message.
4970
4971 NOTE: The maximum message length of an LTTng logger event is
4972 1024{nbsp}bytes. Writing more than this makes the LTTng logger emit more
4973 than one event to contain the remaining data.
4974
4975 You should not use the LTTng logger to trace a user application which
4976 can be instrumented in a more efficient way, namely:
4977
4978 * <<c-application,C and $$C++$$ applications>>.
4979 * <<java-application,Java applications>>.
4980 * <<python-application,Python applications>>.
4981
4982 .Use the LTTng logger.
4983 ====
4984 [source,bash]
4985 .path:{test.bash}
4986 ----
4987 echo 'Hello, World!' > /proc/lttng-logger
4988 sleep 2
4989 df --human-readable --print-type / > /proc/lttng-logger
4990 ----
4991
4992 <<creating-destroying-tracing-sessions,Create a tracing session>>,
4993 <<enabling-disabling-events,create an event rule>> matching the
4994 `lttng_logger` Linux kernel tracepoint, and
4995 <<basic-tracing-session-control,start tracing>>:
4996
4997 [role="term"]
4998 ----
4999 $ lttng create
5000 $ lttng enable-event --kernel lttng_logger
5001 $ lttng start
5002 ----
5003
5004 Run the Bash script:
5005
5006 [role="term"]
5007 ----
5008 $ bash test.bash
5009 ----
5010
5011 <<basic-tracing-session-control,Stop tracing>> and inspect the recorded
5012 events:
5013
5014 [role="term"]
5015 ----
5016 $ lttng stop
5017 $ lttng view
5018 ----
5019 ====
5020
5021
5022 [[instrumenting-linux-kernel]]
5023 === LTTng kernel tracepoints
5024
5025 NOTE: This section shows how to _add_ instrumentation points to the
5026 Linux kernel. The kernel's subsystems are already thoroughly
5027 instrumented at strategic places for LTTng when you
5028 <<installing-lttng,install>> the <<lttng-modules,LTTng-modules>>
5029 package.
5030
5031 ////
5032 There are two methods to instrument the Linux kernel:
5033
5034 . <<linux-add-lttng-layer,Add an LTTng layer>> over an existing ftrace
5035 tracepoint which uses the `TRACE_EVENT()` API.
5036 +
5037 Choose this if you want to instrumentation a Linux kernel tree with an
5038 instrumentation point compatible with ftrace, perf, and SystemTap.
5039
5040 . Use an <<linux-lttng-tracepoint-event,LTTng-only approach>> to
5041 instrument an out-of-tree kernel module.
5042 +
5043 Choose this if you don't need ftrace, perf, or SystemTap support.
5044 ////
5045
5046
5047 [[linux-add-lttng-layer]]
5048 ==== [[instrumenting-linux-kernel-itself]][[mainline-trace-event]][[lttng-adaptation-layer]]Add an LTTng layer to an existing ftrace tracepoint
5049
5050 This section shows how to add an LTTng layer to existing ftrace
5051 instrumentation using the `TRACE_EVENT()` API.
5052
5053 This section does not document the `TRACE_EVENT()` macro. You can
5054 read the following articles to learn more about this API:
5055
5056 * http://lwn.net/Articles/379903/[Using the TRACE_EVENT() macro (Part 1)]
5057 * http://lwn.net/Articles/381064/[Using the TRACE_EVENT() macro (Part 2)]
5058 * http://lwn.net/Articles/383362/[Using the TRACE_EVENT() macro (Part 3)]
5059
5060 The following procedure assumes that your ftrace tracepoints are
5061 correctly defined in their own header and that they are created in
5062 one source file using the `CREATE_TRACE_POINTS` definition.
5063
5064 To add an LTTng layer over an existing ftrace tracepoint:
5065
5066 . Make sure the following kernel configuration options are
5067 enabled:
5068 +
5069 --
5070 * `CONFIG_MODULES`
5071 * `CONFIG_KALLSYMS`
5072 * `CONFIG_HIGH_RES_TIMERS`
5073 * `CONFIG_TRACEPOINTS`
5074 --
5075
5076 . Build the Linux source tree with your custom ftrace tracepoints.
5077 . Boot the resulting Linux image on your target system.
5078 +
5079 Confirm that the tracepoints exist by looking for their names in the
5080 dir:{/sys/kernel/debug/tracing/events/subsys} directory, where `subsys`
5081 is your subsystem's name.
5082
5083 . Get a copy of the latest LTTng-modules{nbsp}{revision}:
5084 +
5085 --
5086 [role="term"]
5087 ----
5088 $ cd $(mktemp -d) &&
5089 wget http://lttng.org/files/lttng-modules/lttng-modules-latest-2.9.tar.bz2 &&
5090 tar -xf lttng-modules-latest-2.9.tar.bz2 &&
5091 cd lttng-modules-2.9.*
5092 ----
5093 --
5094
5095 . In dir:{instrumentation/events/lttng-module}, relative to the root
5096 of the LTTng-modules source tree, create a header file named
5097 +__subsys__.h+ for your custom subsystem +__subsys__+ and write your
5098 LTTng-modules tracepoint definitions using the LTTng-modules
5099 macros in it.
5100 +
5101 Start with this template:
5102 +
5103 --
5104 [source,c]
5105 .path:{instrumentation/events/lttng-module/my_subsys.h}
5106 ----
5107 #undef TRACE_SYSTEM
5108 #define TRACE_SYSTEM my_subsys
5109
5110 #if !defined(_LTTNG_MY_SUBSYS_H) || defined(TRACE_HEADER_MULTI_READ)
5111 #define _LTTNG_MY_SUBSYS_H
5112
5113 #include "../../../probes/lttng-tracepoint-event.h"
5114 #include <linux/tracepoint.h>
5115
5116 LTTNG_TRACEPOINT_EVENT(
5117 /*
5118 * Format is identical to TRACE_EVENT()'s version for the three
5119 * following macro parameters:
5120 */
5121 my_subsys_my_event,
5122 TP_PROTO(int my_int, const char *my_string),
5123 TP_ARGS(my_int, my_string),
5124
5125 /* LTTng-modules specific macros */
5126 TP_FIELDS(
5127 ctf_integer(int, my_int_field, my_int)
5128 ctf_string(my_bar_field, my_bar)
5129 )
5130 )
5131
5132 #endif /* !defined(_LTTNG_MY_SUBSYS_H) || defined(TRACE_HEADER_MULTI_READ) */
5133
5134 #include "../../../probes/define_trace.h"
5135 ----
5136 --
5137 +
5138 The entries in the `TP_FIELDS()` section are the list of fields for the
5139 LTTng tracepoint. This is similar to the `TP_STRUCT__entry()` part of
5140 ftrace's `TRACE_EVENT()` macro.
5141 +
5142 See <<lttng-modules-tp-fields,Tracepoint fields macros>> for a
5143 complete description of the available `ctf_*()` macros.
5144
5145 . Create the LTTng-modules probe's kernel module C source file,
5146 +probes/lttng-probe-__subsys__.c+, where +__subsys__+ is your
5147 subsystem name:
5148 +
5149 --
5150 [source,c]
5151 .path:{probes/lttng-probe-my-subsys.c}
5152 ----
5153 #include <linux/module.h>
5154 #include "../lttng-tracer.h"
5155
5156 /*
5157 * Build-time verification of mismatch between mainline
5158 * TRACE_EVENT() arguments and the LTTng-modules adaptation
5159 * layer LTTNG_TRACEPOINT_EVENT() arguments.
5160 */
5161 #include <trace/events/my_subsys.h>
5162
5163 /* Create LTTng tracepoint probes */
5164 #define LTTNG_PACKAGE_BUILD
5165 #define CREATE_TRACE_POINTS
5166 #define TRACE_INCLUDE_PATH ../instrumentation/events/lttng-module
5167
5168 #include "../instrumentation/events/lttng-module/my_subsys.h"
5169
5170 MODULE_LICENSE("GPL and additional rights");
5171 MODULE_AUTHOR("Your name <your-email>");
5172 MODULE_DESCRIPTION("LTTng my_subsys probes");
5173 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
5174 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
5175 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
5176 LTTNG_MODULES_EXTRAVERSION);
5177 ----
5178 --
5179
5180 . Edit path:{probes/Makefile} and add your new kernel module object
5181 next to the existing ones:
5182 +
5183 --
5184 [source,make]
5185 .path:{probes/Makefile}
5186 ----
5187 # ...
5188
5189 obj-m += lttng-probe-module.o
5190 obj-m += lttng-probe-power.o
5191
5192 obj-m += lttng-probe-my-subsys.o
5193
5194 # ...
5195 ----
5196 --
5197
5198 . Build and install the LTTng kernel modules:
5199 +
5200 --
5201 [role="term"]
5202 ----
5203 $ make KERNELDIR=/path/to/linux
5204 # make modules_install && depmod -a
5205 ----
5206 --
5207 +
5208 Replace `/path/to/linux` with the path to the Linux source tree where
5209 you defined and used tracepoints with ftrace's `TRACE_EVENT()` macro.
5210
5211 Note that you can also use the
5212 <<lttng-tracepoint-event-code,`LTTNG_TRACEPOINT_EVENT_CODE()` macro>>
5213 instead of `LTTNG_TRACEPOINT_EVENT()` to use custom local variables and
5214 C code that need to be executed before the event fields are recorded.
5215
5216 The best way to learn how to use the previous LTTng-modules macros is to
5217 inspect the existing LTTng-modules tracepoint definitions in the
5218 dir:{instrumentation/events/lttng-module} header files. Compare them
5219 with the Linux kernel mainline versions in the
5220 dir:{include/trace/events} directory of the Linux source tree.
5221
5222
5223 [role="since-2.7"]
5224 [[lttng-tracepoint-event-code]]
5225 ===== Use custom C code to access the data for tracepoint fields
5226
5227 Although we recommended to always use the
5228 <<lttng-adaptation-layer,`LTTNG_TRACEPOINT_EVENT()`>> macro to describe
5229 the arguments and fields of an LTTng-modules tracepoint when possible,
5230 sometimes you need a more complex process to access the data that the
5231 tracer records as event record fields. In other words, you need local
5232 variables and multiple C{nbsp}statements instead of simple
5233 argument-based expressions that you pass to the
5234 <<lttng-modules-tp-fields,`ctf_*()` macros of `TP_FIELDS()`>>.
5235
5236 You can use the `LTTNG_TRACEPOINT_EVENT_CODE()` macro instead of
5237 `LTTNG_TRACEPOINT_EVENT()` to declare custom local variables and define
5238 a block of C{nbsp}code to be executed before LTTng records the fields.
5239 The structure of this macro is:
5240
5241 [source,c]
5242 .`LTTNG_TRACEPOINT_EVENT_CODE()` macro syntax.
5243 ----
5244 LTTNG_TRACEPOINT_EVENT_CODE(
5245 /*
5246 * Format identical to the LTTNG_TRACEPOINT_EVENT()
5247 * version for the following three macro parameters:
5248 */
5249 my_subsys_my_event,
5250 TP_PROTO(int my_int, const char *my_string),
5251 TP_ARGS(my_int, my_string),
5252
5253 /* Declarations of custom local variables */
5254 TP_locvar(
5255 int a = 0;
5256 unsigned long b = 0;
5257 const char *name = "(undefined)";
5258 struct my_struct *my_struct;
5259 ),
5260
5261 /*
5262 * Custom code which uses both tracepoint arguments
5263 * (in TP_ARGS()) and local variables (in TP_locvar()).
5264 *
5265 * Local variables are actually members of a structure pointed
5266 * to by the special variable tp_locvar.
5267 */
5268 TP_code(
5269 if (my_int) {
5270 tp_locvar->a = my_int + 17;
5271 tp_locvar->my_struct = get_my_struct_at(tp_locvar->a);
5272 tp_locvar->b = my_struct_compute_b(tp_locvar->my_struct);
5273 tp_locvar->name = my_struct_get_name(tp_locvar->my_struct);
5274 put_my_struct(tp_locvar->my_struct);
5275
5276 if (tp_locvar->b) {
5277 tp_locvar->a = 1;
5278 }
5279 }
5280 ),
5281
5282 /*
5283 * Format identical to the LTTNG_TRACEPOINT_EVENT()
5284 * version for this, except that tp_locvar members can be
5285 * used in the argument expression parameters of
5286 * the ctf_*() macros.
5287 */
5288 TP_FIELDS(
5289 ctf_integer(unsigned long, my_struct_b, tp_locvar->b)
5290 ctf_integer(int, my_struct_a, tp_locvar->a)
5291 ctf_string(my_string_field, my_string)
5292 ctf_string(my_struct_name, tp_locvar->name)
5293 )
5294 )
5295 ----
5296
5297 IMPORTANT: The C code defined in `TP_code()` must not have any side
5298 effects when executed. In particular, the code must not allocate
5299 memory or get resources without deallocating this memory or putting
5300 those resources afterwards.
5301
5302
5303 [[instrumenting-linux-kernel-tracing]]
5304 ==== Load and unload a custom probe kernel module
5305
5306 You must load a <<lttng-adaptation-layer,created LTTng-modules probe
5307 kernel module>> in the kernel before it can emit LTTng events.
5308
5309 To load the default probe kernel modules and a custom probe kernel
5310 module:
5311
5312 * Use the opt:lttng-sessiond(8):--extra-kmod-probes option to give extra
5313 probe modules to load when starting a root <<lttng-sessiond,session
5314 daemon>>:
5315 +
5316 --
5317 .Load the `my_subsys`, `usb`, and the default probe modules.
5318 ====
5319 [role="term"]
5320 ----
5321 # lttng-sessiond --extra-kmod-probes=my_subsys,usb
5322 ----
5323 ====
5324 --
5325 +
5326 You only need to pass the subsystem name, not the whole kernel module
5327 name.
5328
5329 To load _only_ a given custom probe kernel module:
5330
5331 * Use the opt:lttng-sessiond(8):--kmod-probes option to give the probe
5332 modules to load when starting a root session daemon:
5333 +
5334 --
5335 .Load only the `my_subsys` and `usb` probe modules.
5336 ====
5337 [role="term"]
5338 ----
5339 # lttng-sessiond --kmod-probes=my_subsys,usb
5340 ----
5341 ====
5342 --
5343
5344 To confirm that a probe module is loaded:
5345
5346 * Use man:lsmod(8):
5347 +
5348 --
5349 [role="term"]
5350 ----
5351 $ lsmod | grep lttng_probe_usb
5352 ----
5353 --
5354
5355 To unload the loaded probe modules:
5356
5357 * Kill the session daemon with `SIGTERM`:
5358 +
5359 --
5360 [role="term"]
5361 ----
5362 # pkill lttng-sessiond
5363 ----
5364 --
5365 +
5366 You can also use man:modprobe(8)'s `--remove` option if the session
5367 daemon terminates abnormally.
5368
5369
5370 [[controlling-tracing]]
5371 == Tracing control
5372
5373 Once an application or a Linux kernel is
5374 <<instrumenting,instrumented>> for LTTng tracing,
5375 you can _trace_ it.
5376
5377 This section is divided in topics on how to use the various
5378 <<plumbing,components of LTTng>>, in particular the <<lttng-cli,cmd:lttng
5379 command-line tool>>, to _control_ the LTTng daemons and tracers.
5380
5381 NOTE: In the following subsections, we refer to an man:lttng(1) command
5382 using its man page name. For example, instead of _Run the `create`
5383 command to..._, we use _Run the man:lttng-create(1) command to..._.
5384
5385
5386 [[start-sessiond]]
5387 === Start a session daemon
5388
5389 In some situations, you need to run a <<lttng-sessiond,session daemon>>
5390 (man:lttng-sessiond(8)) _before_ you can use the man:lttng(1)
5391 command-line tool.
5392
5393 You will see the following error when you run a command while no session
5394 daemon is running:
5395
5396 ----
5397 Error: No session daemon is available
5398 ----
5399
5400 The only command that automatically runs a session daemon is
5401 man:lttng-create(1), which you use to
5402 <<creating-destroying-tracing-sessions,create a tracing session>>. While
5403 this is most of the time the first operation that you do, sometimes it's
5404 not. Some examples are:
5405
5406 * <<list-instrumentation-points,List the available instrumentation points>>.
5407 * <<saving-loading-tracing-session,Load a tracing session configuration>>.
5408
5409 [[tracing-group]] Each Unix user must have its own running session
5410 daemon to trace user applications. The session daemon that the root user
5411 starts is the only one allowed to control the LTTng kernel tracer. Users
5412 that are part of the _tracing group_ can control the root session
5413 daemon. The default tracing group name is `tracing`; you can set it to
5414 something else with the opt:lttng-sessiond(8):--group option when you
5415 start the root session daemon.
5416
5417 To start a user session daemon:
5418
5419 * Run man:lttng-sessiond(8):
5420 +
5421 --
5422 [role="term"]
5423 ----
5424 $ lttng-sessiond --daemonize
5425 ----
5426 --
5427
5428 To start the root session daemon:
5429
5430 * Run man:lttng-sessiond(8) as the root user:
5431 +
5432 --
5433 [role="term"]
5434 ----
5435 # lttng-sessiond --daemonize
5436 ----
5437 --
5438
5439 In both cases, remove the opt:lttng-sessiond(8):--daemonize option to
5440 start the session daemon in foreground.
5441
5442 To stop a session daemon, use man:kill(1) on its process ID (standard
5443 `TERM` signal).
5444
5445 Note that some Linux distributions could manage the LTTng session daemon
5446 as a service. In this case, you should use the service manager to
5447 start, restart, and stop session daemons.
5448
5449
5450 [[creating-destroying-tracing-sessions]]
5451 === Create and destroy a tracing session
5452
5453 Almost all the LTTng control operations happen in the scope of
5454 a <<tracing-session,tracing session>>, which is the dialogue between the
5455 <<lttng-sessiond,session daemon>> and you.
5456
5457 To create a tracing session with a generated name:
5458
5459 * Use the man:lttng-create(1) command:
5460 +
5461 --
5462 [role="term"]
5463 ----
5464 $ lttng create
5465 ----
5466 --
5467
5468 The created tracing session's name is `auto` followed by the
5469 creation date.
5470
5471 To create a tracing session with a specific name:
5472
5473 * Use the optional argument of the man:lttng-create(1) command:
5474 +
5475 --
5476 [role="term"]
5477 ----
5478 $ lttng create my-session
5479 ----
5480 --
5481 +
5482 Replace `my-session` with the specific tracing session name.
5483
5484 LTTng appends the creation date to the created tracing session's name.
5485
5486 LTTng writes the traces of a tracing session in
5487 +$LTTNG_HOME/lttng-trace/__name__+ by default, where +__name__+ is the
5488 name of the tracing session. Note that the env:LTTNG_HOME environment
5489 variable defaults to `$HOME` if not set.
5490
5491 To output LTTng traces to a non-default location:
5492
5493 * Use the opt:lttng-create(1):--output option of the man:lttng-create(1) command:
5494 +
5495 --
5496 [role="term"]
5497 ----
5498 $ lttng create my-session --output=/tmp/some-directory
5499 ----
5500 --
5501
5502 You may create as many tracing sessions as you wish.
5503
5504 To list all the existing tracing sessions for your Unix user:
5505
5506 * Use the man:lttng-list(1) command:
5507 +
5508 --
5509 [role="term"]
5510 ----
5511 $ lttng list
5512 ----
5513 --
5514
5515 When you create a tracing session, it is set as the _current tracing
5516 session_. The following man:lttng(1) commands operate on the current
5517 tracing session when you don't specify one:
5518
5519 [role="list-3-cols"]
5520 * `add-context`
5521 * `destroy`
5522 * `disable-channel`
5523 * `disable-event`
5524 * `enable-channel`
5525 * `enable-event`
5526 * `load`
5527 * `regenerate`
5528 * `save`
5529 * `snapshot`
5530 * `start`
5531 * `stop`
5532 * `track`
5533 * `untrack`
5534 * `view`
5535
5536 To change the current tracing session:
5537
5538 * Use the man:lttng-set-session(1) command:
5539 +
5540 --
5541 [role="term"]
5542 ----
5543 $ lttng set-session new-session
5544 ----
5545 --
5546 +
5547 Replace `new-session` by the name of the new current tracing session.
5548
5549 When you are done tracing in a given tracing session, you can destroy
5550 it. This operation frees the resources taken by the tracing session
5551 to destroy; it does not destroy the trace data that LTTng wrote for
5552 this tracing session.
5553
5554 To destroy the current tracing session:
5555
5556 * Use the man:lttng-destroy(1) command:
5557 +
5558 --
5559 [role="term"]
5560 ----
5561 $ lttng destroy
5562 ----
5563 --
5564
5565
5566 [[list-instrumentation-points]]
5567 === List the available instrumentation points
5568
5569 The <<lttng-sessiond,session daemon>> can query the running instrumented
5570 user applications and the Linux kernel to get a list of available
5571 instrumentation points. For the Linux kernel <<domain,tracing domain>>,
5572 they are tracepoints and system calls. For the user space tracing
5573 domain, they are tracepoints. For the other tracing domains, they are
5574 logger names.
5575
5576 To list the available instrumentation points:
5577
5578 * Use the man:lttng-list(1) command with the requested tracing domain's
5579 option amongst:
5580 +
5581 --
5582 * opt:lttng-list(1):--kernel: Linux kernel tracepoints (your Unix user
5583 must be a root user, or it must be a member of the
5584 <<tracing-group,tracing group>>).
5585 * opt:lttng-list(1):--kernel with opt:lttng-list(1):--syscall: Linux
5586 kernel system calls (your Unix user must be a root user, or it must be
5587 a member of the tracing group).
5588 * opt:lttng-list(1):--userspace: user space tracepoints.
5589 * opt:lttng-list(1):--jul: `java.util.logging` loggers.
5590 * opt:lttng-list(1):--log4j: Apache log4j loggers.
5591 * opt:lttng-list(1):--python: Python loggers.
5592 --
5593
5594 .List the available user space tracepoints.
5595 ====
5596 [role="term"]
5597 ----
5598 $ lttng list --userspace
5599 ----
5600 ====
5601
5602 .List the available Linux kernel system call tracepoints.
5603 ====
5604 [role="term"]
5605 ----
5606 $ lttng list --kernel --syscall
5607 ----
5608 ====
5609
5610
5611 [[enabling-disabling-events]]
5612 === Create and enable an event rule
5613
5614 Once you <<creating-destroying-tracing-sessions,create a tracing
5615 session>>, you can create <<event,event rules>> with the
5616 man:lttng-enable-event(1) command.
5617
5618 You specify each condition with a command-line option. The available
5619 condition options are shown in the following table.
5620
5621 [role="growable",cols="asciidoc,asciidoc,default"]
5622 .Condition command-line options for the man:lttng-enable-event(1) command.
5623 |====
5624 |Option |Description |Applicable tracing domains
5625
5626 |
5627 One of:
5628
5629 . `--syscall`
5630 . +--probe=__ADDR__+
5631 . +--function=__ADDR__+
5632
5633 |
5634 Instead of using the default _tracepoint_ instrumentation type, use:
5635
5636 . A Linux system call.
5637 . A Linux https://lwn.net/Articles/132196/[KProbe] (symbol or address).
5638 . The entry and return points of a Linux function (symbol or address).
5639
5640 |Linux kernel.
5641
5642 |First positional argument.
5643
5644 |
5645 Tracepoint or system call name. In the case of a Linux KProbe or
5646 function, this is a custom name given to the event rule. With the
5647 JUL, log4j, and Python domains, this is a logger name.
5648
5649 With a tracepoint, logger, or system call name, the last character
5650 can be `*` to match anything that remains.
5651
5652 |All.
5653
5654 |
5655 One of:
5656
5657 . +--loglevel=__LEVEL__+
5658 . +--loglevel-only=__LEVEL__+
5659
5660 |
5661 . Match only tracepoints or log statements with a logging level at
5662 least as severe as +__LEVEL__+.
5663 . Match only tracepoints or log statements with a logging level
5664 equal to +__LEVEL__+.
5665
5666 See man:lttng-enable-event(1) for the list of available logging level
5667 names.
5668
5669 |User space, JUL, log4j, and Python.
5670
5671 |+--exclude=__EXCLUSIONS__+
5672
5673 |
5674 When you use a `*` character at the end of the tracepoint or logger
5675 name (first positional argument), exclude the specific names in the
5676 comma-delimited list +__EXCLUSIONS__+.
5677
5678 |
5679 User space, JUL, log4j, and Python.
5680
5681 |+--filter=__EXPR__+
5682
5683 |
5684 Match only events which satisfy the expression +__EXPR__+.
5685
5686 See man:lttng-enable-event(1) to learn more about the syntax of a
5687 filter expression.
5688
5689 |All.
5690
5691 |====
5692
5693 You attach an event rule to a <<channel,channel>> on creation. If you do
5694 not specify the channel with the opt:lttng-enable-event(1):--channel
5695 option, and if the event rule to create is the first in its
5696 <<domain,tracing domain>> for a given tracing session, then LTTng
5697 creates a _default channel_ for you. This default channel is reused in
5698 subsequent invocations of the man:lttng-enable-event(1) command for the
5699 same tracing domain.
5700
5701 An event rule is always enabled at creation time.
5702
5703 The following examples show how you can combine the previous
5704 command-line options to create simple to more complex event rules.
5705
5706 .Create an event rule targetting a Linux kernel tracepoint (default channel).
5707 ====
5708 [role="term"]
5709 ----
5710 $ lttng enable-event --kernel sched_switch
5711 ----
5712 ====
5713
5714 .Create an event rule matching four Linux kernel system calls (default channel).
5715 ====
5716 [role="term"]
5717 ----
5718 $ lttng enable-event --kernel --syscall open,write,read,close
5719 ----
5720 ====
5721
5722 .Create event rules matching tracepoints with filter expressions (default channel).
5723 ====
5724 [role="term"]
5725 ----
5726 $ lttng enable-event --kernel sched_switch --filter='prev_comm == "bash"'
5727 ----
5728
5729 [role="term"]
5730 ----
5731 $ lttng enable-event --kernel --all \
5732 --filter='$ctx.tid == 1988 || $ctx.tid == 1534'
5733 ----
5734
5735 [role="term"]
5736 ----
5737 $ lttng enable-event --jul my_logger \
5738 --filter='$app.retriever:cur_msg_id > 3'
5739 ----
5740
5741 IMPORTANT: Make sure to always quote the filter string when you
5742 use man:lttng(1) from a shell.
5743 ====
5744
5745 .Create an event rule matching any user space tracepoint of a given tracepoint provider with a log level range (default channel).
5746 ====
5747 [role="term"]
5748 ----
5749 $ lttng enable-event --userspace my_app:'*' --loglevel=TRACE_INFO
5750 ----
5751
5752 IMPORTANT: Make sure to always quote the wildcard character when you
5753 use man:lttng(1) from a shell.
5754 ====
5755
5756 .Create an event rule matching multiple Python loggers with a wildcard and with exclusions (default channel).
5757 ====
5758 [role="term"]
5759 ----
5760 $ lttng enable-event --python my-app.'*' \
5761 --exclude='my-app.module,my-app.hello'
5762 ----
5763 ====
5764
5765 .Create an event rule matching any Apache log4j logger with a specific log level (default channel).
5766 ====
5767 [role="term"]
5768 ----
5769 $ lttng enable-event --log4j --all --loglevel-only=LOG4J_WARN
5770 ----
5771 ====
5772
5773 .Create an event rule attached to a specific channel matching a specific user space tracepoint provider and tracepoint.
5774 ====
5775 [role="term"]
5776 ----
5777 $ lttng enable-event --userspace my_app:my_tracepoint --channel=my-channel
5778 ----
5779 ====
5780
5781 The event rules of a given channel form a whitelist: as soon as an
5782 emitted event passes one of them, LTTng can record the event. For
5783 example, an event named `my_app:my_tracepoint` emitted from a user space
5784 tracepoint with a `TRACE_ERROR` log level passes both of the following
5785 rules:
5786
5787 [role="term"]
5788 ----
5789 $ lttng enable-event --userspace my_app:my_tracepoint
5790 $ lttng enable-event --userspace my_app:my_tracepoint \
5791 --loglevel=TRACE_INFO
5792 ----
5793
5794 The second event rule is redundant: the first one includes
5795 the second one.
5796
5797
5798 [[disable-event-rule]]
5799 === Disable an event rule
5800
5801 To disable an event rule that you <<enabling-disabling-events,created>>
5802 previously, use the man:lttng-disable-event(1) command. This command
5803 disables _all_ the event rules (of a given tracing domain and channel)
5804 which match an instrumentation point. The other conditions are not
5805 supported as of LTTng{nbsp}{revision}.
5806
5807 The LTTng tracer does not record an emitted event which passes
5808 a _disabled_ event rule.
5809
5810 .Disable an event rule matching a Python logger (default channel).
5811 ====
5812 [role="term"]
5813 ----
5814 $ lttng disable-event --python my-logger
5815 ----
5816 ====
5817
5818 .Disable an event rule matching all `java.util.logging` loggers (default channel).
5819 ====
5820 [role="term"]
5821 ----
5822 $ lttng disable-event --jul '*'
5823 ----
5824 ====
5825
5826 .Disable _all_ the event rules of the default channel.
5827 ====
5828 The opt:lttng-disable-event(1):--all-events option is not, like the
5829 opt:lttng-enable-event(1):--all option of man:lttng-enable-event(1), the
5830 equivalent of the event name `*` (wildcard): it disables _all_ the event
5831 rules of a given channel.
5832
5833 [role="term"]
5834 ----
5835 $ lttng disable-event --jul --all-events
5836 ----
5837 ====
5838
5839 NOTE: You cannot delete an event rule once you create it.
5840
5841
5842 [[status]]
5843 === Get the status of a tracing session
5844
5845 To get the status of the current tracing session, that is, its
5846 parameters, its channels, event rules, and their attributes:
5847
5848 * Use the man:lttng-status(1) command:
5849 +
5850 --
5851 [role="term"]
5852 ----
5853 $ lttng status
5854 ----
5855 --
5856 +
5857
5858 To get the status of any tracing session:
5859
5860 * Use the man:lttng-list(1) command with the tracing session's name:
5861 +
5862 --
5863 [role="term"]
5864 ----
5865 $ lttng list my-session
5866 ----
5867 --
5868 +
5869 Replace `my-session` with the desired tracing session's name.
5870
5871
5872 [[basic-tracing-session-control]]
5873 === Start and stop a tracing session
5874
5875 Once you <<creating-destroying-tracing-sessions,create a tracing
5876 session>> and
5877 <<enabling-disabling-events,create one or more event rules>>,
5878 you can start and stop the tracers for this tracing session.
5879
5880 To start tracing in the current tracing session:
5881
5882 * Use the man:lttng-start(1) command:
5883 +
5884 --
5885 [role="term"]
5886 ----
5887 $ lttng start
5888 ----
5889 --
5890
5891 LTTng is very flexible: you can launch user applications before
5892 or after the you start the tracers. The tracers only record the events
5893 if they pass enabled event rules and if they occur while the tracers are
5894 started.
5895
5896 To stop tracing in the current tracing session:
5897
5898 * Use the man:lttng-stop(1) command:
5899 +
5900 --
5901 [role="term"]
5902 ----
5903 $ lttng stop
5904 ----
5905 --
5906 +
5907 If there were <<channel-overwrite-mode-vs-discard-mode,lost event
5908 records>> or lost sub-buffers since the last time you ran
5909 man:lttng-start(1), warnings are printed when you run the
5910 man:lttng-stop(1) command.
5911
5912
5913 [[enabling-disabling-channels]]
5914 === Create a channel
5915
5916 Once you create a tracing session, you can create a <<channel,channel>>
5917 with the man:lttng-enable-channel(1) command.
5918
5919 Note that LTTng automatically creates a default channel when, for a
5920 given <<domain,tracing domain>>, no channels exist and you
5921 <<enabling-disabling-events,create>> the first event rule. This default
5922 channel is named `channel0` and its attributes are set to reasonable
5923 values. Therefore, you only need to create a channel when you need
5924 non-default attributes.
5925
5926 You specify each non-default channel attribute with a command-line
5927 option when you use the man:lttng-enable-channel(1) command. The
5928 available command-line options are:
5929
5930 [role="growable",cols="asciidoc,asciidoc"]
5931 .Command-line options for the man:lttng-enable-channel(1) command.
5932 |====
5933 |Option |Description
5934
5935 |`--overwrite`
5936
5937 |
5938 Use the _overwrite_
5939 <<channel-overwrite-mode-vs-discard-mode,event loss mode>> instead of
5940 the default _discard_ mode.
5941
5942 |`--buffers-pid` (user space tracing domain only)
5943
5944 |
5945 Use the per-process <<channel-buffering-schemes,buffering scheme>>
5946 instead of the default per-user buffering scheme.
5947
5948 |+--subbuf-size=__SIZE__+
5949
5950 |
5951 Allocate sub-buffers of +__SIZE__+ bytes (power of two), for each CPU,
5952 either for each Unix user (default), or for each instrumented process.
5953
5954 See <<channel-subbuf-size-vs-subbuf-count,Sub-buffer count and size>>.
5955
5956 |+--num-subbuf=__COUNT__+
5957
5958 |
5959 Allocate +__COUNT__+ sub-buffers (power of two), for each CPU, either
5960 for each Unix user (default), or for each instrumented process.
5961
5962 See <<channel-subbuf-size-vs-subbuf-count,Sub-buffer count and size>>.
5963
5964 |+--tracefile-size=__SIZE__+
5965
5966 |
5967 Set the maximum size of each trace file that this channel writes within
5968 a stream to +__SIZE__+ bytes instead of no maximum.
5969
5970 See <<tracefile-rotation,Trace file count and size>>.
5971
5972 |+--tracefile-count=__COUNT__+
5973
5974 |
5975 Limit the number of trace files that this channel creates to
5976 +__COUNT__+ channels instead of no limit.
5977
5978 See <<tracefile-rotation,Trace file count and size>>.
5979
5980 |+--switch-timer=__PERIODUS__+
5981
5982 |
5983 Set the <<channel-switch-timer,switch timer period>>
5984 to +__PERIODUS__+{nbsp}µs.
5985
5986 |+--read-timer=__PERIODUS__+
5987
5988 |
5989 Set the <<channel-read-timer,read timer period>>
5990 to +__PERIODUS__+{nbsp}µs.
5991
5992 |+--output=__TYPE__+ (Linux kernel tracing domain only)
5993
5994 |
5995 Set the channel's output type to +__TYPE__+, either `mmap` or `splice`.
5996
5997 |====
5998
5999 You can only create a channel in the Linux kernel and user space
6000 <<domain,tracing domains>>: other tracing domains have their own channel
6001 created on the fly when <<enabling-disabling-events,creating event
6002 rules>>.
6003
6004 [IMPORTANT]
6005 ====
6006 Because of a current LTTng limitation, you must create all channels
6007 _before_ you <<basic-tracing-session-control,start tracing>> in a given
6008 tracing session, that is, before the first time you run
6009 man:lttng-start(1).
6010
6011 Since LTTng automatically creates a default channel when you use the
6012 man:lttng-enable-event(1) command with a specific tracing domain, you
6013 cannot, for example, create a Linux kernel event rule, start tracing,
6014 and then create a user space event rule, because no user space channel
6015 exists yet and it's too late to create one.
6016
6017 For this reason, make sure to configure your channels properly
6018 before starting the tracers for the first time!
6019 ====
6020
6021 The following examples show how you can combine the previous
6022 command-line options to create simple to more complex channels.
6023
6024 .Create a Linux kernel channel with default attributes.
6025 ====
6026 [role="term"]
6027 ----
6028 $ lttng enable-channel --kernel my-channel
6029 ----
6030 ====
6031
6032 .Create a user space channel with 4 sub-buffers or 1{nbsp}MiB each, per CPU, per instrumented process.
6033 ====
6034 [role="term"]
6035 ----
6036 $ lttng enable-channel --userspace --num-subbuf=4 --subbuf-size=1M \
6037 --buffers-pid my-channel
6038 ----
6039 ====
6040
6041 .Create a Linux kernel channel which rotates 8 trace files of 4{nbsp}MiB each for each stream
6042 ====
6043 [role="term"]
6044 ----
6045 $ lttng enable-channel --kernel --tracefile-count=8 \
6046 --tracefile-size=4194304 my-channel
6047 ----
6048 ====
6049
6050 .Create a user space channel in overwrite (or _flight recorder_) mode.
6051 ====
6052 [role="term"]
6053 ----
6054 $ lttng enable-channel --userspace --overwrite my-channel
6055 ----
6056 ====
6057
6058 You can <<enabling-disabling-events,create>> the same event rule in
6059 two different channels:
6060
6061 [role="term"]
6062 ----
6063 $ lttng enable-event --userspace --channel=my-channel app:tp
6064 $ lttng enable-event --userspace --channel=other-channel app:tp
6065 ----
6066
6067 If both channels are enabled, when a tracepoint named `app:tp` is
6068 reached, LTTng records two events, one for each channel.
6069
6070
6071 [[disable-channel]]
6072 === Disable a channel
6073
6074 To disable a specific channel that you <<enabling-disabling-channels,created>>
6075 previously, use the man:lttng-disable-channel(1) command.
6076
6077 .Disable a specific Linux kernel channel.
6078 ====
6079 [role="term"]
6080 ----
6081 $ lttng disable-channel --kernel my-channel
6082 ----
6083 ====
6084
6085 The state of a channel precedes the individual states of event rules
6086 attached to it: event rules which belong to a disabled channel, even if
6087 they are enabled, are also considered disabled.
6088
6089
6090 [[adding-context]]
6091 === Add context fields to a channel
6092
6093 Event record fields in trace files provide important information about
6094 events that occured previously, but sometimes some external context may
6095 help you solve a problem faster. Examples of context fields are:
6096
6097 * The **process ID**, **thread ID**, **process name**, and
6098 **process priority** of the thread in which the event occurs.
6099 * The **hostname** of the system on which the event occurs.
6100 * The current values of many possible **performance counters** using
6101 perf, for example:
6102 ** CPU cycles, stalled cycles, idle cycles, and the other cycle types.
6103 ** Cache misses.
6104 ** Branch instructions, misses, and loads.
6105 ** CPU faults.
6106 * Any context defined at the application level (supported for the
6107 JUL and log4j <<domain,tracing domains>>).
6108
6109 To get the full list of available context fields, see
6110 `lttng add-context --list`. Some context fields are reserved for a
6111 specific <<domain,tracing domain>> (Linux kernel or user space).
6112
6113 You add context fields to <<channel,channels>>. All the events
6114 that a channel with added context fields records contain those fields.
6115
6116 To add context fields to one or all the channels of a given tracing
6117 session:
6118
6119 * Use the man:lttng-add-context(1) command.
6120
6121 .Add context fields to all the channels of the current tracing session.
6122 ====
6123 The following command line adds the virtual process identifier and
6124 the per-thread CPU cycles count fields to all the user space channels
6125 of the current tracing session.
6126
6127 [role="term"]
6128 ----
6129 $ lttng add-context --userspace --type=vpid --type=perf:thread:cpu-cycles
6130 ----
6131 ====
6132
6133 .Add performance counter context fields by raw ID
6134 ====
6135 See man:lttng-add-context(1) for the exact format of the context field
6136 type, which is partly compatible with the format used in
6137 man:perf-record(1).
6138
6139 [role="term"]
6140 ----
6141 $ lttng add-context --userspace --type=perf:thread:raw:r0110:test
6142 $ lttng add-context --kernel --type=perf:cpu:raw:r0013c:x86unhalted
6143 ----
6144 ====
6145
6146 .Add a context field to a specific channel.
6147 ====
6148 The following command line adds the thread identifier context field
6149 to the Linux kernel channel named `my-channel` in the current
6150 tracing session.
6151
6152 [role="term"]
6153 ----
6154 $ lttng add-context --kernel --channel=my-channel --type=tid
6155 ----
6156 ====
6157
6158 .Add an application-specific context field to a specific channel.
6159 ====
6160 The following command line adds the `cur_msg_id` context field of the
6161 `retriever` context retriever for all the instrumented
6162 <<java-application,Java applications>> recording <<event,event records>>
6163 in the channel named `my-channel`:
6164
6165 [role="term"]
6166 ----
6167 $ lttng add-context --kernel --channel=my-channel \
6168 --type='$app:retriever:cur_msg_id'
6169 ----
6170
6171 IMPORTANT: Make sure to always quote the `$` character when you
6172 use man:lttng-add-context(1) from a shell.
6173 ====
6174
6175 NOTE: You cannot remove context fields from a channel once you add it.
6176
6177
6178 [role="since-2.7"]
6179 [[pid-tracking]]
6180 === Track process IDs
6181
6182 It's often useful to allow only specific process IDs (PIDs) to emit
6183 events. For example, you may wish to record all the system calls made by
6184 a given process (à la http://linux.die.net/man/1/strace[strace]).
6185
6186 The man:lttng-track(1) and man:lttng-untrack(1) commands serve this
6187 purpose. Both commands operate on a whitelist of process IDs. You _add_
6188 entries to this whitelist with the man:lttng-track(1) command and remove
6189 entries with the man:lttng-untrack(1) command. Any process which has one
6190 of the PIDs in the whitelist is allowed to emit LTTng events which pass
6191 an enabled <<event,event rule>>.
6192
6193 NOTE: The PID tracker tracks the _numeric process IDs_. Should a
6194 process with a given tracked ID exit and another process be given this
6195 ID, then the latter would also be allowed to emit events.
6196
6197 .Track and untrack process IDs.
6198 ====
6199 For the sake of the following example, assume the target system has 16
6200 possible PIDs.
6201
6202 When you
6203 <<creating-destroying-tracing-sessions,create a tracing session>>,
6204 the whitelist contains all the possible PIDs:
6205
6206 [role="img-100"]
6207 .All PIDs are tracked.
6208 image::track-all.png[]
6209
6210 When the whitelist is full and you use the man:lttng-track(1) command to
6211 specify some PIDs to track, LTTng first clears the whitelist, then it
6212 tracks the specific PIDs. After:
6213
6214 [role="term"]
6215 ----
6216 $ lttng track --pid=3,4,7,10,13
6217 ----
6218
6219 the whitelist is:
6220
6221 [role="img-100"]
6222 .PIDs 3, 4, 7, 10, and 13 are tracked.
6223 image::track-3-4-7-10-13.png[]
6224
6225 You can add more PIDs to the whitelist afterwards:
6226
6227 [role="term"]
6228 ----
6229 $ lttng track --pid=1,15,16
6230 ----
6231
6232 The result is:
6233
6234 [role="img-100"]
6235 .PIDs 1, 15, and 16 are added to the whitelist.
6236 image::track-1-3-4-7-10-13-15-16.png[]
6237
6238 The man:lttng-untrack(1) command removes entries from the PID tracker's
6239 whitelist. Given the previous example, the following command:
6240
6241 [role="term"]
6242 ----
6243 $ lttng untrack --pid=3,7,10,13
6244 ----
6245
6246 leads to this whitelist:
6247
6248 [role="img-100"]
6249 .PIDs 3, 7, 10, and 13 are removed from the whitelist.
6250 image::track-1-4-15-16.png[]
6251
6252 LTTng can track all possible PIDs again using the opt:track(1):--all
6253 option:
6254
6255 [role="term"]
6256 ----
6257 $ lttng track --pid --all
6258 ----
6259
6260 The result is, again:
6261
6262 [role="img-100"]
6263 .All PIDs are tracked.
6264 image::track-all.png[]
6265 ====
6266
6267 .Track only specific PIDs
6268 ====
6269 A very typical use case with PID tracking is to start with an empty
6270 whitelist, then <<basic-tracing-session-control,start the tracers>>, and
6271 then add PIDs manually while tracers are active. You can accomplish this
6272 by using the opt:lttng-untrack(1):--all option of the
6273 man:lttng-untrack(1) command to clear the whitelist after you
6274 <<creating-destroying-tracing-sessions,create a tracing session>>:
6275
6276 [role="term"]
6277 ----
6278 $ lttng untrack --pid --all
6279 ----
6280
6281 gives:
6282
6283 [role="img-100"]
6284 .No PIDs are tracked.
6285 image::untrack-all.png[]
6286
6287 If you trace with this whitelist configuration, the tracer records no
6288 events for this <<domain,tracing domain>> because no processes are
6289 tracked. You can use the man:lttng-track(1) command as usual to track
6290 specific PIDs, for example:
6291
6292 [role="term"]
6293 ----
6294 $ lttng track --pid=6,11
6295 ----
6296
6297 Result:
6298
6299 [role="img-100"]
6300 .PIDs 6 and 11 are tracked.
6301 image::track-6-11.png[]
6302 ====
6303
6304
6305 [role="since-2.5"]
6306 [[saving-loading-tracing-session]]
6307 === Save and load tracing session configurations
6308
6309 Configuring a <<tracing-session,tracing session>> can be long. Some of
6310 the tasks involved are:
6311
6312 * <<enabling-disabling-channels,Create channels>> with
6313 specific attributes.
6314 * <<adding-context,Add context fields>> to specific channels.
6315 * <<enabling-disabling-events,Create event rules>> with specific log
6316 level and filter conditions.
6317
6318 If you use LTTng to solve real world problems, chances are you have to
6319 record events using the same tracing session setup over and over,
6320 modifying a few variables each time in your instrumented program
6321 or environment. To avoid constant tracing session reconfiguration,
6322 the man:lttng(1) command-line tool can save and load tracing session
6323 configurations to/from XML files.
6324
6325 To save a given tracing session configuration:
6326
6327 * Use the man:lttng-save(1) command:
6328 +
6329 --
6330 [role="term"]
6331 ----
6332 $ lttng save my-session
6333 ----
6334 --
6335 +
6336 Replace `my-session` with the name of the tracing session to save.
6337
6338 LTTng saves tracing session configurations to
6339 dir:{$LTTNG_HOME/.lttng/sessions} by default. Note that the
6340 env:LTTNG_HOME environment variable defaults to `$HOME` if not set. Use
6341 the opt:lttng-save(1):--output-path option to change this destination
6342 directory.
6343
6344 LTTng saves all configuration parameters, for example:
6345
6346 * The tracing session name.
6347 * The trace data output path.
6348 * The channels with their state and all their attributes.
6349 * The context fields you added to channels.
6350 * The event rules with their state, log level and filter conditions.
6351
6352 To load a tracing session:
6353
6354 * Use the man:lttng-load(1) command:
6355 +
6356 --
6357 [role="term"]
6358 ----
6359 $ lttng load my-session
6360 ----
6361 --
6362 +
6363 Replace `my-session` with the name of the tracing session to load.
6364
6365 When LTTng loads a configuration, it restores your saved tracing session
6366 as if you just configured it manually.
6367
6368 See man:lttng(1) for the complete list of command-line options. You
6369 can also save and load all many sessions at a time, and decide in which
6370 directory to output the XML files.
6371
6372
6373 [[sending-trace-data-over-the-network]]
6374 === Send trace data over the network
6375
6376 LTTng can send the recorded trace data to a remote system over the
6377 network instead of writing it to the local file system.
6378
6379 To send the trace data over the network:
6380
6381 . On the _remote_ system (which can also be the target system),
6382 start an LTTng <<lttng-relayd,relay daemon>> (man:lttng-relayd(8)):
6383 +
6384 --
6385 [role="term"]
6386 ----
6387 $ lttng-relayd
6388 ----
6389 --
6390
6391 . On the _target_ system, create a tracing session configured to
6392 send trace data over the network:
6393 +
6394 --
6395 [role="term"]
6396 ----
6397 $ lttng create my-session --set-url=net://remote-system
6398 ----
6399 --
6400 +
6401 Replace `remote-system` by the host name or IP address of the
6402 remote system. See man:lttng-create(1) for the exact URL format.
6403
6404 . On the target system, use the man:lttng(1) command-line tool as usual.
6405 When tracing is active, the target's consumer daemon sends sub-buffers
6406 to the relay daemon running on the remote system instead of flushing
6407 them to the local file system. The relay daemon writes the received
6408 packets to the local file system.
6409
6410 The relay daemon writes trace files to
6411 +$LTTNG_HOME/lttng-traces/__hostname__/__session__+ by default, where
6412 +__hostname__+ is the host name of the target system and +__session__+
6413 is the tracing session name. Note that the env:LTTNG_HOME environment
6414 variable defaults to `$HOME` if not set. Use the
6415 opt:lttng-relayd(8):--output option of man:lttng-relayd(8) to write
6416 trace files to another base directory.
6417
6418
6419 [role="since-2.4"]
6420 [[lttng-live]]
6421 === View events as LTTng emits them (noch:{LTTng} live)
6422
6423 LTTng live is a network protocol implemented by the <<lttng-relayd,relay
6424 daemon>> (man:lttng-relayd(8)) to allow compatible trace viewers to
6425 display events as LTTng emits them on the target system while tracing is
6426 active.
6427
6428 The relay daemon creates a _tee_: it forwards the trace data to both
6429 the local file system and to connected live viewers:
6430
6431 [role="img-90"]
6432 .The relay daemon creates a _tee_, forwarding the trace data to both trace files and a connected live viewer.
6433 image::live.png[]
6434
6435 To use LTTng live:
6436
6437 . On the _target system_, create a <<tracing-session,tracing session>>
6438 in _live mode_:
6439 +
6440 --
6441 [role="term"]
6442 ----
6443 $ lttng create my-session --live
6444 ----
6445 --
6446 +
6447 This spawns a local relay daemon.
6448
6449 . Start the live viewer and configure it to connect to the relay
6450 daemon. For example, with http://diamon.org/babeltrace[Babeltrace]:
6451 +
6452 --
6453 [role="term"]
6454 ----
6455 $ babeltrace --input-format=lttng-live \
6456 net://localhost/host/hostname/my-session
6457 ----
6458 --
6459 +
6460 Replace:
6461 +
6462 --
6463 * `hostname` with the host name of the target system.
6464 * `my-session` with the name of the tracing session to view.
6465 --
6466
6467 . Configure the tracing session as usual with the man:lttng(1)
6468 command-line tool, and <<basic-tracing-session-control,start tracing>>.
6469
6470 You can list the available live tracing sessions with Babeltrace:
6471
6472 [role="term"]
6473 ----
6474 $ babeltrace --input-format=lttng-live net://localhost
6475 ----
6476
6477 You can start the relay daemon on another system. In this case, you need
6478 to specify the relay daemon's URL when you create the tracing session
6479 with the opt:lttng-create(1):--set-url option. You also need to replace
6480 `localhost` in the procedure above with the host name of the system on
6481 which the relay daemon is running.
6482
6483 See man:lttng-create(1) and man:lttng-relayd(8) for the complete list of
6484 command-line options.
6485
6486
6487 [role="since-2.3"]
6488 [[taking-a-snapshot]]
6489 === Take a snapshot of the current sub-buffers of a tracing session
6490
6491 The normal behavior of LTTng is to append full sub-buffers to growing
6492 trace data files. This is ideal to keep a full history of the events
6493 that occurred on the target system, but it can
6494 represent too much data in some situations. For example, you may wish
6495 to trace your application continuously until some critical situation
6496 happens, in which case you only need the latest few recorded
6497 events to perform the desired analysis, not multi-gigabyte trace files.
6498
6499 With the man:lttng-snapshot(1) command, you can take a snapshot of the
6500 current sub-buffers of a given <<tracing-session,tracing session>>.
6501 LTTng can write the snapshot to the local file system or send it over
6502 the network.
6503
6504 To take a snapshot:
6505
6506 . Create a tracing session in _snapshot mode_:
6507 +
6508 --
6509 [role="term"]
6510 ----
6511 $ lttng create my-session --snapshot
6512 ----
6513 --
6514 +
6515 The <<channel-overwrite-mode-vs-discard-mode,event loss mode>> of
6516 <<channel,channels>> created in this mode is automatically set to
6517 _overwrite_ (flight recorder mode).
6518
6519 . Configure the tracing session as usual with the man:lttng(1)
6520 command-line tool, and <<basic-tracing-session-control,start tracing>>.
6521
6522 . **Optional**: When you need to take a snapshot,
6523 <<basic-tracing-session-control,stop tracing>>.
6524 +
6525 You can take a snapshot when the tracers are active, but if you stop
6526 them first, you are sure that the data in the sub-buffers does not
6527 change before you actually take the snapshot.
6528
6529 . Take a snapshot:
6530 +
6531 --
6532 [role="term"]
6533 ----
6534 $ lttng snapshot record --name=my-first-snapshot
6535 ----
6536 --
6537 +
6538 LTTng writes the current sub-buffers of all the current tracing
6539 session's channels to trace files on the local file system. Those trace
6540 files have `my-first-snapshot` in their name.
6541
6542 There is no difference between the format of a normal trace file and the
6543 format of a snapshot: viewers of LTTng traces also support LTTng
6544 snapshots.
6545
6546 By default, LTTng writes snapshot files to the path shown by
6547 `lttng snapshot list-output`. You can change this path or decide to send
6548 snapshots over the network using either:
6549
6550 . An output path or URL that you specify when you create the
6551 tracing session.
6552 . An snapshot output path or URL that you add using
6553 `lttng snapshot add-output`
6554 . An output path or URL that you provide directly to the
6555 `lttng snapshot record` command.
6556
6557 Method 3 overrides method 2, which overrides method 1. When you
6558 specify a URL, a relay daemon must listen on a remote system (see
6559 <<sending-trace-data-over-the-network,Send trace data over the network>>).
6560
6561
6562 [role="since-2.6"]
6563 [[mi]]
6564 === Use the machine interface
6565
6566 With any command of the man:lttng(1) command-line tool, you can set the
6567 opt:lttng(1):--mi option to `xml` (before the command name) to get an
6568 XML machine interface output, for example:
6569
6570 [role="term"]
6571 ----
6572 $ lttng --mi=xml enable-event --kernel --syscall open
6573 ----
6574
6575 A schema definition (XSD) is
6576 https://github.com/lttng/lttng-tools/blob/stable-2.9/src/common/mi-lttng-3.0.xsd[available]
6577 to ease the integration with external tools as much as possible.
6578
6579
6580 [role="since-2.8"]
6581 [[metadata-regenerate]]
6582 === Regenerate the metadata of an LTTng trace
6583
6584 An LTTng trace, which is a http://diamon.org/ctf[CTF] trace, has both
6585 data stream files and a metadata file. This metadata file contains,
6586 amongst other things, information about the offset of the clock sources
6587 used to timestamp <<event,event records>> when tracing.
6588
6589 If, once a <<tracing-session,tracing session>> is
6590 <<basic-tracing-session-control,started>>, a major
6591 https://en.wikipedia.org/wiki/Network_Time_Protocol[NTP] correction
6592 happens, the trace's clock offset also needs to be updated. You
6593 can use the `metadata` item of the man:lttng-regenerate(1) command
6594 to do so.
6595
6596 The main use case of this command is to allow a system to boot with
6597 an incorrect wall time and trace it with LTTng before its wall time
6598 is corrected. Once the system is known to be in a state where its
6599 wall time is correct, it can run `lttng regenerate metadata`.
6600
6601 To regenerate the metadata of an LTTng trace:
6602
6603 * Use the `metadata` item of the man:lttng-regenerate(1) command:
6604 +
6605 --
6606 [role="term"]
6607 ----
6608 $ lttng regenerate metadata
6609 ----
6610 --
6611
6612 [IMPORTANT]
6613 ====
6614 `lttng regenerate metadata` has the following limitations:
6615
6616 * Tracing session <<creating-destroying-tracing-sessions,created>>
6617 in non-live mode.
6618 * User space <<channel,channels>>, if any, are using
6619 <<channel-buffering-schemes,per-user buffering>>.
6620 ====
6621
6622
6623 [role="since-2.9"]
6624 [[regenerate-statedump]]
6625 === Regenerate the state dump of a tracing session
6626
6627 The LTTng kernel and user space tracers generate state dump
6628 <<event,event records>> when the application starts or when you
6629 <<basic-tracing-session-control,start a tracing session>>. An analysis
6630 can use the state dump event records to set an initial state before it
6631 builds the rest of the state from the following event records.
6632 http://tracecompass.org/[Trace Compass] is a notable example of an
6633 application which uses the state dump of an LTTng trace.
6634
6635 When you <<taking-a-snapshot,take a snapshot>>, it's possible that the
6636 state dump event records are not included in the snapshot because they
6637 were recorded to a sub-buffer that has been consumed or overwritten
6638 already.
6639
6640 You can use the `lttng regenerate statedump` command to emit the state
6641 dump event records again.
6642
6643 To regenerate the state dump of the current tracing session, provided
6644 create it in snapshot mode, before you take a snapshot:
6645
6646 . Use the `statedump` item of the man:lttng-regenerate(1) command:
6647 +
6648 --
6649 [role="term"]
6650 ----
6651 $ lttng regenerate statedump
6652 ----
6653 --
6654
6655 . <<basic-tracing-session-control,Stop the tracing session>>:
6656 +
6657 --
6658 [role="term"]
6659 ----
6660 $ lttng stop
6661 ----
6662 --
6663
6664 . <<taking-a-snapshot,Take a snapshot>>:
6665 +
6666 --
6667 [role="term"]
6668 ----
6669 $ lttng snapshot record --name=my-snapshot
6670 ----
6671 --
6672
6673 Depending on the event throughput, you should run steps 1 and 2
6674 as closely as possible.
6675
6676 NOTE: To record the state dump events, you need to
6677 <<enabling-disabling-events,create event rules>> which enable them.
6678 LTTng-UST state dump tracepoints start with `lttng_ust_statedump:`.
6679 LTTng-modules state dump tracepoints start with `lttng_statedump_`.
6680
6681
6682 [role="since-2.7"]
6683 [[persistent-memory-file-systems]]
6684 === Record trace data on persistent memory file systems
6685
6686 https://en.wikipedia.org/wiki/Non-volatile_random-access_memory[Non-volatile random-access memory]
6687 (NVRAM) is random-access memory that retains its information when power
6688 is turned off (non-volatile). Systems with such memory can store data
6689 structures in RAM and retrieve them after a reboot, without flushing
6690 to typical _storage_.
6691
6692 Linux supports NVRAM file systems thanks to either
6693 http://pramfs.sourceforge.net/[PRAMFS] or
6694 https://www.kernel.org/doc/Documentation/filesystems/dax.txt[DAX]{nbsp}+{nbsp}http://lkml.iu.edu/hypermail/linux/kernel/1504.1/03463.html[pmem]
6695 (requires Linux 4.1+).
6696
6697 This section does not describe how to operate such file systems;
6698 we assume that you have a working persistent memory file system.
6699
6700 When you create a <<tracing-session,tracing session>>, you can specify
6701 the path of the shared memory holding the sub-buffers. If you specify a
6702 location on an NVRAM file system, then you can retrieve the latest
6703 recorded trace data when the system reboots after a crash.
6704
6705 To record trace data on a persistent memory file system and retrieve the
6706 trace data after a system crash:
6707
6708 . Create a tracing session with a sub-buffer shared memory path located
6709 on an NVRAM file system:
6710 +
6711 --
6712 [role="term"]
6713 ----
6714 $ lttng create my-session --shm-path=/path/to/shm
6715 ----
6716 --
6717
6718 . Configure the tracing session as usual with the man:lttng(1)
6719 command-line tool, and <<basic-tracing-session-control,start tracing>>.
6720
6721 . After a system crash, use the man:lttng-crash(1) command-line tool to
6722 view the trace data recorded on the NVRAM file system:
6723 +
6724 --
6725 [role="term"]
6726 ----
6727 $ lttng-crash /path/to/shm
6728 ----
6729 --
6730
6731 The binary layout of the ring buffer files is not exactly the same as
6732 the trace files layout. This is why you need to use man:lttng-crash(1)
6733 instead of your preferred trace viewer directly.
6734
6735 To convert the ring buffer files to LTTng trace files:
6736
6737 * Use the opt:lttng-crash(1):--extract option of man:lttng-crash(1):
6738 +
6739 --
6740 [role="term"]
6741 ----
6742 $ lttng-crash --extract=/path/to/trace /path/to/shm
6743 ----
6744 --
6745
6746
6747 [[reference]]
6748 == Reference
6749
6750 [[lttng-modules-ref]]
6751 === noch:{LTTng-modules}
6752
6753
6754 [role="since-2.9"]
6755 [[lttng-tracepoint-enum]]
6756 ==== `LTTNG_TRACEPOINT_ENUM()` usage
6757
6758 Use the `LTTNG_TRACEPOINT_ENUM()` macro to define an enumeration:
6759
6760 [source,c]
6761 ----
6762 LTTNG_TRACEPOINT_ENUM(name, TP_ENUM_VALUES(entries))
6763 ----
6764
6765 Replace:
6766
6767 * `name` with the name of the enumeration (C identifier, unique
6768 amongst all the defined enumerations).
6769 * `entries` with a list of enumeration entries.
6770
6771 The available enumeration entry macros are:
6772
6773 +ctf_enum_value(__name__, __value__)+::
6774 Entry named +__name__+ mapped to the integral value +__value__+.
6775
6776 +ctf_enum_range(__name__, __begin__, __end__)+::
6777 Entry named +__name__+ mapped to the range of integral values between
6778 +__begin__+ (included) and +__end__+ (included).
6779
6780 +ctf_enum_auto(__name__)+::
6781 Entry named +__name__+ mapped to the integral value following the
6782 last mapping's value.
6783 +
6784 The last value of a `ctf_enum_value()` entry is its +__value__+
6785 parameter.
6786 +
6787 The last value of a `ctf_enum_range()` entry is its +__end__+ parameter.
6788 +
6789 If `ctf_enum_auto()` is the first entry in the list, its integral
6790 value is 0.
6791
6792 Use the `ctf_enum()` <<lttng-modules-tp-fields,field definition macro>>
6793 to use a defined enumeration as a tracepoint field.
6794
6795 .Define an enumeration with `LTTNG_TRACEPOINT_ENUM()`.
6796 ====
6797 [source,c]
6798 ----
6799 LTTNG_TRACEPOINT_ENUM(
6800 my_enum,
6801 TP_ENUM_VALUES(
6802 ctf_enum_auto("AUTO: EXPECT 0")
6803 ctf_enum_value("VALUE: 23", 23)
6804 ctf_enum_value("VALUE: 27", 27)
6805 ctf_enum_auto("AUTO: EXPECT 28")
6806 ctf_enum_range("RANGE: 101 TO 303", 101, 303)
6807 ctf_enum_auto("AUTO: EXPECT 304")
6808 )
6809 )
6810 ----
6811 ====
6812
6813
6814 [role="since-2.7"]
6815 [[lttng-modules-tp-fields]]
6816 ==== Tracepoint fields macros (for `TP_FIELDS()`)
6817
6818 [[tp-fast-assign]][[tp-struct-entry]]The available macros to define
6819 tracepoint fields, which must be listed within `TP_FIELDS()` in
6820 `LTTNG_TRACEPOINT_EVENT()`, are:
6821
6822 [role="func-desc growable",cols="asciidoc,asciidoc"]
6823 .Available macros to define LTTng-modules tracepoint fields
6824 |====
6825 |Macro |Description and parameters
6826
6827 |
6828 +ctf_integer(__t__, __n__, __e__)+
6829
6830 +ctf_integer_nowrite(__t__, __n__, __e__)+
6831
6832 +ctf_user_integer(__t__, __n__, __e__)+
6833
6834 +ctf_user_integer_nowrite(__t__, __n__, __e__)+
6835 |
6836 Standard integer, displayed in base 10.
6837
6838 +__t__+::
6839 Integer C type (`int`, `long`, `size_t`, ...).
6840
6841 +__n__+::
6842 Field name.
6843
6844 +__e__+::
6845 Argument expression.
6846
6847 |
6848 +ctf_integer_hex(__t__, __n__, __e__)+
6849
6850 +ctf_user_integer_hex(__t__, __n__, __e__)+
6851 |
6852 Standard integer, displayed in base 16.
6853
6854 +__t__+::
6855 Integer C type.
6856
6857 +__n__+::
6858 Field name.
6859
6860 +__e__+::
6861 Argument expression.
6862
6863 |+ctf_integer_oct(__t__, __n__, __e__)+
6864 |
6865 Standard integer, displayed in base 8.
6866
6867 +__t__+::
6868 Integer C type.
6869
6870 +__n__+::
6871 Field name.
6872
6873 +__e__+::
6874 Argument expression.
6875
6876 |
6877 +ctf_integer_network(__t__, __n__, __e__)+
6878
6879 +ctf_user_integer_network(__t__, __n__, __e__)+
6880 |
6881 Integer in network byte order (big-endian), displayed in base 10.
6882
6883 +__t__+::
6884 Integer C type.
6885
6886 +__n__+::
6887 Field name.
6888
6889 +__e__+::
6890 Argument expression.
6891
6892 |
6893 +ctf_integer_network_hex(__t__, __n__, __e__)+
6894
6895 +ctf_user_integer_network_hex(__t__, __n__, __e__)+
6896 |
6897 Integer in network byte order, displayed in base 16.
6898
6899 +__t__+::
6900 Integer C type.
6901
6902 +__n__+::
6903 Field name.
6904
6905 +__e__+::
6906 Argument expression.
6907
6908 |
6909 +ctf_enum(__N__, __t__, __n__, __e__)+
6910
6911 +ctf_enum_nowrite(__N__, __t__, __n__, __e__)+
6912
6913 +ctf_user_enum(__N__, __t__, __n__, __e__)+
6914
6915 +ctf_user_enum_nowrite(__N__, __t__, __n__, __e__)+
6916 |
6917 Enumeration.
6918
6919 +__N__+::
6920 Name of a <<lttng-tracepoint-enum,previously defined enumeration>>.
6921
6922 +__t__+::
6923 Integer C type (`int`, `long`, `size_t`, ...).
6924
6925 +__n__+::
6926 Field name.
6927
6928 +__e__+::
6929 Argument expression.
6930
6931 |
6932 +ctf_string(__n__, __e__)+
6933
6934 +ctf_string_nowrite(__n__, __e__)+
6935
6936 +ctf_user_string(__n__, __e__)+
6937
6938 +ctf_user_string_nowrite(__n__, __e__)+
6939 |
6940 Null-terminated string; undefined behavior if +__e__+ is `NULL`.
6941
6942 +__n__+::
6943 Field name.
6944
6945 +__e__+::
6946 Argument expression.
6947
6948 |
6949 +ctf_array(__t__, __n__, __e__, __s__)+
6950
6951 +ctf_array_nowrite(__t__, __n__, __e__, __s__)+
6952
6953 +ctf_user_array(__t__, __n__, __e__, __s__)+
6954
6955 +ctf_user_array_nowrite(__t__, __n__, __e__, __s__)+
6956 |
6957 Statically-sized array of integers.
6958
6959 +__t__+::
6960 Array element C type.
6961
6962 +__n__+::
6963 Field name.
6964
6965 +__e__+::
6966 Argument expression.
6967
6968 +__s__+::
6969 Number of elements.
6970
6971 |
6972 +ctf_array_bitfield(__t__, __n__, __e__, __s__)+
6973
6974 +ctf_array_bitfield_nowrite(__t__, __n__, __e__, __s__)+
6975
6976 +ctf_user_array_bitfield(__t__, __n__, __e__, __s__)+
6977
6978 +ctf_user_array_bitfield_nowrite(__t__, __n__, __e__, __s__)+
6979 |
6980 Statically-sized array of bits.
6981
6982 The type of +__e__+ must be an integer type. +__s__+ is the number
6983 of elements of such type in +__e__+, not the number of bits.
6984
6985 +__t__+::
6986 Array element C type.
6987
6988 +__n__+::
6989 Field name.
6990
6991 +__e__+::
6992 Argument expression.
6993
6994 +__s__+::
6995 Number of elements.
6996
6997 |
6998 +ctf_array_text(__t__, __n__, __e__, __s__)+
6999
7000 +ctf_array_text_nowrite(__t__, __n__, __e__, __s__)+
7001
7002 +ctf_user_array_text(__t__, __n__, __e__, __s__)+
7003
7004 +ctf_user_array_text_nowrite(__t__, __n__, __e__, __s__)+
7005 |
7006 Statically-sized array, printed as text.
7007
7008 The string does not need to be null-terminated.
7009
7010 +__t__+::
7011 Array element C type (always `char`).
7012
7013 +__n__+::
7014 Field name.
7015
7016 +__e__+::
7017 Argument expression.
7018
7019 +__s__+::
7020 Number of elements.
7021
7022 |
7023 +ctf_sequence(__t__, __n__, __e__, __T__, __E__)+
7024
7025 +ctf_sequence_nowrite(__t__, __n__, __e__, __T__, __E__)+
7026
7027 +ctf_user_sequence(__t__, __n__, __e__, __T__, __E__)+
7028
7029 +ctf_user_sequence_nowrite(__t__, __n__, __e__, __T__, __E__)+
7030 |
7031 Dynamically-sized array of integers.
7032
7033 The type of +__E__+ must be unsigned.
7034
7035 +__t__+::
7036 Array element C type.
7037
7038 +__n__+::
7039 Field name.
7040
7041 +__e__+::
7042 Argument expression.
7043
7044 +__T__+::
7045 Length expression C type.
7046
7047 +__E__+::
7048 Length expression.
7049
7050 |
7051 +ctf_sequence_hex(__t__, __n__, __e__, __T__, __E__)+
7052
7053 +ctf_user_sequence_hex(__t__, __n__, __e__, __T__, __E__)+
7054 |
7055 Dynamically-sized array of integers, displayed in base 16.
7056
7057 The type of +__E__+ must be unsigned.
7058
7059 +__t__+::
7060 Array element C type.
7061
7062 +__n__+::
7063 Field name.
7064
7065 +__e__+::
7066 Argument expression.
7067
7068 +__T__+::
7069 Length expression C type.
7070
7071 +__E__+::
7072 Length expression.
7073
7074 |+ctf_sequence_network(__t__, __n__, __e__, __T__, __E__)+
7075 |
7076 Dynamically-sized array of integers in network byte order (big-endian),
7077 displayed in base 10.
7078
7079 The type of +__E__+ must be unsigned.
7080
7081 +__t__+::
7082 Array element C type.
7083
7084 +__n__+::
7085 Field name.
7086
7087 +__e__+::
7088 Argument expression.
7089
7090 +__T__+::
7091 Length expression C type.
7092
7093 +__E__+::
7094 Length expression.
7095
7096 |
7097 +ctf_sequence_bitfield(__t__, __n__, __e__, __T__, __E__)+
7098
7099 +ctf_sequence_bitfield_nowrite(__t__, __n__, __e__, __T__, __E__)+
7100
7101 +ctf_user_sequence_bitfield(__t__, __n__, __e__, __T__, __E__)+
7102
7103 +ctf_user_sequence_bitfield_nowrite(__t__, __n__, __e__, __T__, __E__)+
7104 |
7105 Dynamically-sized array of bits.
7106
7107 The type of +__e__+ must be an integer type. +__s__+ is the number
7108 of elements of such type in +__e__+, not the number of bits.
7109
7110 The type of +__E__+ must be unsigned.
7111
7112 +__t__+::
7113 Array element C type.
7114
7115 +__n__+::
7116 Field name.
7117
7118 +__e__+::
7119 Argument expression.
7120
7121 +__T__+::
7122 Length expression C type.
7123
7124 +__E__+::
7125 Length expression.
7126
7127 |
7128 +ctf_sequence_text(__t__, __n__, __e__, __T__, __E__)+
7129
7130 +ctf_sequence_text_nowrite(__t__, __n__, __e__, __T__, __E__)+
7131
7132 +ctf_user_sequence_text(__t__, __n__, __e__, __T__, __E__)+
7133
7134 +ctf_user_sequence_text_nowrite(__t__, __n__, __e__, __T__, __E__)+
7135 |
7136 Dynamically-sized array, displayed as text.
7137
7138 The string does not need to be null-terminated.
7139
7140 The type of +__E__+ must be unsigned.
7141
7142 The behaviour is undefined if +__e__+ is `NULL`.
7143
7144 +__t__+::
7145 Sequence element C type (always `char`).
7146
7147 +__n__+::
7148 Field name.
7149
7150 +__e__+::
7151 Argument expression.
7152
7153 +__T__+::
7154 Length expression C type.
7155
7156 +__E__+::
7157 Length expression.
7158 |====
7159
7160 Use the `_user` versions when the argument expression, `e`, is
7161 a user space address. In the cases of `ctf_user_integer*()` and
7162 `ctf_user_float*()`, `&e` must be a user space address, thus `e` must
7163 be addressable.
7164
7165 The `_nowrite` versions omit themselves from the session trace, but are
7166 otherwise identical. This means the `_nowrite` fields won't be written
7167 in the recorded trace. Their primary purpose is to make some
7168 of the event context available to the
7169 <<enabling-disabling-events,event filters>> without having to
7170 commit the data to sub-buffers.
7171
7172
7173 [[glossary]]
7174 == Glossary
7175
7176 Terms related to LTTng and to tracing in general:
7177
7178 Babeltrace::
7179 The http://diamon.org/babeltrace[Babeltrace] project, which includes
7180 the cmd:babeltrace command, some libraries, and Python bindings.
7181
7182 <<channel-buffering-schemes,buffering scheme>>::
7183 A layout of sub-buffers applied to a given channel.
7184
7185 <<channel,channel>>::
7186 An entity which is responsible for a set of ring buffers.
7187 +
7188 <<event,Event rules>> are always attached to a specific channel.
7189
7190 clock::
7191 A reference of time for a tracer.
7192
7193 <<lttng-consumerd,consumer daemon>>::
7194 A process which is responsible for consuming the full sub-buffers
7195 and write them to a file system or send them over the network.
7196
7197 <<channel-overwrite-mode-vs-discard-mode,discard mode>>:: The event loss
7198 mode in which the tracer _discards_ new event records when there's no
7199 sub-buffer space left to store them.
7200
7201 event::
7202 The consequence of the execution of an instrumentation
7203 point, like a tracepoint that you manually place in some source code,
7204 or a Linux kernel KProbe.
7205 +
7206 An event is said to _occur_ at a specific time. Different actions can
7207 be taken upon the occurance of an event, like record the event's payload
7208 to a sub-buffer.
7209
7210 <<channel-overwrite-mode-vs-discard-mode,event loss mode>>::
7211 The mechanism by which event records of a given channel are lost
7212 (not recorded) when there is no sub-buffer space left to store them.
7213
7214 [[def-event-name]]event name::
7215 The name of an event, which is also the name of the event record.
7216 This is also called the _instrumentation point name_.
7217
7218 event record::
7219 A record, in a trace, of the payload of an event which occured.
7220
7221 <<event,event rule>>::
7222 Set of conditions which must be satisfied for one or more occuring
7223 events to be recorded.
7224
7225 `java.util.logging`::
7226 Java platform's
7227 https://docs.oracle.com/javase/7/docs/api/java/util/logging/package-summary.html[core logging facilities].
7228
7229 <<instrumenting,instrumentation>>::
7230 The use of LTTng probes to make a piece of software traceable.
7231
7232 instrumentation point::
7233 A point in the execution path of a piece of software that, when
7234 reached by this execution, can emit an event.
7235
7236 instrumentation point name::
7237 See _<<def-event-name,event name>>_.
7238
7239 log4j::
7240 A http://logging.apache.org/log4j/1.2/[logging library] for Java
7241 developed by the Apache Software Foundation.
7242
7243 log level::
7244 Level of severity of a log statement or user space
7245 instrumentation point.
7246
7247 LTTng::
7248 The _Linux Trace Toolkit: next generation_ project.
7249
7250 <<lttng-cli,cmd:lttng>>::
7251 A command-line tool provided by the LTTng-tools project which you
7252 can use to send and receive control messages to and from a
7253 session daemon.
7254
7255 LTTng analyses::
7256 The https://github.com/lttng/lttng-analyses[LTTng analyses] project,
7257 which is a set of analyzing programs that are used to obtain a
7258 higher level view of an LTTng trace.
7259
7260 cmd:lttng-consumerd::
7261 The name of the consumer daemon program.
7262
7263 cmd:lttng-crash::
7264 A utility provided by the LTTng-tools project which can convert
7265 ring buffer files (usually
7266 <<persistent-memory-file-systems,saved on a persistent memory file system>>)
7267 to trace files.
7268
7269 LTTng Documentation::
7270 This document.
7271
7272 <<lttng-live,LTTng live>>::
7273 A communication protocol between the relay daemon and live viewers
7274 which makes it possible to see events "live", as they are received by
7275 the relay daemon.
7276
7277 <<lttng-modules,LTTng-modules>>::
7278 The https://github.com/lttng/lttng-modules[LTTng-modules] project,
7279 which contains the Linux kernel modules to make the Linux kernel
7280 instrumentation points available for LTTng tracing.
7281
7282 cmd:lttng-relayd::
7283 The name of the relay daemon program.
7284
7285 cmd:lttng-sessiond::
7286 The name of the session daemon program.
7287
7288 LTTng-tools::
7289 The https://github.com/lttng/lttng-tools[LTTng-tools] project, which
7290 contains the various programs and libraries used to
7291 <<controlling-tracing,control tracing>>.
7292
7293 <<lttng-ust,LTTng-UST>>::
7294 The https://github.com/lttng/lttng-ust[LTTng-UST] project, which
7295 contains libraries to instrument user applications.
7296
7297 <<lttng-ust-agents,LTTng-UST Java agent>>::
7298 A Java package provided by the LTTng-UST project to allow the
7299 LTTng instrumentation of `java.util.logging` and Apache log4j 1.2
7300 logging statements.
7301
7302 <<lttng-ust-agents,LTTng-UST Python agent>>::
7303 A Python package provided by the LTTng-UST project to allow the
7304 LTTng instrumentation of Python logging statements.
7305
7306 <<channel-overwrite-mode-vs-discard-mode,overwrite mode>>::
7307 The event loss mode in which new event records overwrite older
7308 event records when there's no sub-buffer space left to store them.
7309
7310 <<channel-buffering-schemes,per-process buffering>>::
7311 A buffering scheme in which each instrumented process has its own
7312 sub-buffers for a given user space channel.
7313
7314 <<channel-buffering-schemes,per-user buffering>>::
7315 A buffering scheme in which all the processes of a Unix user share the
7316 same sub-buffer for a given user space channel.
7317
7318 <<lttng-relayd,relay daemon>>::
7319 A process which is responsible for receiving the trace data sent by
7320 a distant consumer daemon.
7321
7322 ring buffer::
7323 A set of sub-buffers.
7324
7325 <<lttng-sessiond,session daemon>>::
7326 A process which receives control commands from you and orchestrates
7327 the tracers and various LTTng daemons.
7328
7329 <<taking-a-snapshot,snapshot>>::
7330 A copy of the current data of all the sub-buffers of a given tracing
7331 session, saved as trace files.
7332
7333 sub-buffer::
7334 One part of an LTTng ring buffer which contains event records.
7335
7336 timestamp::
7337 The time information attached to an event when it is emitted.
7338
7339 trace (_noun_)::
7340 A set of files which are the concatenations of one or more
7341 flushed sub-buffers.
7342
7343 trace (_verb_)::
7344 The action of recording the events emitted by an application
7345 or by a system, or to initiate such recording by controlling
7346 a tracer.
7347
7348 Trace Compass::
7349 The http://tracecompass.org[Trace Compass] project and application.
7350
7351 tracepoint::
7352 An instrumentation point using the tracepoint mechanism of the Linux
7353 kernel or of LTTng-UST.
7354
7355 tracepoint definition::
7356 The definition of a single tracepoint.
7357
7358 tracepoint name::
7359 The name of a tracepoint.
7360
7361 tracepoint provider::
7362 A set of functions providing tracepoints to an instrumented user
7363 application.
7364 +
7365 Not to be confused with a _tracepoint provider package_: many tracepoint
7366 providers can exist within a tracepoint provider package.
7367
7368 tracepoint provider package::
7369 One or more tracepoint providers compiled as an object file or as
7370 a shared library.
7371
7372 tracer::
7373 A software which records emitted events.
7374
7375 <<domain,tracing domain>>::
7376 A namespace for event sources.
7377
7378 <<tracing-group,tracing group>>::
7379 The Unix group in which a Unix user can be to be allowed to trace the
7380 Linux kernel.
7381
7382 <<tracing-session,tracing session>>::
7383 A stateful dialogue between you and a <<lttng-sessiond,session
7384 daemon>>.
7385
7386 user application::
7387 An application running in user space, as opposed to a Linux kernel
7388 module, for example.
This page took 0.182151 seconds and 4 git commands to generate.