Search code examples
gdbcopy-pasteqemu

qemu quits when pressing ctrl-c in gdb


Debugging my own kernel with qemu and gdb seems to be unnecessarily hard because pressing ctrl-c in gdb to break qemu does not break it, but makes it quit with the message

qemu-system-x86_64: terminating on signal 2
[Inferior 1 (Remote target) exited normally]

qemu command line:

qemu-system-x86_64 -s -no-shutdown -no-reboot -enable-kvm -m 1G -smp cores=1 -cpu qemu64 -drive if=pflash,format=raw,file=ovmf/OVMF.fd -drive file=fat:rw:hda,format=raw -net none -debugcon file:debug.log -global isa-debugcon.iobase=0x402 &

The behavior is the same without KVM. Could someone please help, how to solve this?

  • qemu-system-x86_64 v3.1.0
  • gdb v8.2.1

I would like not to build the latest versions of these from source as it seems to be a daunting task to do.

EDIT: Created a minimal environment where the issue can be reproduced. I may have tracked it down to running the whole thing from a shell script, but can't seem to progress further. Commenting out the gdb call in the script and starting it from a separate terminal, solves the issue (however i like things that work with as few keystrokes as possible). You can download it here. Just start the script called qd (Is there a nicer way to provide files? I will delete this after a while.)


Solution

  • I tested with QEMU 5.0.0 and GDB 9.2, same issue, and same solution, that is commenting out the GDB call in the script and starting it from a separate terminal. You could probably just modify your script so that QEMU would be started in another terminal. Starting QEMU using nohup is not working either.

    I included the script I am usually using for building fresh versions of QEMU and GDB: latest versions are likely to have fixed bugs. The script is working on Ubuntu 20.04, and is probably still working on 16.04 and 18.04 - you may have to make small adjustments at the beginning of the script. Feel free to report issues, I would be willing to fix them.

    build-qemu-gdb.sh:

    #!/bin/bash
    
    set -e 
    
    # Xenial/16.04
    PERL_MODULES_VERSION=5.22
    SPHINX=python-sphinx
    
    # Bionic/18.04
    PERL_MODULES_VERSION=5.26
    SPHINX=python-sphinx
    
    # Focal/20.04
    PERL_MODULES_VERSION=5.30
    SPHINX="sphinx-doc sphinx-common"
    
    # Qemu
    QEMU_VERSION=5.0.0
    PREFIX=/opt/qemu-${QEMU_VERSION}
    
    # GDB
    GDB_VERSION=9.2
    
    do_get_gdb()
    {
      if [ -f gdb-${GDB_VERSION}.tar.xz ]
      then
        echo "gdb-${GDB_VERSION}.tar.xz is present."
      else
        wget http://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.xz
      fi
    }
    
    do_get_qemu()
    {
      if [ -f qemu-${QEMU_VERSION}.tar.xz ]
      then
        echo "qemu-${QEMU_VERSION}.tar.xz is present."
      else
        wget https://download.qemu.org/qemu-${QEMU_VERSION}.tar.xz
      fi
    }
    
    do_install_prerequisites()
    {
      sudo apt-get install libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev libaio-dev libbluetooth-dev libbrlapi-dev libbz2-dev  libcap-dev libcap-ng-dev libcurl4-gnutls-dev libgtk-3-dev libibverbs-dev \
      libjpeg8-dev libncurses5-dev libnuma-dev librbd-dev librdmacm-dev libsasl2-dev libsdl2-dev libseccomp-dev libsnappy-dev libssh2-1-dev libvde-dev libvdeplug-dev libvte-2.91-dev libxen-dev liblzo2-dev \
      valgrind xfslibs-dev liblzma-dev flex bison texinfo gettext perl perl-modules-${PERL_MODULES_VERSION} ${SPHINX}
    }
    
    do_configure()
    {
      local TARGET_LIST="x86_64-softmmu"
      pushd qemu-${QEMU_VERSION}
      ./configure --target-list="${TARGET_LIST}" --prefix=${PREFIX} --extra-cflags="-I$(pwd)/packages/include" --extra-ldflags="-L$(pwd)/packages/lib" 
      popd
    }
    
    do_extract_qemu()
    {
      echo "extracting QEMU..."
      rm -rf qemu-${QEMU_VERSION}
      tar Jxf qemu-${QEMU_VERSION}.tar.xz
    }
    
    do_build_qemu()
    {
      echo "building..."
      pushd qemu-${QEMU_VERSION}
      make all
      popd
    }
    
    do_install_qemu()
    {
      echo "installing..."
      pushd qemu-${QEMU_VERSION}
      sudo make install
      popd
    }
    
    do_build_qemu()
    {
      do_extract_qemu
      do_configure
      do_build_qemu
      do_install_qemu
    }
    
    
    do_extract_gdb()
    {
      echo "extracting GDB..."
      rm -rf gdb-${GDB_VERSION}
      tar Jxf gdb-${GDB_VERSION}.tar.xz
    }
    
    do_build_gdb()
    {
      do_extract_gdb
      rm -rf gdb
      mkdir gdb
      pushd gdb
      ../gdb-${GDB_VERSION}/configure --enable-tui --prefix=/opt/gdb-${GDB_VERSION}-x86_64-none-elf --target=x86_64-none-elf --program-prefix=x86_64-none-elf-
      make all install
      popd
    }
    
    # main
    do_install_prerequisites
    
    do_get_qemu
    do_build_qemu
    
    do_get_gdb
    do_build_gdb
    

    The resulting new paths for QEMU and GDB after installation would be:

    /opt/qemu-5.0.0/bin/qemu-system-x86_64
    /opt/gdb-9.2-x86_64-none-elf/bin/x86_64-none-elf-gdb