如何启动gdb调试异常程序
gdb调试机制
调试的过程中可以清晰的了解上下文,以便分析程序原理与产生错误的原因。
- 代码
- 堆栈信息
- 进程与线程状态
调试信息
- 使用gcc或者makefile时编译过程中加上
-g
选项表示编译过程中保留调试符号信息,此方法编译的程序明显比平时编译的程序要大的多
$ g++ -g -o core_dump2 core_dump2.cpp $ g++ -o core_dump22 core_dump2.cpp -rwxrwxr-x 1 fl fl 56K 1月 15 17:59 core_dump2* -rwxrwxr-x 1 fl fl 19K 1月 15 18:18 core_dump22*
- cmake中集成gdb
## gdb set(CMAKE_BUILD_TYPE "Debug") set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb") set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
进程ID
$ ps -ef | grep core_dump2 fl 71382 71367 0 17:59 pts/1 00:00:00 ./core_dump2 fl 71393 69623 0 18:00 pts/0 00:00:00 grep --color=auto core_dump2
core文件
-
利用命令
ulimit -a
查看程序崩溃后是否会保存core文件,如果保留来core文件就可以用来定位问题了。 -
利用命令
ulimit -option value
来编辑指定参数,如ulimit -c unlimited
编辑core file size选项的值为unlimited。 -
编辑完成后,利用
source /etc/profile
来刷新指定文件包含的环境配置信息。
$ ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 31113 max locked memory (kbytes, -l) 65536 max memory size (kbytes, -m) unlimited open files (-n) 8192 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 31113 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited $ ulimit -c unlimited $ source /etc/profile $ ulimit -a core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 31113 max locked memory (kbytes, -l) 65536 max memory size (kbytes, -m) unlimited open files (-n) 8192 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 31113 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
-
core dump的核心转储文件目录与命令规则
-
默认在应用程序的执行目录下或者程序启动后调用来
chdir
之后的目录 -
在
/etc/sysctl.conf
中写入core dump文件路径kernel.core_pattern=/home/aaa/core_dump/core_%e-%p-%u-%s-%t
%e:应用程序名称 %p:应用程序进程id %u:应用程序线程id %s:导致dump的信号id %t:产生dump的事件戳 -
执行命令是配置生效
sudo sysctl -p /etc/sysctl.conf
-
创建core_dump目录
mkdir /home/aaa/core_dump
-
gdb调试的启动与停止
目标程序
确认符号信息加载成功
- 加载失败情况显示
fl@fl:~/tmp/test$ gdb ./core_dump GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2 Copyright (C) 2020 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 "x86_64-linux-gnu". 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"... ./core_dump: No such file or directory. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< (gdb)
- 加载成功情况显示
fl@fl:~/tmp/test$ gdb ./core_dump GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2 Copyright (C) 2020 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 "x86_64-linux-gnu". 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"... Reading symbols from ./core_dump... <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< (gdb)
指定进程
- 运行程序
./core_dump22
$ ./core_dump22 1 2 3 ...
- 查询该程序的进程id
$ ps -ef | grep core_dump22 fl 75837 69623 0 18:48 pts/0 00:00:00 ./core_dump22 fl 76089 71367 0 18:50 pts/1 00:00:00 grep --color=auto core_dump22
- attach进程,此操作需要root权限,
sudo gdb attach 75837
$ sudo gdb attach 75837 GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2 Copyright (C) 2020 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 "x86_64-linux-gnu". 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"... attach: No such file or directory. Attaching to process 75837 Reading symbols from /home/fl/tmp/test/core_dump22... (No debugging symbols found in /home/fl/tmp/test/core_dump22) Reading symbols from /lib/x86_64-linux-gnu/libstdc++.so.6... (No debugging symbols found in /lib/x86_64-linux-gnu/libstdc++.so.6) Reading symbols from /lib/x86_64-linux-gnu/libc.so.6... Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/libc-2.31.so... Reading symbols from /lib/x86_64-linux-gnu/libm.so.6... Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/libm-2.31.so... Reading symbols from /lib64/ld-linux-x86-64.so.2... (No debugging symbols found in /lib64/ld-linux-x86-64.so.2) Reading symbols from /lib/x86_64-linux-gnu/libgcc_s.so.1... (No debugging symbols found in /lib/x86_64-linux-gnu/libgcc_s.so.1) --Type <RET> for more, q to quit, c to continue without paging--
- 此时,程序进入挂起状态
- 执行具体的调试操作,需要继续执行程序使用
continue
命令 - 调试完成后使用命令
detach
指定进程,使用命令quit
退出gdb
(gdb) detach Detaching from program: /home/fl/tmp/test/core_dump22, process 75837 [Inferior 1 (process 75837) detached] (gdb) q $
core dump文件
- 获取core文件和应用程序
- 使用命令
gdb 应用程序 core文件
调试,获取crash原因
$ gdb ./core_dump ~/core_dump/core_core_dump-77936-1000-8-1642244418 GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2 Copyright (C) 2020 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 "x86_64-linux-gnu". 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"... Reading symbols from ./core_dump... [New LWP 77936] Core was generated by `./core_dump'. Program terminated with signal SIGFPE, Arithmetic exception. #0 0x0000560c0c46719f in main (argc=1, argv=0x7fff72528708) at core_dump.c:9 9 c = a / b; (gdb)
- 调试。。。找到carsh原因。
结论
不论是gdb还是windbg,差别是命令语法不一致,调试原理大同小异。
本文来自博客园,作者:flxx,转载请注明原文链接:https://www.cnblogs.com/faithlocus/p/15807788.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