Steps to Debug the Linux Kernel in QEMU - 2022.2 English

PetaLinux Tools Documentation: Reference Guide (UG1144)

Document ID
UG1144
Release Date
2022-10-19
Version
2022.2 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:

    GNU gdb (GDB) 8.3.1
    Copyright (C) 2019 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-oesdk-linux --target=aarch64-xilinx-elf".
    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"...
    vmlinux: No such file or directory.
    (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 may 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.