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