Search code examples
linux-kernelgdbkernelbreakpointsqemu

Can't get gdb to stop at breakpoint in Linux kernel running under Qemu


Have compiled linux 5.5.5 kernel, with make menuconfig added option CONFIG_GDB_SCRIPTS and turned off option CONFIG_DEBUG_INFO_REDUCED . Runned qemu

qemu-system-x86_64 \
    -kernel arch/x86/boot/bzImage \
    -append "root=/dev/sda1" \
    -device virtio-scsi-pci,id=scsi0 \
  -drive file=../../zso2020_cow.qcow2,if=none,id=drive0 \
  -device scsi-hd,bus=scsi0.0,drive=drive0 \
  -enable-kvm \
  -smp 1 \
  -net nic,model=virtio -net user \
  -net user,hostfwd=tcp::2222-:22 \
  -m 1G -balloon virtio \
  -fsdev local,id=hshare,path=$(pwd),security_model=none -device virtio-9p-pci,fsdev=hshare,mount_tag=hshare \
  -chardev stdio,id=cons,signal=off -device virtio-serial-pci -device virtconsole,chardev=cons \
  -soundhw hda \
  -usb -device usb-mouse \
  -gdb tcp::23308 \
  -display none \
  -S

Qemu runs with compiled kernel, what I have checked using kprint in source code. Then I have runned

gdb \
    -ex "add-auto-load-safe-path $(pwd)" \
    -ex "file vmlinux" \
    -ex 'target remote localhost:23308' \
    -ex 'break start_kernel' \
    -ex 'continue'

(both scripts were runned from directory with compiled kernel)

Qemu goes to user login, and gdb outputs (waits for breakpoint)

GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Reading symbols from vmlinux...done.
Remote debugging using localhost:23308
0x000000000000fff0 in exception_stacks ()
Breakpoint 1 at 0xffffffff8271db30: file init/main.c, line 577.
Continuing.

I tried also

  • hbr insted of br

  • first target remote :23308 i and set breakpoint, then file vmlinux

  • go to compiled kernel directory and install kernel from qemu level

in every case gdb does not stops at breakpoint.

How to properly connect to kernel with gdb, where to look for mistake?


Solution

  • The solution to the problem was to add nokaslr option and use hbreak. That means replace

    -append "root=/dev/sda1"

    with

    `-append "root=/dev/sda1 nokaslr"

    and

    break start_kernel

    with

    hbreak start_kernel

    then gdb properly catches kernel breakpoints.