C語言 --段錯誤(核心已轉儲)

文章原地址:https://blog.csdn.net/qq_29350001/article/details/53780697

參看:Linux下的段錯誤產生的原因及調試方法

參看:Linux環境下段錯誤的產生原因及調試方法小結

參看:維基百科--Segmentation fault

參看:LINUX內核段錯誤調試詳細指南精品培訓PPT講義

一、什麼是段錯誤?

一旦一個程序發生了越界訪問,cpu 就會產生相應的保護,於是 segmentation fault 就出現了,通過上面的解釋,段錯誤應該就是訪問了不可訪問的內存,這個內存區要麼是不存在的,要麼是受到系統保護的,還有可能是缺少文件或者文件損壞。

二、段錯誤產生的原因

下面是一些典型的段錯誤的原因:
非關聯化空指針——這是特殊情況由內存管理硬件
試圖訪問一個不存在的內存地址(在進程的地址空間)
試圖訪問內存的程序沒有權利(如內核結構流程上下文)
試圖寫入只讀存儲器(如代碼段)

1、訪問不存在的內存地址

在C代碼,分割錯誤通常發生由於指針的錯誤使用,特別是在C動態內存分配。非關聯化一個空指針總是導致段錯誤,但野指針和懸空指針指向的內存,可能會或可能不會存在,而且可能或不可能是可讀的還是可寫的,因此會導致瞬態錯誤。

  1. #include <stdio.h>  
  2.   
  3. int main (void)  
  4. {  
  5.     int *ptr = NULL;  
  6.     *ptr = 0;  
  7.     return 0;  
  8. }  
  9. 輸出結果:  
  10. 段錯誤(核心已轉儲)  

現在,非關聯化這些變量可能導致段錯誤:非關聯化空指針通常會導致段錯誤,閱讀時從野指針可能導致隨機數據但沒有段錯誤,和閱讀從懸空指針可能導致有效數據,然後隨機數據覆蓋。

2、訪問系統保護的內存地址 

  1. #include <stdio.h>  
  2.   
  3. int main (void)  
  4. {  
  5.     int *ptr = (int *)0;  
  6.     *ptr = 100;  
  7.     return 0;  
  8. }  
  9. 輸出結果:  
  10. 段錯誤(核心已轉儲)  

3、訪問只讀的內存地址

寫入只讀存儲器提出了一個 segmentation fault,這個發生在程序寫入自己的一部分代碼段或者是隻讀的數據段,這些都是由操作系統加載到只讀存儲器。

  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int main (void)  
  5. {  
  6.     char *ptr = "test";  
  7.     strcpy (ptr, "TEST");  
  8.     return 0;  
  9. }  
  10. 輸出結果:  
  11. 段錯誤(核心已轉儲)  
  1. #include <stdio.h>  
  2.   
  3. int main (void)  
  4. {  
  5.     char *ptr = "hello";  
  6.     *ptr = 'H';  
  7.     return 0;  
  8. }  
  9. 輸出結果:  
  10. 段錯誤(核心已轉儲)  
上述例子ANSI C代碼通常會導致段錯誤和內存保護平臺。它試圖修改一個字符串文字,這是根據ANSI C標準未定義的行爲。大多數編譯器在編譯時不會抓,而是編譯這個可執行代碼,將崩潰。

包含這個代碼被編譯程序時,字符串“hello”位於rodata部分程序的可執行文件的只讀部分數據段。當加載時,操作系統與其他字符串和地方常數只讀段的內存中的數據。當執行時,一個變量 ptr 設置爲指向字符串的位置,並試圖編寫一個H字符通過變量進入內存,導致段錯誤。編譯程序的編譯器不檢查作業的只讀的位置在編譯時,和運行類unix操作系統產生以下運行時發生 segmentation fault。

可以糾正這個代碼使用一個數組而不是一個字符指針,這個棧上分配內存並初始化字符串的值:

  1. #include <stdio.h>  
  2.   
  3. int main (void)  
  4. {  
  5.     char ptr[] = "hello";  
  6.     ptr[0] = 'H';  
  7.     return 0;  
  8. }  

即使不能修改字符串(相反,這在C標準未定義行爲),在C char *類型,所以沒有隱式轉換原始代碼,在c++的 const char *類型,因此有一個隱式轉換,所以編譯器通常會抓住這個特定的錯誤。

4、空指針廢棄

因爲是一個很常見的程序錯誤空指針廢棄(讀或寫在一個空指針,用於C的意思是“沒有對象指針”作爲一個錯誤指示器),大多數操作系統內存訪問空指針的地址,這樣它會導致段錯誤。

  1. #include <stdio.h>  
  2.   
  3. int main (void)  
  4. {  
  5.     int *ptr = NULL;  
  6.     printf ("%d\n", *ptr);  
  7.     return 0;  
  8. }  
  9. 輸出結果:  
  10. 段錯誤(核心已轉儲)  
這個示例代碼創建了一個空指針,然後試圖訪問它的值(讀值)。在運行時在許多操作系統中,這樣做會導致段錯誤。

非關聯化一個空指針,然後分配(寫一個值到一個不存在的目標)也通常會導致段錯誤。

  1. #include <stdio.h>  
  2.   
  3. int main (void)  
  4. {  
  5.     int *ptr = NULL;  
  6.     *ptr = 1;  
  7.     return 0;  
  8. }  
  9. 輸出結果:  
  10. 段錯誤(核心已轉儲)  
下面的代碼包含一個空指針,但當編譯通常不會導致段錯誤,值是未使用的。因此,廢棄通常會被優化掉,死代碼消除。

  1. #include <stdio.h>  
  2.   
  3. int main (void)  
  4. {  
  5.     int *ptr = NULL;  
  6.     *ptr;  
  7.     return 0;  
  8. }  

還有,比如malloc 動態分配內存,釋放、置空完成後,不可再使用該指針。

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. int main()  
  6. {  
  7.     char* str=(char* )malloc(100);  
  8.     if(*str)  
  9.     {  
  10.         return;   
  11.     }  
  12.     strcpy(str,"hello");  
  13.     printf("%s\n",str);  
  14.     free(str);  
  15.     str=NULL;  
  16.     strcpy(str,"abcdef");  
  17.     return 0;  
  18. }  
  19. 輸出結果:  
  20. hello  
  21. 段錯誤 (核心已轉儲)  

5、堆棧溢出

  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int main (void)  
  5. {  
  6.     main ();  
  7.     return 0;  
  8. }  
  9. 輸出結果:  
  10. 段錯誤(核心已轉儲)  

上述例子的無限遞歸,導致的堆棧溢出會導致段錯誤,但無線遞歸未必導致堆棧溢出,優化執行的編譯器和代碼的確切結構。在這種情況下,遙不可及的代碼(返回語句)行爲是未定義的。因此,編譯器可以消除它,使用尾部調用優化,可能導致沒有堆棧使用。其他優化可能包括將遞歸轉換成迭代,給出例子的結構功能永遠會導致程序運行,雖然可能不是其他堆棧溢出。

