使用 GDB 调试源码
若将待调试应用的源代码复制至当前工作目录,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]);
- 设置主函数断点:输入
