1 \input texinfo @c -*-texinfo-*-
4 @settitle LTTng Userspace Tracer (UST) Manual
8 This manual is for program, version version.
10 Copyright @copyright{} copyright-owner.
13 Permission is granted to ...
18 @title LTTng Userspace Tracer (UST) Manual
19 @c @subtitle subtitle-if-any
20 @c @subtitle second-subtitle
23 @c The following two commands
24 @c start the copyright page.
26 @c @vskip 0pt plus 1filll
32 @c So the toc is printed at the start.
37 @top LTTng Userspace Tracer
39 This manual is for UST 0.1.
46 * Instrumenting an application::
50 @c * Copying:: Your rights and freedoms.
59 * Supported platforms::
65 The LTTng Userspace Tracer (UST) is a library accompanied by a set of tools to
68 Code may be instrumented with either markers or tracepoints. A highly efficient
69 lockless tracer records these events to a trace buffers. These buffers are reaped
70 by a deamon which writes trace data to disk.
72 High performance is achieved by the use of lockless buffering algorithms, RCU and
73 per-cpu buffers. In addition, special care is taken to minize cache impact.
77 The LTTng Userspace Tracer is intended to be linkable to open source software
78 as well as to proprietary applications. This was accomplished by licensing
79 the code that needs to be linked to the traced program as @acronym{LGPL}.
81 Components licensed as LGPL v2.1:
88 Components licensed as GPL v2:
95 @node Supported platforms
96 @section Supported platforms
98 UST can currently trace applications running on Linux, on the x86-32 and x86-64 architectures.
101 @chapter Installation
103 The LTTng userspace tracer is a library and a set of userspace tools.
105 The following packages are required:
111 This contains the tracing library, the ustd daemon, trace control tools
112 and other helper tools.
114 Repository: http://git.dorsal.polymtl.ca
119 This is a library that contains a userspace port of some kernel APIs.
121 Repository: http://git.dorsal.polymtl.ca
126 This is the userspace read-copy update library by Mathieu Desnoyers.
128 Available in Debian as package liburcu-dev.
130 Home page: http://lttng.org/?q=node/18
135 LTTV is a graphical (and text) viewer for LTTng traces.
137 Home page: http://lttng.org
141 Libkcompat and liburcu should be installed first. UST may then be compiled
142 and installed. LTTV has no dependency on the other packages; it may therefore
143 be installed on a system which does not have UST installed.
145 Refer to the README in each of these packages for installation instructions.
153 First, instrument a program with a marker.
158 #include <ust/marker.h>
160 int main(int argc, char **argv)
165 /* ... set values of v and st ... */
168 trace_mark(ust, myevent, "firstarg %d secondarg %s", v, st);
170 /* a marker without arguments: */
171 trace_mark(ust, myotherevent, MARK_NOARGS);
179 Then compile it in the regular way, linking it with libust. For example:
182 gcc -o foo -lust foo.c
185 Run the program with @command{usttrace}. The @command{usttrace} output says where the trace
192 Finally, open the trace in LTTV.
195 lttv-gui -t /path/to/trace
198 The trace can also be dumped as text in the console:
201 lttv -m textDump -t /path/to/trace
204 @node Instrumenting an application
205 @chapter Instrumenting an application
207 In order to record a trace of events occurring in a application, the
208 application must be instrumented. Instrumentation points resemble function
209 calls. When the program reaches an instrumentation point, an event is
212 There are no limitations on the type of code that may be instrumented.
213 Multi-threaded programs may be instrumented without problem. Signal handlers
214 may be instrumented as well.
216 There are two APIs to instrument programs: markers and tracepoints. Markers are
217 quick to add and are usually used for temporary instrumentation. Tracepoints
218 provide a way to instrument code more cleanly and are suited for permanent
221 In addition to executable programs, shared libraries may also be instrumented
222 with the methods described in this chapter.
232 Adding a marker is simply a matter of inserting one line in the program.
236 #include <ust/marker.h>
238 int main(int argc, char **argv)
243 /* ... set values of v and st ... */
246 trace_mark(main, myevent, "firstarg %d secondarg %s", v, st);
248 /* another marker without arguments: */
249 trace_mark(main, myotherevent, MARK_NOARGS);
256 The invocation of the trace_mark() macro requires at least 3 arguments. The
257 first, here "main", is the name of the event category. It is also the name of
258 the channel the event will go in. The second, here "myevent" is the name of the
259 event. The third is a format string that announces the names and the types of
260 the event arguments. Its format resembles that of a printf() format string; it
261 is described thoroughly in Appendix x.
263 A given Marker may appear more than once in the same program. Other Markers may
264 have the same name and a different format string, although this might induce
265 some confusion at analysis time.
270 The Tracepoints API uses the Markers, but provides a higher-level abstraction.
271 Whereas the markers API provides limited type checking, the Tracepoints API
272 provides more thorough type checking and discharges from the need to insert
273 format strings directly in the code and to have format strings appear more than
274 once if a given marker is reused.
276 @quotation Note Although this example uses @emph{mychannel} as the channel, the
277 only channel name currently supported with early tracing is @strong{ust}. The
278 @command{usttrace} tool always uses the early tracing mode. When using manual
279 mode without early tracing, any channel name may be used. @end quotation
281 A function instrumented with a tracepoint looks like this:
292 /* ... set values of v and st ... */
295 trace_mychannel_myevent(v, st);
300 Another file, here tp.h, contains declarations for the tracepoint.
304 #include <ust/tracepoint.h>
306 DECLARE_TRACE(mychannel_myevent, TPPROTO(int v, char *st),
311 A third file, here tp.c, contains definitions for the tracepoint.
315 #include <ust/marker.h>
318 DEFINE_TRACE(mychannel_myevent);
320 void mychannel_myevent_probe(int v, char *st)
322 trace_mark(mychannel, myevent, "v %d st %s", v, st);
325 static void __attribute__((constructor)) init()
327 register_trace_mychannel_myevent(mychannel_myevent_probe);
332 Here, tp.h and tp.c could contain declarations and definitions for other
333 tracepoints. The constructor would contain other register_* calls.
335 @node Recording a trace
336 @chapter Recording a trace
339 * Using @command{usttrace}::
340 * Setting up the recording manually::
341 * Using early tracing::
343 * Tracing across @code{fork()} and @code{clone()}::
344 * Tracing programs and libraries that were not linked to libust::
347 @node Using @command{usttrace}
348 @section Using @command{usttrace}
350 The simplest way to record a trace is to use the @command{usttrace} script. An
351 example is given in the quickstart above.
353 The @command{usttrace} script automatically:
355 @item creates a daemon
356 @item enables all markers
357 @item runs the command specified on the command line
358 @item after the command ends, prints the location where the trace was saved
361 Each subdirectory of the save location contains the trace of one process that
362 was generated by the command. The name of a subdirectory consists in the the PID
363 of the process, followed by the timestamp of its creation.
365 The save location also contains logs of the tracing.
367 When using @command{usttrace}, the early tracing is always active, which means
368 that the tracing is guaranteed to be started by the time the process enters its
371 Several @command{usttrace}'s may be run simultaneously without risk of
372 conflict. This facilitates the use of the tracer by idependent users on a
373 system. Each instance of @command{usttrace} starts its own daemon which
374 collects the events of the processes it creates.
376 @node Setting up the recording manually
377 @section Setting up the recording manually
379 Instead of using @command{usttrace}, a trace may be recorded on an already
382 First the daemon must be started.
386 # Make sure the directory for the communication sockets exists.
387 $ mkdir /tmp/ustsocks
389 # Make sure the directory where ustd will write the trace exists.
395 # We assume the program we want to trace is already running and that
398 # List the available markers
399 $ ustctl --list-markers 1234
400 # A column indicates 0 for an inactive marker and 1 for an active marker.
403 $ ustctl --enable-marker ust/mymark 1234
406 $ ustctl --create-trace 1234
409 $ ustctl --start-trace 1234
414 $ ustctl --stop-trace 1234
417 $ ustctl --destroy-trace 1234
421 @node Using early tracing
422 @section Using early tracing
424 Early tracing consists in starting the tracing as early as possible in the
425 program, so no events are lost between program start and the point where the
426 command to start the tracing is given. When using early tracing, it is
427 guaranteed that by the time the traced program enters its @code{main()}
428 function, the tracing will be started.
430 When using @command{usttrace}, the early tracing is always active.
432 When using the manual mode (@command{ustctl}), early tracing is enabled using
433 environment variables. Setting @env{UST_TRACE} to @code{1}, enables early
434 tracing, while setting @env{UST_AUTOPROBE} to @code{1} enables all markers
439 @section Crash recovery
441 When a process being traced crashes, the daemon is able to recover all the
442 events in its buffers that were successfully commited. This is possible because
443 the buffers are in a shared memory segment which remains available to the
444 daemon even after the termination of the traced process.
446 @node Tracing across @code{fork()} and @code{clone()}
447 @section Tracing across @code{fork()} and @code{clone()}
449 Tracing across @code{clone()} when the @code{CLONE_VM} flag is specified is
450 supported without any particular action.
452 When @code{clone()} is called without @code{CLONE_VM} or @code{fork()} is
453 called, a new address space is created and the tracer must be notified to
454 create new buffers for it.
456 This can be done automatically, by @env{LD_PRELOAD}'ing @file{libinterfork.so}.
457 This library intercepts calls to @code{fork()} and informs the tracer it is
458 being called. When using @command{usttrace}, this is accomplied by specifying
459 the @option{-f} command line argument.
461 Alternatively, the program can call @code{ust_before_fork()} before calling
462 @code{fork()} or @code{clone()} with @code{CLONE_VM}. After the call,
463 @code{ust_after_fork_parent()} must be called in the parent process and
464 @code{ust_after_fork_child()} must be called in the child process.
467 @node Tracing programs and libraries that were not linked to libust
468 @section Tracing programs and libraries that were not linked to libust
470 Some programs need to be traced even though they were not linked to libust
471 either because they were not instrumented or because it was not practical.
473 An executable that is not instrumented can still yield interesting traces when
474 at least one of its dynamic libraries is instrumented. It is also possible to
475 trace certain function calls by intercepting them with a specially crafted
476 library that is linked with @env{LD_PRELOAD} at program start.
478 In any case, a program that was not linked to libust at compile time must be
479 linked to it at run time with @env{LD_PRELOAD}. This can be accomplished with
480 @command{usttrace}'s @option{-l} option. It can also be done by setting the
481 @env{LD_PRELOAD} environment variable on the command line. For example:
485 # Run ls with usttrace, LD_PRELOAD'ing libust
486 # (assuming one of the libraries used by ls is instrumented).
489 # Run ls, manually adding the LD_PRELOAD.
490 $ LD_PRELOAD=/usr/local/lib/libust.so.0 ls
501 @chapter Viewing traces
503 Traces may be viewed with LTTV. An example of command for launching LTTV is
504 given in the quickstart.
507 * Viewing multiple traces::
508 * Combined kernel-userspace tracing::
511 @node Viewing multiple traces
512 @section Viewing multiple traces
514 When tracing multi-process applications or several applications simultaneously,
515 more than one trace will be obtained. LTTV can open and display all these
516 traces simultaneously.
518 @node Combined kernel-userspace tracing
519 @section Combined kernel-userspace tracing
521 In addition to multiple userspace traces, LTTV can open a kernel trace recorded
522 with the LTTng kernel tracer. This provides events that enable the rendering of
523 the Control Flow View and the Resource View.
525 When doing so, it is necessary to use the same time source for the kernel
526 tracer as well as the userspace tracer. Currently, the recommended method is to
527 use the timestamp counter for both. The TSC can however only be used on architectures
528 where it is synchronized across cores.