6、內存越界(數組越界,變量類型不一致等)

  1. #include <stdio.h>  
  2.   
  3. int main (void)  
  4. {  
  5.     char test[10];  
  6.     printf ("%c\n", test[100000]);  
  7.     return 0;  
  8. }  
  9. 輸出結果:  
  10. 段錯誤(核心已轉儲)  

三、段錯誤信息的獲取

程序發生段錯誤時,提示信息很少,下面有幾種查看段錯誤的發生信息的途徑。

1、dmesg

dmesg 可以在應用程序崩潰時,顯示內存中保存的相關信息。如下所示,通過 dmesg 命令可以查看發生段錯誤的程序名稱、引起段錯誤發生的內存地址、指令指針地址、堆棧指針地址、錯誤代碼、錯誤原因等。

  1. root@#dmesg  
  2. [ 6357.422282] a.out[3044]: segfault at 806851c ip b75cd668 sp bf8b2100 error 4 in libc-2.15.so[b7559000+19f000]  

2、-g

使用gcc編譯程序的源碼時,加上 -g 參數,這樣可以使得生成的二進制文件中加入可以用於 gdb 調試的有用信息。

參看:C語言再學習 -- GCC編譯過程

可產生供gdb調試用的可執行文件,大小明顯比只用-o選項編譯彙編連接後的文件大。

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. root@ubuntu:/home/tarena/project/c_test# gcc hello.c   
  2. root@ubuntu:/home/tarena/project/c_test# ls -la a.out   
  3. -rwxr-xr-x 1 root root 7159 Nov 26 23:32 a.out  
  4. root@ubuntu:/home/tarena/project/c_test# gcc -g hello.c   
  5. root@ubuntu:/home/tarena/project/c_test# ls -la a.out   
  6. -rwxr-xr-x 1 root root 8051 Nov 26 23:32 a.out  

gdb的簡單使用:

(gdb)l  列表(list)
(gdb)r  執行(run)
(gdb)n  下一個(next)
(gdb)q  退出(quit)
(gdb)p  輸出(print)
(gdb)c  繼續(continue)
(gdb)b 4 設置斷點(break)
(gdb)d   刪除斷點(delete)

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. root@ubuntu:/home/tarena/project/c_test# gdb a.out  
  2. GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02  
  3. Copyright (C) 2012 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 "i686-linux-gnu".  
  9. For bug reporting instructions, please see:  
  10. <http://bugs.launchpad.net/gdb-linaro/>...  
  11. Reading symbols from /home/tarena/project/c_test/a.out...done.  
  12. (gdb) l  
  13. 1   #include <stdio.h>  
  14. 2   int main (void)  
  15. 3   {  
  16. 4       printf ("hello world!\n");  
  17. 5       return 0;  
  18. 6   }  
  19. (gdb) r  
  20. Starting program: /home/tarena/project/c_test/a.out   
  21. hello world!  
  22. [Inferior 1 (process 6906) exited normally]  
  23. (gdb) q  
  24. root@ubuntu:/home/tarena/project/c_test#   

3、nm

使用 nm 命令列出二進制文件中符號表,包括符號地址、符號類型、符號名等。這樣可以幫助定位在哪裏發生了段錯誤。

  1. root@# nm a.out   
  2. 08049f28 d _DYNAMIC  
  3. 08049ff4 d _GLOBAL_OFFSET_TABLE_  
  4. 080484ac R _IO_stdin_used  
  5.          w _Jv_RegisterClasses  
  6. 08049f18 d __CTOR_END__  
  7. 08049f14 d __CTOR_LIST__  
  8. 08049f20 D __DTOR_END__  
  9. 08049f1c d __DTOR_LIST__  
  10. 080485a4 r __FRAME_END__  
  11. 08049f24 d __JCR_END__  
  12. 08049f24 d __JCR_LIST__  
  13. 0804a010 A __bss_start  
  14. 0804a008 D __data_start  
  15. 08048460 t __do_global_ctors_aux  
  16. 08048330 t __do_global_dtors_aux  
  17. 0804a00c D __dso_handle  
  18.          w __gmon_start__  
  19. 08048452 T __i686.get_pc_thunk.bx  
  20. 08049f14 d __init_array_end  
  21. 08049f14 d __init_array_start  
  22. 08048450 T __libc_csu_fini  
  23. 080483e0 T __libc_csu_init  
  24.          U __libc_start_main@@GLIBC_2.0  
  25. 0804a010 A _edata  
  26. 0804a018 A _end  
  27. 0804848c T _fini  
  28. 080484a8 R _fp_hw  
  29. 08048294 T _init  
  30. 08048300 T _start  
  31. 0804a010 b completed.6159  
  32. 0804a008 W data_start  
  33. 0804a014 b dtor_idx.6161  
  34. 08048390 t frame_dummy  
  35. 080483b4 T main  

4、ldd

使用 ldd 命令查看二進制程序的共享鏈接庫依賴,包括庫的名稱、起始地址,這樣可以確定段錯誤到底是發生在了自己的程序中還是依賴的共享庫中。

  1. root@t# ldd a.out   
  2.     linux-gate.so.1 =>  (0xb7765000)  
  3.     libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb75ac000)  
  4.     /lib/ld-linux.so.2 (0xb7766000)  

四、段錯誤的調試方法

接下來的講解是圍繞下面的代碼進行的:

  1. #include <stdio.h>  
  2.   
  3. int main (void)  
  4. {  
  5.     int *ptr = NULL;  
  6.     *ptr = 10;  
  7.     return 0;  
  8. }  
  9. 輸出結果:  
  10. 段錯誤(核心已轉儲)  

1、使用 printf 輸出信息

這個是看似最簡單,但往往很多情況下十分有效的調試方式,也許可以說是程序員用的最多的調試方式。簡單來說,就是在程序的重要代碼附近加上像 printf 這類輸出信息,這樣可以跟蹤並打印出段錯誤在代碼中可能出現的位置。

爲了方便使用這種方法,可以使用條件編譯指令 #define DEBUG 和 #endif 把 printf 函數包起來。這樣在程序編譯時,如果加上 -DDEBUG 參數就可以查看調試信息;否則不加上參數就不會顯示調試信息。

參看:C語言再學習 -- C 預處理器

2、使用 gcc 和 gdb

1)調試步驟

A、爲了能夠使用 gdb 調試程序,在編譯階段加上 -g 參數。

  1. root@# gcc -g test.c  
B、使用 gdb 命令調試程序
  1. root@# gdb a.out   
  2. GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02  
  3. Copyright (C) 2012 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 "i686-linux-gnu".  
  9. For bug reporting instructions, please see:  
  10. <http://bugs.launchpad.net/gdb-linaro/>...  
  11. Reading symbols from /home/tarena/project/c_test/a.out...done.  
  12. (gdb)   
