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