Steps to Debug the Linux Kernel in QEMU - 2023.1 English

PetaLinux Tools Documentation: Reference Guide (UG1144)

Document ID
UG1144
Release Date
2023-05-16
Version
2023.1 English
  1. Launch QEMU with the currently built Linux by running the following command:
    petalinux-boot --qemu --kernel
  2. Watch the QEMU console. You should see the details of the QEMU command. Get the GDB TCP port from -gdb tcp:<TCP_PORT>.
  3. Open another command console (ensuring the PetaLinux settings script has been sourced), and change to the Linux directory:
    cd "<plnx-proj-root>/images/linux"
  4. Start GDB on the vmlinux kernel image in command mode:
    petalinux-util --gdb vmlinux

    You should see the GDB prompt. For example:

    petalinux-util --gdb vmlinux
    GNU gdb (GDB) 12.1
    Copyright (C) 2022 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 "--host=x86_64-petalinux-linux --target=aarch64-xilinx-linux".
    Type "show configuration" for configuration details.
    For bug reporting instructions, please see:
    <https://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...
    (No debugging symbols found in vmlinux)
    (gdb)
    
  5. Attach to the QEMU target in GDB by running the following GDB command:
    (gdb) target remote :9000
  6. To let QEMU continue execution:
    (gdb) continue
  7. You can use Ctrl+C to interrupt the kernel and get back the GDB prompt.
  8. You can set break points and run other GDB commands to debug the kernel.
CAUTION:
If another process is using port 9000, petalinux-boot attempts to use a different port. See the output of petalinux-boot to determine what port was used. In the following example, port 9001 is used: INFO: qemu-system-arm ... -gdb tcp::9001 ...
Tip: It can be helpful to enable kernel debugging in the kernel configuration menu (petalinux-config --kernel > Kernel hacking > Kernel debugging), so that kernel debug symbols are present in the image.