C、進入 gdb 後,運行程序
  1. (gdb) r  
  2. Starting program: /home/tarena/project/c_test/a.out   
  3.   
  4. Program received signal SIGSEGV, Segmentation fault.  
  5. 0x080483c4 in main () at test.c:6  
  6. 6       *ptr = 10;  
  7. (gdb)   

從輸出看出,程序收到 SIGSEGV 信號,觸發段錯誤,並提示地址 0x080483c4、創建了一個空指針,然後試圖訪問它的值(讀值)。

可以通過man 7 signal查看SIGSEGV的信息

  1. Signal     Value     Action   Comment  
  2.       ──────────────────────────────────────────────────────────────────────  
  3.       SIGHUP        1       Term    Hangup detected on controlling terminal  
  4.                                     or death of controlling process  
  5.       SIGINT        2       Term    Interrupt from keyboard  
  6.       SIGQUIT       3       Core    Quit from keyboard  
  7.       SIGILL        4       Core    Illegal Instruction  
  8.       SIGABRT       6       Core    Abort signal from abort(3)  
  9.       SIGFPE        8       Core    Floating point exception  
  10.       SIGKILL       9       Term    Kill signal  
  11.       SIGSEGV      11       Core    Invalid memory reference  
  12.       SIGPIPE      13       Term    Broken pipe: write to pipe with no  
  13.                                     readers  
  14.       SIGALRM      14       Term    Timer signal from alarm(2)  
  15.       SIGTERM      15       Term    Termination signal  
  16.       SIGUSR1   30,10,16    Term    User-defined signal 1  
  17.       SIGUSR2   31,12,17    Term    User-defined signal 2  
  18.       SIGCHLD   20,17,18    Ign     Child stopped or terminated  
  19.       SIGCONT   19,18,25    Cont    Continue if stopped  
  20.       SIGSTOP   17,19,23    Stop    Stop process  
  21.       SIGTSTP   18,20,24    Stop    Stop typed at tty  
  22.       SIGTTIN   21,21,26    Stop    tty input for background process  
  23.       SIGTTOU   22,22,27    Stop    tty output for background process  
  24.   
  25.       The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.  
D、完成調試後,輸入 q 命令退出 gdb
  1. (gdb) q  
  2. A debugging session is active.  
  3.   
  4.     Inferior 1 [process 3483] will be killed.  
  5.   
  6. Quit anyway? (y or n) y  

2)適用場景

A、僅當能確定程序一定會發生段錯誤的情況下適用。

B、當程序的源碼可以獲得的情況下,使用 -g 參數編譯程序

C、一般用於測試階段,生產環境下 gdb 會有副作用:使程序運行減慢,運行不夠穩定,等等。

D、即使在測試階段,如果程序過於複雜,gdb 也不能處理。

3、使用 core 文件和 gdb

上面有提到段錯誤觸發SIGSEGV信號,通過man 7 signal,可以看到SIGSEGV默認的處理程序(handler)會打印段錯誤信息,併產生 core 文件,由此我們可以藉助於程序異常退出生成的 core 文件中的調試信息,使用 gdb 工具來調試程序中的段錯誤。

1)調試步驟

A、在一些Linux版本下,默認是不產生 core 文件的,首先可以查看一下系統 core 文件的大小限制:

  1. root@# ulimit -c  
  2. 0  
B、可以看到默認設置情況下,本機Linux環境下發生段錯誤不會自動生成 core 文件,下面設置下 core 文件的大小限制(單位爲KB)
  1. root@# ulimit -c 1024  
  2. root@# ulimit -c   
  3. 1024  
C、運行程序,發生段錯誤生成的 core 文件
  1. root@# ./a.out   
  2. 段錯誤 (核心已轉儲)  
