1. 程式人生 > >gdb中core檔案除錯

gdb中core檔案除錯

程式執行時,如果有錯誤,則會產生core檔案。
core檔案如何檢視錯誤

在Linux系統下,應用程式崩潰,一般會產生core檔案,如何根據core檔案查詢問題的所在,並做相應的分析和除錯,是非常重要的,本文對此做簡單介紹。
例如,一個程式cmm_test_tool在執行的時候發生了錯誤,並生成了一個core檔案,如下:

-rw-r–r– 1 root cmm_test_tool.c
-rw-r–r– 1 root
cmm_test_tool.o
-rwxr-xr-x 1 root cmm_test_tool
-rw--- 1 root
core.19344
-rw--- 1 root core.19351
-rw-r–r– 1 root
cmm_test_tool.cfg
-rw-r–r– 1 root cmm_test_tool.res
-rw-r–r– 1 root
cmm_test_tool.log
[
[email protected]
_SIM2 mam2cm]#

就可以利用命令gdb進行查詢,引數一是應用程式的名稱,引數二是core檔案,執行

gdb  cmm_test_tool core.19351

結果如下:

[[email protected]_SIM2 mam2cm]# gdb cmm_test_tool core.19351
GNU gdb Red Hat
Linux (5.2.1-4)
Copyright 2002 Free Software Foundation, Inc.
GDB is free
software, covered by the GNU General Public License, and you are
welcome to
change it and/or distribute copies of it under certain conditions.
Type “show
copying” to see the conditions.
There is absolutely no warranty for GDB. Type
“show warranty” for details.
This GDB was configured as
“i386-redhat-linux”…
Core was generated by `./cmm_test_tool’.
Program
terminated with signal 11, Segmentation fault.
Reading symbols from
/lib/i686/libpthread.so.0…done.
Loaded symbols for
/lib/i686/libpthread.so.0
Reading symbols from
/lib/i686/libm.so.6…done.
Loaded symbols for /lib/i686/libm.so.6
Reading
symbols from /usr/lib/libz.so.1…done.
Loaded symbols for
/usr/lib/libz.so.1
Reading symbols from
/usr/lib/libstdc++.so.5…done.
Loaded symbols for
/usr/lib/libstdc++.so.5
Reading symbols from
/lib/i686/libc.so.6…done.
Loaded symbols for /lib/i686/libc.so.6
Reading
symbols from /lib/libgcc_s.so.1…done.
Loaded symbols for
/lib/libgcc_s.so.1
Reading symbols from /lib/ld-linux.so.2…done.
Loaded
symbols for /lib/ld-linux.so.2
Reading symbols from
/lib/libnss_files.so.2…done.
Loaded symbols for /lib/libnss_files.so.2
#0
0×4202cec1 in __strtoul_internal () from
/lib/i686/libc.so.6
(gdb)

進入gdb提示符,輸入where,找到錯誤發生的位置和堆疊,如下:

(gdb) where
#0 0×4202cec1 in __strtoul_internal () from
/lib/i686/libc.so.6
#1 0×4202d4e7 in strtoul () from
/lib/i686/libc.so.6
#2 0×0804b4da in GetMaxIDFromDB (get_type=2,
max_id=0×806fd20) at cmm_test_tool.c:788
#3 0×0804b9d7 in ConstrctVODProgram
(vod_program=0×40345bdc) at cmm_test_tool.c:946
#4 0×0804a2f4 in
TVRequestThread (arg=0×0) at cmm_test_tool.c:372
#5 0×40021941 in
pthread_start_thread () from /lib/i686/libpthread.so.0
(gdb)

至此,可以看出檔案出錯的位置是函式 GetMaxIDFromDB
,兩個引數分別是2和0×806fd20,這個函式位於原始碼的788行,基於此,我們就可以有針對性的找到問題的根源,並加以解決。