开启coredump

#如果该命令的返回值是0,则表示不开启coredump
ulimit -c

# 开启coredump
ulimit -c unlimited

准备c文件

#include<stdio.h>


void crash() {
    char * p = NULL;
    *p = 0;
}

int main(){
    printf("hello world 1");
    int phone [4];
    phone[232] = 12;
    crash();
    return 0;
}

编译执行

gcc -g  hello.c -o hello
./hello

之后程序崩溃,产生core文件。

gdb分析

gdb 启动的二进制文件 core文件

gdb ./hello ./core

之后输入: bt full 可以查看到更详细的信息

➜  c-sandbox gdb ./hello ./core
GNU gdb (Raspbian 7.12-6) 7.12.0.20161007-git
Copyright (C) 2016 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 "arm-linux-gnueabihf".
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 ./hello...done.
[New LWP 25571]
Core was generated by `./hello'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0001045c in crash () at hello.c:6
6           *p = 0;
(gdb) bt full
#0  0x0001045c in crash () at hello.c:6
        p = 0x0
#1  0x00010490 in main () at hello.c:13
        phone = {66328, 0, 0, 0}