linux下用core和gdb查询出现"段错误"的地方

579 篇文章 5 订阅
21 篇文章 0 订阅
有些时候我们在一段C代码的时候,由于对一个非法内存进行了操作,在程序运行的过程中,出现了"段错误"。

呵呵,这种问题我想很多人会经常遇到。遇到这种问题是非常无语的,只是提示了"段错误",接着什么都没 有,如果我们一味的去看代码找太疼苦了,因为我们都相信自己写的代码没问题,现实就是现实。下面介绍一种方法,可以有效的定位出现"段错误的地方"。

当我们的程序崩溃时,内核有可能把该程序当前内存映射到core文件里,方便程序员找到程序出现问题的地方。

什么是core dump?
core的意思是内存,dump的意思是扔出来,堆出来。

为什么没有core文件生成呢?

有时候程序down了,但是core文件却没有生成.core文件的生成跟你当前系统的环境设置有关系,可以用下面的语句设置一下便生成core文件了

ulimit  -c  unlimited

core 文件生成的位置一般于运行程序的路径相同,在ubuntu下文件名一般 为core.

什么是core文件
当一个程序奔溃时,在进程当前工作目录的core文件中复制了该进程的存储图像。core文件仅仅是一个内存映像(同时加上调试信息),主要用来调试的。



问题程序

segment.c

  1. #include <stdio.h>  
  2. #include <signal.h>  
  3. #include <unistd.h>  
  4. #include <stdlib.h>  
  5.   
  6. void func()  
  7. {  
  8.   char *p = NULL;  
  9.   
  10.   *p = 3;  
  11. }  
  12.   
  13. main()  
  14. {  
  15.   func();  
  16.   
  17.   return;  
  18. }  

编译:
[plain] view plain copy
  1. gcc -g -o segment segment.c  

Core文件:

    1. 查看系统是否允许生成core文件  

[plain] view plain copy
  1. #ulimit -a  
  2. core file size          (blocks, -c) 0  

core文件大小限制为0,不能生成core文件。

    2. 使用如下命令取消限制,使系统能生成core文件

[plain] view plain copy
  1. ulimit -c unlimited  
或者指定core文件大小,如1K
[plain] view plain copy
  1. ulimit -c 1024  

执行程序:        

[plain] view plain copy
  1. # ./segment  
  2. egmentation fault (core dumped)  
在程序当前目录下生成了core文件


GDB调试:

[plain] view plain copy
  1. # gdb ./segment core  
  2. GNU gdb (GDB) 7.1-ubuntu  
  3. Copyright (C) 2010 Free Software Foundation, Inc.  
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>  
  5. This is free software: you are free to change and redistribute it.  
  6. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"  
  7. and "show warranty" for details.  
  8. This GDB was configured as "i486-linux-gnu".  
  9. For bug reporting instructions, please see:  
  10. <http://www.gnu.org/software/gdb/bugs/>...  
  11. Reading symbols from /home/duanbb/test/segment/segment...done.  
  12. [New Thread 3655]  
  13.   
  14. warning: Can't read pathname for load map: Input/output error.  
  15. Reading symbols from /lib/tls/i686/cmov/libc.so.6...Reading symbols from /usr/lib/debug/lib/tls/i686/cmov/libc-2.11.1.so...done.  
  16. done.  
  17. Loaded symbols for /lib/tls/i686/cmov/libc.so.6  
  18. Reading symbols from /lib/ld-linux.so.2...Reading symbols from /usr/lib/debug/lib/ld-2.11.1.so...done.  
  19. done.  
  20. Loaded symbols for /lib/ld-linux.so.2  
  21. Core was generated by `./segment'.  
  22. Program terminated with signal 11, Segmentation fault.  
  23. #0  0x080483c4 in func () at segment.c:10  
  24. 10    *p = 3;  

可以清楚地看到,程序在第10行代码,func()函数内,执行"*p = 3"时发生了segment错误
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------      

Core文件的命名和路径

默认情况下core文件名即为"core",文件路径:运行可执行文件时,环境所在的目录


两个控制文件

(1)    /proc/sys/kernel/core_uses_pid

             0: 默认值,生成的文件名不会带上PID后缀

             1: 文件名带上PID后缀,如"core.2976"

         修改方法: "echo 1 > /proc/sys/kernel/core_uses_pid"

(2)  /proc/sys/kernel/core_pattern

            用来控制文件名格式

            默认值: core   

            假设将其值改变为:"/home/duanbei/corefile/core-%e-%p-%t", 那么所有的core文件将保存在"/home/duanbei/corefile"目录下,文件名格式为“core-程序名-pid-时间戳”

            格式参数列表

[plain] view plain copy
  1. %p - insert pid into filename  
  2. %u - insert current uid into filename  
  3. %g - insert current gid into filename  
  4. %s - insert signal that caused the coredump into the filename  
  5. %t - insert UNIX time that the coredump occurred into filename  
  6. %h - insert hostname where the coredump happened into filename  
  7. %e - insert coredumping executable name into filename  

注:如果设置了“%p”,而“/proc/sys/kernel/core_uses_pid”值又为1,则不会添加PID后缀,因文件名中已含该信息


PS:

      要生成core文件,就不要在程序中用signal()捕获‘SIGSEGV’信号了,不然生成不了core文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值