Segmentation fault(core dumped) 调试

ReadingList:

  • https://mytechrants.wordpress.com/2009/05/22/debugging-a-segmentation-fault-using-gdb/
  • https://wiki.archlinux.org/index.php/Step-by-step_debugging_guide
  • http://www.cprogramming.com/debugging/segfaults.html

简述:

  1. 首先在编译生成程序的时候要使用“-g”选项使得编译文件带有调试信息,若果没有调试信息,gdb无法工作
    gcc -g -o hello hello.c
  2. 使得Linux可以在段错误时生成的core dump文件大小无限
    ulimit -c unlimited
  3. 运行可能出错的程序,直到出现“Segmentation fault(core dumped)”信息,这是在程序工作目录下会生成一个core文件
  4. 用gdb分析core文件
    gdb {executable} {dump file}
    
    eg. gdb hello core.1324
  5. 如果gdb调试符号正常加载,查看堆栈,找到出错位置(哪个函数,哪个文件,哪一行出错)
    (gdb) bt   //查看出错时的堆栈
    (bt = backtrace .. prints stack strace)
  6. 还可以分析每个栈帧frame中的变量情况
    (gdb) frame {num}
    eg. (gdb) frame 2

    and use:
    (gdb) info locals //查看局部变量
    (gdb) info args //查看参数
  7. 通过以上步骤,可以定位到出错位置,找出出错原因。
原文地址:https://www.cnblogs.com/bohaoist/p/5116685.html