See the above result is not the kind of feeling old acquaintance of Kazakhstan, then try to use the core to solve the above problem
core files
After running the program in question, create the file with the stack, and debug information generated when "Segmentation fault".
You need to add the -g option to generate debugging information when compiling the program: gcc -g core_test.c -o core_test
Configured to generate core files
使用 ulimit -c 查看core开关,如果为0表示关闭,不会生成core文件;
使用 ulimit -c [filesize] 设置core文件大小,当最小设置为4之后才会生成core文件;
使用 ulimit -c unlimited 设置core文件大小为不限制,这是常用的做法;
core file name and save path
①core文件有默认的名称和路径,但为了方便,我们通常会自己命名和指定保存路径;
②可以通过 /proc/sys/kernel/core_pattern 设置 core 文件名和保存路径,方法如下:
echo “/corefile/core-%e-%p-%t” > /proc/sys/kernel/core_pattern
Named parameter list:
%p - insert pid into filename 添加pid
%u - insert current uid into filename 添加当前uid
%g - insert current gid into filename 添加当前gid
%s - insert signal that caused the coredump into the filename 添加导致产生core的信号
%t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间
%h - insert hostname where the coredump happened into filename 添加主机名
%e - insert coredumping executable name into filename 添加命令名。
Debugging core files
gdb [exec file] [core file]
Basics above finished, the next will look real
//test.cpp
#include <iostream>
using namespace std;
int main(){
int*a = NULL;
for(int i =0;i < 11;i++)
a[i] = i;
return 0;
}
First check whether the core documents on state, then the above files are compiled, executed
after the execution, the current folder found one more document, yes, that core file
at this time. We started gdb debugger
Can be known by the content of the second frame, due to the execution of a [i] = i generated by the error, which is to view the values of a and i found a 0 address, the address is not assigned directly used, re-edit source file, allocate memory for a re-compile and run. Run ok