Linux的段错误调试方法

 

linux段错误的调试方法

 

相关博文:

http://blog.csdn.net/htianlong/article/details/7439030

http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html

http://www.cnblogs.com/panfeng412/archive/2011/10/24/2222413.html

http://hi.baidu.com/zmohadnaadbfntr/item/5732180d816c1d813c42e211

 

 如下面的的程序就会产生段错误

cs.c

  1. #include <stdio.h>  
  2.   
  3. int main(void)  
  4. {  
  5.     int *p = NULL;  
  6.   
  7.     *p = 10;  
  8.   
  9.     return 0;  
  10. }  


Makefile

  1. CFLAGS=-Wall  
  2. CC=gcc  
  3.   
  4. cs:cs.o  
  5.     $(CC) cs.o -o cs   
  6. cs.o:cs.c  
  7.     $(CC) -c -o cs.o cs.c  
  8. clean:  
  9.     $(RM) *.o  
  10.   
  11. .PHONY:clean  


执行命令

make

./cs

输出结果

 

调试方法:

  1. 切换到root用户     su root
  2. 执行 ulimit -a
  3. 执行 ulimit -c unlimited
  4. 更改Makefile文件   在-Wall后增加 -g
  5. 执行 make
  6. 执行 ./cs   目的 生成core文件
  7. 执行 gdb ./cs core
  8. 执行quit 退出gdb
  9. ctrl d 切换回普通用户

 

posted @ 2013-08-06 20:22  摩斯电码  阅读(292)  评论(0编辑  收藏  举报