tracing.txt 16 KB

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