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 18*24b1a6aaSSebastian Meyerconnection, use the ``-gdb dev`` option instead of ``-s``. See 19*24b1a6aaSSebastian 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 49d211556fSPeter MaydellDebugging multicore machines 50d211556fSPeter Maydell============================ 51d211556fSPeter Maydell 52d211556fSPeter MaydellGDB's abstraction for debugging targets with multiple possible 53d211556fSPeter Maydellparallel flows of execution is a two layer one: it supports multiple 54d211556fSPeter Maydell"inferiors", each of which can have multiple "threads". When the QEMU 55d211556fSPeter Maydellmachine has more than one CPU, QEMU exposes each CPU cluster as a 56d211556fSPeter Maydellseparate "inferior", where each CPU within the cluster is a separate 57d211556fSPeter Maydell"thread". Most QEMU machine types have identical CPUs, so there is a 58d211556fSPeter Maydellsingle cluster which has all the CPUs in it. A few machine types are 59d211556fSPeter Maydellheterogenous and have multiple clusters: for example the ``sifive_u`` 60d211556fSPeter Maydellmachine has a cluster with one E51 core and a second cluster with four 61d211556fSPeter MaydellU54 cores. Here the E51 is the only thread in the first inferior, and 62d211556fSPeter Maydellthe U54 cores are all threads in the second inferior. 63d211556fSPeter Maydell 64d211556fSPeter MaydellWhen you connect gdb to the gdbstub, it will automatically 65d211556fSPeter Maydellconnect to the first inferior; you can display the CPUs in this 66d211556fSPeter Maydellcluster using the gdb ``info thread`` command, and switch between 67d211556fSPeter Maydellthem using gdb's usual thread-management commands. 68d211556fSPeter Maydell 69d211556fSPeter MaydellFor multi-cluster machines, unfortunately gdb does not by default 70d211556fSPeter Maydellhandle multiple inferiors, and so you have to explicitly connect 71d211556fSPeter Maydellto them. First, you must connect with the ``extended-remote`` 72d211556fSPeter Maydellprotocol, not ``remote``:: 73d211556fSPeter Maydell 74d211556fSPeter Maydell (gdb) target extended-remote localhost:1234 75d211556fSPeter Maydell 76d211556fSPeter MaydellOnce connected, gdb will have a single inferior, for the 77d211556fSPeter Maydellfirst cluster. You need to create inferiors for the other 78d211556fSPeter Maydellclusters and attach to them, like this:: 79d211556fSPeter Maydell 80d211556fSPeter Maydell (gdb) add-inferior 81d211556fSPeter Maydell Added inferior 2 82d211556fSPeter Maydell (gdb) inferior 2 83d211556fSPeter Maydell [Switching to inferior 2 [<null>] (<noexec>)] 84d211556fSPeter Maydell (gdb) attach 2 85d211556fSPeter Maydell Attaching to process 2 86d211556fSPeter Maydell warning: No executable has been specified and target does not support 87d211556fSPeter Maydell determining executable automatically. Try using the "file" command. 88d211556fSPeter Maydell 0x00000000 in ?? () 89d211556fSPeter Maydell 90d211556fSPeter MaydellOnce you've done this, ``info threads`` will show CPUs in 91d211556fSPeter Maydellall the clusters you have attached to:: 92d211556fSPeter Maydell 93d211556fSPeter Maydell (gdb) info threads 94d211556fSPeter Maydell Id Target Id Frame 95d211556fSPeter Maydell 1.1 Thread 1.1 (cortex-m33-arm-cpu cpu [running]) 0x00000000 in ?? () 96d211556fSPeter Maydell * 2.1 Thread 2.2 (cortex-m33-arm-cpu cpu [halted ]) 0x00000000 in ?? () 97d211556fSPeter Maydell 98d211556fSPeter MaydellYou probably also want to set gdb to ``schedule-multiple`` mode, 99d211556fSPeter Maydellso that when you tell gdb to ``continue`` it resumes all CPUs, 100d211556fSPeter Maydellnot just those in the cluster you are currently working on:: 101d211556fSPeter Maydell 102d211556fSPeter Maydell (gdb) set schedule-multiple on 103d211556fSPeter Maydell 104*24b1a6aaSSebastian MeyerUsing unix sockets 105*24b1a6aaSSebastian Meyer================== 106*24b1a6aaSSebastian Meyer 107*24b1a6aaSSebastian MeyerAn alternate method for connecting gdb to the QEMU gdbstub is to use 108*24b1a6aaSSebastian Meyera unix socket (if supported by your operating system). This is useful when 109*24b1a6aaSSebastian Meyerrunning several tests in parallel, or if you do not have a known free TCP 110*24b1a6aaSSebastian Meyerport (e.g. when running automated tests). 111*24b1a6aaSSebastian Meyer 112*24b1a6aaSSebastian MeyerFirst create a chardev with the appropriate options, then 113*24b1a6aaSSebastian Meyerinstruct the gdbserver to use that device: 114*24b1a6aaSSebastian Meyer 115*24b1a6aaSSebastian Meyer.. parsed-literal:: 116*24b1a6aaSSebastian Meyer 117*24b1a6aaSSebastian Meyer |qemu_system| -chardev socket,path=/tmp/gdb-socket,server=on,wait=off,id=gdb0 -gdb chardev:gdb0 -S ... 118*24b1a6aaSSebastian Meyer 119*24b1a6aaSSebastian MeyerStart gdb as before, but this time connect using the path to 120*24b1a6aaSSebastian Meyerthe socket:: 121*24b1a6aaSSebastian Meyer 122*24b1a6aaSSebastian Meyer (gdb) target remote /tmp/gdb-socket 123*24b1a6aaSSebastian Meyer 124*24b1a6aaSSebastian MeyerNote that to use a unix socket for the connection you will need 125*24b1a6aaSSebastian Meyergdb version 9.0 or newer. 126*24b1a6aaSSebastian Meyer 127acb0a27eSPeter MaydellAdvanced debugging options 128acb0a27eSPeter Maydell========================== 129acb0a27eSPeter Maydell 130acb0a27eSPeter MaydellChanging single-stepping behaviour 131acb0a27eSPeter Maydell^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 132324b2298SPaolo Bonzini 133324b2298SPaolo BonziniThe default single stepping behavior is step with the IRQs and timer 134324b2298SPaolo Bonziniservice routines off. It is set this way because when gdb executes a 135324b2298SPaolo Bonzinisingle step it expects to advance beyond the current instruction. With 136324b2298SPaolo Bonzinithe IRQs and timer service routines on, a single step might jump into 137324b2298SPaolo Bonzinithe one of the interrupt or exception vectors instead of executing the 138324b2298SPaolo Bonzinicurrent instruction. This means you may hit the same breakpoint a number 139324b2298SPaolo Bonziniof times before executing the instruction gdb wants to have executed. 140324b2298SPaolo BonziniBecause there are rare circumstances where you want to single step into 141324b2298SPaolo Bonzinian interrupt vector the behavior can be controlled from GDB. There are 142324b2298SPaolo Bonzinithree commands you can query and set the single step behavior: 143324b2298SPaolo Bonzini 144324b2298SPaolo Bonzini``maintenance packet qqemu.sstepbits`` 145324b2298SPaolo Bonzini This will display the MASK bits used to control the single stepping 146324b2298SPaolo Bonzini IE: 147324b2298SPaolo Bonzini 148324b2298SPaolo Bonzini :: 149324b2298SPaolo Bonzini 150324b2298SPaolo Bonzini (gdb) maintenance packet qqemu.sstepbits 151324b2298SPaolo Bonzini sending: "qqemu.sstepbits" 152324b2298SPaolo Bonzini received: "ENABLE=1,NOIRQ=2,NOTIMER=4" 153324b2298SPaolo Bonzini 154324b2298SPaolo Bonzini``maintenance packet qqemu.sstep`` 155324b2298SPaolo Bonzini This will display the current value of the mask used when single 156324b2298SPaolo Bonzini stepping IE: 157324b2298SPaolo Bonzini 158324b2298SPaolo Bonzini :: 159324b2298SPaolo Bonzini 160324b2298SPaolo Bonzini (gdb) maintenance packet qqemu.sstep 161324b2298SPaolo Bonzini sending: "qqemu.sstep" 162324b2298SPaolo Bonzini received: "0x7" 163324b2298SPaolo Bonzini 164324b2298SPaolo Bonzini``maintenance packet Qqemu.sstep=HEX_VALUE`` 165324b2298SPaolo Bonzini This will change the single step mask, so if wanted to enable IRQs on 166324b2298SPaolo Bonzini the single step, but not timers, you would use: 167324b2298SPaolo Bonzini 168324b2298SPaolo Bonzini :: 169324b2298SPaolo Bonzini 170324b2298SPaolo Bonzini (gdb) maintenance packet Qqemu.sstep=0x5 171324b2298SPaolo Bonzini sending: "qemu.sstep=0x5" 172324b2298SPaolo Bonzini received: "OK" 17350679467SJon Doron 174acb0a27eSPeter MaydellExamining physical memory 175acb0a27eSPeter Maydell^^^^^^^^^^^^^^^^^^^^^^^^^ 17650679467SJon Doron 17750679467SJon DoronAnother feature that QEMU gdbstub provides is to toggle the memory GDB 17850679467SJon Doronworks with, by default GDB will show the current process memory respecting 17950679467SJon Doronthe virtual address translation. 18050679467SJon Doron 18150679467SJon DoronIf you want to examine/change the physical memory you can set the gdbstub 18250679467SJon Doronto work with the physical memory rather with the virtual one. 18350679467SJon Doron 18450679467SJon DoronThe memory mode can be checked by sending the following command: 18550679467SJon Doron 18650679467SJon Doron``maintenance packet qqemu.PhyMemMode`` 18750679467SJon Doron This will return either 0 or 1, 1 indicates you are currently in the 18850679467SJon Doron physical memory mode. 18950679467SJon Doron 19050679467SJon Doron``maintenance packet Qqemu.PhyMemMode:1`` 19150679467SJon Doron This will change the memory mode to physical memory. 19250679467SJon Doron 19350679467SJon Doron``maintenance packet Qqemu.PhyMemMode:0`` 19450679467SJon Doron This will change it back to normal memory mode. 195