GDB Core

来源:互联网 发布:网络禁书 编辑:程序博客网 时间:2024/05/11 23:59

原文: http://blog.csdn.net/duanbeibei/article/details/6923716


程序运行发生异常退出,比如segment错误,此时可以利用系统生成的core文件,配合GDB来定位问题。


问题程序

segment.c

[cpp] view plaincopy
  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 plaincopy
  1. gcc -g -o segment segment.c  

Core文件:

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

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

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

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

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

执行程序:        

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


GDB调试:

[plain] view plaincopy
  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错误

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------       

Release版 Core的调试

众所周知,发布程序时必须是release版的,杜绝debug版。

下面就来看看如何调试release 版程序生成的core文件

还是如上程序,编译release版:

[plain] view plaincopy
  1. # gcc -o segment segment.c  

运行程序:
[plain] view plaincopy
  1. ./segment  
  2. Segmentation fault (core dumped)  

调试程序:
[plain] view plaincopy
  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...(no debugging symbols found)...done.  
  12. [New Thread 4711]  
  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. <span style="color:#ff0000;">#0  0x080483c4 in func ()  
  24. </span>(gdb)   
只能显示出出错时的函数,而看不到出错的具体位置,怎么办?


方法: 用发布时的原代码,在原有的编译选项上(不要改变任何编译参数,即使有"-O2"参数,也不要动),只加上"-g"选项,编译出对应的debug程序

本例:

[plain] view plaincopy
  1. # gcc -g -o segment_d segment.c  
调试:
[plain] view plaincopy
  1. # gdb ./segment_d 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_d...done.  
  12.   
  13. warning: core file may not match specified executable file.  
  14. [New Thread 4711]  
  15.   
  16. warning: Can't read pathname for load map: Input/output error.  
  17. 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.  
  18. done.  
  19. Loaded symbols for /lib/tls/i686/cmov/libc.so.6  
  20. Reading symbols from /lib/ld-linux.so.2...Reading symbols from /usr/lib/debug/lib/ld-2.11.1.so...done.  
  21. done.  
  22. Loaded symbols for /lib/ld-linux.so.2  
  23. Core was generated by `./segment'.  
  24. Program terminated with signal 11, Segmentation fault.  
  25. #0  0x080483c4 in func () at segment.c:10  
  26. 10    *p = 3;  

即可定位出错误位置


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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 plaincopy
  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
原创粉丝点击