D、加載 core 文件,使用 gdb 工具進行調試
  1. root@ubuntu:/home/tarena/project/c_test# gdb a.out core   
  2. GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02  
  3. Copyright (C) 2012 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 "i686-linux-gnu".  
  9. For bug reporting instructions, please see:  
  10. <http://bugs.launchpad.net/gdb-linaro/>...  
  11. Reading symbols from /home/tarena/project/c_test/a.out...done.  
  12. [New LWP 3491]  
  13.   
  14. warning: Can't read pathname for load map: 輸入/輸出錯誤.  
  15. Core was generated by `./a.out'.  
  16. Program terminated with signal 11, Segmentation fault.  
  17. #0  0x080483c4 in main () at test.c:6  
  18. 6       *ptr = 10;  
  19. (gdb)   
從輸出看出,可以顯示出異樣的段錯誤信息

E、完成調試後,輸入 q 命令退出 gdb

  1. (gdb) q  

2)適用場景

A、適合於在實際生成環境下調試程度的段錯誤(即在不用重新發生段錯誤的情況下重現段錯誤)

B、當程序很複雜,core 文件相當大時,該方法不可用

4、使用 objdump

1)調試步驟

A、使用 dmesg 命令,找到最近發生的段錯誤輸入信息

  1. root@# dmesg  
  2. [  372.350652] a.out[2712]: segfault at 0 ip 080483c4 sp bfd1f7b8 error 6 in a.out[8048000+1000]  

其中,對我們接下來的調試過程有用的是發生段錯誤的地址 0 和指令指針地址 080483c4。

有時候,“地址引起的錯”可以告訴你問題的根源。看到上面的例子,我們可以說,int *ptr = NULL; *ptr = 10;,創建了一個空指針,然後試圖訪問它的值(讀值)。

B、使用 objdump 生成二進制的相關信息,重定向到文件中

  1. root@# objdump -d a.out > a.outDump   
  2. root@# ls  
  3. a.out  a.outDump  core  test.c    
其中,生成的 a.outDump 文件中包含了二進制文件的 a.out 的彙編代碼

C、在 a.outDump 文件中查找發生段錯誤的地址

  1. root@ubuntu:/home/tarena/project/c_test# grep -n -A 10 -B 10 "0" a.outDump  
  2. 1-  
  3. 2-a.out:     file format elf32-i386  
  4.   
  5. 118:080483b4 <main>:  
  6. 119: 80483b4:   55                      push   %ebp  
  7. 120: 80483b5:   89 e5                   mov    %esp,%ebp  
  8. 121: 80483b7:   83 ec 10                sub    $0x10,%esp  
  9. 122: 80483ba:   c7 45 fc 00 00 00 00    movl   $0x0,-0x4(%ebp)  
  10. 123: 80483c1:   8b 45 fc                mov    -0x4(%ebp),%eax  
  11. 124: 80483c4:   c7 00 0a 00 00 00       movl   $0xa,(%eax)  
  12. 125: 80483ca:   b8 00 00 00 00          mov    $0x0,%eax  
  13. 126: 80483cf:   c9                      leave    
  14. 127: 80483d0:   c3                      ret      
  15. 128: 80483d1:   90                      nop  
  16. 129: 80483d2:   90                      nop  
  17. 130: 80483d3:   90                      nop  
  18. 131: 80483d4:   90                      nop  
  19. 132: 80483d5:   90                      nop  
  20. 133: 80483d6:   90                      nop  
  21. 134: 80483d7:   90                      nop  
  22. 135: 80483d8:   90                      nop  
  23. 136: 80483d9:   90                      nop  
  24. 137: 80483da:   90                      nop  
  25. 138: 80483db:   90                      nop  
  26. 139: 80483dc:   90                      nop  
  27. 140: 80483dd:   90                      nop  
  28. 141: 80483de:   90                      nop  
  29. 142: 80483df:   90                      nop  
通過對以上彙編代碼分析,得知段錯誤發生main函數,對應的彙編指令是movl   $0xa,(%eax),接下來打開程序的源碼,找到彙編指令對應的源碼,也就定位到段錯誤了。 

2)適用場景

A、不需要 -g 參數編譯,不需要藉助於core文件,但需要有一定的彙編語言基礎。

B、如果使用 gcc 編譯優化參數(-O1,-O2,-O3)的話,生成的彙編指令將會被優化,使得調試過程有些難度。

5、使用catchsegv

catchsegv 命令專門用來補貨段錯誤,它通過動態加載器(ld-linux.so)的預加載機制(PRELOAD)把一個事先寫好的 庫(/lib/libSegFault.so)加載上,用於捕捉段錯誤的出錯信息。

  1. root@t# catchsegv ./a.out  
  2. Segmentation fault (core dumped)  
  3. *** Segmentation fault  
  4. Register dump:  
  5.   
  6.  EAX: 00000000   EBX: b77a1ff4   ECX: bfd8a0e4   EDX: bfd8a074  
  7.  ESI: 00000000   EDI: 00000000   EBP: bfd8a048   ESP: bfd8a038  
  8.   
  9.  EIP: 080483c4   EFLAGS: 00010282  
  10.   
  11.  CS: 0073   DS: 007b   ES: 007b   FS: 0000   GS: 0033   SS: 007b  
  12.   
  13.  Trap: 0000000e   Error: 00000006   OldMask: 00000000  
  14.  ESP/signal: bfd8a038   CR2: 00000000  
  15.   
  16. Backtrace:  
  17. ??:0(main)[0x80483c4]  
  18. /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb761a4d3]  
  19. ??:0(_start)[0x8048321]  
  20.   
  21. Memory map:  
  22.   
  23. 08048000-08049000 r-xp 00000000 08:01 2102158 /home/tarena/project/c_test/a.out  
  24. 08049000-0804a000 r--p 00000000 08:01 2102158 /home/tarena/project/c_test/a.out  
  25. 0804a000-0804b000 rw-p 00001000 08:01 2102158 /home/tarena/project/c_test/a.out  
  26. 09467000-0948c000 rw-p 00000000 00:00 0 [heap]  
  27. b75e1000-b75fd000 r-xp 00000000 08:01 1704884 /lib/i386-linux-gnu/libgcc_s.so.1  
  28. b75fd000-b75fe000 r--p 0001b000 08:01 1704884 /lib/i386-linux-gnu/libgcc_s.so.1  
  29. b75fe000-b75ff000 rw-p 0001c000 08:01 1704884 /lib/i386-linux-gnu/libgcc_s.so.1  
  30. b75ff000-b7601000 rw-p 00000000 00:00 0  
  31. b7601000-b77a0000 r-xp 00000000 08:01 1704863 /lib/i386-linux-gnu/libc-2.15.so  
  32. b77a0000-b77a2000 r--p 0019f000 08:01 1704863 /lib/i386-linux-gnu/libc-2.15.so  
  33. b77a2000-b77a3000 rw-p 001a1000 08:01 1704863 /lib/i386-linux-gnu/libc-2.15.so  
  34. b77a3000-b77a6000 rw-p 00000000 00:00 0  
  35. b77b8000-b77bb000 r-xp 00000000 08:01 1704847 /lib/i386-linux-gnu/libSegFault.so  
  36. b77bb000-b77bc000 r--p 00002000 08:01 1704847 /lib/i386-linux-gnu/libSegFault.so  
  37. b77bc000-b77bd000 rw-p 00003000 08:01 1704847 /lib/i386-linux-gnu/libSegFault.so  
  38. b77bd000-b77bf000 rw-p 00000000 00:00 0  
  39. b77bf000-b77c0000 r-xp 00000000 00:00 0 [vdso]  
  40. b77c0000-b77e0000 r-xp 00000000 08:01 1704843 /lib/i386-linux-gnu/ld-2.15.so  
  41. b77e0000-b77e1000 r--p 0001f000 08:01 1704843 /lib/i386-linux-gnu/ld-2.15.so  
  42. b77e1000-b77e2000 rw-p 00020000 08:01 1704843 /lib/i386-linux-gnu/ld-2.15.so  
  43. bfd6b000-bfd8c000 rw-p 00000000 00:00 0 [stack]  

五、內存泄漏檢測工具Valgrind

參看:Unix下C程序內存泄漏檢測工具Valgrind安裝與使用

參看:valgrind 的使用簡介

Valgrind是一款用於內存調試、內存泄漏檢測以及性能分析的軟件開發工具。

官網:valgrind

(1)下載安裝

下載:https://sourceware.org/pub/valgrind/valgrind-3.13.0.tar.bz2
安裝:

  1. #tar xvf valgrind-3.13.0.tar.bz2  
  2. #cd valgrind-3.13.0  
  3. #./configure --prefix=/usr/local/webserver/valgrind  
  4. #make  
  5. #make install  
  6.   
  7. 添加環境變量:  
  8. #vi /etc/profile  
  9. PATH=${PATH}:/usr/local/webserver/valgrind/bin  
  10. #source /etc/profile  
執行 valgrind ls -l 出現如下錯誤:

  1. # valgrind ls -l  
  2. ==16243== Memcheck, a memory error detector  
  3. ==16243== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.  
  4. ==16243== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info  
  5. ==16243== Command: ls -l  
  6. ==16243==   
  7.   
  8. valgrind:  Fatal error at startup: a function redirection  
  9. valgrind:  which is mandatory for this platform-tool combination  
  10. valgrind:  cannot be set up.  Details of the redirection are:  
  11. valgrind:    
  12. valgrind:  A must-be-redirected function  
  13. valgrind:  whose name matches the pattern:      strlen  
  14. valgrind:  in an object with soname matching:   ld-linux.so.2  
  15. valgrind:  was not found whilst processing  
  16. valgrind:  symbols from the object with soname: ld-linux.so.2  
  17. valgrind:    
  18. valgrind:  Possible fixes: (1, short term): install glibc's debuginfo  
  19. valgrind:  package on this machine.  (2, longer term): ask the packagers  
  20. valgrind:  for your Linux distribution to please in future ship a non-  
  21. valgrind:  stripped ld.so (or whatever the dynamic linker .so is called)  
  22. valgrind:  that exports the above-named function using the standard  
  23. valgrind:  calling conventions for this platform.  The package you need  
  24. valgrind:  to install for fix (1) is called  
  25. valgrind:    
  26. valgrind:    On Debian, Ubuntu:                 libc6-dbg  
  27. valgrind:    On SuSE, openSuSE, Fedora, RHEL:   glibc-debuginfo  
  28. valgrind:    
  29. valgrind:  Note that if you are debugging a 32 bit process on a  
  30. valgrind:  64 bit system, you will need a corresponding 32 bit debuginfo  
  31. valgrind:  package (e.g. libc6-dbg:i386).  
  32. valgrind:    
  33. valgrind:  Cannot continue -- exiting now.  Sorry.  
 以上錯誤,是因爲在Ubuntu下還需要一個LIB:libc6-dbg
運行:sudo apt-get install libc6-dbg 以安裝libc6-dbg

然後再次運行: valgrind ls -l

  1. $ valgrind ls -l    
  2. ==14297== Memcheck, a memory error detector  
  3. ==14297== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.  
  4. ==14297== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info  
  5. ==14297== Command: ls -l  
  6. ==14297==   
  7. 總用量 4  
  8. -rw-rw-r-- 1 tarena tarena 105  1月 15 10:04 test.c  
  9. ==14297==   
  10. ==14297== HEAP SUMMARY:  
  11. ==14297==     in use at exit: 13,265 bytes in 29 blocks  
  12. ==14297==   total heap usage: 218 allocs, 189 frees, 73,974 bytes allocated  
  13. ==14297==   
  14. ==14297== LEAK SUMMARY:  
  15. ==14297==    definitely lost: 80 bytes in 2 blocks  
  16. ==14297==    indirectly lost: 240 bytes in 20 blocks  
  17. ==14297==      possibly lost: 0 bytes in 0 blocks  
  18. ==14297==    still reachable: 12,945 bytes in 7 blocks  
  19. ==14297==         suppressed: 0 bytes in 0 blocks  
  20. ==14297== Rerun with --leak-check=full to see details of leaked memory  
  21. ==14297==   
  22. ==14297== For counts of detected and suppressed errors, rerun with: -v  
  23. ==14297== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)  

(2)選項:

  1. <span style="color:#000000;">#valgrind -h  
  2. usage: valgrind [options] prog-and-args  
  3.   
  4.   tool-selection option, with default in [ ]:  
  5.     --tool=<name>             use the Valgrind tool named <name> [memcheck]  
  6.   
  7.   basic user options for all Valgrind tools, with defaults in [ ]:  
  8.     -h --help                 show this message  
  9.     --help-debug              show this message, plus debugging options  
  10.     --version                 show version  
  11.     -q --quiet                run silently; only print error msgs  
  12.     -v --verbose              be more verbose -- show misc extra info  
  13.     --trace-children=no|yes   Valgrind-ise child processes (follow execve)? [no]  
  14.     --trace-children-skip=patt1,patt2,...    specifies a list of executables  
  15.                               that --trace-children=yes should not trace into  
  16.     --trace-children-skip-by-arg=patt1,patt2,...   same as --trace-children-skip=  
  17.                               but check the argv[] entries for children, rather  
  18.                               than the exe name, to make a follow/no-follow decision  
  19.     --child-silent-after-fork=no|yes omit child output between fork & exec? [no]  
  20.     --vgdb=no|yes|full        activate gdbserver? [yes]  
  21.                               full is slower but provides precise watchpoint/step  
  22.     --vgdb-error=<number>     invoke gdbserver after <number> errors [999999999]  
  23.                               to get started quickly, use --vgdb-error=0  
  24.                               and follow the on-screen directions  
  25.     --vgdb-stop-at=event1,event2,... invoke gdbserver for given events [none]  
  26.          where event is one of:  
  27.            startup exit valgrindabexit all none  
  28.     --track-fds=no|yes        track open file descriptors? [no]  
  29.     --time-stamp=no|yes       add timestamps to log messages? [no]  
  30.     --log-fd=<number>         log messages to file descriptor [2=stderr]  
  31.     --log-file=<file>         log messages to <file>  
  32.     --log-socket=ipaddr:port  log messages to socket ipaddr:port  
  33.   
  34.   user options for Valgrind tools that report errors:  
  35.     --xml=yes                 emit error output in XML (some tools only)  
  36.     --xml-fd=<number>         XML output to file descriptor  
  37.     --xml-file=<file>         XML output to <file>  
  38.     --xml-socket=ipaddr:port  XML output to socket ipaddr:port  
  39.     --xml-user-comment=STR    copy STR verbatim into XML output  
  40.     --demangle=no|yes         automatically demangle C++ names? [yes]  
  41.     --num-callers=<number>    show <number> callers in stack traces [12]  
  42.     --error-limit=no|yes      stop showing new errors if too many? [yes]  
  43.     --error-exitcode=<number> exit code to return if errors found [0=disable]  
  44.     --error-markers=<begin>,<end> add lines with begin/end markers before/after  
  45.                               each error output in plain text mode [none]  
  46.     --show-below-main=no|yes  continue stack traces below main() [no]  
  47.     --default-suppressions=yes|no  
  48.                               load default suppressions [yes]  
  49.     --suppressions=<filename> suppress errors described in <filename>  
  50.     --gen-suppressions=no|yes|all    print suppressions for errors? [no]  
  51.     --input-fd=<number>       file descriptor for input [0=stdin]  
  52.     --dsymutil=no|yes         run dsymutil on Mac OS X when helpful? [yes]  
  53.     --max-stackframe=<number> assume stack switch for SP changes larger  
  54.                               than <number> bytes [2000000]  
  55.     --main-stacksize=<number> set size of main thread's stack (in bytes)  
  56.                               [min(max(current 'ulimit' value,1MB),16MB)]  
  57.   
  58.   user options for Valgrind tools that replace malloc:  
  59.     --alignment=<number>      set minimum alignment of heap allocations [8]  
  60.     --redzone-size=<number>   set minimum size of redzones added before/after  
  61.                               heap blocks (in bytes). [16]  
  62.     --xtree-memory=none|allocs|full   profile heap memory in an xtree [none]  
  63.                               and produces a report at the end of the execution  
  64.                               none: no profiling, allocs: current allocated  
  65.                               size/blocks, full: profile current and cumulative  
  66.                               allocated size/blocks and freed size/blocks.  
  67.     --xtree-memory-file=<file>   xtree memory report file [xtmemory.kcg.%p]  
  68.   
  69.   uncommon user options for all Valgrind tools:  
  70.     --fullpath-after=         (with nothing after the '=')  
  71.                               show full source paths in call stacks  
  72.     --fullpath-after=string   like --fullpath-after=, but only show the  
  73.                               part of the path after 'string'.  Allows removal  
  74.                               of path prefixes.  Use this flag multiple times  
  75.                               to specify a set of prefixes to remove.  
  76.     --extra-debuginfo-path=path    absolute path to search for additional  
  77.                               debug symbols, in addition to existing default  
  78.                               well known search paths.  
  79.     --debuginfo-server=ipaddr:port    also query this server  
  80.                               (valgrind-di-server) for debug symbols  
  81.     --allow-mismatched-debuginfo=no|yes  [no]  
  82.                               for the above two flags only, accept debuginfo  
  83.                               objects that don't "match" the main object  
  84.     --smc-check=none|stack|all|all-non-file [all-non-file]  
  85.                               checks for self-modifying code: none, only for  
  86.                               code found in stacks, for all code, or for all  
  87.                               code except that from file-backed mappings  
  88.     --read-inline-info=yes|no read debug info about inlined function calls  
  89.                               and use it to do better stack traces.  [yes]  
  90.                               on Linux/Android/Solaris for Memcheck/Helgrind/DRD  
  91.                               only.  [no] for all other tools and platforms.  
  92.     --read-var-info=yes|no    read debug info on stack and global variables  
  93.                               and use it to print better error messages in  
  94.                               tools that make use of it (Memcheck, Helgrind,  
  95.                               DRD) [no]  
  96.     --vgdb-poll=<number>      gdbserver poll max every <number> basic blocks [5000]   
  97.     --vgdb-shadow-registers=no|yes   let gdb see the shadow registers [no]  
  98.     --vgdb-prefix=<prefix>    prefix for vgdb FIFOs [/tmp/vgdb-pipe]  
  99.     --run-libc-freeres=no|yes free up glibc memory at exit on Linux? [yes]  
  100.     --run-cxx-freeres=no|yes  free up libstdc++ memory at exit on Linux  
  101.                               and Solaris? [yes]  
  102.     --sim-hints=hint1,hint2,...  activate unusual sim behaviours [none]   
  103.          where hint is one of:  
  104.            lax-ioctls lax-doors fuse-compatible enable-outer  
  105.            no-inner-prefix no-nptl-pthread-stackcache fallback-llsc none  
  106.     --fair-sched=no|yes|try   schedule threads fairly on multicore systems [no]  
  107.     --kernel-variant=variant1,variant2,...  
  108.          handle non-standard kernel variants [none]  
  109.          where variant is one of:  
  110.            bproc android-no-hw-tls  
  111.            android-gpu-sgx5xx android-gpu-adreno3xx none  
  112.     --merge-recursive-frames=<number>  merge frames between identical  
  113.            program counters in max <number> frames) [0]  
  114.     --num-transtab-sectors=<number> size of translated code cache [32]  
  115.            more sectors may increase performance, but use more memory.  
  116.     --avg-transtab-entry-size=<number> avg size in bytes of a translated  
  117.            basic block [0, meaning use tool provided default]  
  118.     --aspace-minaddr=0xPP     avoid mapping memory below 0xPP [guessed]  
  119.     --valgrind-stacksize=<number> size of valgrind (host) thread's stack  
  120.                                (in bytes) [1048576]  
  121.     --show-emwarns=no|yes     show warnings about emulation limits? [no]  
  122.     --require-text-symbol=:sonamepattern:symbolpattern    abort run if the  
  123.                               stated shared object doesn't have the stated  
  124.                               text symbol.  Patterns can contain ? and *.  
  125.     --soname-synonyms=syn1=pattern1,syn2=pattern2,... synonym soname  
  126.               specify patterns for function wrapping or replacement.  
  127.               To use a non-libc malloc library that is  
  128.                   in the main exe:  --soname-synonyms=somalloc=NONE  
  129.                   in libxyzzy.so:   --soname-synonyms=somalloc=libxyzzy.so  
  130.     --sigill-diagnostics=yes|no  warn about illegal instructions? [yes]  
  131.     --unw-stack-scan-thresh=<number>   Enable stack-scan unwind if fewer  
  132.                   than <number> good frames found  [0, meaning "disabled"]  
  133.                   NOTE: stack scanning is only available on arm-linux.  
  134.     --unw-stack-scan-frames=<number>   Max number of frames that can be  
  135.                   recovered by stack scanning [5]  
  136.     --resync-filter=no|yes|verbose [yes on MacOS, no on other OSes]  
  137.               attempt to avoid expensive address-space-resync operations  
  138.     --max-threads=<number>    maximum number of threads that valgrind can  
  139.                               handle [500]  
  140.   
  141.   user options for Memcheck:  
  142.     --leak-check=no|summary|full     search for memory leaks at exit?  [summary]  
  143.     --leak-resolution=low|med|high   differentiation of leak stack traces [high]  
  144.     --show-leak-kinds=kind1,kind2,.. which leak kinds to show?  
  145.                                             [definite,possible]  
  146.     --errors-for-leak-kinds=kind1,kind2,..  which leak kinds are errors?  
  147.                                             [definite,possible]  
  148.         where kind is one of:  
  149.           definite indirect possible reachable all none  
  150.     --leak-check-heuristics=heur1,heur2,... which heuristics to use for  
  151.         improving leak search false positive [all]  
  152.         where heur is one of:  
  153.           stdstring length64 newarray multipleinheritance all none  
  154.     --show-reachable=yes             same as --show-leak-kinds=all  
  155.     --show-reachable=no --show-possibly-lost=yes  
  156.                                      same as --show-leak-kinds=definite,possible  
  157.     --show-reachable=no --show-possibly-lost=no  
  158.                                      same as --show-leak-kinds=definite  
  159.     --xtree-leak=no|yes              output leak result in xtree format? [no]  
  160.     --xtree-leak-file=<file>         xtree leak report file [xtleak.kcg.%p]  
  161.     --undef-value-errors=no|yes      check for undefined value errors [yes]  
  162.     --track-origins=no|yes           show origins of undefined values? [no]  
  163.     --partial-loads-ok=no|yes        too hard to explain here; see manual [yes]  
  164.     --expensive-definedness-checks=no|yes  
  165.                                      Use extra-precise definedness tracking [no]  
  166.     --freelist-vol=<number>          volume of freed blocks queue     [20000000]  
  167.     --freelist-big-blocks=<number>   releases first blocks with size>= [1000000]  
  168.     --workaround-gcc296-bugs=no|yes  self explanatory [no].  Deprecated.  
  169.                                      Use --ignore-range-below-sp instead.  
  170.     --ignore-ranges=0xPP-0xQQ[,0xRR-0xSS]   assume given addresses are OK  
  171.     --ignore-range-below-sp=<number>-<number>  do not report errors for  
  172.                                      accesses at the given offsets below SP  
  173.     --malloc-fill=<hexnumber>        fill malloc'd areas with given value  
  174.     --free-fill=<hexnumber>          fill free'd areas with given value  
  175.     --keep-stacktraces=alloc|free|alloc-and-free|alloc-then-free|none  
  176.         stack trace(s) to keep for malloc'd/free'd areas       [alloc-and-free]  
  177.     --show-mismatched-frees=no|yes   show frees that don't match the allocator? [yes]  
  178.   
  179.   Extra options read from ~/.valgrindrc, $VALGRIND_OPTS, ./.valgrindrc  
  180.   
  181.   Memcheck is Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.  
  182.   Valgrind is Copyright (C) 2000-2017, and GNU GPL'd, by Julian Seward et al.  
  183.   LibVEX is Copyright (C) 2004-2017, and GNU GPL'd, by OpenWorks LLP et al.  
  184.   
  185.   Bug reports, feedback, admiration, abuse, etc, to: www.valgrind.org.</span><span style="color:#993399;">  
  186. </span>  

(3)使用

1、使用未初始化內存問題

  1. #include<stdio.h>  
  2. int main(void)  
  3. {  
  4.       
  5.     int a[5];  
  6.     int i,s;  
  7.     a[0]=a[1]=a[2]=a[3]=a[4]=0;  
  8.     s=0;  
  9.     for (i =0;i<5;i++)  
  10.         s+=a[i];  
  11.     if(s==377)  
  12.         printf("sum is %d\n",s);  
  13.     return 0;  
  14. }  
  15. 執行:  
  16. # gcc -Wall test.c -g -o test  
  17. # valgrind --tool=memcheck --leak-check=yes ./test  
  18.   
  19. $ valgrind --tool=memcheck --leak-check=yes ./test  
  20. ==14496== Memcheck, a memory error detector  
  21. ==14496== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.  
  22. ==14496== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info  
  23. ==14496== Command: ./test  
  24. ==14496==   
  25. ==14496==   
  26. ==14496== HEAP SUMMARY:  
  27. ==14496==     in use at exit: 0 bytes in 0 blocks  
  28. ==14496==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated  
  29. ==14496==   
  30. ==14496== All heap blocks were freed -- no leaks are possible  
  31. ==14496==   
  32. ==14496== For counts of detected and suppressed errors, rerun with: -v  
  33. ==14496== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)  

2、內存讀寫越界

  1. #include<stdlib.h>  
  2. #include<stdio.h>  
  3.   
  4. int main(int argc,char *argv[])  
  5. {  
  6.     int len =4;  
  7.     int* pt =(int*)malloc(len*sizeof(int));  
  8.     int* p=pt;  
  9.     int i;  
  10.     for(i=0;i<len;i++)  
  11.     {  
  12.         p++;  
  13.     }  
  14.     *p=5;  
  15.     printf("the value of p equal:%d\n",*p);  
  16.     return 0;  
  17. }  
  18. 執行:  
  19. # gcc -Wall test.c -g -o test  
  20. # valgrind --tool=memcheck --leak-check=yes ./test  
  21.   
  22. $ valgrind --tool=memcheck --leak-check=yes ./test  
  23. ==14514== Memcheck, a memory error detector  
  24. ==14514== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.  
  25. ==14514== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info  
  26. ==14514== Command: ./test  
  27. ==14514==   
  28. ==14514== Invalid write of size 4  
  29. ==14514==    at 0x8048462: main (test.c:14)  
  30. ==14514==  Address 0x41f4038 is 0 bytes after a block of size 16 alloc'd  
  31. ==14514==    at 0x402BD74: malloc (vg_replace_malloc.c:270)  
  32. ==14514==    by 0x8048433: main (test.c:7)  
  33. ==14514==   
  34. ==14514== Invalid read of size 4  
  35. ==14514==    at 0x804846C: main (test.c:15)  
  36. ==14514==  Address 0x41f4038 is 0 bytes after a block of size 16 alloc'd  
  37. ==14514==    at 0x402BD74: malloc (vg_replace_malloc.c:270)  
  38. ==14514==    by 0x8048433: main (test.c:7)  
  39. ==14514==   
  40. the value of p equal:5  
  41. ==14514==   
  42. ==14514== HEAP SUMMARY:  
  43. ==14514==     in use at exit: 16 bytes in 1 blocks  
  44. ==14514==   total heap usage: 1 allocs, 0 frees, 16 bytes allocated  
  45. ==14514==   
  46. ==14514== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1  
  47. ==14514==    at 0x402BD74: malloc (vg_replace_malloc.c:270)  
  48. ==14514==    by 0x8048433: main (test.c:7)  
  49. ==14514==   
  50. ==14514== LEAK SUMMARY:  
  51. ==14514==    definitely lost: 16 bytes in 1 blocks  
  52. ==14514==    indirectly lost: 0 bytes in 0 blocks  
  53. ==14514==      possibly lost: 0 bytes in 0 blocks  
  54. ==14514==    still reachable: 0 bytes in 0 blocks  
  55. ==14514==         suppressed: 0 bytes in 0 blocks  
  56. ==14514==   
  57. ==14514== For counts of detected and suppressed errors, rerun with: -v  
  58. ==14514== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)  

3、內存覆蓋

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<string.h>  
  4.   
  5. int main(int argc, char* argv[])  
  6. {  
  7.     char x[50];  
  8.     int i;  
  9.     for(i=0;i<50;i++)  
  10.     {  
  11.         x[i]=i+1;  
  12.     }  
  13.     strncpy(x+20,x,20);  
  14.     strncpy(x+20,x,21);  
  15.     strncpy(x,x+20,20);  
  16.     strncpy(x,x+20,21);  
  17.       
  18.     x[39]='\0';  
  19.     strcpy(x,x+20);  
  20.       
  21.     x[39]=39;  
  22.     x[50]='\0';  
  23.     strcpy(x,x+20);  
  24.       
  25.     return 0;  
  26. }  
  27. 執行:  
  28. # gcc -Wall test.c -g -o test  
  29. # valgrind --tool=memcheck --leak-check=yes ./test  
  30.   
  31. $ valgrind --tool=memcheck --leak-check=yes ./test  
  32. ==14551== Memcheck, a memory error detector  
  33. ==14551== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.  
  34. ==14551== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info  
  35. ==14551== Command: ./test  
  36. ==14551==   
  37. ==14551== Source and destination overlap in strncpy(0xbec6a0c3, 0xbec6a0af, 21)  
  38. ==14551==    at 0x402C860: strncpy (mc_replace_strmem.c:472)  
  39. ==14551==    by 0x80484E6: main (test.c:14)  
  40. ==14551==   
  41. ==14551== Source and destination overlap in strncpy(0xbec6a0af, 0xbec6a0c3, 21)  
  42. ==14551==    at 0x402C860: strncpy (mc_replace_strmem.c:472)  
  43. ==14551==    by 0x8048524: main (test.c:16)  
  44. ==14551==   
  45. ==14551== Source and destination overlap in strcpy(0xbec6a09a, 0xbec6a0ae)  
  46. ==14551==    at 0x402C6AB: strcpy (mc_replace_strmem.c:438)  
  47. ==14551==    by 0x8048561: main (test.c:23)  
  48. ==14551==   
  49. ==14551==   
  50. ==14551== HEAP SUMMARY:  
  51. ==14551==     in use at exit: 0 bytes in 0 blocks  
  52. ==14551==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated  
  53. ==14551==   
  54. ==14551== All heap blocks were freed -- no leaks are possible  
  55. ==14551==   
  56. ==14551== For counts of detected and suppressed errors, rerun with: -v  
  57. ==14551== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)  

4、動態內存管理錯誤

  1. #include<stdlib.h>  
  2. #include<stdio.h>  
  3.   
  4. int main(int argc,char *argv[])  
  5. {  
  6.     int i;  
  7.     char* p=(char*)malloc(10);  
  8.     char* pt=p;  
  9.     for(i =0;i<10;i++)  
  10.     {  
  11.         p[i]='z';  
  12.     }  
  13.     delete p;  
  14.       
  15.     pt[1]='x';  
  16.     free(pt);  
  17.     return 0;  
  18. }  
  19. 執行:  
  20. # g++ -Wall test.cpp -g -o test  
  21. # valgrind --tool=memcheck --leak-check=yes ./test  
  22.   
  23. $ valgrind --tool=memcheck --leak-check=yes ./test  
  24. ==14607== Memcheck, a memory error detector  
  25. ==14607== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.  
  26. ==14607== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info  
  27. ==14607== Command: ./test  
  28. ==14607==   
  29. ==14607== Mismatched free() / delete / delete []  
  30. ==14607==    at 0x402A92A: operator delete(void*) (vg_replace_malloc.c:480)  
  31. ==14607==    by 0x80484F6: main (test.cpp:13)  
  32. ==14607==  Address 0x4324028 is 0 bytes inside a block of size 10 alloc'd  
  33. ==14607==    at 0x402BD74: malloc (vg_replace_malloc.c:270)  
  34. ==14607==    by 0x80484B8: main (test.cpp:7)  
  35. ==14607==   
  36. ==14607== Invalid write of size 1  
  37. ==14607==    at 0x80484FE: main (test.cpp:15)  
  38. ==14607==  Address 0x4324029 is 1 bytes inside a block of size 10 free'd  
  39. ==14607==    at 0x402A92A: operator delete(void*) (vg_replace_malloc.c:480)  
  40. ==14607==    by 0x80484F6: main (test.cpp:13)  
  41. ==14607==   
  42. ==14607== Invalid free() / delete / delete[] / realloc()  
  43. ==14607==    at 0x402AD3D: free (vg_replace_malloc.c:446)  
  44. ==14607==    by 0x804850C: main (test.cpp:16)  
  45. ==14607==  Address 0x4324028 is 0 bytes inside a block of size 10 free'd  
  46. ==14607==    at 0x402A92A: operator delete(void*) (vg_replace_malloc.c:480)  
  47. ==14607==    by 0x80484F6: main (test.cpp:13)  
  48. ==14607==   
  49. ==14607==   
  50. ==14607== HEAP SUMMARY:  
  51. ==14607==     in use at exit: 0 bytes in 0 blocks  
  52. ==14607==   total heap usage: 1 allocs, 2 frees, 10 bytes allocated  
  53. ==14607==   
  54. ==14607== All heap blocks were freed -- no leaks are possible  
  55. ==14607==   
  56. ==14607== For counts of detected and suppressed errors, rerun with: -v  
  57. ==14607== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)  

5、內存泄漏

  1. //tree.h  
  2. #ifndef _BADLEAK_  
  3. #define _BAD_LEAK_  
  4.   
  5. typedef struct _node{  
  6.     struct _node *l;  
  7.     struct _node * r;  
  8.     char v;  
  9. }node;  
  10.   
  11. node *mk( node *l,  node *r, char val);  
  12. void nodefr(node *n);  
  13. #endif  
  14.   
  15. //tree.cpp  
  16. #include <stdio.h>  
  17. #include "tree.h"  
  18. #include <stdlib.h>  
  19.   
  20. node *mk(node *l, node * r, char val)  
  21. {  
  22.     node *f=(node* )malloc(sizeof(node));  
  23.     f->l=l;  
  24.     f->r=r;  
  25.     f->v=val;  
  26.     return f;  
  27. }  
  28.   
  29. void nodefr(node* n)  
  30. {  
  31.     if(n)  
  32.     {  
  33.         nodefr(n->l);  
  34.         nodefr(n->r);  
  35.         free(n);  
  36.     }  
  37. }  
  38.   
  39. //badleak.cpp  
  40. #include <stdio.h>  
  41. #include <stdlib.h>  
  42. #include "tree.h"  
  43.   
  44. int main()  
  45. {  
  46.     node* tree1, *tree2, * tree3;  
  47.     tree1=mk(mk(mk(0,0,'3'),0,'2'),0,'1');  
  48.     tree2=mk(0,mk(0,mk(0,0,'6'),'5'),'4');  
  49.     tree3=mk(mk(tree1,tree2,'8'),0,'7');  
  50.     //nodefr(tree3);  
  51.     return 0;  
  52. }  
  53.       
  54. 執行:  
  55. # g++ -Wall tree.cpp badleak.cpp -g -o badleak  
  56. #valgrind --tool=memcheck --leak-check=yes ./badleak   
  57.   
  58. $ valgrind --tool=memcheck --leak-check=yes ./badleak   
  59. ==14676== Memcheck, a memory error detector  
  60. ==14676== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.  
  61. ==14676== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info  
  62. ==14676== Command: ./badleak  
  63. ==14676==   
  64. ==14676==   
  65. ==14676== HEAP SUMMARY:  
  66. ==14676==     in use at exit: 96 bytes in 8 blocks  
  67. ==14676==   total heap usage: 8 allocs, 0 frees, 96 bytes allocated  
  68. ==14676==   
  69. ==14676== 96 (12 direct, 84 indirect) bytes in 1 blocks are definitely lost in loss record 8 of 8  
  70. ==14676==    at 0x402BD74: malloc (vg_replace_malloc.c:270)  
  71. ==14676==    by 0x804842B: mk(_node*, _node*, char) (tree.cpp:7)  
  72. ==14676==    by 0x8048560: main (badleak.cpp:10)  
  73. ==14676==   
  74. ==14676== LEAK SUMMARY:  
  75. ==14676==    definitely lost: 12 bytes in 1 blocks  
  76. ==14676==    indirectly lost: 84 bytes in 7 blocks  
  77. ==14676==      possibly lost: 0 bytes in 0 blocks  
  78. ==14676==    still reachable: 0 bytes in 0 blocks  
  79. ==14676==         suppressed: 0 bytes in 0 blocks  
  80. ==14676==   
  81. ==14676== For counts of detected and suppressed errors, rerun with: -v  
  82. ==14676== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)  

六、一些注意事項

1)出現段錯誤時,首先應該想到段錯誤的定義,從它出發考慮引發錯誤的原因。

2)在使用指針時,定義了指針後記得初始化指針,在使用的時候記得判斷是否爲 NULL

3)在使用數組時,注意數組是否被初始化,數組下標是否越界,數組元素是否存在等

4)在訪問變量,注意變量所佔地址空間是否已經被程序釋放掉

5)在處理變量時,注意變量的格式控制是否合理等

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章