tracing.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. .. _tracing:
  2. =======
  3. Tracing
  4. =======
  5. Introduction
  6. ============
  7. This document describes the tracing infrastructure in QEMU and how to use it
  8. for debugging, profiling, and observing execution.
  9. Quickstart
  10. ==========
  11. Enable tracing of ``memory_region_ops_read`` and ``memory_region_ops_write``
  12. events::
  13. $ qemu --trace "memory_region_ops_*" ...
  14. ...
  15. 719585@1608130130.441188:memory_region_ops_read cpu 0 mr 0x562fdfbb3820 addr 0x3cc value 0x67 size 1
  16. 719585@1608130130.441190:memory_region_ops_write cpu 0 mr 0x562fdfbd2f00 addr 0x3d4 value 0x70e size 2
  17. This output comes from the "log" trace backend that is enabled by default when
  18. ``./configure --enable-trace-backends=BACKENDS`` was not explicitly specified.
  19. Multiple patterns can be specified by repeating the ``--trace`` option::
  20. $ qemu --trace "kvm_*" --trace "virtio_*" ...
  21. When patterns are used frequently it is more convenient to store them in a
  22. file to avoid long command-line options::
  23. $ echo "memory_region_ops_*" >/tmp/events
  24. $ echo "kvm_*" >>/tmp/events
  25. $ qemu --trace events=/tmp/events ...
  26. Trace events
  27. ============
  28. Sub-directory setup
  29. -------------------
  30. Each directory in the source tree can declare a set of trace events in a local
  31. "trace-events" file. All directories which contain "trace-events" files must be
  32. listed in the "trace_events_subdirs" variable in the top level meson.build
  33. file. During build, the "trace-events" file in each listed subdirectory will be
  34. processed by the "tracetool" script to generate code for the trace events.
  35. The individual "trace-events" files are merged into a "trace-events-all" file,
  36. which is also installed into "/usr/share/qemu".
  37. This merged file is to be used by the "simpletrace.py" script to later analyse
  38. traces in the simpletrace data format.
  39. The following files are automatically generated in <builddir>/trace/ during the
  40. build:
  41. - trace-<subdir>.c - the trace event state declarations
  42. - trace-<subdir>.h - the trace event enums and probe functions
  43. - trace-dtrace-<subdir>.h - DTrace event probe specification
  44. - trace-dtrace-<subdir>.dtrace - DTrace event probe helper declaration
  45. - trace-dtrace-<subdir>.o - binary DTrace provider (generated by dtrace)
  46. - trace-ust-<subdir>.h - UST event probe helper declarations
  47. Here <subdir> is the sub-directory path with '/' replaced by '_'. For example,
  48. "accel/kvm" becomes "accel_kvm" and the final filename for "trace-<subdir>.c"
  49. becomes "trace-accel_kvm.c".
  50. Source files in the source tree do not directly include generated files in
  51. "<builddir>/trace/". Instead they #include the local "trace.h" file, without
  52. any sub-directory path prefix. eg io/channel-buffer.c would do::
  53. #include "trace.h"
  54. The "io/trace.h" file must be created manually with an #include of the
  55. corresponding "trace/trace-<subdir>.h" file that will be generated in the
  56. builddir::
  57. $ echo '#include "trace/trace-io.h"' >io/trace.h
  58. While it is possible to include a trace.h file from outside a source file's own
  59. sub-directory, this is discouraged in general. It is strongly preferred that
  60. all events be declared directly in the sub-directory that uses them. The only
  61. exception is where there are some shared trace events defined in the top level
  62. directory trace-events file. The top level directory generates trace files
  63. with a filename prefix of "trace/trace-root" instead of just "trace". This is
  64. to avoid ambiguity between a trace.h in the current directory, vs the top level
  65. directory.
  66. Using trace events
  67. ------------------
  68. Trace events are invoked directly from source code like this::
  69. #include "trace.h" /* needed for trace event prototype */
  70. void *qemu_vmalloc(size_t size)
  71. {
  72. void *ptr;
  73. size_t align = QEMU_VMALLOC_ALIGN;
  74. if (size < align) {
  75. align = getpagesize();
  76. }
  77. ptr = qemu_memalign(align, size);
  78. trace_qemu_vmalloc(size, ptr);
  79. return ptr;
  80. }
  81. Declaring trace events
  82. ----------------------
  83. The "tracetool" script produces the trace.h header file which is included by
  84. every source file that uses trace events. Since many source files include
  85. trace.h, it uses a minimum of types and other header files included to keep the
  86. namespace clean and compile times and dependencies down.
  87. Trace events should use types as follows:
  88. * Use stdint.h types for fixed-size types. Most offsets and guest memory
  89. addresses are best represented with uint32_t or uint64_t. Use fixed-size
  90. types over primitive types whose size may change depending on the host
  91. (32-bit versus 64-bit) so trace events don't truncate values or break
  92. the build.
  93. * Use void * for pointers to structs or for arrays. The trace.h header
  94. cannot include all user-defined struct declarations and it is therefore
  95. necessary to use void * for pointers to structs.
  96. * For everything else, use primitive scalar types (char, int, long) with the
  97. appropriate signedness.
  98. * Avoid floating point types (float and double) because SystemTap does not
  99. support them. In most cases it is possible to round to an integer type
  100. instead. This may require scaling the value first by multiplying it by 1000
  101. or the like when digits after the decimal point need to be preserved.
  102. Format strings should reflect the types defined in the trace event. Take
  103. special care to use PRId64 and PRIu64 for int64_t and uint64_t types,
  104. respectively. This ensures portability between 32- and 64-bit platforms.
  105. Format strings must not end with a newline character. It is the responsibility
  106. of backends to adapt line ending for proper logging.
  107. Each event declaration will start with the event name, then its arguments,
  108. finally a format string for pretty-printing. For example::
  109. qemu_vmalloc(size_t size, void *ptr) "size %zu ptr %p"
  110. qemu_vfree(void *ptr) "ptr %p"
  111. Hints for adding new trace events
  112. ---------------------------------
  113. 1. Trace state changes in the code. Interesting points in the code usually
  114. involve a state change like starting, stopping, allocating, freeing. State
  115. changes are good trace events because they can be used to understand the
  116. execution of the system.
  117. 2. Trace guest operations. Guest I/O accesses like reading device registers
  118. are good trace events because they can be used to understand guest
  119. interactions.
  120. 3. Use correlator fields so the context of an individual line of trace output
  121. can be understood. For example, trace the pointer returned by malloc and
  122. used as an argument to free. This way mallocs and frees can be matched up.
  123. Trace events with no context are not very useful.
  124. 4. Name trace events after their function. If there are multiple trace events
  125. in one function, append a unique distinguisher at the end of the name.
  126. Generic interface and monitor commands
  127. ======================================
  128. You can programmatically query and control the state of trace events through a
  129. backend-agnostic interface provided by the header "trace/control.h".
  130. Note that some of the backends do not provide an implementation for some parts
  131. of this interface, in which case QEMU will just print a warning (please refer to
  132. header "trace/control.h" to see which routines are backend-dependent).
  133. The state of events can also be queried and modified through monitor commands:
  134. * ``info trace-events``
  135. View available trace events and their state. State 1 means enabled, state 0
  136. means disabled.
  137. * ``trace-event NAME on|off``
  138. Enable/disable a given trace event or a group of events (using wildcards).
  139. The "--trace events=<file>" command line argument can be used to enable the
  140. events listed in <file> from the very beginning of the program. This file must
  141. contain one event name per line.
  142. If a line in the "--trace events=<file>" file begins with a '-', the trace event
  143. will be disabled instead of enabled. This is useful when a wildcard was used
  144. to enable an entire family of events but one noisy event needs to be disabled.
  145. Wildcard matching is supported in both the monitor command "trace-event" and the
  146. events list file. That means you can enable/disable the events having a common
  147. prefix in a batch. For example, virtio-blk trace events could be enabled using
  148. the following monitor command::
  149. trace-event virtio_blk_* on
  150. Trace backends
  151. ==============
  152. The "tracetool" script automates tedious trace event code generation and also
  153. keeps the trace event declarations independent of the trace backend. The trace
  154. events are not tightly coupled to a specific trace backend, such as LTTng or
  155. SystemTap. Support for trace backends can be added by extending the "tracetool"
  156. script.
  157. The trace backends are chosen at configure time::
  158. ./configure --enable-trace-backends=simple,dtrace
  159. For a list of supported trace backends, try ./configure --help or see below.
  160. If multiple backends are enabled, the trace is sent to them all.
  161. If no backends are explicitly selected, configure will default to the
  162. "log" backend.
  163. The following subsections describe the supported trace backends.
  164. Nop
  165. ---
  166. The "nop" backend generates empty trace event functions so that the compiler
  167. can optimize out trace events completely. This imposes no performance
  168. penalty.
  169. Note that regardless of the selected trace backend, events with the "disable"
  170. property will be generated with the "nop" backend.
  171. Log
  172. ---
  173. The "log" backend sends trace events directly to standard error. This
  174. effectively turns trace events into debug printfs.
  175. This is the simplest backend and can be used together with existing code that
  176. uses DPRINTF().
  177. The -msg timestamp=on|off command-line option controls whether or not to print
  178. the tid/timestamp prefix for each trace event.
  179. Simpletrace
  180. -----------
  181. The "simple" backend writes binary trace logs to a file from a thread, making
  182. it lower overhead than the "log" backend. A Python API is available for writing
  183. offline trace file analysis scripts. It may not be as powerful as
  184. platform-specific or third-party trace backends but it is portable and has no
  185. special library dependencies.
  186. Monitor commands
  187. ~~~~~~~~~~~~~~~~
  188. * ``trace-file on|off|flush|set <path>``
  189. Enable/disable/flush the trace file or set the trace file name.
  190. Analyzing trace files
  191. ~~~~~~~~~~~~~~~~~~~~~
  192. The "simple" backend produces binary trace files that can be formatted with the
  193. simpletrace.py script. The script takes the "trace-events-all" file and the
  194. binary trace::
  195. ./scripts/simpletrace.py trace-events-all trace-12345
  196. You must ensure that the same "trace-events-all" file was used to build QEMU,
  197. otherwise trace event declarations may have changed and output will not be
  198. consistent.
  199. Ftrace
  200. ------
  201. The "ftrace" backend writes trace data to ftrace marker. This effectively
  202. sends trace events to ftrace ring buffer, and you can compare qemu trace
  203. data and kernel(especially kvm.ko when using KVM) trace data.
  204. if you use KVM, enable kvm events in ftrace::
  205. # echo 1 > /sys/kernel/debug/tracing/events/kvm/enable
  206. After running qemu by root user, you can get the trace::
  207. # cat /sys/kernel/debug/tracing/trace
  208. Restriction: "ftrace" backend is restricted to Linux only.
  209. Syslog
  210. ------
  211. The "syslog" backend sends trace events using the POSIX syslog API. The log
  212. is opened specifying the LOG_DAEMON facility and LOG_PID option (so events
  213. are tagged with the pid of the particular QEMU process that generated
  214. them). All events are logged at LOG_INFO level.
  215. NOTE: syslog may squash duplicate consecutive trace events and apply rate
  216. limiting.
  217. Restriction: "syslog" backend is restricted to POSIX compliant OS.
  218. LTTng Userspace Tracer
  219. ----------------------
  220. The "ust" backend uses the LTTng Userspace Tracer library. There are no
  221. monitor commands built into QEMU, instead UST utilities should be used to list,
  222. enable/disable, and dump traces.
  223. Package lttng-tools is required for userspace tracing. You must ensure that the
  224. current user belongs to the "tracing" group, or manually launch the
  225. lttng-sessiond daemon for the current user prior to running any instance of
  226. QEMU.
  227. While running an instrumented QEMU, LTTng should be able to list all available
  228. events::
  229. lttng list -u
  230. Create tracing session::
  231. lttng create mysession
  232. Enable events::
  233. lttng enable-event qemu:g_malloc -u
  234. Where the events can either be a comma-separated list of events, or "-a" to
  235. enable all tracepoint events. Start and stop tracing as needed::
  236. lttng start
  237. lttng stop
  238. View the trace::
  239. lttng view
  240. Destroy tracing session::
  241. lttng destroy
  242. Babeltrace can be used at any later time to view the trace::
  243. babeltrace $HOME/lttng-traces/mysession-<date>-<time>
  244. SystemTap
  245. ---------
  246. The "dtrace" backend uses DTrace sdt probes but has only been tested with
  247. SystemTap. When SystemTap support is detected a .stp file with wrapper probes
  248. is generated to make use in scripts more convenient. This step can also be
  249. performed manually after a build in order to change the binary name in the .stp
  250. probes::
  251. scripts/tracetool.py --backends=dtrace --format=stap \
  252. --binary path/to/qemu-binary \
  253. --probe-prefix qemu.system.x86_64 \
  254. --group=all \
  255. trace-events-all \
  256. qemu.stp
  257. To facilitate simple usage of systemtap where there merely needs to be printf
  258. logging of certain probes, a helper script "qemu-trace-stap" is provided.
  259. Consult its manual page for guidance on its usage.
  260. Trace event properties
  261. ======================
  262. Each event in the "trace-events-all" file can be prefixed with a space-separated
  263. list of zero or more of the following event properties.
  264. "disable"
  265. ---------
  266. If a specific trace event is going to be invoked a huge number of times, this
  267. might have a noticeable performance impact even when the event is
  268. programmatically disabled.
  269. In this case you should declare such event with the "disable" property. This
  270. will effectively disable the event at compile time (by using the "nop" backend),
  271. thus having no performance impact at all on regular builds (i.e., unless you
  272. edit the "trace-events-all" file).
  273. In addition, there might be cases where relatively complex computations must be
  274. performed to generate values that are only used as arguments for a trace
  275. function. In these cases you can use 'trace_event_get_state_backends()' to
  276. guard such computations, so they are skipped if the event has been either
  277. compile-time disabled or run-time disabled. If the event is compile-time
  278. disabled, this check will have no performance impact.
  279. ::
  280. #include "trace.h" /* needed for trace event prototype */
  281. void *qemu_vmalloc(size_t size)
  282. {
  283. void *ptr;
  284. size_t align = QEMU_VMALLOC_ALIGN;
  285. if (size < align) {
  286. align = getpagesize();
  287. }
  288. ptr = qemu_memalign(align, size);
  289. if (trace_event_get_state_backends(TRACE_QEMU_VMALLOC)) {
  290. void *complex;
  291. /* some complex computations to produce the 'complex' value */
  292. trace_qemu_vmalloc(size, ptr, complex);
  293. }
  294. return ptr;
  295. }