C++ CORE DUMP gdb 调试

先查看内存转储的大小限制:

ulimit -c

返回的结果是以512 bytes为单位的。
如果没有返回结果,说明设置为0,需要进行手动设置:ulimit -c unlimited
测试代码:

#include <stdio.h>
void foo()
{
    int *ptr = 0;
    *ptr = 7;
}

int main()
{
    foo();
    return 0;
}

运行会报错如下:

segmentation fault (core dumped)

因为之前设置了大小为500,所以当前目录下回出现文件core

下面对core文件进行分析,一般是使用gdb进行分析(Linux下C++的调试工具基本只有gdb)

-> # gdb ./test core    
GNU gdb (Debian 7.12-6) 7.12.0.20161007-git
Reading symbols from ./test...done.
[New LWP 5794]
Core was generated by `./test'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000400492 in foo () at test.cpp:5
5       *ptr = 7;
warning: File "/usr/local/lib64/libstdc++.so.6.0.26-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
    add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.26-gdb.py
line to your configuration file "/root/.gdbinit".
To completely disable this security protection add
    set auto-load safe-path /
line to your configuration file "/root/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
    info "(gdb)Auto-loading safe path"

这里说明了第5行有问题。
我们可以使用backtrace列出程序崩溃时产生的调用堆栈:

(gdb) backtrace 
#0  0x0000000000400492 in foo () at test.cpp:5
#1  0x00000000004004a4 in main () at test.cpp:10

在调用堆栈中上下移动:

(gdb) up
#1  0x00000000004004a4 in main () at test.cpp:10
10      foo();
(gdb) down
#0  0x0000000000400492 in foo () at test.cpp:5
5       *ptr = 7;

参考:C++ Tutorial - Debugging: Core/Memory Dump - 2018

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页