段錯誤bug的調試

咱們在用C/C++語言寫程序的時侯,內存管理的絕大部分工做都是須要咱們來作的。實際上,內存管理是一個比較繁瑣的工做,不管你多高明,經驗多豐富,難 免會在此處犯些小錯誤,而一般這些錯誤又是那麼的淺顯而易於消除。可是手工「除蟲」(debug),每每是效率低下且讓人厭煩的,本文將就"段錯誤"這個 內存訪問越界的錯誤談談如何快速定位這些"段錯誤"的語句。
下面將就如下的一個存在段錯誤的程序介紹幾種調試方法:linux

dummy_function (void)
       {
               unsigned char *ptr = 0x00;
               *ptr = 0x00;
       }
     
      int main (void)
      {
              dummy_function ();
    
              return 0;
      }

做爲一個熟練的C/C++程序員,以上代碼的bug應該是很清楚的,由於它嘗試操做地址爲0的內存區域,而這個內存區域一般是不可訪問的禁區,固然就會出錯了。咱們嘗試編譯運行它:程序員

xiaosuo@gentux test $ ./a.out
段錯誤

果真不出所料,它出錯並退出了。
1.利用gdb逐步查找段錯誤:
這種方法也是被大衆所熟知並普遍採用的方法,首先咱們須要一個帶有調試信息的可執行程序,因此咱們加上「-g -rdynamic"的參數進行編譯,而後用gdb調試運行這個新編譯的程序,具體步驟以下:debug

xiaosuo@gentux test $ gcc -g -rdynamic d.c
xiaosuo@gentux test $ gdb ./a.out
GNU gdb 6.5
Copyright (C) 2006 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 "i686-pc-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

(gdb) r
Starting program: /home/xiaosuo/test/a.out

Program received signal SIGSEGV, Segmentation fault.
0x08048524 in dummy_function () at d.c:4
4               *ptr = 0x00;
(gdb)

哦?!好像不用一步步調試咱們就找到了出錯位置d.c文件的第4行,其實就是如此的簡單。
從這裏咱們還發現進程是因爲收到了SIGSEGV信號而結束的。經過進一步的查閱文檔(man 7 signal),咱們知道SIGSEGV默認handler的動做是打印」段錯誤"的出錯信息,併產生Core文件,由此咱們又產生了方法二。
2.分析Core文件:
Core文件是什麼呢?調試

The  default action of certain signals is to cause a process to terminate and produce a core dump file, a disk file containing an image of the process's memory  at the time of termination.  A list of the signals which cause a process to dump core can be found in signal(7).

以 上資料摘自man page(man 5 core)。不過奇怪了,個人系統上並無找到core文件。後來,憶起爲了漸少系統上的拉圾文件的數量(本人有些潔癖,這也是我喜歡Gentoo的緣由 之一),禁止了core文件的生成,查看了如下果然如此,將系統的core文件的大小限制在512K大小,再試:code

xiaosuo@gentux test $ ulimit -c
0
xiaosuo@gentux test $ ulimit -c 1000
xiaosuo@gentux test $ ulimit -c
1000
xiaosuo@gentux test $ ./a.out
段錯誤 (core dumped)
xiaosuo@gentux test $ ls
a.out  core  d.c  f.c  g.c  pango.c  test_iconv.c  test_regex.c

core文件終於產生了,用gdb調試一下看看吧:進程

xiaosuo@gentux test $ gdb ./a.out core
GNU gdb 6.5
Copyright (C) 2006 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 "i686-pc-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".


warning: Can't read pathname for load map: 輸入/輸出錯誤.
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x08048524 in dummy_function () at d.c:4
4               *ptr = 0x00;
相關文章
相關標籤/搜索