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