一、找到core dump产生的文件
1.打开/etc/profile文件添加如下配置:ulimit -c unlimited
sudo gedit /etc/profile
ulimit -c unlimited
2.在bashrc中进行生效,添加如下配置:source /etc/profile
gedit ~/.bashrc
source /etc/profile
3.设置core文件的目录
sudo sysctl -w kernel.core_pattern=/var/crash/core.%u.%e.%p
4.执行产生错误的可执行程序
./HelloGDB
5.用gdb调试工具查看core文件出现错误的行
gdb HelloGDB /var/crash/core.1000.HelloGDB.4393
Core was generated by `/home/bisheng/CLionProjects/gdb/cmake-build-debug/HelloGDB'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000561c01500301 in fun1 () at /home/bisheng/CLionProjects/gdb/fun.h:6
6 *p = 0;
[Current thread is 1 (Thread 0x7fa7c0f0c700 (LWP 4818))]
可以看出,上述core dump 1)是由/home/bisheng/CLionProjects/gdb/cmake-build-debug/HelloGDB这个可执行程序产生的; 2)是在fun.h这个函数的第6行*p = 0 产生的 3)产生core dump的线程是线程1
6.查看函数调用栈
bt
#0 0x0000561c01500301 in fun1 () at /home/bisheng/CLionProjects/gdb/fun.h:6
#1 0x0000561c015003ad in thread1 () at /home/bisheng/CLionProjects/gdb/main.cpp:12
#2 0x0000561c015012b6 in std::__invoke_impl<void, void (*)()> (__f=@0x561c015aaeb8: 0x561c01500365 <thread1()>) at /usr/include/c++/9/bits/invoke.h:60
#3 0x0000561c0150124e in std::__invoke<void (*)()> (__fn=@0x561c015aaeb8: 0x561c01500365 <thread1()>) at /usr/include/c++/9/bits/invoke.h:95
#4 0x0000561c015011e0 in std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul> (this=0x561c015aaeb8) at /usr/include/c++/9/thread:244
#5 0x0000561c0150119d in std::thread::_Invoker<std::tuple<void (*)()> >::operator() (this=0x561c015aaeb8) at /usr/include/c++/9/thread:251
--Type <RET> for more, q to quit, c to continue without paging--
#6 0x0000561c0150116e in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> > >::_M_run (this=0x561c015aaeb0) at /usr/include/c++/9/thread:195
#7 0x00007fa7c1344de4 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#8 0x00007fa7c1458609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#9 0x00007fa7c1180163 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
可以看到函数依次的调用顺序
|