简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!
优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
1.前言
本篇目的:GDB之调试手段之生成、使用coredump文件。
1.程序crash后,可以生成coredump文件,保存core.xxx结尾文件。
2.然后gdb加载core.xxx文件,就可以知道crash的原因。
2.调试实例
<1>.生成coredump文件
#include <cstdio>
int main(void) {
int *p = nullptr;
printf("12345\n");
*p = 0;
return 0;
}
生成coredump过程
(gdb) n
5 printf("12345\n");
(gdb) n
hello world
6 *p = 0;
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555170 in main () at test.cpp:6
6 *p = 0;
(gdb) generate-core-file
warning: Memory read failed for corefile section, 4096 bytes at 0xffffffffff600000.
Saved corefile core.422717
生成的coredump文件名为:core.422717
<2>.加载coredump文件:core.422717
加载coredump,获取crash原因
# gdb ./test core.422717
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...
[New LWP 422717]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./test'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000555555555170 in main () at test.cpp:6
warning: Source file is more recent than executable.
6 *p = 0;
以上描述crash原因:
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000555555555170 in main () at test.cpp:6
6 *p = 0;
具体原因:第6行空指针取*,然后将0赋值给它,导致段错误,因为int *p = nullptr;被定义为空指针。