| 
									
										
										
										
											2022-01-26 17:11:28 +01:00
										 |  |  | .. _tracing:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | =======
 | 
					
						
							|  |  |  | Tracing
 | 
					
						
							|  |  |  | =======
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Introduction
 | 
					
						
							|  |  |  | ============
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | This document describes the tracing infrastructure in QEMU and how to use it
 | 
					
						
							|  |  |  | for debugging, profiling, and observing execution.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Quickstart
 | 
					
						
							|  |  |  | ==========
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:23 +00:00
										 |  |  | Enable tracing of ``memory_region_ops_read`` and ``memory_region_ops_write``
 | 
					
						
							|  |  |  | events::
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:23 +00:00
										 |  |  |     $ qemu --trace "memory_region_ops_*" ...
 | 
					
						
							|  |  |  |     ...
 | 
					
						
							|  |  |  |     719585@1608130130.441188:memory_region_ops_read cpu 0 mr 0x562fdfbb3820 addr 0x3cc value 0x67 size 1
 | 
					
						
							|  |  |  |     719585@1608130130.441190:memory_region_ops_write cpu 0 mr 0x562fdfbd2f00 addr 0x3d4 value 0x70e size 2
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:23 +00:00
										 |  |  | This output comes from the "log" trace backend that is enabled by default when
 | 
					
						
							|  |  |  | ``./configure --enable-trace-backends=BACKENDS`` was not explicitly specified.
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 16:58:58 +00:00
										 |  |  | Multiple patterns can be specified by repeating the ``--trace`` option::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $ qemu --trace "kvm_*" --trace "virtio_*" ...
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | When patterns are used frequently it is more convenient to store them in a
 | 
					
						
							|  |  |  | file to avoid long command-line options::
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:23 +00:00
										 |  |  |     $ echo "memory_region_ops_*" >/tmp/events
 | 
					
						
							| 
									
										
										
										
											2021-01-12 16:58:58 +00:00
										 |  |  |     $ echo "kvm_*" >>/tmp/events
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:23 +00:00
										 |  |  |     $ qemu --trace events=/tmp/events ...
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Trace events
 | 
					
						
							|  |  |  | ============
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Sub-directory setup
 | 
					
						
							|  |  |  | -------------------
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 16:58:59 +00:00
										 |  |  | Each directory in the source tree can declare a set of trace events in a local
 | 
					
						
							|  |  |  | "trace-events" file. All directories which contain "trace-events" files must be
 | 
					
						
							|  |  |  | listed in the "trace_events_subdirs" variable in the top level meson.build
 | 
					
						
							|  |  |  | file. During build, the "trace-events" file in each listed subdirectory will be
 | 
					
						
							|  |  |  | processed by the "tracetool" script to generate code for the trace events.
 | 
					
						
							| 
									
										
										
										
											2017-01-25 16:14:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The individual "trace-events" files are merged into a "trace-events-all" file,
 | 
					
						
							| 
									
										
										
										
											2022-04-20 17:25:55 +04:00
										 |  |  | which is also installed into "/usr/share/qemu".
 | 
					
						
							| 
									
										
										
										
											2017-01-25 16:14:16 +00:00
										 |  |  | This merged file is to be used by the "simpletrace.py" script to later analyse
 | 
					
						
							|  |  |  | traces in the simpletrace data format.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 16:58:59 +00:00
										 |  |  | The following files are automatically generated in <builddir>/trace/ during the
 | 
					
						
							|  |  |  | build:
 | 
					
						
							| 
									
										
										
										
											2017-01-25 16:14:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 16:58:59 +00:00
										 |  |  |  - trace-<subdir>.c - the trace event state declarations
 | 
					
						
							|  |  |  |  - trace-<subdir>.h - the trace event enums and probe functions
 | 
					
						
							|  |  |  |  - trace-dtrace-<subdir>.h - DTrace event probe specification
 | 
					
						
							|  |  |  |  - trace-dtrace-<subdir>.dtrace - DTrace event probe helper declaration
 | 
					
						
							|  |  |  |  - trace-dtrace-<subdir>.o - binary DTrace provider (generated by dtrace)
 | 
					
						
							|  |  |  |  - trace-ust-<subdir>.h - UST event probe helper declarations
 | 
					
						
							| 
									
										
										
										
											2017-01-25 16:14:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 16:58:59 +00:00
										 |  |  | Here <subdir> is the sub-directory path with '/' replaced by '_'. For example,
 | 
					
						
							|  |  |  | "accel/kvm" becomes "accel_kvm" and the final filename for "trace-<subdir>.c"
 | 
					
						
							|  |  |  | becomes "trace-accel_kvm.c".
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Source files in the source tree do not directly include generated files in
 | 
					
						
							|  |  |  | "<builddir>/trace/". Instead they #include the local "trace.h" file, without
 | 
					
						
							|  |  |  | any sub-directory path prefix. eg io/channel-buffer.c would do::
 | 
					
						
							| 
									
										
										
										
											2017-01-25 16:14:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   #include "trace.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 16:58:59 +00:00
										 |  |  | The "io/trace.h" file must be created manually with an #include of the
 | 
					
						
							|  |  |  | corresponding "trace/trace-<subdir>.h" file that will be generated in the
 | 
					
						
							|  |  |  | builddir::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $ echo '#include "trace/trace-io.h"' >io/trace.h
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | While it is possible to include a trace.h file from outside a source file's own
 | 
					
						
							|  |  |  | sub-directory, this is discouraged in general. It is strongly preferred that
 | 
					
						
							|  |  |  | all events be declared directly in the sub-directory that uses them. The only
 | 
					
						
							|  |  |  | exception is where there are some shared trace events defined in the top level
 | 
					
						
							|  |  |  | directory trace-events file.  The top level directory generates trace files
 | 
					
						
							|  |  |  | with a filename prefix of "trace/trace-root" instead of just "trace". This is
 | 
					
						
							|  |  |  | to avoid ambiguity between a trace.h in the current directory, vs the top level
 | 
					
						
							|  |  |  | directory.
 | 
					
						
							| 
									
										
										
										
											2017-01-25 16:14:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Using trace events
 | 
					
						
							|  |  |  | ------------------
 | 
					
						
							| 
									
										
										
										
											2016-06-16 09:39:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Trace events are invoked directly from source code like this::
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     #include "trace.h"  /* needed for trace event prototype */
 | 
					
						
							| 
									
										
										
										
											2011-08-31 20:31:10 +02:00
										 |  |  |     
 | 
					
						
							| 
									
										
										
										
											2011-09-20 21:03:48 +02:00
										 |  |  |     void *qemu_vmalloc(size_t size)
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  |     {
 | 
					
						
							|  |  |  |         void *ptr;
 | 
					
						
							| 
									
										
										
										
											2011-09-20 21:03:48 +02:00
										 |  |  |         size_t align = QEMU_VMALLOC_ALIGN;
 | 
					
						
							|  |  |  |      
 | 
					
						
							|  |  |  |         if (size < align) {
 | 
					
						
							|  |  |  |             align = getpagesize();
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2011-09-20 21:03:48 +02:00
										 |  |  |         ptr = qemu_memalign(align, size);
 | 
					
						
							|  |  |  |         trace_qemu_vmalloc(size, ptr);
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  |         return ptr;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Declaring trace events
 | 
					
						
							|  |  |  | ----------------------
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-06 20:33:56 +02:00
										 |  |  | The "tracetool" script produces the trace.h header file which is included by
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | every source file that uses trace events.  Since many source files include
 | 
					
						
							| 
									
										
										
										
											2011-04-06 20:33:56 +02:00
										 |  |  | trace.h, it uses a minimum of types and other header files included to keep the
 | 
					
						
							|  |  |  | namespace clean and compile times and dependencies down.
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | Trace events should use types as follows:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * Use stdint.h types for fixed-size types.  Most offsets and guest memory
 | 
					
						
							|  |  |  |    addresses are best represented with uint32_t or uint64_t.  Use fixed-size
 | 
					
						
							|  |  |  |    types over primitive types whose size may change depending on the host
 | 
					
						
							|  |  |  |    (32-bit versus 64-bit) so trace events don't truncate values or break
 | 
					
						
							|  |  |  |    the build.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * Use void * for pointers to structs or for arrays.  The trace.h header
 | 
					
						
							|  |  |  |    cannot include all user-defined struct declarations and it is therefore
 | 
					
						
							|  |  |  |    necessary to use void * for pointers to structs.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * For everything else, use primitive scalar types (char, int, long) with the
 | 
					
						
							|  |  |  |    appropriate signedness.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 16:02:54 +01:00
										 |  |  |  * Avoid floating point types (float and double) because SystemTap does not
 | 
					
						
							|  |  |  |    support them.  In most cases it is possible to round to an integer type
 | 
					
						
							|  |  |  |    instead.  This may require scaling the value first by multiplying it by 1000
 | 
					
						
							|  |  |  |    or the like when digits after the decimal point need to be preserved.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-05 14:28:50 +01:00
										 |  |  | Format strings should reflect the types defined in the trace event.  Take
 | 
					
						
							|  |  |  | special care to use PRId64 and PRIu64 for int64_t and uint64_t types,
 | 
					
						
							| 
									
										
										
										
											2011-09-13 13:34:35 +01:00
										 |  |  | respectively.  This ensures portability between 32- and 64-bit platforms.
 | 
					
						
							| 
									
										
										
										
											2019-09-16 11:51:21 +02:00
										 |  |  | Format strings must not end with a newline character.  It is the responsibility
 | 
					
						
							|  |  |  | of backends to adapt line ending for proper logging.
 | 
					
						
							| 
									
										
										
										
											2010-10-05 14:28:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-25 16:14:16 +00:00
										 |  |  | Each event declaration will start with the event name, then its arguments,
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | finally a format string for pretty-printing. For example::
 | 
					
						
							| 
									
										
										
										
											2017-01-25 16:14:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     qemu_vmalloc(size_t size, void *ptr) "size %zu ptr %p"
 | 
					
						
							|  |  |  |     qemu_vfree(void *ptr) "ptr %p"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Hints for adding new trace events
 | 
					
						
							|  |  |  | ---------------------------------
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 1. Trace state changes in the code.  Interesting points in the code usually
 | 
					
						
							|  |  |  |    involve a state change like starting, stopping, allocating, freeing.  State
 | 
					
						
							|  |  |  |    changes are good trace events because they can be used to understand the
 | 
					
						
							|  |  |  |    execution of the system.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 2. Trace guest operations.  Guest I/O accesses like reading device registers
 | 
					
						
							|  |  |  |    are good trace events because they can be used to understand guest
 | 
					
						
							|  |  |  |    interactions.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 3. Use correlator fields so the context of an individual line of trace output
 | 
					
						
							|  |  |  |    can be understood.  For example, trace the pointer returned by malloc and
 | 
					
						
							|  |  |  |    used as an argument to free.  This way mallocs and frees can be matched up.
 | 
					
						
							|  |  |  |    Trace events with no context are not very useful.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 4. Name trace events after their function.  If there are multiple trace events
 | 
					
						
							|  |  |  |    in one function, append a unique distinguisher at the end of the name.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Generic interface and monitor commands
 | 
					
						
							|  |  |  | ======================================
 | 
					
						
							| 
									
										
										
										
											2011-08-31 20:31:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 14:47:38 +01:00
										 |  |  | You can programmatically query and control the state of trace events through a
 | 
					
						
							|  |  |  | backend-agnostic interface provided by the header "trace/control.h".
 | 
					
						
							| 
									
										
										
										
											2011-08-31 20:31:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 14:47:38 +01:00
										 |  |  | Note that some of the backends do not provide an implementation for some parts
 | 
					
						
							|  |  |  | of this interface, in which case QEMU will just print a warning (please refer to
 | 
					
						
							|  |  |  | header "trace/control.h" to see which routines are backend-dependent).
 | 
					
						
							| 
									
										
										
										
											2011-08-31 20:31:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 14:47:38 +01:00
										 |  |  | The state of events can also be queried and modified through monitor commands:
 | 
					
						
							| 
									
										
										
										
											2011-08-31 20:31:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | * ``info trace-events``
 | 
					
						
							| 
									
										
										
										
											2011-08-31 20:31:24 +02:00
										 |  |  |   View available trace events and their state.  State 1 means enabled, state 0
 | 
					
						
							|  |  |  |   means disabled.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | * ``trace-event NAME on|off``
 | 
					
						
							| 
									
										
										
										
											2013-03-05 14:47:38 +01:00
										 |  |  |   Enable/disable a given trace event or a group of events (using wildcards).
 | 
					
						
							| 
									
										
										
										
											2011-08-31 20:31:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-04 11:17:27 +08:00
										 |  |  | The "--trace events=<file>" command line argument can be used to enable the
 | 
					
						
							| 
									
										
										
										
											2011-08-31 20:31:31 +02:00
										 |  |  | events listed in <file> from the very beginning of the program. This file must
 | 
					
						
							|  |  |  | contain one event name per line.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-04 11:17:27 +08:00
										 |  |  | If a line in the "--trace events=<file>" file begins with a '-', the trace event
 | 
					
						
							| 
									
										
										
										
											2012-11-05 08:48:29 +01:00
										 |  |  | will be disabled instead of enabled.  This is useful when a wildcard was used
 | 
					
						
							|  |  |  | to enable an entire family of events but one noisy event needs to be disabled.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-05 14:47:38 +01:00
										 |  |  | Wildcard matching is supported in both the monitor command "trace-event" and the
 | 
					
						
							|  |  |  | events list file. That means you can enable/disable the events having a common
 | 
					
						
							|  |  |  | prefix in a batch. For example, virtio-blk trace events could be enabled using
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | the following monitor command::
 | 
					
						
							| 
									
										
										
										
											2013-03-05 14:47:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     trace-event virtio_blk_* on
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Trace backends
 | 
					
						
							|  |  |  | ==============
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-06 20:33:56 +02:00
										 |  |  | The "tracetool" script automates tedious trace event code generation and also
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | keeps the trace event declarations independent of the trace backend.  The trace
 | 
					
						
							|  |  |  | events are not tightly coupled to a specific trace backend, such as LTTng or
 | 
					
						
							| 
									
										
										
										
											2011-04-06 20:33:56 +02:00
										 |  |  | SystemTap.  Support for trace backends can be added by extending the "tracetool"
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | script.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | The trace backends are chosen at configure time::
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:23 +00:00
										 |  |  |     ./configure --enable-trace-backends=simple,dtrace
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | For a list of supported trace backends, try ./configure --help or see below.
 | 
					
						
							| 
									
										
										
										
											2014-10-07 15:12:41 +01:00
										 |  |  | If multiple backends are enabled, the trace is sent to them all.
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-04 16:27:17 +00:00
										 |  |  | If no backends are explicitly selected, configure will default to the
 | 
					
						
							|  |  |  | "log" backend.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | The following subsections describe the supported trace backends.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Nop
 | 
					
						
							|  |  |  | ---
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | The "nop" backend generates empty trace event functions so that the compiler
 | 
					
						
							| 
									
										
										
										
											2016-11-04 16:27:17 +00:00
										 |  |  | can optimize out trace events completely.  This imposes no performance
 | 
					
						
							|  |  |  | penalty.
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-31 20:31:38 +02:00
										 |  |  | Note that regardless of the selected trace backend, events with the "disable"
 | 
					
						
							|  |  |  | property will be generated with the "nop" backend.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Log
 | 
					
						
							|  |  |  | ---
 | 
					
						
							| 
									
										
										
										
											2011-02-23 14:00:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 21:00:14 +00:00
										 |  |  | The "log" backend sends trace events directly to standard error.  This
 | 
					
						
							| 
									
										
										
										
											2011-02-23 14:00:21 +00:00
										 |  |  | effectively turns trace events into debug printfs.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This is the simplest backend and can be used together with existing code that
 | 
					
						
							|  |  |  | uses DPRINTF().
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-25 11:35:07 +00:00
										 |  |  | The -msg timestamp=on|off command-line option controls whether or not to print
 | 
					
						
							|  |  |  | the tid/timestamp prefix for each trace event.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Simpletrace
 | 
					
						
							|  |  |  | -----------
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:23 +00:00
										 |  |  | The "simple" backend writes binary trace logs to a file from a thread, making
 | 
					
						
							|  |  |  | it lower overhead than the "log" backend. A Python API is available for writing
 | 
					
						
							|  |  |  | offline trace file analysis scripts. It may not be as powerful as
 | 
					
						
							|  |  |  | platform-specific or third-party trace backends but it is portable and has no
 | 
					
						
							|  |  |  | special library dependencies.
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Monitor commands
 | 
					
						
							|  |  |  | ~~~~~~~~~~~~~~~~
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | * ``trace-file on|off|flush|set <path>``
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:21 +00:00
										 |  |  |   Enable/disable/flush the trace file or set the trace file name.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Analyzing trace files
 | 
					
						
							|  |  |  | ~~~~~~~~~~~~~~~~~~~~~
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The "simple" backend produces binary trace files that can be formatted with the
 | 
					
						
							|  |  |  | simpletrace.py script.  The script takes the "trace-events-all" file and the
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | binary trace::
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ./scripts/simpletrace.py trace-events-all trace-12345
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You must ensure that the same "trace-events-all" file was used to build QEMU,
 | 
					
						
							|  |  |  | otherwise trace event declarations may have changed and output will not be
 | 
					
						
							|  |  |  | consistent.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Ftrace
 | 
					
						
							|  |  |  | ------
 | 
					
						
							| 
									
										
										
										
											2013-04-11 20:25:16 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | The "ftrace" backend writes trace data to ftrace marker. This effectively
 | 
					
						
							|  |  |  | sends trace events to ftrace ring buffer, and you can compare qemu trace
 | 
					
						
							|  |  |  | data and kernel(especially kvm.ko when using KVM) trace data.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | if you use KVM, enable kvm events in ftrace::
 | 
					
						
							| 
									
										
										
										
											2013-04-11 20:25:16 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |    # echo 1 > /sys/kernel/debug/tracing/events/kvm/enable
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | After running qemu by root user, you can get the trace::
 | 
					
						
							| 
									
										
										
										
											2013-04-11 20:25:16 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |    # cat /sys/kernel/debug/tracing/trace
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Restriction: "ftrace" backend is restricted to Linux only.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Syslog
 | 
					
						
							|  |  |  | ------
 | 
					
						
							| 
									
										
										
										
											2016-08-04 14:44:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | The "syslog" backend sends trace events using the POSIX syslog API. The log
 | 
					
						
							|  |  |  | is opened specifying the LOG_DAEMON facility and LOG_PID option (so events
 | 
					
						
							|  |  |  | are tagged with the pid of the particular QEMU process that generated
 | 
					
						
							|  |  |  | them). All events are logged at LOG_INFO level.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NOTE: syslog may squash duplicate consecutive trace events and apply rate
 | 
					
						
							|  |  |  |       limiting.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Restriction: "syslog" backend is restricted to POSIX compliant OS.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | LTTng Userspace Tracer
 | 
					
						
							|  |  |  | ----------------------
 | 
					
						
							| 
									
										
										
										
											2010-06-22 15:07:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | The "ust" backend uses the LTTng Userspace Tracer library.  There are no
 | 
					
						
							|  |  |  | monitor commands built into QEMU, instead UST utilities should be used to list,
 | 
					
						
							|  |  |  | enable/disable, and dump traces.
 | 
					
						
							| 
									
										
										
										
											2011-02-23 14:00:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-29 22:47:57 -05:00
										 |  |  | Package lttng-tools is required for userspace tracing. You must ensure that the
 | 
					
						
							|  |  |  | current user belongs to the "tracing" group, or manually launch the
 | 
					
						
							|  |  |  | lttng-sessiond daemon for the current user prior to running any instance of
 | 
					
						
							|  |  |  | QEMU.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | While running an instrumented QEMU, LTTng should be able to list all available
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | events::
 | 
					
						
							| 
									
										
										
										
											2014-01-29 22:47:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lttng list -u
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Create tracing session::
 | 
					
						
							| 
									
										
										
										
											2014-01-29 22:47:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lttng create mysession
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Enable events::
 | 
					
						
							| 
									
										
										
										
											2014-01-29 22:47:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lttng enable-event qemu:g_malloc -u
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Where the events can either be a comma-separated list of events, or "-a" to
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | enable all tracepoint events. Start and stop tracing as needed::
 | 
					
						
							| 
									
										
										
										
											2014-01-29 22:47:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lttng start
 | 
					
						
							|  |  |  |     lttng stop
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | View the trace::
 | 
					
						
							| 
									
										
										
										
											2014-01-29 22:47:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lttng view
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Destroy tracing session::
 | 
					
						
							| 
									
										
										
										
											2014-01-29 22:47:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lttng destroy
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Babeltrace can be used at any later time to view the trace::
 | 
					
						
							| 
									
										
										
										
											2014-01-29 22:47:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     babeltrace $HOME/lttng-traces/mysession-<date>-<time>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | SystemTap
 | 
					
						
							|  |  |  | ---------
 | 
					
						
							| 
									
										
										
										
											2011-02-23 14:00:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The "dtrace" backend uses DTrace sdt probes but has only been tested with
 | 
					
						
							|  |  |  | SystemTap.  When SystemTap support is detected a .stp file with wrapper probes
 | 
					
						
							|  |  |  | is generated to make use in scripts more convenient.  This step can also be
 | 
					
						
							|  |  |  | performed manually after a build in order to change the binary name in the .stp
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | probes::
 | 
					
						
							| 
									
										
										
										
											2011-02-23 14:00:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-11 14:58:50 +08:00
										 |  |  |     scripts/tracetool.py --backends=dtrace --format=stap \
 | 
					
						
							|  |  |  |                          --binary path/to/qemu-binary \
 | 
					
						
							| 
									
										
										
										
											2024-01-08 17:13:55 +00:00
										 |  |  |                          --probe-prefix qemu.system.x86_64 \
 | 
					
						
							| 
									
										
										
										
											2019-10-09 14:51:54 +01:00
										 |  |  |                          --group=all \
 | 
					
						
							| 
									
										
										
										
											2020-08-27 15:29:12 +01:00
										 |  |  |                          trace-events-all \
 | 
					
						
							|  |  |  |                          qemu.stp
 | 
					
						
							| 
									
										
										
										
											2011-12-06 17:38:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-23 12:00:16 +00:00
										 |  |  | To facilitate simple usage of systemtap where there merely needs to be printf
 | 
					
						
							|  |  |  | logging of certain probes, a helper script "qemu-trace-stap" is provided.
 | 
					
						
							|  |  |  | Consult its manual page for guidance on its usage.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | Trace event properties
 | 
					
						
							|  |  |  | ======================
 | 
					
						
							| 
									
										
										
										
											2011-12-06 17:38:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 09:39:47 +01:00
										 |  |  | Each event in the "trace-events-all" file can be prefixed with a space-separated
 | 
					
						
							| 
									
										
										
										
											2011-12-06 17:38:15 +01:00
										 |  |  | list of zero or more of the following event properties.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | "disable"
 | 
					
						
							|  |  |  | ---------
 | 
					
						
							| 
									
										
										
										
											2011-12-06 17:38:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | If a specific trace event is going to be invoked a huge number of times, this
 | 
					
						
							|  |  |  | might have a noticeable performance impact even when the event is
 | 
					
						
							|  |  |  | programmatically disabled.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In this case you should declare such event with the "disable" property. This
 | 
					
						
							|  |  |  | will effectively disable the event at compile time (by using the "nop" backend),
 | 
					
						
							|  |  |  | thus having no performance impact at all on regular builds (i.e., unless you
 | 
					
						
							| 
									
										
										
										
											2016-06-16 09:39:47 +01:00
										 |  |  | edit the "trace-events-all" file).
 | 
					
						
							| 
									
										
										
										
											2011-12-06 17:38:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | In addition, there might be cases where relatively complex computations must be
 | 
					
						
							|  |  |  | performed to generate values that are only used as arguments for a trace
 | 
					
						
							| 
									
										
										
										
											2020-01-20 15:11:40 +00:00
										 |  |  | function. In these cases you can use 'trace_event_get_state_backends()' to
 | 
					
						
							|  |  |  | guard such computations, so they are skipped if the event has been either
 | 
					
						
							|  |  |  | compile-time disabled or run-time disabled. If the event is compile-time
 | 
					
						
							|  |  |  | disabled, this check will have no performance impact.
 | 
					
						
							| 
									
										
										
										
											2011-12-06 17:38:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 16:09:22 +00:00
										 |  |  | ::
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-06 17:38:15 +01:00
										 |  |  |     #include "trace.h"  /* needed for trace event prototype */
 | 
					
						
							|  |  |  |     
 | 
					
						
							|  |  |  |     void *qemu_vmalloc(size_t size)
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         void *ptr;
 | 
					
						
							|  |  |  |         size_t align = QEMU_VMALLOC_ALIGN;
 | 
					
						
							|  |  |  |     
 | 
					
						
							|  |  |  |         if (size < align) {
 | 
					
						
							|  |  |  |             align = getpagesize();
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         ptr = qemu_memalign(align, size);
 | 
					
						
							| 
									
										
										
										
											2020-01-20 15:11:40 +00:00
										 |  |  |         if (trace_event_get_state_backends(TRACE_QEMU_VMALLOC)) {
 | 
					
						
							| 
									
										
										
										
											2011-12-06 17:38:15 +01:00
										 |  |  |             void *complex;
 | 
					
						
							|  |  |  |             /* some complex computations to produce the 'complex' value */
 | 
					
						
							|  |  |  |             trace_qemu_vmalloc(size, ptr, complex);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         return ptr;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2013-03-05 14:47:38 +01:00
										 |  |  | 
 |