使用gdb和core dump迅速定位段錯誤(完)

使用gdb和core dump迅速定位段錯誤
關鍵字:gdb、段錯誤、core dump
 
1、什麼是core dump
    core:內存、核心的意思;
    dump:拋出,扔出;
    core dump:前提:當某程序崩潰的一瞬間,內核會拋出當時該程序進程的內存詳細狀況,存儲在一個名叫core.xxx(xxx爲一個數字,好比core.699)的文件中。
2、更改生成的core文件的大小限制
    可見,core文件是內核生成的,那某一個進程由於段錯誤而崩潰的時候的內存映像很大,那必然會生成一個很大的core文件,因此咱們能夠經過ulimit命令來設置生成core文件的大小,例如$ulimit -c unlimited,這裏就是設置生成的core文件無大小限制。
3、生成core文件
    當第二步完成了,就運行一次那個有問題的程序,而後天然就會由於段錯誤而崩潰,在當前目錄下就生成了core.xxx文件。
4、分析core文件
    使用命令$gdb 程序名 core.xxx,而後再輸入where就能夠看到產生段錯誤的地方。
5、實例分析
1.test.c文件的源代碼
  1 void do_it();
  2 int main()
  3 {
  4         do_it();
  5         return 0;
  6 }
  7 void do_it()
  8 {
  9         char* p = 1; //定義一個字符指針變量a,指向地址1,這個地址確定不是本身能夠訪問的,可是這行不會產生段錯誤
 10         *p = 'a'; //真正產生段錯誤的在這裏,試圖更改地址1的值,此時內核會終止該進程,而且把core文件dump出來
 11 }
2.編譯該源代碼,請注意,加-g標籤,能夠在where命令後看到更加詳細的信息。
    運行編譯命令:$gcc -g ./test.c,看到以下打印
[michael @localhost core_dump]$ gcc -g ./test.c 
./test.c: In function ‘do_it’:
./test.c:9:19: warning: initialization makes pointer from integer without a cast
[michael @localhost core_dump]$ 
    從而獲得帶調試信息的(由於加了-g 編譯標籤)a.out二進制文件。3.設置core文件大小限制爲無限大。    運行命令:$ulimit -c unlimited,便可。4.生成core文件。    運行命令:$./a.out,便可,可看到以下打印:
[michael @localhost core_dump]$ ./a.out 
Segmentation fault (core dumped)
[michael @localhost core_dump]$ 
    運行命令:$ll,可看到以下打印:
[michael @localhost core_dump]$ ll
total 80
-rwxrwxr-x. 1 michael michael   5612 May  2 15:54 a.out
-rw-------. 1 michael michael 204800 May  2 15:58 core.7369
-rw-rw-r--. 1 michael michael    383 May  2 15:53 test.c
[michael @localhost core_dump]$ 
5.使用gdb調試core文件。    運行命令:$gdb ./a.out ./core.7369,可看到以下打印:
[michael @localhost core_dump]$ gdb ./a.out ./core.7369 
GNU gdb (GDB) Fedora (7.2-52.fc14)
Copyright (C) 2010 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 "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/michael/core_dump/a.out...done.
[New Thread 7369]
Missing separate debuginfo for 
Try: yum --disablerepo='*' --enablerepo='*-debuginfo' install /usr/lib/debug/.build-id/c4/1c574f31a203492b9389c783adad6ff1989915
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x080483b8 in do_it () at ./test.c:10
10        *p = 'a'; //真正產生段錯誤的在這裏,試圖更改地址1的值,此時內核會終止該進程,而且把core文件dump出來
Missing separate debuginfos, use: debuginfo-install glibc-2.13-2.i686
(gdb) 
    運行命令:where,便可看到出現段錯誤的行數了,以下打印:
(gdb) where
#0  0x080483b8 in do_it () at ./test.c:10
#1  0x0804839f in main () at ./test.c:4
(gdb) 
    在第10行,很容易吧。

2012年5月2日15:44:54
相關文章
相關標籤/搜索