xref: /qemu/docs/system/gdb.rst (revision abf7ba310b7b0c13b4fe936621bbb8e416754c0c)
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
63ab9d29b0SAlex 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
71d211556fSPeter MaydellDebugging multicore machines
72d211556fSPeter Maydell============================
73d211556fSPeter Maydell
74d211556fSPeter MaydellGDB's abstraction for debugging targets with multiple possible
75d211556fSPeter Maydellparallel flows of execution is a two layer one: it supports multiple
76d211556fSPeter Maydell"inferiors", each of which can have multiple "threads". When the QEMU
77d211556fSPeter Maydellmachine has more than one CPU, QEMU exposes each CPU cluster as a
78d211556fSPeter Maydellseparate "inferior", where each CPU within the cluster is a separate
79d211556fSPeter Maydell"thread". Most QEMU machine types have identical CPUs, so there is a
80d211556fSPeter Maydellsingle cluster which has all the CPUs in it.  A few machine types are
81b980c1aeSStefan Weilheterogeneous and have multiple clusters: for example the ``sifive_u``
82d211556fSPeter Maydellmachine has a cluster with one E51 core and a second cluster with four
83d211556fSPeter MaydellU54 cores. Here the E51 is the only thread in the first inferior, and
84d211556fSPeter Maydellthe U54 cores are all threads in the second inferior.
85d211556fSPeter Maydell
86d211556fSPeter MaydellWhen you connect gdb to the gdbstub, it will automatically
87d211556fSPeter Maydellconnect to the first inferior; you can display the CPUs in this
88d211556fSPeter Maydellcluster using the gdb ``info thread`` command, and switch between
89d211556fSPeter Maydellthem using gdb's usual thread-management commands.
90d211556fSPeter Maydell
91d211556fSPeter MaydellFor multi-cluster machines, unfortunately gdb does not by default
92d211556fSPeter Maydellhandle multiple inferiors, and so you have to explicitly connect
93d211556fSPeter Maydellto them. First, you must connect with the ``extended-remote``
94d211556fSPeter Maydellprotocol, not ``remote``::
95d211556fSPeter Maydell
96d211556fSPeter Maydell    (gdb) target extended-remote localhost:1234
97d211556fSPeter Maydell
98d211556fSPeter MaydellOnce connected, gdb will have a single inferior, for the
99d211556fSPeter Maydellfirst cluster. You need to create inferiors for the other
100d211556fSPeter Maydellclusters and attach to them, like this::
101d211556fSPeter Maydell
102d211556fSPeter Maydell  (gdb) add-inferior
103d211556fSPeter Maydell  Added inferior 2
104d211556fSPeter Maydell  (gdb) inferior 2
105d211556fSPeter Maydell  [Switching to inferior 2 [<null>] (<noexec>)]
106d211556fSPeter Maydell  (gdb) attach 2
107d211556fSPeter Maydell  Attaching to process 2
108d211556fSPeter Maydell  warning: No executable has been specified and target does not support
109d211556fSPeter Maydell  determining executable automatically.  Try using the "file" command.
110d211556fSPeter Maydell  0x00000000 in ?? ()
111d211556fSPeter Maydell
112d211556fSPeter MaydellOnce you've done this, ``info threads`` will show CPUs in
113d211556fSPeter Maydellall the clusters you have attached to::
114d211556fSPeter Maydell
115d211556fSPeter Maydell  (gdb) info threads
116d211556fSPeter Maydell    Id   Target Id         Frame
117d211556fSPeter Maydell    1.1  Thread 1.1 (cortex-m33-arm-cpu cpu [running]) 0x00000000 in ?? ()
118d211556fSPeter Maydell  * 2.1  Thread 2.2 (cortex-m33-arm-cpu cpu [halted ]) 0x00000000 in ?? ()
119d211556fSPeter Maydell
120d211556fSPeter MaydellYou probably also want to set gdb to ``schedule-multiple`` mode,
121d211556fSPeter Maydellso that when you tell gdb to ``continue`` it resumes all CPUs,
122d211556fSPeter Maydellnot just those in the cluster you are currently working on::
123d211556fSPeter Maydell
124d211556fSPeter Maydell  (gdb) set schedule-multiple on
125d211556fSPeter Maydell
12624b1a6aaSSebastian MeyerUsing unix sockets
12724b1a6aaSSebastian Meyer==================
12824b1a6aaSSebastian Meyer
12924b1a6aaSSebastian MeyerAn alternate method for connecting gdb to the QEMU gdbstub is to use
13024b1a6aaSSebastian Meyera unix socket (if supported by your operating system). This is useful when
13124b1a6aaSSebastian Meyerrunning several tests in parallel, or if you do not have a known free TCP
13224b1a6aaSSebastian Meyerport (e.g. when running automated tests).
13324b1a6aaSSebastian Meyer
13424b1a6aaSSebastian MeyerFirst create a chardev with the appropriate options, then
13524b1a6aaSSebastian Meyerinstruct the gdbserver to use that device:
13624b1a6aaSSebastian Meyer
13724b1a6aaSSebastian Meyer.. parsed-literal::
13824b1a6aaSSebastian Meyer
13924b1a6aaSSebastian Meyer   |qemu_system| -chardev socket,path=/tmp/gdb-socket,server=on,wait=off,id=gdb0 -gdb chardev:gdb0 -S ...
14024b1a6aaSSebastian Meyer
14124b1a6aaSSebastian MeyerStart gdb as before, but this time connect using the path to
14224b1a6aaSSebastian Meyerthe socket::
14324b1a6aaSSebastian Meyer
14424b1a6aaSSebastian Meyer   (gdb) target remote /tmp/gdb-socket
14524b1a6aaSSebastian Meyer
14624b1a6aaSSebastian MeyerNote that to use a unix socket for the connection you will need
14724b1a6aaSSebastian Meyergdb version 9.0 or newer.
14824b1a6aaSSebastian Meyer
149acb0a27eSPeter MaydellAdvanced debugging options
150acb0a27eSPeter Maydell==========================
151acb0a27eSPeter Maydell
152acb0a27eSPeter MaydellChanging single-stepping behaviour
153acb0a27eSPeter Maydell^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
154324b2298SPaolo Bonzini
155324b2298SPaolo BonziniThe default single stepping behavior is step with the IRQs and timer
156324b2298SPaolo Bonziniservice routines off. It is set this way because when gdb executes a
157324b2298SPaolo Bonzinisingle step it expects to advance beyond the current instruction. With
158324b2298SPaolo Bonzinithe IRQs and timer service routines on, a single step might jump into
159324b2298SPaolo Bonzinithe one of the interrupt or exception vectors instead of executing the
160324b2298SPaolo Bonzinicurrent instruction. This means you may hit the same breakpoint a number
161324b2298SPaolo Bonziniof times before executing the instruction gdb wants to have executed.
162324b2298SPaolo BonziniBecause there are rare circumstances where you want to single step into
163324b2298SPaolo Bonzinian interrupt vector the behavior can be controlled from GDB. There are
164324b2298SPaolo Bonzinithree commands you can query and set the single step behavior:
165324b2298SPaolo Bonzini
166324b2298SPaolo Bonzini``maintenance packet qqemu.sstepbits``
167324b2298SPaolo Bonzini   This will display the MASK bits used to control the single stepping
168324b2298SPaolo Bonzini   IE:
169324b2298SPaolo Bonzini
170324b2298SPaolo Bonzini   ::
171324b2298SPaolo Bonzini
172324b2298SPaolo Bonzini      (gdb) maintenance packet qqemu.sstepbits
173324b2298SPaolo Bonzini      sending: "qqemu.sstepbits"
174324b2298SPaolo Bonzini      received: "ENABLE=1,NOIRQ=2,NOTIMER=4"
175324b2298SPaolo Bonzini
176324b2298SPaolo Bonzini``maintenance packet qqemu.sstep``
177324b2298SPaolo Bonzini   This will display the current value of the mask used when single
178324b2298SPaolo Bonzini   stepping IE:
179324b2298SPaolo Bonzini
180324b2298SPaolo Bonzini   ::
181324b2298SPaolo Bonzini
182324b2298SPaolo Bonzini      (gdb) maintenance packet qqemu.sstep
183324b2298SPaolo Bonzini      sending: "qqemu.sstep"
184324b2298SPaolo Bonzini      received: "0x7"
185324b2298SPaolo Bonzini
186324b2298SPaolo Bonzini``maintenance packet Qqemu.sstep=HEX_VALUE``
187324b2298SPaolo Bonzini   This will change the single step mask, so if wanted to enable IRQs on
188324b2298SPaolo Bonzini   the single step, but not timers, you would use:
189324b2298SPaolo Bonzini
190324b2298SPaolo Bonzini   ::
191324b2298SPaolo Bonzini
192324b2298SPaolo Bonzini      (gdb) maintenance packet Qqemu.sstep=0x5
193324b2298SPaolo Bonzini      sending: "qemu.sstep=0x5"
194324b2298SPaolo Bonzini      received: "OK"
19550679467SJon Doron
196acb0a27eSPeter MaydellExamining physical memory
197acb0a27eSPeter Maydell^^^^^^^^^^^^^^^^^^^^^^^^^
19850679467SJon Doron
19950679467SJon DoronAnother feature that QEMU gdbstub provides is to toggle the memory GDB
20050679467SJon Doronworks with, by default GDB will show the current process memory respecting
20150679467SJon Doronthe virtual address translation.
20250679467SJon Doron
20350679467SJon DoronIf you want to examine/change the physical memory you can set the gdbstub
20450679467SJon Doronto work with the physical memory rather with the virtual one.
20550679467SJon Doron
20650679467SJon DoronThe memory mode can be checked by sending the following command:
20750679467SJon Doron
20850679467SJon Doron``maintenance packet qqemu.PhyMemMode``
20950679467SJon Doron    This will return either 0 or 1, 1 indicates you are currently in the
21050679467SJon Doron    physical memory mode.
21150679467SJon Doron
21250679467SJon Doron``maintenance packet Qqemu.PhyMemMode:1``
21350679467SJon Doron    This will change the memory mode to physical memory.
21450679467SJon Doron
21550679467SJon Doron``maintenance packet Qqemu.PhyMemMode:0``
21650679467SJon Doron    This will change it back to normal memory mode.
217*abf7ba31SIlya Leoshkevich
218*abf7ba31SIlya LeoshkevichSecurity considerations
219*abf7ba31SIlya Leoshkevich=======================
220*abf7ba31SIlya Leoshkevich
221*abf7ba31SIlya LeoshkevichConnecting to the GDB socket allows running arbitrary code inside the guest;
222*abf7ba31SIlya Leoshkevichin case of the TCG emulation, which is not considered a security boundary, this
223*abf7ba31SIlya Leoshkevichalso means running arbitrary code on the host. Additionally, when debugging
224*abf7ba31SIlya Leoshkevichqemu-user, it allows directly downloading any file readable by QEMU from the
225*abf7ba31SIlya Leoshkevichhost.
226*abf7ba31SIlya Leoshkevich
227*abf7ba31SIlya LeoshkevichThe GDB socket is not protected by authentication, authorization or encryption.
228*abf7ba31SIlya LeoshkevichIt is therefore a responsibility of the user to make sure that only authorized
229*abf7ba31SIlya Leoshkevichclients can connect to it, e.g., by using a unix socket with proper
230*abf7ba31SIlya Leoshkevichpermissions, or by opening a TCP socket only on interfaces that are not
231*abf7ba31SIlya Leoshkevichreachable by potential attackers.
232