gdb.rst 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. .. _GDB usage:
  2. GDB usage
  3. ---------
  4. QEMU supports working with gdb via gdb's remote-connection facility
  5. (the "gdbstub"). This allows you to debug guest code in the same
  6. way that you might with a low-level debug facility like JTAG
  7. on real hardware. You can stop and start the virtual machine,
  8. examine state like registers and memory, and set breakpoints and
  9. watchpoints.
  10. In order to use gdb, launch QEMU with the ``-s`` and ``-S`` options.
  11. The ``-s`` option will make QEMU listen for an incoming connection
  12. from gdb on TCP port 1234, and ``-S`` will make QEMU not start the
  13. guest until you tell it to from gdb. (If you want to specify which
  14. TCP port to use or to use something other than TCP for the gdbstub
  15. connection, use the ``-gdb dev`` option instead of ``-s``. See
  16. `Using unix sockets`_ for an example.)
  17. .. parsed-literal::
  18. |qemu_system| -s -S -kernel bzImage -hda rootdisk.img -append "root=/dev/hda"
  19. QEMU will launch but will silently wait for gdb to connect.
  20. Then launch gdb on the 'vmlinux' executable::
  21. > gdb vmlinux
  22. In gdb, connect to QEMU::
  23. (gdb) target remote localhost:1234
  24. Then you can use gdb normally. For example, type 'c' to launch the
  25. kernel::
  26. (gdb) c
  27. Here are some useful tips in order to use gdb on system code:
  28. 1. Use ``info reg`` to display all the CPU registers.
  29. 2. Use ``x/10i $eip`` to display the code at the PC position.
  30. 3. Use ``set architecture i8086`` to dump 16 bit code. Then use
  31. ``x/10i $cs*16+$eip`` to dump the code at the PC position.
  32. Breakpoint and Watchpoint support
  33. =================================
  34. While GDB can always fall back to inserting breakpoints into memory
  35. (if writable) other features are very much dependent on support of the
  36. accelerator. For TCG system emulation we advertise an infinite number
  37. of hardware assisted breakpoints and watchpoints. For other
  38. accelerators it will depend on if support has been added (see
  39. supports_guest_debug and related hooks in AccelOpsClass).
  40. As TCG cannot track all memory accesses in user-mode there is no
  41. support for watchpoints.
  42. Relocating code
  43. ===============
  44. On modern kernels confusion can be caused by code being relocated by
  45. features such as address space layout randomisation. To avoid
  46. confusion when debugging such things you either need to update gdb's
  47. view of where things are in memory or perhaps more trivially disable
  48. ASLR when booting the system.
  49. Debugging user-space in system emulation
  50. ========================================
  51. While it is technically possible to debug a user-space program running
  52. inside a system image, it does present challenges. Kernel preemption
  53. and execution mode changes between kernel and user mode can make it
  54. hard to follow what's going on. Unless you are specifically trying to
  55. debug some interaction between kernel and user-space you are better
  56. off running your guest program with gdb either in the guest or using
  57. a gdbserver exposed via a port to the outside world.
  58. Debugging multicore machines
  59. ============================
  60. GDB's abstraction for debugging targets with multiple possible
  61. parallel flows of execution is a two layer one: it supports multiple
  62. "inferiors", each of which can have multiple "threads". When the QEMU
  63. machine has more than one CPU, QEMU exposes each CPU cluster as a
  64. separate "inferior", where each CPU within the cluster is a separate
  65. "thread". Most QEMU machine types have identical CPUs, so there is a
  66. single cluster which has all the CPUs in it. A few machine types are
  67. heterogeneous and have multiple clusters: for example the ``sifive_u``
  68. machine has a cluster with one E51 core and a second cluster with four
  69. U54 cores. Here the E51 is the only thread in the first inferior, and
  70. the U54 cores are all threads in the second inferior.
  71. When you connect gdb to the gdbstub, it will automatically
  72. connect to the first inferior; you can display the CPUs in this
  73. cluster using the gdb ``info thread`` command, and switch between
  74. them using gdb's usual thread-management commands.
  75. For multi-cluster machines, unfortunately gdb does not by default
  76. handle multiple inferiors, and so you have to explicitly connect
  77. to them. First, you must connect with the ``extended-remote``
  78. protocol, not ``remote``::
  79. (gdb) target extended-remote localhost:1234
  80. Once connected, gdb will have a single inferior, for the
  81. first cluster. You need to create inferiors for the other
  82. clusters and attach to them, like this::
  83. (gdb) add-inferior
  84. Added inferior 2
  85. (gdb) inferior 2
  86. [Switching to inferior 2 [<null>] (<noexec>)]
  87. (gdb) attach 2
  88. Attaching to process 2
  89. warning: No executable has been specified and target does not support
  90. determining executable automatically. Try using the "file" command.
  91. 0x00000000 in ?? ()
  92. Once you've done this, ``info threads`` will show CPUs in
  93. all the clusters you have attached to::
  94. (gdb) info threads
  95. Id Target Id Frame
  96. 1.1 Thread 1.1 (cortex-m33-arm-cpu cpu [running]) 0x00000000 in ?? ()
  97. * 2.1 Thread 2.2 (cortex-m33-arm-cpu cpu [halted ]) 0x00000000 in ?? ()
  98. You probably also want to set gdb to ``schedule-multiple`` mode,
  99. so that when you tell gdb to ``continue`` it resumes all CPUs,
  100. not just those in the cluster you are currently working on::
  101. (gdb) set schedule-multiple on
  102. Using unix sockets
  103. ==================
  104. An alternate method for connecting gdb to the QEMU gdbstub is to use
  105. a unix socket (if supported by your operating system). This is useful when
  106. running several tests in parallel, or if you do not have a known free TCP
  107. port (e.g. when running automated tests).
  108. First create a chardev with the appropriate options, then
  109. instruct the gdbserver to use that device:
  110. .. parsed-literal::
  111. |qemu_system| -chardev socket,path=/tmp/gdb-socket,server=on,wait=off,id=gdb0 -gdb chardev:gdb0 -S ...
  112. Start gdb as before, but this time connect using the path to
  113. the socket::
  114. (gdb) target remote /tmp/gdb-socket
  115. Note that to use a unix socket for the connection you will need
  116. gdb version 9.0 or newer.
  117. Advanced debugging options
  118. ==========================
  119. Changing single-stepping behaviour
  120. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  121. The default single stepping behavior is step with the IRQs and timer
  122. service routines off. It is set this way because when gdb executes a
  123. single step it expects to advance beyond the current instruction. With
  124. the IRQs and timer service routines on, a single step might jump into
  125. the one of the interrupt or exception vectors instead of executing the
  126. current instruction. This means you may hit the same breakpoint a number
  127. of times before executing the instruction gdb wants to have executed.
  128. Because there are rare circumstances where you want to single step into
  129. an interrupt vector the behavior can be controlled from GDB. There are
  130. three commands you can query and set the single step behavior:
  131. ``maintenance packet qqemu.sstepbits``
  132. This will display the MASK bits used to control the single stepping
  133. IE:
  134. ::
  135. (gdb) maintenance packet qqemu.sstepbits
  136. sending: "qqemu.sstepbits"
  137. received: "ENABLE=1,NOIRQ=2,NOTIMER=4"
  138. ``maintenance packet qqemu.sstep``
  139. This will display the current value of the mask used when single
  140. stepping IE:
  141. ::
  142. (gdb) maintenance packet qqemu.sstep
  143. sending: "qqemu.sstep"
  144. received: "0x7"
  145. ``maintenance packet Qqemu.sstep=HEX_VALUE``
  146. This will change the single step mask, so if wanted to enable IRQs on
  147. the single step, but not timers, you would use:
  148. ::
  149. (gdb) maintenance packet Qqemu.sstep=0x5
  150. sending: "qemu.sstep=0x5"
  151. received: "OK"
  152. Examining physical memory
  153. ^^^^^^^^^^^^^^^^^^^^^^^^^
  154. Another feature that QEMU gdbstub provides is to toggle the memory GDB
  155. works with, by default GDB will show the current process memory respecting
  156. the virtual address translation.
  157. If you want to examine/change the physical memory you can set the gdbstub
  158. to work with the physical memory rather with the virtual one.
  159. The memory mode can be checked by sending the following command:
  160. ``maintenance packet qqemu.PhyMemMode``
  161. This will return either 0 or 1, 1 indicates you are currently in the
  162. physical memory mode.
  163. ``maintenance packet Qqemu.PhyMemMode:1``
  164. This will change the memory mode to physical memory.
  165. ``maintenance packet Qqemu.PhyMemMode:0``
  166. This will change it back to normal memory mode.
  167. Security considerations
  168. =======================
  169. Connecting to the GDB socket allows running arbitrary code inside the guest;
  170. in case of the TCG emulation, which is not considered a security boundary, this
  171. also means running arbitrary code on the host. Additionally, when debugging
  172. qemu-user, it allows directly downloading any file readable by QEMU from the
  173. host.
  174. The GDB socket is not protected by authentication, authorization or encryption.
  175. It is therefore a responsibility of the user to make sure that only authorized
  176. clients can connect to it, e.g., by using a unix socket with proper
  177. permissions, or by opening a TCP socket only on interfaces that are not
  178. reachable by potential attackers.