標籤:blog http 使用 os strong 檔案 io art
有的程式可以通過編譯, 但在運行時會出現Segment fault(段錯誤). 這通常都是指標錯誤引起的.但這不像編譯錯誤一樣會提示到檔案->行, 而是沒有任何資訊, 使得我們的調試變得困難起來.
1.在一般Linux系統中,預設是不會產生core dump檔案的。通過ulimit -c來查看core dump檔案的大小,一般開始是0。
可以設定core檔案大小,ulimit -c 1024(kbytes單位)或者ulimit -c unlimited。(注: 使用-c unlimited不限制core檔案大小)
2.產生core檔案: $ sudo sh -c "ulimit -c unlimited; ./main_arm" # main_arm 是可執行檔名。
3.記得在編譯時間加上 -g 方便調試,注意不要加上最佳化選項 -Os(或者-O0 -O1 ....)
4.用gdb查看core檔案:
gdb [exec file] [core file]
如:gdb main_arm core
在進入gdb後, 用 bt 或 where 命令查看backtrace以檢查發生程式運行到哪裡, 來定位core dump的檔案行.
5.舉例:
[html] view plaincopy
- 一個程式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.19344結果如下:
-
- [[email protected]_SIM2 mam2cm]# gdb cmm_test_tool core.19344
-
- 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行,基於此,我們就可以有針對性的找到問題的根源,並加以解決