How to debug qemu and libvirt using gdb

Think about this: you are going to start a virtual machine using qemu command line or libvirt xml or virt-install command line, but fails due to some errors. Now you would like to see what is going on around the values of function parameter and registers during the whole calling procedure. Let's see how to debug qemu and libvirt in this blog.

Precondition:
Install the gdb package.

For QEMU:
- If you use the default qemu package from the distros, like opensuse and redhat, you need to install the debuginfo and debugsource subpackages.
  = Firstly, enable the reporitory includes the debug* subpackages if it is disabled:
    # zypper mr -e openSUSE-Leap-42.3-Debug
  = Secondly, install the debug* subpackages.
    # zypper in qemu-x86-debuginfo qemu-debugsource
  = Now you can use gdb to debug your qemu source code, like
    # gdb --args qemu-system-x86 -m 2048 -enable-kvm -drive file=/opt/opensuse423.qcow2,format=qcow2,if=none,id=drive0 -device virtio-blk-pci,drive=drive0,id=dev0,bootindex=1 -cpu host -name "opensuse423" -boot order=c
- If you have qemu sourcecode by cloning from upstream and plan to build by yourself.
  = Fristly, add the --enable-debug* when you configure:
    # cd qemu
    # ./configure --prefix=/usr --libdir=/usr/lib64/ --target-list=x86_64-softmmu --enable-kvm --enable-debug --enable-sdl //use `./configure --help | less` to see more
    # make && make install
  = Now use gdb to debug qemu
    # gdb --args qemu-system-x86 ...

For Libvirt:
- If you use the dedault libvirt package from distros, install the debug* subpackages.
  = Firstly, enable the reporitory includes the debug* subpackages:
    # zypper mr -e openSUSE-Leap-42.3-Debug
  = Secondly, install the debug* subpackages:
    # zypper in libvirt-debugsource libvirt-daemon-debuginfo
    Install other corresponding subpackages if you need, like libvirt-daemon-driver-network-debuginfo, libvirt-daemon-driver-nodedev-debuginfo and so on.
  = Now use gdb to debug Libvirt
    # gdb --args libvirtd
- If you clone libvirt from upstream, and plan to build my yourself.
  = Firstly, build your own Libvirt:
    # cd libvirt
    # ./autogen.sh --prefix=/usr
    # make && make install
  = Now can use gdb to debug libvirt:
    # gdb --args ./daemon/libvirtd

你可能感兴趣的:(How to debug qemu and libvirt using gdb)