xref: /qemu/docs/system/gdb.rst (revision acb0a27eb85ccf5de6052bf7407531e76fb13a5d)
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
18e5910d42SPeter Maydellconnection, use the ``-gdb dev`` option instead of ``-s``.)
19324b2298SPaolo Bonzini
20324b2298SPaolo Bonzini.. parsed-literal::
21324b2298SPaolo Bonzini
22e5910d42SPeter Maydell   |qemu_system| -s -S -kernel bzImage -hda rootdisk.img -append "root=/dev/hda"
23e5910d42SPeter Maydell
24e5910d42SPeter MaydellQEMU will launch but will silently wait for gdb to connect.
25324b2298SPaolo Bonzini
26324b2298SPaolo BonziniThen launch gdb on the 'vmlinux' executable::
27324b2298SPaolo Bonzini
28324b2298SPaolo Bonzini   > gdb vmlinux
29324b2298SPaolo Bonzini
30324b2298SPaolo BonziniIn gdb, connect to QEMU::
31324b2298SPaolo Bonzini
32324b2298SPaolo Bonzini   (gdb) target remote localhost:1234
33324b2298SPaolo Bonzini
34324b2298SPaolo BonziniThen you can use gdb normally. For example, type 'c' to launch the
35324b2298SPaolo Bonzinikernel::
36324b2298SPaolo Bonzini
37324b2298SPaolo Bonzini   (gdb) c
38324b2298SPaolo Bonzini
39324b2298SPaolo BonziniHere are some useful tips in order to use gdb on system code:
40324b2298SPaolo Bonzini
41324b2298SPaolo Bonzini1. Use ``info reg`` to display all the CPU registers.
42324b2298SPaolo Bonzini
43324b2298SPaolo Bonzini2. Use ``x/10i $eip`` to display the code at the PC position.
44324b2298SPaolo Bonzini
45324b2298SPaolo Bonzini3. Use ``set architecture i8086`` to dump 16 bit code. Then use
46324b2298SPaolo Bonzini   ``x/10i $cs*16+$eip`` to dump the code at the PC position.
47324b2298SPaolo Bonzini
48*acb0a27eSPeter MaydellAdvanced debugging options
49*acb0a27eSPeter Maydell==========================
50*acb0a27eSPeter Maydell
51*acb0a27eSPeter MaydellChanging single-stepping behaviour
52*acb0a27eSPeter Maydell^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
53324b2298SPaolo Bonzini
54324b2298SPaolo BonziniThe default single stepping behavior is step with the IRQs and timer
55324b2298SPaolo Bonziniservice routines off. It is set this way because when gdb executes a
56324b2298SPaolo Bonzinisingle step it expects to advance beyond the current instruction. With
57324b2298SPaolo Bonzinithe IRQs and timer service routines on, a single step might jump into
58324b2298SPaolo Bonzinithe one of the interrupt or exception vectors instead of executing the
59324b2298SPaolo Bonzinicurrent instruction. This means you may hit the same breakpoint a number
60324b2298SPaolo Bonziniof times before executing the instruction gdb wants to have executed.
61324b2298SPaolo BonziniBecause there are rare circumstances where you want to single step into
62324b2298SPaolo Bonzinian interrupt vector the behavior can be controlled from GDB. There are
63324b2298SPaolo Bonzinithree commands you can query and set the single step behavior:
64324b2298SPaolo Bonzini
65324b2298SPaolo Bonzini``maintenance packet qqemu.sstepbits``
66324b2298SPaolo Bonzini   This will display the MASK bits used to control the single stepping
67324b2298SPaolo Bonzini   IE:
68324b2298SPaolo Bonzini
69324b2298SPaolo Bonzini   ::
70324b2298SPaolo Bonzini
71324b2298SPaolo Bonzini      (gdb) maintenance packet qqemu.sstepbits
72324b2298SPaolo Bonzini      sending: "qqemu.sstepbits"
73324b2298SPaolo Bonzini      received: "ENABLE=1,NOIRQ=2,NOTIMER=4"
74324b2298SPaolo Bonzini
75324b2298SPaolo Bonzini``maintenance packet qqemu.sstep``
76324b2298SPaolo Bonzini   This will display the current value of the mask used when single
77324b2298SPaolo Bonzini   stepping IE:
78324b2298SPaolo Bonzini
79324b2298SPaolo Bonzini   ::
80324b2298SPaolo Bonzini
81324b2298SPaolo Bonzini      (gdb) maintenance packet qqemu.sstep
82324b2298SPaolo Bonzini      sending: "qqemu.sstep"
83324b2298SPaolo Bonzini      received: "0x7"
84324b2298SPaolo Bonzini
85324b2298SPaolo Bonzini``maintenance packet Qqemu.sstep=HEX_VALUE``
86324b2298SPaolo Bonzini   This will change the single step mask, so if wanted to enable IRQs on
87324b2298SPaolo Bonzini   the single step, but not timers, you would use:
88324b2298SPaolo Bonzini
89324b2298SPaolo Bonzini   ::
90324b2298SPaolo Bonzini
91324b2298SPaolo Bonzini      (gdb) maintenance packet Qqemu.sstep=0x5
92324b2298SPaolo Bonzini      sending: "qemu.sstep=0x5"
93324b2298SPaolo Bonzini      received: "OK"
9450679467SJon Doron
95*acb0a27eSPeter MaydellExamining physical memory
96*acb0a27eSPeter Maydell^^^^^^^^^^^^^^^^^^^^^^^^^
9750679467SJon Doron
9850679467SJon DoronAnother feature that QEMU gdbstub provides is to toggle the memory GDB
9950679467SJon Doronworks with, by default GDB will show the current process memory respecting
10050679467SJon Doronthe virtual address translation.
10150679467SJon Doron
10250679467SJon DoronIf you want to examine/change the physical memory you can set the gdbstub
10350679467SJon Doronto work with the physical memory rather with the virtual one.
10450679467SJon Doron
10550679467SJon DoronThe memory mode can be checked by sending the following command:
10650679467SJon Doron
10750679467SJon Doron``maintenance packet qqemu.PhyMemMode``
10850679467SJon Doron    This will return either 0 or 1, 1 indicates you are currently in the
10950679467SJon Doron    physical memory mode.
11050679467SJon Doron
11150679467SJon Doron``maintenance packet Qqemu.PhyMemMode:1``
11250679467SJon Doron    This will change the memory mode to physical memory.
11350679467SJon Doron
11450679467SJon Doron``maintenance packet Qqemu.PhyMemMode:0``
11550679467SJon Doron    This will change it back to normal memory mode.
116