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.4.
46 * Instrumenting an application::
51 @c * Copying:: Your rights and freedoms.
60 * Supported platforms::
66 The LTTng Userspace Tracer (UST) is a library accompanied by a set of tools to
69 Code may be instrumented with either markers or tracepoints. A highly efficient
70 lockless tracer records these events to a trace buffers. These buffers are reaped
71 by a deamon which writes trace data to disk.
73 High performance is achieved by the use of lockless buffering algorithms, RCU and
74 per-cpu buffers. In addition, special care is taken to minize cache impact.
78 The LTTng Userspace Tracer is intended to be linkable to open source software
79 as well as to proprietary applications. This was accomplished by licensing
80 the code that needs to be linked to the traced program as @acronym{LGPL}.
82 Components licensed as LGPL v2.1:
89 Components licensed as GPL v2:
96 @node Supported platforms
97 @section Supported platforms
99 UST can currently trace applications running on Linux, on the x86-32, x86-64
100 and PowerPC 32 architectures.
103 @chapter Installation
105 The LTTng userspace tracer is a library and a set of userspace tools.
107 The following packages are required:
113 This contains the tracing library, the ustd daemon, trace control tools
114 and other helper tools.
116 Repository: @url{http://git.dorsal.polymtl.ca}
121 This is the userspace read-copy update library by Mathieu Desnoyers.
123 Available in Debian as package liburcu-dev.
125 Home page: @url{http://lttng.org/urcu}
130 LTTV is a graphical (and text) viewer for LTTng traces.
132 Home page: @url{http://lttng.org}
136 Liburcu should be installed first. UST may then be compiled and installed. LTTV
137 has no dependency on the other packages; it may therefore be installed on a
138 system which does not have UST installed.
140 Refer to the README in each of these packages for installation instructions.
148 First, instrument a program with a marker.
153 #include <ust/marker.h>
155 int main(int argc, char **argv)
160 /* ... set values of v and st ... */
163 trace_mark(ust, myevent, "firstarg %d secondarg %s", v, st);
165 /* a marker without arguments: */
166 trace_mark(ust, myotherevent, MARK_NOARGS);
174 Then compile it in the regular way, linking it with libust. For example:
177 gcc -o foo -lust foo.c
180 Run the program with @command{usttrace}. The @command{usttrace} output says where the trace
187 Finally, open the trace in LTTV.
190 lttv-gui -t /path/to/trace
193 The trace can also be dumped as text in the console:
196 lttv -m textDump -t /path/to/trace
199 @node Instrumenting an application
200 @chapter Instrumenting an application
202 In order to record a trace of events occurring in a application, the
203 application must be instrumented. Instrumentation points resemble function
204 calls. When the program reaches an instrumentation point, an event is
207 There are no limitations on the type of code that may be instrumented.
208 Multi-threaded programs may be instrumented without problem. Signal handlers
209 may be instrumented as well.
211 There are two APIs to instrument programs: markers and tracepoints. Markers are
212 quick to add and are usually used for temporary instrumentation. Tracepoints
213 provide a way to instrument code more cleanly and are suited for permanent
216 In addition to executable programs, shared libraries may also be instrumented
217 with the methods described in this chapter.
227 Adding a marker is simply a matter of inserting one line in the program.
231 #include <ust/marker.h>
233 int main(int argc, char **argv)
238 /* ... set values of v and st ... */
241 trace_mark(main, myevent, "firstarg %d secondarg %s", v, st);
243 /* another marker without arguments: */
244 trace_mark(main, myotherevent, MARK_NOARGS);
251 The invocation of the trace_mark() macro requires at least 3 arguments. The
252 first, here "main", is the name of the event category. It is also the name of
253 the channel the event will go in. The second, here "myevent" is the name of the
254 event. The third is a format string that announces the names and the types of
255 the event arguments. Its format resembles that of a printf() format string; it
256 is described thoroughly in Appendix x.
258 A given Marker may appear more than once in the same program. Other Markers may
259 have the same name and a different format string, although this might induce
260 some confusion at analysis time.
265 The Tracepoints API uses the Markers, but provides a higher-level abstraction.
266 Whereas the markers API provides limited type checking, the Tracepoints API
267 provides more thorough type checking and discharges from the need to insert
268 format strings directly in the code and to have format strings appear more than
269 once if a given marker is reused.
272 Although this example uses @emph{mychannel} as the channel, the
273 only channel name currently supported with early tracing is @strong{ust}. The
274 @command{usttrace} tool always uses the early tracing mode. When using manual
275 mode without early tracing, any channel name may be used.
278 A function instrumented with a tracepoint looks like this:
289 /* ... set values of v and st ... */
292 trace_mychannel_myevent(v, st);
297 Another file, here tp.h, contains declarations for the tracepoint.
301 #include <ust/tracepoint.h>
303 DECLARE_TRACE(mychannel_myevent, TP_PROTO(int v, char *st),
308 A third file, here tp.c, contains definitions for the tracepoint.
312 #include <ust/marker.h>
315 DEFINE_TRACE(mychannel_myevent);
317 void mychannel_myevent_probe(int v, char *st)
319 trace_mark(mychannel, myevent, "v %d st %s", v, st);
322 static void __attribute__((constructor)) init()
324 register_trace_mychannel_myevent(mychannel_myevent_probe);
329 Here, tp.h and tp.c could contain declarations and definitions for other
330 tracepoints. The constructor would contain other register_* calls.
332 @node Recording a trace
333 @chapter Recording a trace
336 * Using @command{usttrace}::
337 * Setting up the recording manually::
338 * Using early tracing::
340 * Tracing across @code{fork()} and @code{clone()}::
341 * Tracing programs and libraries that were not linked to libust::
344 @node Using @command{usttrace}
345 @section Using @command{usttrace}
347 The simplest way to record a trace is to use the @command{usttrace} script. An
348 example is given in the quickstart above.
350 The @command{usttrace} script automatically:
352 @item creates a daemon
353 @item enables all markers
354 @item runs the command specified on the command line
355 @item after the command ends, prints the location where the trace was saved
358 Each subdirectory of the save location contains the trace of one process that
359 was generated by the command. The name of a subdirectory consists in the the PID
360 of the process, followed by the timestamp of its creation.
362 The save location also contains logs of the tracing.
364 When using @command{usttrace}, the early tracing is always active, which means
365 that the tracing is guaranteed to be started by the time the process enters its
368 Several @command{usttrace}'s may be run simultaneously without risk of
369 conflict. This facilitates the use of the tracer by idependent users on a
370 system. Each instance of @command{usttrace} starts its own daemon which
371 collects the events of the processes it creates.
373 @node Setting up the recording manually
374 @section Setting up the recording manually
376 Instead of using @command{usttrace}, a trace may be recorded on an already
379 First the daemon must be started.
383 # Make sure the directory for the communication sockets exists.
384 $ mkdir /tmp/ustsocks
386 # Make sure the directory where ustd will write the trace exists.
392 # We assume the program we want to trace is already running and that
395 # List the available markers
396 $ ustctl --list-markers 1234
397 # A column indicates 0 for an inactive marker and 1 for an active marker.
400 $ ustctl --enable-marker ust/mymark 1234
403 $ ustctl --create-trace 1234
406 $ ustctl --start-trace 1234
411 $ ustctl --stop-trace 1234
414 $ ustctl --destroy-trace 1234
418 For more information about the manual mode, see the ustctl(1) man page.
420 @node Using early tracing
421 @section Using early tracing
423 Early tracing consists in starting the tracing as early as possible in the
424 program, so no events are lost between program start and the point where the
425 command to start the tracing is given. When using early tracing, it is
426 guaranteed that by the time the traced program enters its @code{main()}
427 function, the tracing will be started.
429 When using @command{usttrace}, the early tracing is always active.
431 When using the manual mode (@command{ustctl}), early tracing is enabled using
432 environment variables. Setting @env{UST_TRACE} to @code{1}, enables early
433 tracing, while setting @env{UST_AUTOPROBE} to @code{1} enables all markers
438 @section Crash recovery
440 When a process being traced crashes, the daemon is able to recover all the
441 events in its buffers that were successfully commited. This is possible because
442 the buffers are in a shared memory segment which remains available to the
443 daemon even after the termination of the traced process.
445 @node Tracing across @code{fork()} and @code{clone()}
446 @section Tracing across @code{fork()} and @code{clone()}
448 Tracing across @code{clone()} when the @code{CLONE_VM} flag is specified is
449 supported without any particular action.
451 When @code{clone()} is called without @code{CLONE_VM} or @code{fork()} is
452 called, a new address space is created and the tracer must be notified to
453 create new buffers for it.
455 This can be done automatically, by @env{LD_PRELOAD}'ing @file{libinterfork.so}.
456 This library intercepts calls to @code{fork()} and informs the tracer it is
457 being called. When using @command{usttrace}, this is accomplied by specifying
458 the @option{-f} command line argument.
460 Alternatively, the program can call @code{ust_before_fork()} before calling
461 @code{fork()} or @code{clone()} with @code{CLONE_VM}. After the call,
462 @code{ust_after_fork_parent()} must be called in the parent process and
463 @code{ust_after_fork_child()} must be called in the child process.
466 @node Tracing programs and libraries that were not linked to libust
467 @section Tracing programs and libraries that were not linked to libust
469 Some programs need to be traced even though they were not linked to libust
470 either because they were not instrumented or because it was not practical.
472 An executable that is not instrumented can still yield interesting traces when
473 at least one of its dynamic libraries is instrumented. It is also possible to
474 trace certain function calls by intercepting them with a specially crafted
475 library that is linked with @env{LD_PRELOAD} at program start.
477 In any case, a program that was not linked to libust at compile time must be
478 linked to it at run time with @env{LD_PRELOAD}. This can be accomplished with
479 @command{usttrace}'s @option{-l} option. It can also be done by setting the
480 @env{LD_PRELOAD} environment variable on the command line. For example:
484 # Run ls with usttrace, LD_PRELOAD'ing libust
485 # (assuming one of the libraries used by ls is instrumented).
488 # Run ls, manually adding the LD_PRELOAD.
489 $ LD_PRELOAD=/usr/local/lib/libust.so.0 ls
500 @chapter Viewing traces
502 Traces may be viewed with LTTV. An example of command for launching LTTV is
503 given in the quickstart.
506 * Viewing multiple traces::
507 * Combined kernel-userspace tracing::
510 @node Viewing multiple traces
511 @section Viewing multiple traces
513 When tracing multi-process applications or several applications simultaneously,
514 more than one trace will be obtained. LTTV can open and display all these
515 traces simultaneously.
517 @node Combined kernel-userspace tracing
518 @section Combined kernel-userspace tracing
520 In addition to multiple userspace traces, LTTV can open a kernel trace recorded
521 with the LTTng kernel tracer. This provides events that enable the rendering of
522 the Control Flow View and the Resource View.
524 When doing so, it is necessary to use the same time source for the kernel
525 tracer as well as the userspace tracer. Currently, the recommended method is to
526 use the timestamp counter for both. The TSC can however only be used on architectures
527 where it is synchronized across cores.
530 @chapter Resource Usage
532 The purpose of this section is to give an overview of the resource usage of libust. For
533 a developer, knowing this can be important: because libust is linked with applications, it
534 needs to share some resources with it. Some applications may make some assumptions that are in
535 conflict with libust's usage of resources.
537 In practice however, libust is designed to be transparent and is compatible
538 with the vast majority of applications. This means no changes are required in
539 the application (or library) being linked to libust.
541 Libust is initialized by a constructor, which by definition runs before the main() function
542 of the application starts. This constructor creates a thread called the @emph{listener thread}.
543 The listener thread initializes a named socket and waits for connections for ustd or ustctl.
545 Libust-specific code may:
547 @item use @code{malloc()} and @code{free()}
548 @item map shared memory segment in the process adress space
549 @item intercept some library calls, specifically @code{fork()} and @code{clone()}
550 @item do interprocess communication with the daemon or ustctl
551 @item create and open named sockets
557 @item handle any signal (all signals are blocked in the listener thread)
558 @item change any process-wide setting that could confuse the application