tracing.txt 16 KB

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