• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Khan's Notebook GCC/GNU/Linux Delphi/Window Java/Anywhere

            路漫漫,長修遠,我們不能沒有錢
            隨筆 - 173, 文章 - 0, 評論 - 257, 引用 - 0
            數據加載中……

            內存越界和泄露調試工具(轉載自賽迪網)

            作者:sixth

            用C/C++開發其中最令人頭疼的一個問題就是內存管理,有時候為了查找一個內存泄漏或者一個內存訪問越界,需要要花上好幾天時間,如果有一款工具能夠幫助我們做這件事情就好了,valgrind正好就是這樣的一款工具。

            Valgrind是一款基于模擬linux下的程序調試器和剖析器的軟件套件,可以 運行于x86, amd64和ppc32架構上。valgrind包含一個核心,它提供一個虛擬的CPU運行程序,還有一系列的工具,它們完成調試,剖析和一些類似的任 務。valgrind是高度模塊化的,所以開發人員或者用戶可以給它添加新的工具而不會損壞己有的結構。

            valgrind的官方網址是:http://valgrind.org

            你可以在它的網站上下載到最新的valgrind,它是開放源碼和免費的。

            一、介紹

            valgrind包含幾個標準的工具,它們是:

            1、memcheck

            memcheck探測程序中內存管理存在的問題。它檢查所有對內存的讀/寫操作,并截取所有的malloc/new/free/delete調用。因此memcheck工具能夠探測到以下問題:

            1)使用未初始化的內存

            2)讀/寫已經被釋放的內存

            3)讀/寫內存越界

            4)讀/寫不恰當的內存棧空間

            5)內存泄漏

            6)使用malloc/new/new[]和free/delete/delete[]不匹配。

            2、cachegrind

            cachegrind是一個cache剖析器。它模擬執行CPU中的L1, D1和L2 cache,因此它能很精確的指出代碼中的cache未命中。如果你需要,它可以打印出cache未命中的次數,內存引用和發生cache未命中的每一行 代碼,每一個函數,每一個模塊和整個程序的摘要。如果你要求更細致的信息,它可以打印出每一行機器碼的未命中次數。在x86和amd64上, cachegrind通過CPUID自動探測機器的cache配置,所以在多數情況下它不再需要更多的配置信息了。

            3、helgrind

            helgrind查找多線程程序中的競爭數據。helgrind查找內存地址,那些被多于一條線程訪問的內存地址,但是沒有使用一致的鎖就會被查出。這表示這些地址在多線程間訪問的時候沒有進行同步,很可能會引起很難查找的時序問題。

            二、valgrind對你的程序都做了些什么

            valgrind被設計成非侵入式的,它直接工作于可執行文件上,因此在檢查前不需要重新編譯、連接和修改你的程序。要檢查一個程序很簡單,只需要執行下面的命令就可以了

            valgrind --tool=tool_name program_name

            比如我們要對ls -l命令做內存檢查,只需要執行下面的命令就可以了

            valgrind --tool=memcheck ls -l

            不管是使用哪個工具,valgrind在開始之前總會先取得對你的程序的控制權,從 可執行關聯庫里讀取調試信息。然后在valgrind核心提供的虛擬CPU上運行程序,valgrind會根據選擇的工具來處理代碼,該工具會向代碼中加 入檢測代碼,并把這些代碼作為最終代碼返回給valgrind核心,最后valgrind核心運行這些代碼。

            如果要檢查內存泄漏,只需要增加--leak-check=yes就可以了,命令如下

            valgrind --tool=memcheck --leak-check=yes ls -l

            不同工具間加入的代碼變化非常的大。在每個作用域的末尾,memcheck加入代碼檢查每一片內存的訪問和進行值計算,代碼大小至少增加12倍,運行速度要比平時慢25到50倍。

            valgrind模擬程序中的每一條指令執行,因此,檢查工具和剖析工具不僅僅是對你的應用程序,還有對共享庫,GNU C庫,X的客戶端庫都起作用。

            三、現在開始

            首先,在編譯程序的時候打開調試模式(gcc編譯器的-g選項)。如果沒有調試信 息,即使最好的valgrind工具也將中能夠猜測特定的代碼是屬于哪一個函數。打開調試選項進行編譯后再用valgrind檢查,valgrind將會 給你的個詳細的報告,比如哪一行代碼出現了內存泄漏。

            當檢查的是C++程序的時候,還應該考慮另一個選項 -fno-inline。它使得函數調用鏈很清晰,這樣可以減少你在瀏覽大型C++程序時的混亂。比如在使用這個選項的時候,用memcheck檢查 openoffice就很容易。當然,你可能不會做這項工作,但是使用這一選項使得valgrind生成更精確的錯誤報告和減少混亂。

            一些編譯優化選項(比如-O2或者更高的優化選項),可能會使得memcheck提交錯誤的未初始化報告,因此,為了使得valgrind的報告更精確,在編譯的時候最好不要使用優化選項。

            如果程序是通過腳本啟動的,可以修改腳本里啟動程序的代碼,或者使用--trace-children=yes選項來運行腳本。

            下面是用memcheck檢查ls -l命令的輸出報告,在終端下執行下面的命令

            valgrind --tool=memcheck ls -l

            程序會打印出ls -l命令的結果,最后是valgrind的檢查報告如下:

            ==4187==

            ==4187== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 19 from 2)

            ==4187== malloc/free: in use at exit: 15,154 bytes in 105 blocks.

            ==4187== malloc/free: 310 allocs, 205 frees, 60,093 bytes allocated.

            ==4187== For counts of detected errors, rerun with: -v

            ==4187== searching for pointers to 105 not-freed blocks.

            ==4187== checked 145,292 bytes.

            ==4187==

            ==4187== LEAK SUMMARY:

            ==4187== definitely lost: 0 bytes in 0 blocks.

            ==4187== possibly lost: 0 bytes in 0 blocks.

            ==4187== still reachable: 15,154 bytes in 105 blocks.

            ==4187== suppressed: 0 bytes in 0 blocks.

            ==4187== Reachable blocks (those to which a pointer was found) are not shown.

            ==4187== To see them, rerun with: --show-reachable=yes

            這里的“4187”指的是執行ls -l的進程ID,這有利于區別不同進程的報告。memcheck會給出報告,分配置和釋放了多少內存,有多少內存泄漏了,還有多少內存的訪問是可達的,檢查了多少字節的內存。

            下面舉兩個用valgrind做內存檢查的例子

            例子一 (test.c):

            #include <string.h>

            int main(int argc, char *argv[])
            {
            char *ptr;

            ptr = (char*) malloc(10);
            strcpy(ptr, "01234567890");

            return 0;
            }

            編譯程序

            gcc -g -o test test.c

            用valgrind執行命令

            valgrind --tool=memcheck --leak-check=yes ./test

            報告如下

            ==4270== Memcheck, a memory error detector.

            ==4270== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.

            ==4270== Using LibVEX rev 1606, a library for dynamic binary translation.

            ==4270== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.

            ==4270== Using valgrind-3.2.0, a dynamic binary instrumentation framework.

            ==4270== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.

            ==4270== For more details, rerun with: -v

            ==4270==

            ==4270== Invalid write of size 1

            ==4270== at 0x4006190: strcpy (mc_replace_strmem.c:271)

            ==4270== by 0x80483DB: main (test.c:8)

            ==4270== Address 0x4023032 is 0 bytes after a block of size 10 alloc'd

            ==4270== at 0x40044F6: malloc (vg_replace_malloc.c:149)

            ==4270== by 0x80483C5: main (test.c:7)

            ==4270==

            ==4270== Invalid write of size 1

            ==4270== at 0x400619C: strcpy (mc_replace_strmem.c:271)

            ==4270== by 0x80483DB: main (test.c:8)

            ==4270== Address 0x4023033 is 1 bytes after a block of size 10 alloc'd

            ==4270== at 0x40044F6: malloc (vg_replace_malloc.c:149)

            ==4270== by 0x80483C5: main (test.c:7)

            ==4270==

            ==4270== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 12 from 1)

            ==4270== malloc/free: in use at exit: 10 bytes in 1 blocks.

            ==4270== malloc/free: 1 allocs, 0 frees, 10 bytes allocated.

            ==4270== For counts of detected errors, rerun with: -v

            ==4270== searching for pointers to 1 not-freed blocks.

            ==4270== checked 51,496 bytes.

            ==4270==

            ==4270==

            ==4270== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1

            ==4270== at 0x40044F6: malloc (vg_replace_malloc.c:149)

            ==4270== by 0x80483C5: main (test.c:7)

            ==4270==

            ==4270== LEAK SUMMARY:

            ==4270== definitely lost: 10 bytes in 1 blocks.

            ==4270== possibly lost: 0 bytes in 0 blocks.

            ==4270== still reachable: 0 bytes in 0 blocks.

            ==4270== suppressed: 0 bytes in 0 blocks.

            ==4270== Reachable blocks (those to which a pointer was found) are not shown.

            ==4270== To see them, rerun with: --show-reachable=yes

            從這份報告可以看出,進程號是4270,test.c的第8行寫內存越界了,引起寫內存越界的是strcpy函數,

            第7行泄漏了10個字節的內存,引起內存泄漏的是malloc函數。

            例子二(test2.c)

            #include <stdio.h>

            int foo(int x)
            {
            if (x < 0) {
            printf("%d ", x);
            }

            return 0;
            }

            int main(int argc, char *argv[])
            {
            int x;

            foo(x);

            return 0;
            }

            編譯程序

            gcc -g -o test2 test2.c

            用valgrind做內存檢查

            valgrind --tool=memcheck ./test2

            輸出報告如下

            ==4285== Memcheck, a memory error detector.

            ==4285== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.

            ==4285== Using LibVEX rev 1606, a library for dynamic binary translation.

            ==4285== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.

            ==4285== Using valgrind-3.2.0, a dynamic binary instrumentation framework.

            ==4285== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.

            ==4285== For more details, rerun with: -v

            ==4285==

            ==4285== Conditional jump or move depends on uninitialised value(s)

            ==4285== at 0x8048372: foo (test2.c:5)

            ==4285== by 0x80483B4: main (test2.c:16)

            ==4285==p p

            ==4285== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 1)

            ==4285== malloc/free: in use at exit: 0 bytes in 0 blocks.

            ==4285== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.

            ==4285== For counts of detected errors, rerun with: -v

            ==4285== All heap blocks were freed -- no leaks are possible.

            從這份報告可以看出進程PID是4285,test2.c文件的第16行調用了foo函數,在test2.c文件的第5行foo函數使用了一個未初始化的變量。

            valgrind還有很多使用選項,具體可以查看valgrind的man手冊頁和valgrind官方網站的在線文檔。





            Windows用戶不必沮喪,雖然在Windows上沒有Valgrind可用,但是你可以試一試IBM的Purify,它在功能上和Valgrind相似。

            posted on 2008-03-20 12:23 Khan 閱讀(4967) 評論(0)  編輯 收藏 引用 所屬分類: GCC/G++跨平臺開發

            久久综合成人网| 精品久久久久久国产| 国产精品热久久毛片| 亚洲国产成人久久精品99 | 精品一二三区久久aaa片| 麻豆一区二区99久久久久| 99久久这里只有精品| 7777精品伊人久久久大香线蕉| 久久91精品久久91综合| 亚洲精品视频久久久| 91精品国产综合久久精品| 亚洲精品国产第一综合99久久| 久久99精品国产| 少妇久久久久久久久久| 91精品国产91久久久久久| 一本一本久久A久久综合精品| 国产视频久久| 久久精品国产精品亚洲精品| 久久精品日日躁夜夜躁欧美| 国产精品gz久久久| 久久久国产精品网站| 无码人妻久久一区二区三区 | 国内精品久久久久影院免费| 免费一级做a爰片久久毛片潮 | 亚洲国产一成久久精品国产成人综合 | 亚洲国产成人久久综合一 | 一本色道久久综合亚洲精品| 久久人人超碰精品CAOPOREN| 国产成人精品久久综合| 久久久久四虎国产精品| 91精品国产色综合久久| 久久精品亚洲中文字幕无码麻豆| 久久久SS麻豆欧美国产日韩| 亚洲中文字幕伊人久久无码| 99久久国产亚洲综合精品| 国产精品久久久久久久app| 亚洲国产综合久久天堂| 久久久久av无码免费网| 亚洲国产精品无码久久SM| 久久久久久午夜成人影院| 国产午夜福利精品久久2021|