Edit online

使用 GDB 调试应用程序

Luban SDK 中集成了支持 GNU symbolic debugger (GDB) 调试的三方开源工具包。

GDB 是一种高级调试手段,有一定的门槛,建议对此熟悉的工程师使用。

详细调试流程如下所示:
  1. 在 SDK 根目录执行下列命令,进入 menuconfig 配置界面:
    make menuconfig
  2. 在 menuconfig 的配置界面中勾选下列选项,配置 GDB 工具链:
    Third-party packages  --->
        [*] binutils  --->
            [*]   binutils binaries
        [*] gdb  --->
            [*]   gdbserver
            [*]   full debugger
            [*]   TUI support
  3. 在 SDK 根目录下执行下列命令,进入 menuconfig 配置界面:
    make menuconfig
  4. 在配置界面中设置保留应用程序调试信息,以应用 test_uart 为例:
    Build options  --->
        [*] build packages with debugging symbols
            gcc debug level (debug level 2)  --->
        [*] strip target binaries
            (test_uart)    executables that should not be stripped
    ArtInChip packages  --->
        Sample code  --->
            [*] test-uart  --->

    配置项 executables that should not be stripped 用于指定不进行 stripped 的二进制文件,多个文件可使用空格分隔。

  5. 在 SDK 根目录下执行下列命令,对指定源码包重新编译:
    make <pkg>-reconfigure
    例如,重新编译 test-uart:
    make test-uart-reconfigure
  6. 重新编译完成后,在 SDK 根目录下执行下列命令重新打包并烧录源码包:
    make
  7. 重新烧录启动后在控制台执行下列命令:
    nm usr/bin/test_uart | grep 'T'

    如打印下列类似符号表,表示配置成功:

    0000000000012020 d _GLOBAL_OFFSET_TABLE_
    0000000000010390 t _PROCEDURE_LINKAGE_TABLE_
    0000000000012000 D __DATA_BEGIN__
    0000000000012028 D __SDATA_BEGIN__
    0000000000012000 D __TMC_END__
    0000000000010560 T __libc_csu_fini
    0000000000010500 T __libc_csu_init
    00000000000103e0 T _start
    0000000000010484 T add
    00000000000103d0 T main
    00000000000104a6 T sub
  8. 执行下列命令对指定应用程序进行调试:
    gdb usr/bin/test_uart
    输出信息示例如下:
    GNU gdb (GDB) 14.2
    Copyright (C) 2023 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 "riscv64-linux-gnu".
    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 usr/bin/test_uart...
    
    warning: could not convert 'main' from the host encoding (ANSI_X3.4-1968) to UTF-32.
    This normally should not happen, please file a bug report.
  9. 执行下列 命令在主函数中打断点:
    b main
    输出示例如下:
    Breakpoint 1 at 0x103d4
  10. 执行 run 命令,运行程序并停止在断点处,既可以开始单步调试:
    Starting program: /usr/bin/test_uart
    
    Breakpoint 1, 0x00000000000103d4 in main ()

关于更多 GDB 的使用方法,参考 GDB 官方文档。