[Yocto] QEMU kernel debugging with GDB

Created:

Updated:

This is a post about aarch64 kernel debugging using qemu and gdb in yocto environment

kernel configuration to debug kernel with gdb

To include debugging information in the vmlinux binary image,set CONFIG_DEBUG_INFO to y. Usually it is not set as default.

1
CONFIG_DEBUG_INFO=y

kernel compilation option in the yocto environment

In ~~~temp/log.do_compile file, there is a compilation log as below. This information is used when specifying the path to the source code in gdb.

1
-fdebug-prefix-map=/mnt/sdb1/src/git/yocto/poky/build/tmp/work-shared/qemuarm64/kernel-source=/usr/src/kernel 

Look at the description about the debug-prefix-map option here

  • This option is added by the code below.
    In conf/bitbake.conf
    1
    
      KERNEL_SRC_PATH = "/usr/src/kernel"
    

    In conf/bitbake.conf

    1
    
      STAGING_KERNEL_DIR = "${TMPDIR}/work-shared/${MACHINE}/kernel-source"
    

    In classes/kernel-arch.bbclass

    1
    
     KERNEL_CC = "${CCACHE}${HOST_PREFIX}gcc ${HOST_CC_KERNEL_ARCH} -fuse-ld=bfd ${DEBUG_PREFIX_MAP} -fdebug-prefix-map=${STAGING_KERNEL_DIR}=${KERNEL_SRC_PATH}"
    

Run qemu

After building, run the command below in the build directory.
-s means -gdb tcp::1234. -S means Do not start CPU at startup(you must type ‘c’ in the monitor)

1
runqemu qemuarm64 qemuparams="-s -S"

Run gdb for aarch64

  1. Run gdb
    1
    2
    3
    4
    5
    6
    
      $ ./aarch64-poky-linux-gdb
      ...
      ...
      For help, type "help".
      Type "apropos word" to search for commands related to "word".
      (gdb) 
    
  2. Load symbol from vmlinux
    1
    
    (gdb) add-symbol-file ~/src//git/yocto/poky/build/tmp/work/qemuarm64-poky-linux/linux-yocto/5.15.14+gitAUTOINC+72e4eafb6b_f77b2ba7d5-r0/linux-qemuarm64-standard-build/vmlinux
    
  3. Connect to a target machine
    1
    
     (gdb) target remote:1234
    
  4. Set source path. Specify the path according to the debug-prefix-map setting specified during compilation. Specify the path as an absolute path.
    1
    
     (gdb) set substitute-path /usr/src/kernel /home/chuljeon39a/src/git/yocto/poky/build/tmp/work/qemuarm64-poky-linux/linux-yocto/5.15.14+gitAUTOINC+72e4eafb6b_f77b2ba7d5-r0/linux-qemuarm64-standard-build/source/
    
  5. Run layout subcommands
    1
    2
    3
    
     (gdb) layout asm
     (gdb) layout src
     (gdb) layout split
    
  6. set breakpoint at start_kernel
    1
    
     (gdb) b start_kernel
    
  7. Continue program being debugged
    1
    
     (gdb) c
    

    yocto_qemu_gdb_kernel_debug_1

Debugging using eclipse cdt

If you use eclipse cdt, you can debug more conveniently in the GUI environment.

  1. Execute eclipse
  2. In menu, Run -> Debug Configurations…
  3. Choose GDB QEMU Debugging and click the right mouse button. Then choose New Configuration.
  4. On Main tab, set Name and C/C++ Application: to the path of vmlinux. eclipse_cdt_yocto_qemu_kernel_debugging_1
  5. On Debugger tab, set GDB Client Setup to the path of GDB and set Remote Target to localhost 1234. eclipse_cdt_yocto_qemu_kernel_debugging_2
  6. On Startup tab, set Load Symbols and Executable to the path of vmlinux.
    Set Set breakpoint at: to start_kernel. eclipse_cdt_yocto_qemu_kernel_debugging_3
  7. On Source tab, set Path Mapping to the paths that were set as debug-prefix-map during compilation. eclipse_cdt_yocto_qemu_kernel_debugging_4
  8. Click Apply and Debug eclipse_cdt_yocto_qemu_kernel_debugging_5
  9. Click resume(F8) button or press ‘c’ at Debugger Console window. Then it stops at start_kernel eclipse_cdt_yocto_qemu_kernel_debugging_6

* To debug repeatedly,

  1. Run qemu
  2. Relaunch Debug
    Do debugging
  3. Terminate gdb and qemu
  4. repeat 1 ~ 3 eclipse_cdt_yocto_qemu_kernel_debugging_7

Leave a comment