xref: /openbmc/qemu/docs/devel/tracing.rst (revision bab9dabe7df6a52209b2646da5479ffb629ab8f5)
1ff8e4827SVladimir Sementsov-Ogievskiy.. _tracing:
2ff8e4827SVladimir Sementsov-Ogievskiy
3e50caf4aSStefan Hajnoczi=======
4e50caf4aSStefan HajnocziTracing
5e50caf4aSStefan Hajnoczi=======
6e50caf4aSStefan Hajnoczi
7e50caf4aSStefan HajnocziIntroduction
8e50caf4aSStefan Hajnoczi============
9e50caf4aSStefan Hajnoczi
10e50caf4aSStefan HajnocziThis document describes the tracing infrastructure in QEMU and how to use it
11e50caf4aSStefan Hajnoczifor debugging, profiling, and observing execution.
12e50caf4aSStefan Hajnoczi
13e50caf4aSStefan HajnocziQuickstart
14e50caf4aSStefan Hajnoczi==========
15e50caf4aSStefan Hajnoczi
167e46d5f3SStefan HajnocziEnable tracing of ``memory_region_ops_read`` and ``memory_region_ops_write``
177e46d5f3SStefan Hajnoczievents::
18e50caf4aSStefan Hajnoczi
197e46d5f3SStefan Hajnoczi    $ qemu --trace "memory_region_ops_*" ...
207e46d5f3SStefan Hajnoczi    ...
217e46d5f3SStefan Hajnoczi    719585@1608130130.441188:memory_region_ops_read cpu 0 mr 0x562fdfbb3820 addr 0x3cc value 0x67 size 1
227e46d5f3SStefan Hajnoczi    719585@1608130130.441190:memory_region_ops_write cpu 0 mr 0x562fdfbd2f00 addr 0x3d4 value 0x70e size 2
23e50caf4aSStefan Hajnoczi
247e46d5f3SStefan HajnocziThis output comes from the "log" trace backend that is enabled by default when
257e46d5f3SStefan Hajnoczi``./configure --enable-trace-backends=BACKENDS`` was not explicitly specified.
26e50caf4aSStefan Hajnoczi
273faf22efSStefan HajnocziMultiple patterns can be specified by repeating the ``--trace`` option::
283faf22efSStefan Hajnoczi
293faf22efSStefan Hajnoczi    $ qemu --trace "kvm_*" --trace "virtio_*" ...
303faf22efSStefan Hajnoczi
313faf22efSStefan HajnocziWhen patterns are used frequently it is more convenient to store them in a
323faf22efSStefan Hajnoczifile to avoid long command-line options::
33e50caf4aSStefan Hajnoczi
347e46d5f3SStefan Hajnoczi    $ echo "memory_region_ops_*" >/tmp/events
353faf22efSStefan Hajnoczi    $ echo "kvm_*" >>/tmp/events
367e46d5f3SStefan Hajnoczi    $ qemu --trace events=/tmp/events ...
37e50caf4aSStefan Hajnoczi
38e50caf4aSStefan HajnocziTrace events
39e50caf4aSStefan Hajnoczi============
40e50caf4aSStefan Hajnoczi
41e50caf4aSStefan HajnocziSub-directory setup
42e50caf4aSStefan Hajnoczi-------------------
43e50caf4aSStefan Hajnoczi
440dfb3ca7SStefan HajnocziEach directory in the source tree can declare a set of trace events in a local
450dfb3ca7SStefan Hajnoczi"trace-events" file. All directories which contain "trace-events" files must be
460dfb3ca7SStefan Hajnoczilisted in the "trace_events_subdirs" variable in the top level meson.build
470dfb3ca7SStefan Hajnoczifile. During build, the "trace-events" file in each listed subdirectory will be
480dfb3ca7SStefan Hajnocziprocessed by the "tracetool" script to generate code for the trace events.
49e50caf4aSStefan Hajnoczi
50e50caf4aSStefan HajnocziThe individual "trace-events" files are merged into a "trace-events-all" file,
517a42e688SMarc-André Lureauwhich is also installed into "/usr/share/qemu".
52e50caf4aSStefan HajnocziThis merged file is to be used by the "simpletrace.py" script to later analyse
53e50caf4aSStefan Hajnoczitraces in the simpletrace data format.
54e50caf4aSStefan Hajnoczi
550dfb3ca7SStefan HajnocziThe following files are automatically generated in <builddir>/trace/ during the
560dfb3ca7SStefan Hajnoczibuild:
57e50caf4aSStefan Hajnoczi
580dfb3ca7SStefan Hajnoczi - trace-<subdir>.c - the trace event state declarations
590dfb3ca7SStefan Hajnoczi - trace-<subdir>.h - the trace event enums and probe functions
600dfb3ca7SStefan Hajnoczi - trace-dtrace-<subdir>.h - DTrace event probe specification
610dfb3ca7SStefan Hajnoczi - trace-dtrace-<subdir>.dtrace - DTrace event probe helper declaration
620dfb3ca7SStefan Hajnoczi - trace-dtrace-<subdir>.o - binary DTrace provider (generated by dtrace)
630dfb3ca7SStefan Hajnoczi - trace-ust-<subdir>.h - UST event probe helper declarations
64e50caf4aSStefan Hajnoczi
650dfb3ca7SStefan HajnocziHere <subdir> is the sub-directory path with '/' replaced by '_'. For example,
660dfb3ca7SStefan Hajnoczi"accel/kvm" becomes "accel_kvm" and the final filename for "trace-<subdir>.c"
670dfb3ca7SStefan Hajnoczibecomes "trace-accel_kvm.c".
680dfb3ca7SStefan Hajnoczi
690dfb3ca7SStefan HajnocziSource files in the source tree do not directly include generated files in
700dfb3ca7SStefan Hajnoczi"<builddir>/trace/". Instead they #include the local "trace.h" file, without
710dfb3ca7SStefan Hajnocziany sub-directory path prefix. eg io/channel-buffer.c would do::
72e50caf4aSStefan Hajnoczi
73e50caf4aSStefan Hajnoczi  #include "trace.h"
74e50caf4aSStefan Hajnoczi
750dfb3ca7SStefan HajnocziThe "io/trace.h" file must be created manually with an #include of the
760dfb3ca7SStefan Hajnoczicorresponding "trace/trace-<subdir>.h" file that will be generated in the
770dfb3ca7SStefan Hajnoczibuilddir::
780dfb3ca7SStefan Hajnoczi
790dfb3ca7SStefan Hajnoczi  $ echo '#include "trace/trace-io.h"' >io/trace.h
800dfb3ca7SStefan Hajnoczi
810dfb3ca7SStefan HajnocziWhile it is possible to include a trace.h file from outside a source file's own
820dfb3ca7SStefan Hajnoczisub-directory, this is discouraged in general. It is strongly preferred that
830dfb3ca7SStefan Hajnocziall events be declared directly in the sub-directory that uses them. The only
840dfb3ca7SStefan Hajnocziexception is where there are some shared trace events defined in the top level
850dfb3ca7SStefan Hajnoczidirectory trace-events file.  The top level directory generates trace files
860dfb3ca7SStefan Hajnocziwith a filename prefix of "trace/trace-root" instead of just "trace". This is
870dfb3ca7SStefan Hajnoczito avoid ambiguity between a trace.h in the current directory, vs the top level
880dfb3ca7SStefan Hajnoczidirectory.
89e50caf4aSStefan Hajnoczi
90e50caf4aSStefan HajnocziUsing trace events
91e50caf4aSStefan Hajnoczi------------------
92e50caf4aSStefan Hajnoczi
93e50caf4aSStefan HajnocziTrace events are invoked directly from source code like this::
94e50caf4aSStefan Hajnoczi
95e50caf4aSStefan Hajnoczi    #include "trace.h"  /* needed for trace event prototype */
96e50caf4aSStefan Hajnoczi
97e50caf4aSStefan Hajnoczi    void *qemu_vmalloc(size_t size)
98e50caf4aSStefan Hajnoczi    {
99e50caf4aSStefan Hajnoczi        void *ptr;
100e50caf4aSStefan Hajnoczi        size_t align = QEMU_VMALLOC_ALIGN;
101e50caf4aSStefan Hajnoczi
102e50caf4aSStefan Hajnoczi        if (size < align) {
103e50caf4aSStefan Hajnoczi            align = getpagesize();
104e50caf4aSStefan Hajnoczi        }
105e50caf4aSStefan Hajnoczi        ptr = qemu_memalign(align, size);
106e50caf4aSStefan Hajnoczi        trace_qemu_vmalloc(size, ptr);
107e50caf4aSStefan Hajnoczi        return ptr;
108e50caf4aSStefan Hajnoczi    }
109e50caf4aSStefan Hajnoczi
110e50caf4aSStefan HajnocziDeclaring trace events
111e50caf4aSStefan Hajnoczi----------------------
112e50caf4aSStefan Hajnoczi
113e50caf4aSStefan HajnocziThe "tracetool" script produces the trace.h header file which is included by
114e50caf4aSStefan Hajnoczievery source file that uses trace events.  Since many source files include
115e50caf4aSStefan Hajnoczitrace.h, it uses a minimum of types and other header files included to keep the
116e50caf4aSStefan Hajnoczinamespace clean and compile times and dependencies down.
117e50caf4aSStefan Hajnoczi
118e50caf4aSStefan HajnocziTrace events should use types as follows:
119e50caf4aSStefan Hajnoczi
120e50caf4aSStefan Hajnoczi * Use stdint.h types for fixed-size types.  Most offsets and guest memory
121e50caf4aSStefan Hajnoczi   addresses are best represented with uint32_t or uint64_t.  Use fixed-size
122e50caf4aSStefan Hajnoczi   types over primitive types whose size may change depending on the host
123e50caf4aSStefan Hajnoczi   (32-bit versus 64-bit) so trace events don't truncate values or break
124e50caf4aSStefan Hajnoczi   the build.
125e50caf4aSStefan Hajnoczi
126e50caf4aSStefan Hajnoczi * Use void * for pointers to structs or for arrays.  The trace.h header
127e50caf4aSStefan Hajnoczi   cannot include all user-defined struct declarations and it is therefore
128e50caf4aSStefan Hajnoczi   necessary to use void * for pointers to structs.
129e50caf4aSStefan Hajnoczi
130e50caf4aSStefan Hajnoczi * For everything else, use primitive scalar types (char, int, long) with the
131e50caf4aSStefan Hajnoczi   appropriate signedness.
132e50caf4aSStefan Hajnoczi
133e50caf4aSStefan Hajnoczi * Avoid floating point types (float and double) because SystemTap does not
134e50caf4aSStefan Hajnoczi   support them.  In most cases it is possible to round to an integer type
135e50caf4aSStefan Hajnoczi   instead.  This may require scaling the value first by multiplying it by 1000
136e50caf4aSStefan Hajnoczi   or the like when digits after the decimal point need to be preserved.
137e50caf4aSStefan Hajnoczi
138e50caf4aSStefan HajnocziFormat strings should reflect the types defined in the trace event.  Take
139e50caf4aSStefan Hajnoczispecial care to use PRId64 and PRIu64 for int64_t and uint64_t types,
140e50caf4aSStefan Hajnoczirespectively.  This ensures portability between 32- and 64-bit platforms.
141e50caf4aSStefan HajnocziFormat strings must not end with a newline character.  It is the responsibility
142e50caf4aSStefan Hajnocziof backends to adapt line ending for proper logging.
143e50caf4aSStefan Hajnoczi
144e50caf4aSStefan HajnocziEach event declaration will start with the event name, then its arguments,
145e50caf4aSStefan Hajnoczifinally a format string for pretty-printing. For example::
146e50caf4aSStefan Hajnoczi
147e50caf4aSStefan Hajnoczi    qemu_vmalloc(size_t size, void *ptr) "size %zu ptr %p"
148e50caf4aSStefan Hajnoczi    qemu_vfree(void *ptr) "ptr %p"
149e50caf4aSStefan Hajnoczi
150e50caf4aSStefan Hajnoczi
151e50caf4aSStefan HajnocziHints for adding new trace events
152e50caf4aSStefan Hajnoczi---------------------------------
153e50caf4aSStefan Hajnoczi
154e50caf4aSStefan Hajnoczi1. Trace state changes in the code.  Interesting points in the code usually
155e50caf4aSStefan Hajnoczi   involve a state change like starting, stopping, allocating, freeing.  State
156e50caf4aSStefan Hajnoczi   changes are good trace events because they can be used to understand the
157e50caf4aSStefan Hajnoczi   execution of the system.
158e50caf4aSStefan Hajnoczi
159e50caf4aSStefan Hajnoczi2. Trace guest operations.  Guest I/O accesses like reading device registers
160e50caf4aSStefan Hajnoczi   are good trace events because they can be used to understand guest
161e50caf4aSStefan Hajnoczi   interactions.
162e50caf4aSStefan Hajnoczi
163e50caf4aSStefan Hajnoczi3. Use correlator fields so the context of an individual line of trace output
164e50caf4aSStefan Hajnoczi   can be understood.  For example, trace the pointer returned by malloc and
165e50caf4aSStefan Hajnoczi   used as an argument to free.  This way mallocs and frees can be matched up.
166e50caf4aSStefan Hajnoczi   Trace events with no context are not very useful.
167e50caf4aSStefan Hajnoczi
168e50caf4aSStefan Hajnoczi4. Name trace events after their function.  If there are multiple trace events
169e50caf4aSStefan Hajnoczi   in one function, append a unique distinguisher at the end of the name.
170e50caf4aSStefan Hajnoczi
171e50caf4aSStefan HajnocziGeneric interface and monitor commands
172e50caf4aSStefan Hajnoczi======================================
173e50caf4aSStefan Hajnoczi
174e50caf4aSStefan HajnocziYou can programmatically query and control the state of trace events through a
175e50caf4aSStefan Hajnoczibackend-agnostic interface provided by the header "trace/control.h".
176e50caf4aSStefan Hajnoczi
177e50caf4aSStefan HajnocziNote that some of the backends do not provide an implementation for some parts
178e50caf4aSStefan Hajnocziof this interface, in which case QEMU will just print a warning (please refer to
179e50caf4aSStefan Hajnocziheader "trace/control.h" to see which routines are backend-dependent).
180e50caf4aSStefan Hajnoczi
181e50caf4aSStefan HajnocziThe state of events can also be queried and modified through monitor commands:
182e50caf4aSStefan Hajnoczi
183e50caf4aSStefan Hajnoczi* ``info trace-events``
184e50caf4aSStefan Hajnoczi  View available trace events and their state.  State 1 means enabled, state 0
185e50caf4aSStefan Hajnoczi  means disabled.
186e50caf4aSStefan Hajnoczi
187e50caf4aSStefan Hajnoczi* ``trace-event NAME on|off``
188e50caf4aSStefan Hajnoczi  Enable/disable a given trace event or a group of events (using wildcards).
189e50caf4aSStefan Hajnoczi
190e50caf4aSStefan HajnocziThe "--trace events=<file>" command line argument can be used to enable the
191e50caf4aSStefan Hajnoczievents listed in <file> from the very beginning of the program. This file must
192e50caf4aSStefan Hajnoczicontain one event name per line.
193e50caf4aSStefan Hajnoczi
194e50caf4aSStefan HajnocziIf a line in the "--trace events=<file>" file begins with a '-', the trace event
195e50caf4aSStefan Hajnocziwill be disabled instead of enabled.  This is useful when a wildcard was used
196e50caf4aSStefan Hajnoczito enable an entire family of events but one noisy event needs to be disabled.
197e50caf4aSStefan Hajnoczi
198e50caf4aSStefan HajnocziWildcard matching is supported in both the monitor command "trace-event" and the
199e50caf4aSStefan Hajnoczievents list file. That means you can enable/disable the events having a common
200e50caf4aSStefan Hajnocziprefix in a batch. For example, virtio-blk trace events could be enabled using
201e50caf4aSStefan Hajnoczithe following monitor command::
202e50caf4aSStefan Hajnoczi
203e50caf4aSStefan Hajnoczi    trace-event virtio_blk_* on
204e50caf4aSStefan Hajnoczi
205e50caf4aSStefan HajnocziTrace backends
206e50caf4aSStefan Hajnoczi==============
207e50caf4aSStefan Hajnoczi
208e50caf4aSStefan HajnocziThe "tracetool" script automates tedious trace event code generation and also
209e50caf4aSStefan Hajnoczikeeps the trace event declarations independent of the trace backend.  The trace
210e50caf4aSStefan Hajnoczievents are not tightly coupled to a specific trace backend, such as LTTng or
211e50caf4aSStefan HajnocziSystemTap.  Support for trace backends can be added by extending the "tracetool"
212e50caf4aSStefan Hajnocziscript.
213e50caf4aSStefan Hajnoczi
214e50caf4aSStefan HajnocziThe trace backends are chosen at configure time::
215e50caf4aSStefan Hajnoczi
2167e46d5f3SStefan Hajnoczi    ./configure --enable-trace-backends=simple,dtrace
217e50caf4aSStefan Hajnoczi
218e50caf4aSStefan HajnocziFor a list of supported trace backends, try ./configure --help or see below.
219e50caf4aSStefan HajnocziIf multiple backends are enabled, the trace is sent to them all.
220e50caf4aSStefan Hajnoczi
221e50caf4aSStefan HajnocziIf no backends are explicitly selected, configure will default to the
222e50caf4aSStefan Hajnoczi"log" backend.
223e50caf4aSStefan Hajnoczi
224e50caf4aSStefan HajnocziThe following subsections describe the supported trace backends.
225e50caf4aSStefan Hajnoczi
226e50caf4aSStefan HajnocziNop
227e50caf4aSStefan Hajnoczi---
228e50caf4aSStefan Hajnoczi
229e50caf4aSStefan HajnocziThe "nop" backend generates empty trace event functions so that the compiler
230e50caf4aSStefan Hajnoczican optimize out trace events completely.  This imposes no performance
231e50caf4aSStefan Hajnoczipenalty.
232e50caf4aSStefan Hajnoczi
233e50caf4aSStefan HajnocziNote that regardless of the selected trace backend, events with the "disable"
234e50caf4aSStefan Hajnocziproperty will be generated with the "nop" backend.
235e50caf4aSStefan Hajnoczi
236e50caf4aSStefan HajnocziLog
237e50caf4aSStefan Hajnoczi---
238e50caf4aSStefan Hajnoczi
239e50caf4aSStefan HajnocziThe "log" backend sends trace events directly to standard error.  This
240e50caf4aSStefan Hajnoczieffectively turns trace events into debug printfs.
241e50caf4aSStefan Hajnoczi
242e50caf4aSStefan HajnocziThis is the simplest backend and can be used together with existing code that
243e50caf4aSStefan Hajnocziuses DPRINTF().
244e50caf4aSStefan Hajnoczi
245418ed142SStefan HajnocziThe -msg timestamp=on|off command-line option controls whether or not to print
246418ed142SStefan Hajnoczithe tid/timestamp prefix for each trace event.
247418ed142SStefan Hajnoczi
248e50caf4aSStefan HajnocziSimpletrace
249e50caf4aSStefan Hajnoczi-----------
250e50caf4aSStefan Hajnoczi
2517e46d5f3SStefan HajnocziThe "simple" backend writes binary trace logs to a file from a thread, making
2527e46d5f3SStefan Hajnocziit lower overhead than the "log" backend. A Python API is available for writing
2537e46d5f3SStefan Hajnoczioffline trace file analysis scripts. It may not be as powerful as
2547e46d5f3SStefan Hajnocziplatform-specific or third-party trace backends but it is portable and has no
2557e46d5f3SStefan Hajnoczispecial library dependencies.
256e50caf4aSStefan Hajnoczi
257e50caf4aSStefan HajnocziMonitor commands
258e50caf4aSStefan Hajnoczi~~~~~~~~~~~~~~~~
259e50caf4aSStefan Hajnoczi
260e50caf4aSStefan Hajnoczi* ``trace-file on|off|flush|set <path>``
261e50caf4aSStefan Hajnoczi  Enable/disable/flush the trace file or set the trace file name.
262e50caf4aSStefan Hajnoczi
263e50caf4aSStefan HajnocziAnalyzing trace files
264e50caf4aSStefan Hajnoczi~~~~~~~~~~~~~~~~~~~~~
265e50caf4aSStefan Hajnoczi
266e50caf4aSStefan HajnocziThe "simple" backend produces binary trace files that can be formatted with the
267e50caf4aSStefan Hajnoczisimpletrace.py script.  The script takes the "trace-events-all" file and the
268e50caf4aSStefan Hajnoczibinary trace::
269e50caf4aSStefan Hajnoczi
270e50caf4aSStefan Hajnoczi    ./scripts/simpletrace.py trace-events-all trace-12345
271e50caf4aSStefan Hajnoczi
272e50caf4aSStefan HajnocziYou must ensure that the same "trace-events-all" file was used to build QEMU,
273e50caf4aSStefan Hajnocziotherwise trace event declarations may have changed and output will not be
274e50caf4aSStefan Hajnocziconsistent.
275e50caf4aSStefan Hajnoczi
276e50caf4aSStefan HajnocziFtrace
277e50caf4aSStefan Hajnoczi------
278e50caf4aSStefan Hajnoczi
279e50caf4aSStefan HajnocziThe "ftrace" backend writes trace data to ftrace marker. This effectively
280e50caf4aSStefan Hajnoczisends trace events to ftrace ring buffer, and you can compare qemu trace
281e50caf4aSStefan Hajnoczidata and kernel(especially kvm.ko when using KVM) trace data.
282e50caf4aSStefan Hajnoczi
283e50caf4aSStefan Hajnocziif you use KVM, enable kvm events in ftrace::
284e50caf4aSStefan Hajnoczi
285e50caf4aSStefan Hajnoczi   # echo 1 > /sys/kernel/debug/tracing/events/kvm/enable
286e50caf4aSStefan Hajnoczi
287e50caf4aSStefan HajnocziAfter running qemu by root user, you can get the trace::
288e50caf4aSStefan Hajnoczi
289e50caf4aSStefan Hajnoczi   # cat /sys/kernel/debug/tracing/trace
290e50caf4aSStefan Hajnoczi
291e50caf4aSStefan HajnocziRestriction: "ftrace" backend is restricted to Linux only.
292e50caf4aSStefan Hajnoczi
293e50caf4aSStefan HajnocziSyslog
294e50caf4aSStefan Hajnoczi------
295e50caf4aSStefan Hajnoczi
296e50caf4aSStefan HajnocziThe "syslog" backend sends trace events using the POSIX syslog API. The log
297e50caf4aSStefan Hajnocziis opened specifying the LOG_DAEMON facility and LOG_PID option (so events
298e50caf4aSStefan Hajnocziare tagged with the pid of the particular QEMU process that generated
299e50caf4aSStefan Hajnoczithem). All events are logged at LOG_INFO level.
300e50caf4aSStefan Hajnoczi
301e50caf4aSStefan HajnocziNOTE: syslog may squash duplicate consecutive trace events and apply rate
302e50caf4aSStefan Hajnoczi      limiting.
303e50caf4aSStefan Hajnoczi
304e50caf4aSStefan HajnocziRestriction: "syslog" backend is restricted to POSIX compliant OS.
305e50caf4aSStefan Hajnoczi
306e50caf4aSStefan HajnocziLTTng Userspace Tracer
307e50caf4aSStefan Hajnoczi----------------------
308e50caf4aSStefan Hajnoczi
309e50caf4aSStefan HajnocziThe "ust" backend uses the LTTng Userspace Tracer library.  There are no
310e50caf4aSStefan Hajnoczimonitor commands built into QEMU, instead UST utilities should be used to list,
311e50caf4aSStefan Hajnoczienable/disable, and dump traces.
312e50caf4aSStefan Hajnoczi
313e50caf4aSStefan HajnocziPackage lttng-tools is required for userspace tracing. You must ensure that the
314e50caf4aSStefan Hajnoczicurrent user belongs to the "tracing" group, or manually launch the
315e50caf4aSStefan Hajnoczilttng-sessiond daemon for the current user prior to running any instance of
316e50caf4aSStefan HajnocziQEMU.
317e50caf4aSStefan Hajnoczi
318e50caf4aSStefan HajnocziWhile running an instrumented QEMU, LTTng should be able to list all available
319e50caf4aSStefan Hajnoczievents::
320e50caf4aSStefan Hajnoczi
321e50caf4aSStefan Hajnoczi    lttng list -u
322e50caf4aSStefan Hajnoczi
323e50caf4aSStefan HajnocziCreate tracing session::
324e50caf4aSStefan Hajnoczi
325e50caf4aSStefan Hajnoczi    lttng create mysession
326e50caf4aSStefan Hajnoczi
327e50caf4aSStefan HajnocziEnable events::
328e50caf4aSStefan Hajnoczi
329e50caf4aSStefan Hajnoczi    lttng enable-event qemu:g_malloc -u
330e50caf4aSStefan Hajnoczi
331e50caf4aSStefan HajnocziWhere the events can either be a comma-separated list of events, or "-a" to
332e50caf4aSStefan Hajnoczienable all tracepoint events. Start and stop tracing as needed::
333e50caf4aSStefan Hajnoczi
334e50caf4aSStefan Hajnoczi    lttng start
335e50caf4aSStefan Hajnoczi    lttng stop
336e50caf4aSStefan Hajnoczi
337e50caf4aSStefan HajnocziView the trace::
338e50caf4aSStefan Hajnoczi
339e50caf4aSStefan Hajnoczi    lttng view
340e50caf4aSStefan Hajnoczi
341e50caf4aSStefan HajnocziDestroy tracing session::
342e50caf4aSStefan Hajnoczi
343e50caf4aSStefan Hajnoczi    lttng destroy
344e50caf4aSStefan Hajnoczi
345e50caf4aSStefan HajnocziBabeltrace can be used at any later time to view the trace::
346e50caf4aSStefan Hajnoczi
347e50caf4aSStefan Hajnoczi    babeltrace $HOME/lttng-traces/mysession-<date>-<time>
348e50caf4aSStefan Hajnoczi
349e50caf4aSStefan HajnocziSystemTap
350e50caf4aSStefan Hajnoczi---------
351e50caf4aSStefan Hajnoczi
352e50caf4aSStefan HajnocziThe "dtrace" backend uses DTrace sdt probes but has only been tested with
353e50caf4aSStefan HajnocziSystemTap.  When SystemTap support is detected a .stp file with wrapper probes
354e50caf4aSStefan Hajnocziis generated to make use in scripts more convenient.  This step can also be
355e50caf4aSStefan Hajnocziperformed manually after a build in order to change the binary name in the .stp
356e50caf4aSStefan Hajnocziprobes::
357e50caf4aSStefan Hajnoczi
358e50caf4aSStefan Hajnoczi    scripts/tracetool.py --backends=dtrace --format=stap \
359e50caf4aSStefan Hajnoczi                         --binary path/to/qemu-binary \
360*081340d1SDaniel P. Berrangé                         --probe-prefix qemu.system.x86_64 \
361e50caf4aSStefan Hajnoczi                         --group=all \
362e50caf4aSStefan Hajnoczi                         trace-events-all \
363e50caf4aSStefan Hajnoczi                         qemu.stp
364e50caf4aSStefan Hajnoczi
365e50caf4aSStefan HajnocziTo facilitate simple usage of systemtap where there merely needs to be printf
366e50caf4aSStefan Hajnoczilogging of certain probes, a helper script "qemu-trace-stap" is provided.
367e50caf4aSStefan HajnocziConsult its manual page for guidance on its usage.
368e50caf4aSStefan Hajnoczi
369e50caf4aSStefan HajnocziTrace event properties
370e50caf4aSStefan Hajnoczi======================
371e50caf4aSStefan Hajnoczi
372e50caf4aSStefan HajnocziEach event in the "trace-events-all" file can be prefixed with a space-separated
373e50caf4aSStefan Hajnoczilist of zero or more of the following event properties.
374e50caf4aSStefan Hajnoczi
375e50caf4aSStefan Hajnoczi"disable"
376e50caf4aSStefan Hajnoczi---------
377e50caf4aSStefan Hajnoczi
378e50caf4aSStefan HajnocziIf a specific trace event is going to be invoked a huge number of times, this
379e50caf4aSStefan Hajnoczimight have a noticeable performance impact even when the event is
380e50caf4aSStefan Hajnocziprogrammatically disabled.
381e50caf4aSStefan Hajnoczi
382e50caf4aSStefan HajnocziIn this case you should declare such event with the "disable" property. This
383e50caf4aSStefan Hajnocziwill effectively disable the event at compile time (by using the "nop" backend),
384e50caf4aSStefan Hajnoczithus having no performance impact at all on regular builds (i.e., unless you
385e50caf4aSStefan Hajnocziedit the "trace-events-all" file).
386e50caf4aSStefan Hajnoczi
387e50caf4aSStefan HajnocziIn addition, there might be cases where relatively complex computations must be
388e50caf4aSStefan Hajnocziperformed to generate values that are only used as arguments for a trace
389e50caf4aSStefan Hajnoczifunction. In these cases you can use 'trace_event_get_state_backends()' to
390e50caf4aSStefan Hajnocziguard such computations, so they are skipped if the event has been either
391e50caf4aSStefan Hajnoczicompile-time disabled or run-time disabled. If the event is compile-time
392e50caf4aSStefan Hajnoczidisabled, this check will have no performance impact.
393e50caf4aSStefan Hajnoczi
394e50caf4aSStefan Hajnoczi::
395e50caf4aSStefan Hajnoczi
396e50caf4aSStefan Hajnoczi    #include "trace.h"  /* needed for trace event prototype */
397e50caf4aSStefan Hajnoczi
398e50caf4aSStefan Hajnoczi    void *qemu_vmalloc(size_t size)
399e50caf4aSStefan Hajnoczi    {
400e50caf4aSStefan Hajnoczi        void *ptr;
401e50caf4aSStefan Hajnoczi        size_t align = QEMU_VMALLOC_ALIGN;
402e50caf4aSStefan Hajnoczi
403e50caf4aSStefan Hajnoczi        if (size < align) {
404e50caf4aSStefan Hajnoczi            align = getpagesize();
405e50caf4aSStefan Hajnoczi        }
406e50caf4aSStefan Hajnoczi        ptr = qemu_memalign(align, size);
407e50caf4aSStefan Hajnoczi        if (trace_event_get_state_backends(TRACE_QEMU_VMALLOC)) {
408e50caf4aSStefan Hajnoczi            void *complex;
409e50caf4aSStefan Hajnoczi            /* some complex computations to produce the 'complex' value */
410e50caf4aSStefan Hajnoczi            trace_qemu_vmalloc(size, ptr, complex);
411e50caf4aSStefan Hajnoczi        }
412e50caf4aSStefan Hajnoczi        return ptr;
413e50caf4aSStefan Hajnoczi    }
414e50caf4aSStefan Hajnoczi
415