xref: /openbmc/qemu/docs/system/gdb.rst (revision 4705fc0c8511d073bee4751c3c974aab2b10a970)
1923e9311SThomas Huth.. _GDB usage:
2324b2298SPaolo Bonzini
3324b2298SPaolo BonziniGDB usage
4324b2298SPaolo Bonzini---------
5324b2298SPaolo Bonzini
6e5910d42SPeter MaydellQEMU supports working with gdb via gdb's remote-connection facility
7e5910d42SPeter Maydell(the "gdbstub"). This allows you to debug guest code in the same
8e5910d42SPeter Maydellway that you might with a low-level debug facility like JTAG
9e5910d42SPeter Maydellon real hardware. You can stop and start the virtual machine,
10e5910d42SPeter Maydellexamine state like registers and memory, and set breakpoints and
11e5910d42SPeter Maydellwatchpoints.
12324b2298SPaolo Bonzini
13e5910d42SPeter MaydellIn order to use gdb, launch QEMU with the ``-s`` and ``-S`` options.
14e5910d42SPeter MaydellThe ``-s`` option will make QEMU listen for an incoming connection
15e5910d42SPeter Maydellfrom gdb on TCP port 1234, and ``-S`` will make QEMU not start the
16e5910d42SPeter Maydellguest until you tell it to from gdb. (If you want to specify which
17e5910d42SPeter MaydellTCP port to use or to use something other than TCP for the gdbstub
1824b1a6aaSSebastian Meyerconnection, use the ``-gdb dev`` option instead of ``-s``. See
1924b1a6aaSSebastian Meyer`Using unix sockets`_ for an example.)
20324b2298SPaolo Bonzini
21324b2298SPaolo Bonzini.. parsed-literal::
22324b2298SPaolo Bonzini
23e5910d42SPeter Maydell   |qemu_system| -s -S -kernel bzImage -hda rootdisk.img -append "root=/dev/hda"
24e5910d42SPeter Maydell
25e5910d42SPeter MaydellQEMU will launch but will silently wait for gdb to connect.
26324b2298SPaolo Bonzini
27324b2298SPaolo BonziniThen launch gdb on the 'vmlinux' executable::
28324b2298SPaolo Bonzini
29324b2298SPaolo Bonzini   > gdb vmlinux
30324b2298SPaolo Bonzini
31324b2298SPaolo BonziniIn gdb, connect to QEMU::
32324b2298SPaolo Bonzini
33324b2298SPaolo Bonzini   (gdb) target remote localhost:1234
34324b2298SPaolo Bonzini
35324b2298SPaolo BonziniThen you can use gdb normally. For example, type 'c' to launch the
36324b2298SPaolo Bonzinikernel::
37324b2298SPaolo Bonzini
38324b2298SPaolo Bonzini   (gdb) c
39324b2298SPaolo Bonzini
40324b2298SPaolo BonziniHere are some useful tips in order to use gdb on system code:
41324b2298SPaolo Bonzini
42324b2298SPaolo Bonzini1. Use ``info reg`` to display all the CPU registers.
43324b2298SPaolo Bonzini
44324b2298SPaolo Bonzini2. Use ``x/10i $eip`` to display the code at the PC position.
45324b2298SPaolo Bonzini
46324b2298SPaolo Bonzini3. Use ``set architecture i8086`` to dump 16 bit code. Then use
47324b2298SPaolo Bonzini   ``x/10i $cs*16+$eip`` to dump the code at the PC position.
48324b2298SPaolo Bonzini
49ab9d29b0SAlex BennéeBreakpoint and Watchpoint support
50ab9d29b0SAlex Bennée=================================
51ab9d29b0SAlex Bennée
52ab9d29b0SAlex BennéeWhile GDB can always fall back to inserting breakpoints into memory
53ab9d29b0SAlex Bennée(if writable) other features are very much dependent on support of the
54ab9d29b0SAlex Bennéeaccelerator. For TCG system emulation we advertise an infinite number
55ab9d29b0SAlex Bennéeof hardware assisted breakpoints and watchpoints. For other
56ab9d29b0SAlex Bennéeaccelerators it will depend on if support has been added (see
57ab9d29b0SAlex Bennéesupports_guest_debug and related hooks in AccelOpsClass).
58ab9d29b0SAlex Bennée
59ab9d29b0SAlex BennéeAs TCG cannot track all memory accesses in user-mode there is no
60ab9d29b0SAlex Bennéesupport for watchpoints.
61ab9d29b0SAlex Bennée
62ab9d29b0SAlex BennéeRelocating code
63*84dd7d88SAlex Bennée===============
64ab9d29b0SAlex Bennée
65ab9d29b0SAlex BennéeOn modern kernels confusion can be caused by code being relocated by
66ab9d29b0SAlex Bennéefeatures such as address space layout randomisation. To avoid
67ab9d29b0SAlex Bennéeconfusion when debugging such things you either need to update gdb's
68ab9d29b0SAlex Bennéeview of where things are in memory or perhaps more trivially disable
69ab9d29b0SAlex BennéeASLR when booting the system.
70ab9d29b0SAlex Bennée
71*84dd7d88SAlex BennéeDebugging user-space in system emulation
72*84dd7d88SAlex Bennée========================================
73*84dd7d88SAlex Bennée
74*84dd7d88SAlex BennéeWhile it is technically possible to debug a user-space program running
75*84dd7d88SAlex Bennéeinside a system image, it does present challenges. Kernel preemption
76*84dd7d88SAlex Bennéeand execution mode changes between kernel and user mode can make it
77*84dd7d88SAlex Bennéehard to follow what's going on. Unless you are specifically trying to
78*84dd7d88SAlex Bennéedebug some interaction between kernel and user-space you are better
79*84dd7d88SAlex Bennéeoff running your guest program with gdb either in the guest or using
80*84dd7d88SAlex Bennéea gdbserver exposed via a port to the outside world.
81*84dd7d88SAlex Bennée
82d211556fSPeter MaydellDebugging multicore machines
83d211556fSPeter Maydell============================
84d211556fSPeter Maydell
85d211556fSPeter MaydellGDB's abstraction for debugging targets with multiple possible
86d211556fSPeter Maydellparallel flows of execution is a two layer one: it supports multiple
87d211556fSPeter Maydell"inferiors", each of which can have multiple "threads". When the QEMU
88d211556fSPeter Maydellmachine has more than one CPU, QEMU exposes each CPU cluster as a
89d211556fSPeter Maydellseparate "inferior", where each CPU within the cluster is a separate
90d211556fSPeter Maydell"thread". Most QEMU machine types have identical CPUs, so there is a
91d211556fSPeter Maydellsingle cluster which has all the CPUs in it.  A few machine types are
92b980c1aeSStefan Weilheterogeneous and have multiple clusters: for example the ``sifive_u``
93d211556fSPeter Maydellmachine has a cluster with one E51 core and a second cluster with four
94d211556fSPeter MaydellU54 cores. Here the E51 is the only thread in the first inferior, and
95d211556fSPeter Maydellthe U54 cores are all threads in the second inferior.
96d211556fSPeter Maydell
97d211556fSPeter MaydellWhen you connect gdb to the gdbstub, it will automatically
98d211556fSPeter Maydellconnect to the first inferior; you can display the CPUs in this
99d211556fSPeter Maydellcluster using the gdb ``info thread`` command, and switch between
100d211556fSPeter Maydellthem using gdb's usual thread-management commands.
101d211556fSPeter Maydell
102d211556fSPeter MaydellFor multi-cluster machines, unfortunately gdb does not by default
103d211556fSPeter Maydellhandle multiple inferiors, and so you have to explicitly connect
104d211556fSPeter Maydellto them. First, you must connect with the ``extended-remote``
105d211556fSPeter Maydellprotocol, not ``remote``::
106d211556fSPeter Maydell
107d211556fSPeter Maydell    (gdb) target extended-remote localhost:1234
108d211556fSPeter Maydell
109d211556fSPeter MaydellOnce connected, gdb will have a single inferior, for the
110d211556fSPeter Maydellfirst cluster. You need to create inferiors for the other
111d211556fSPeter Maydellclusters and attach to them, like this::
112d211556fSPeter Maydell
113d211556fSPeter Maydell  (gdb) add-inferior
114d211556fSPeter Maydell  Added inferior 2
115d211556fSPeter Maydell  (gdb) inferior 2
116d211556fSPeter Maydell  [Switching to inferior 2 [<null>] (<noexec>)]
117d211556fSPeter Maydell  (gdb) attach 2
118d211556fSPeter Maydell  Attaching to process 2
119d211556fSPeter Maydell  warning: No executable has been specified and target does not support
120d211556fSPeter Maydell  determining executable automatically.  Try using the "file" command.
121d211556fSPeter Maydell  0x00000000 in ?? ()
122d211556fSPeter Maydell
123d211556fSPeter MaydellOnce you've done this, ``info threads`` will show CPUs in
124d211556fSPeter Maydellall the clusters you have attached to::
125d211556fSPeter Maydell
126d211556fSPeter Maydell  (gdb) info threads
127d211556fSPeter Maydell    Id   Target Id         Frame
128d211556fSPeter Maydell    1.1  Thread 1.1 (cortex-m33-arm-cpu cpu [running]) 0x00000000 in ?? ()
129d211556fSPeter Maydell  * 2.1  Thread 2.2 (cortex-m33-arm-cpu cpu [halted ]) 0x00000000 in ?? ()
130d211556fSPeter Maydell
131d211556fSPeter MaydellYou probably also want to set gdb to ``schedule-multiple`` mode,
132d211556fSPeter Maydellso that when you tell gdb to ``continue`` it resumes all CPUs,
133d211556fSPeter Maydellnot just those in the cluster you are currently working on::
134d211556fSPeter Maydell
135d211556fSPeter Maydell  (gdb) set schedule-multiple on
136d211556fSPeter Maydell
13724b1a6aaSSebastian MeyerUsing unix sockets
13824b1a6aaSSebastian Meyer==================
13924b1a6aaSSebastian Meyer
14024b1a6aaSSebastian MeyerAn alternate method for connecting gdb to the QEMU gdbstub is to use
14124b1a6aaSSebastian Meyera unix socket (if supported by your operating system). This is useful when
14224b1a6aaSSebastian Meyerrunning several tests in parallel, or if you do not have a known free TCP
14324b1a6aaSSebastian Meyerport (e.g. when running automated tests).
14424b1a6aaSSebastian Meyer
14524b1a6aaSSebastian MeyerFirst create a chardev with the appropriate options, then
14624b1a6aaSSebastian Meyerinstruct the gdbserver to use that device:
14724b1a6aaSSebastian Meyer
14824b1a6aaSSebastian Meyer.. parsed-literal::
14924b1a6aaSSebastian Meyer
15024b1a6aaSSebastian Meyer   |qemu_system| -chardev socket,path=/tmp/gdb-socket,server=on,wait=off,id=gdb0 -gdb chardev:gdb0 -S ...
15124b1a6aaSSebastian Meyer
15224b1a6aaSSebastian MeyerStart gdb as before, but this time connect using the path to
15324b1a6aaSSebastian Meyerthe socket::
15424b1a6aaSSebastian Meyer
15524b1a6aaSSebastian Meyer   (gdb) target remote /tmp/gdb-socket
15624b1a6aaSSebastian Meyer
15724b1a6aaSSebastian MeyerNote that to use a unix socket for the connection you will need
15824b1a6aaSSebastian Meyergdb version 9.0 or newer.
15924b1a6aaSSebastian Meyer
160acb0a27eSPeter MaydellAdvanced debugging options
161acb0a27eSPeter Maydell==========================
162acb0a27eSPeter Maydell
163acb0a27eSPeter MaydellChanging single-stepping behaviour
164acb0a27eSPeter Maydell^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
165324b2298SPaolo Bonzini
166324b2298SPaolo BonziniThe default single stepping behavior is step with the IRQs and timer
167324b2298SPaolo Bonziniservice routines off. It is set this way because when gdb executes a
168324b2298SPaolo Bonzinisingle step it expects to advance beyond the current instruction. With
169324b2298SPaolo Bonzinithe IRQs and timer service routines on, a single step might jump into
170324b2298SPaolo Bonzinithe one of the interrupt or exception vectors instead of executing the
171324b2298SPaolo Bonzinicurrent instruction. This means you may hit the same breakpoint a number
172324b2298SPaolo Bonziniof times before executing the instruction gdb wants to have executed.
173324b2298SPaolo BonziniBecause there are rare circumstances where you want to single step into
174324b2298SPaolo Bonzinian interrupt vector the behavior can be controlled from GDB. There are
175324b2298SPaolo Bonzinithree commands you can query and set the single step behavior:
176324b2298SPaolo Bonzini
177324b2298SPaolo Bonzini``maintenance packet qqemu.sstepbits``
178324b2298SPaolo Bonzini   This will display the MASK bits used to control the single stepping
179324b2298SPaolo Bonzini   IE:
180324b2298SPaolo Bonzini
181324b2298SPaolo Bonzini   ::
182324b2298SPaolo Bonzini
183324b2298SPaolo Bonzini      (gdb) maintenance packet qqemu.sstepbits
184324b2298SPaolo Bonzini      sending: "qqemu.sstepbits"
185324b2298SPaolo Bonzini      received: "ENABLE=1,NOIRQ=2,NOTIMER=4"
186324b2298SPaolo Bonzini
187324b2298SPaolo Bonzini``maintenance packet qqemu.sstep``
188324b2298SPaolo Bonzini   This will display the current value of the mask used when single
189324b2298SPaolo Bonzini   stepping IE:
190324b2298SPaolo Bonzini
191324b2298SPaolo Bonzini   ::
192324b2298SPaolo Bonzini
193324b2298SPaolo Bonzini      (gdb) maintenance packet qqemu.sstep
194324b2298SPaolo Bonzini      sending: "qqemu.sstep"
195324b2298SPaolo Bonzini      received: "0x7"
196324b2298SPaolo Bonzini
197324b2298SPaolo Bonzini``maintenance packet Qqemu.sstep=HEX_VALUE``
198324b2298SPaolo Bonzini   This will change the single step mask, so if wanted to enable IRQs on
199324b2298SPaolo Bonzini   the single step, but not timers, you would use:
200324b2298SPaolo Bonzini
201324b2298SPaolo Bonzini   ::
202324b2298SPaolo Bonzini
203324b2298SPaolo Bonzini      (gdb) maintenance packet Qqemu.sstep=0x5
204324b2298SPaolo Bonzini      sending: "qemu.sstep=0x5"
205324b2298SPaolo Bonzini      received: "OK"
20650679467SJon Doron
207acb0a27eSPeter MaydellExamining physical memory
208acb0a27eSPeter Maydell^^^^^^^^^^^^^^^^^^^^^^^^^
20950679467SJon Doron
21050679467SJon DoronAnother feature that QEMU gdbstub provides is to toggle the memory GDB
21150679467SJon Doronworks with, by default GDB will show the current process memory respecting
21250679467SJon Doronthe virtual address translation.
21350679467SJon Doron
21450679467SJon DoronIf you want to examine/change the physical memory you can set the gdbstub
21550679467SJon Doronto work with the physical memory rather with the virtual one.
21650679467SJon Doron
21750679467SJon DoronThe memory mode can be checked by sending the following command:
21850679467SJon Doron
21950679467SJon Doron``maintenance packet qqemu.PhyMemMode``
22050679467SJon Doron    This will return either 0 or 1, 1 indicates you are currently in the
22150679467SJon Doron    physical memory mode.
22250679467SJon Doron
22350679467SJon Doron``maintenance packet Qqemu.PhyMemMode:1``
22450679467SJon Doron    This will change the memory mode to physical memory.
22550679467SJon Doron
22650679467SJon Doron``maintenance packet Qqemu.PhyMemMode:0``
22750679467SJon Doron    This will change it back to normal memory mode.
228abf7ba31SIlya Leoshkevich
229abf7ba31SIlya LeoshkevichSecurity considerations
230abf7ba31SIlya Leoshkevich=======================
231abf7ba31SIlya Leoshkevich
232abf7ba31SIlya LeoshkevichConnecting to the GDB socket allows running arbitrary code inside the guest;
233abf7ba31SIlya Leoshkevichin case of the TCG emulation, which is not considered a security boundary, this
234abf7ba31SIlya Leoshkevichalso means running arbitrary code on the host. Additionally, when debugging
235abf7ba31SIlya Leoshkevichqemu-user, it allows directly downloading any file readable by QEMU from the
236abf7ba31SIlya Leoshkevichhost.
237abf7ba31SIlya Leoshkevich
238abf7ba31SIlya LeoshkevichThe GDB socket is not protected by authentication, authorization or encryption.
239abf7ba31SIlya LeoshkevichIt is therefore a responsibility of the user to make sure that only authorized
240abf7ba31SIlya Leoshkevichclients can connect to it, e.g., by using a unix socket with proper
241abf7ba31SIlya Leoshkevichpermissions, or by opening a TCP socket only on interfaces that are not
242abf7ba31SIlya Leoshkevichreachable by potential attackers.
243