应用调试
Read time: 8 minute(s)
使用 GDB 调试应用程序
Read time: 8 minute(s)
Luban SDK 中集成了支持 GNU symbolic debugger (GDB) 调试的三方开源工具包。
GDB 是一种高级调试手段,有一定的门槛,建议对此熟悉的工程师使用。
详细调试流程如下所示:
- 在 SDK 根目录执行下列命令,进入 menuconfig
配置界面:
make menuconfig
- 在 menuconfig 的配置界面中勾选下列选项,配置 GDB
工具链:
Third-party packages ---> [*] binutils ---> [*] binutils binaries [*] gdb ---> [*] gdbserver [*] full debugger [*] TUI support
- 在 SDK 根目录下执行下列命令,进入 menuconfig
配置界面:
make menuconfig
- 在配置界面中设置保留应用程序调试信息,以应用 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 的二进制文件,多个文件可使用空格分隔。 - 在 SDK
根目录下执行下列命令,对指定源码包重新编译:
make <pkg>-reconfigure
例如,重新编译 test-uart:make test-uart-reconfigure
- 重新编译完成后,在 SDK
根目录下执行下列命令重新打包并烧录源码包:
make
- 重新烧录启动后在控制台执行下列命令:
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
- 执行下列命令对指定应用程序进行调试:
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.
-
执行下列 命令在主函数中打断点:
b main
输出示例如下:Breakpoint 1 at 0x103d4
-
执行
run
命令,运行程序并停止在断点处,既可以开始单步调试:Starting program: /usr/bin/test_uart Breakpoint 1, 0x00000000000103d4 in main ()
关于更多 GDB 的使用方法,参考 GDB 官方文档。
使用 GDB 调试源码
Read time: 8 minute(s)
若将待调试应用的源代码复制至当前工作目录,GDB 可精准定位到具体代码行。例如:
- 使用
ls
命令显示包含 test_uart.c 的目录结构:bin lib media root test_uart var dev lib64 mnt run test_uart.c etc lib64xthead opt sbin tmp init linuxrc proc sys usr
-
执行
gdb 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 test_uart...
-
在 GDB 交互界面中执行以下操作:
- 设置主函数断点:输入
b main
,触发警告但成功绑定到源码位置: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. Breakpoint 1 at 0x10eb0: file /disk2/keliang.liu/1602/develop/source/artinchip/test-uart/test_uart.c, line 675.
- 启动程序:输入
run
,程序在断点处暂停并显示调用栈信息:Starting program: /test_uart [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64xthead/lp64d/libthread_db.so.1". Breakpoint 1, main (argc=1, argv=0x3fffb46d08) at /d21x/develop/source/artinchip/test-uart/test_uart.c:675 675 if (argc != SIMPLE_ARGC_NUM && argc != NORMAL_ARGC_NUM) {
- 单步执行:输入
next
跳过当前行,进入下一条语句:681 print_usage(argv[0]);
- 设置主函数断点:输入
CoreDump 调试
Read time: 8 minute(s)
CoreDump(核心转储)是一款调试复杂软件问题的核心工具。CoreDump 通过操作系统在进程异常终止时生成的内存快照文件,记录崩溃瞬间的寄存器状态、堆栈信息、内存布局等关键数据。
CoreDump 详细使用说明如下:
- 启用内核 CoreDump 功能:
- 在 SDK 根目录下执行下列命令打开 kernel
的配置界面:
make kenel-menuconfig
- 勾选下列选项,启用内核 CoreDump
功能:
Executable file formats ---> [*] Kernel support for ELF binaries [*] Write ELF core dumps with partial segments [*] Enable core dump support
- 在 SDK 根目录下执行下列命令打开 kernel
的配置界面:
- 配置 SDK 工程化适配功能:
- 在 SDK 根层目录执行下列命令,进入 menuconfig
配置界面:
make menuconfig
- 在 menuconfig 配置界面,勾选下列选项使能应用
test_coredump,并保留应用调试信息:
Build options ---> [*] build packages with debugging symbols gcc debug level (debug level 2) ---> [*] strip target binaries (test_coredump) executables that should not be stripped
-
同时勾选下列选项,使能调试工具 GDB:
Third-party packages ---> [*] binutils ---> [*] binutils binaries [*] gdb ---> [*] gdbserver [*] full debugger [*] TUI support
- 在 SDK 根层目录执行下列命令,进入 menuconfig
配置界面:
- 在 SDK
根目录下执行下列命令,对指定源码包重新编译:
make <pkg>-reconfigure
例如,重新编译 test-coredump:make test-coredump-reconfigure
- 重新编译完成后,在 SDK
根目录下执行下列命令重新打包并烧录源码包:
make
- 重新烧录启动后在控制台执行下列命令:
nm usr/bin/test_coredump | 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 00000000000104c8 T test_add_and_sub
- 执行下列命令解除系统对 CoreDump
文件大小的限制:
ulimit -c unlimited
- 运行测试程序
test_coredump
,当发生段错误时会自动在当前目录生成完整的 CoreDump 文件:Segmentation fault (core dumped)
-
执行下列命令,对指定应用程序进行调试:
gdb /usr/bin/test_coredump core
GDB 初始化信息显示: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_coredump... 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. warning: exec file is newer than core file. [New LWP 181] Core was generated by `test_coredump'. Program terminated with signal SIGSEGV, Segmentation fault. #0 0x0000000000010488 in add ()
-
在断点处执行下列命令查看函数调用栈:
bt
0x0000000000010488 in add () 0x00000000000104d6 in test_add_and_sub () 0x00000000000103d8 in main ()
关于更多 GDB 的使用方法,参考 GDB 官方文档。