• <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>

            吾嘗終日而思矣 不如須臾之所學(xué)也

            御物而行 御風(fēng)而飛
            隨筆 - 10, 文章 - 1, 評(píng)論 - 9, 引用 - 0
            數(shù)據(jù)加載中……

            Valgrind 使用簡單說明(轉(zhuǎn)載)

            Valgrind 介紹

            Valgrind是一個(gè)GPL的軟件,用于Linux(For x86, amd64 and ppc32)程序的內(nèi)存調(diào)試和代碼剖析。你可以在它的環(huán)境中運(yùn)行你的程序來監(jiān)視內(nèi)存的使用情況,比如C 語言中的malloc和free或者 C++中的new和 delete。使用Valgrind的工具包,你可以自動(dòng)的檢測許多內(nèi)存管理和線程的bug,避免花費(fèi)太多的時(shí)間在bug尋找上,使得你的程序更加穩(wěn)固。

            Valgrind的主要功能

            Valgrind工具包包含多個(gè)工具,如Memcheck,Cachegrind,Helgrind, Callgrind,Massif。下面分別介紹個(gè)工具的作用:

            Memcheck 工具主要檢查下面的程序錯(cuò)誤:

            • 使用未初始化的內(nèi)存 (Use of uninitialised memory)
            • 使用已經(jīng)釋放了的內(nèi)存 (Reading/writing memory after it has been free’d)
            • 使用超過 malloc分配的內(nèi)存空間(Reading/writing off the end of malloc’d blocks)
            • 對(duì)堆棧的非法訪問 (Reading/writing inappropriate areas on the stack)
            • 申請(qǐng)的空間是否有釋放 (Memory leaks – where pointers to malloc’d blocks are lost forever)
            • malloc/free/new/delete申請(qǐng)和釋放內(nèi)存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
            • src和dst的重疊(Overlapping src and dst pointers in memcpy() and related functions)

            Callgrind

            Callgrind收集程序運(yùn)行時(shí)的一些數(shù)據(jù),函數(shù)調(diào)用關(guān)系等信息,還可以有選擇地進(jìn)行cache 模擬。在運(yùn)行結(jié)束時(shí),它會(huì)把分析數(shù)據(jù)寫入一個(gè)文件。callgrind_annotate可以把這個(gè)文件的內(nèi)容轉(zhuǎn)化成可讀的形式。

            Cachegrind

            它模擬 CPU中的一級(jí)緩存I1,D1和L2二級(jí)緩存,能夠精確地指出程序中 cache的丟失和命中。如果需要,它還能夠?yàn)槲覀兲峁ヽache丟失次數(shù),內(nèi)存引用次數(shù),以及每行代碼,每個(gè)函數(shù),每個(gè)模塊,整個(gè)程序產(chǎn)生的指令數(shù)。這對(duì)優(yōu)化程序有很大的幫助。

            Helgrind

            它主要用來檢查多線程程序中出現(xiàn)的競爭問題。Helgrind 尋找內(nèi)存中被多個(gè)線程訪問,而又沒有一貫加鎖的區(qū)域,這些區(qū)域往往是線程之間失去同步的地方,而且會(huì)導(dǎo)致難以發(fā)掘的錯(cuò)誤。Helgrind實(shí)現(xiàn)了名為” Eraser” 的競爭檢測算法,并做了進(jìn)一步改進(jìn),減少了報(bào)告錯(cuò)誤的次數(shù)。

            Massif

            堆棧分析器,它能測量程序在堆棧中使用了多少內(nèi)存,告訴我們堆塊,堆管理塊和棧的大小。Massif能幫助我們減少內(nèi)存的使用,在帶有虛擬內(nèi)存的現(xiàn)代系統(tǒng)中,它還能夠加速我們程序的運(yùn)行,減少程序停留在交換區(qū)中的幾率。

            Valgrind 安裝

            1、 到www.valgrind.org下載最新版valgrind-3.2.3.tar.bz2
            2、 解壓安裝包:tar –jxvf valgrind-3.2.3.tar.bz2
            3、 解壓后生成目錄valgrind-3.2.3
            4、 cd valgrind-3.2.3
            5、 ./configure
            6、 Make;make install

            Valgrind 使用

            用法: valgrind [options] prog-and-args [options]: 常用選項(xiàng),適用于所有Valgrind工具

            1. -tool=<name> 最常用的選項(xiàng)。運(yùn)行 valgrind中名為toolname的工具。默認(rèn)memcheck。
            2. h –help 顯示幫助信息。
            3. -version 顯示valgrind內(nèi)核的版本,每個(gè)工具都有各自的版本。
            4. q –quiet 安靜地運(yùn)行,只打印錯(cuò)誤信息。
            5. v –verbose 更詳細(xì)的信息, 增加錯(cuò)誤數(shù)統(tǒng)計(jì)。
            6. -trace-children=no|yes 跟蹤子線程? [no]
            7. -track-fds=no|yes 跟蹤打開的文件描述?[no]
            8. -time-stamp=no|yes 增加時(shí)間戳到LOG信息? [no]
            9. -log-fd=<number> 輸出LOG到描述符文件 [2=stderr]
            10. -log-file=<file> 將輸出的信息寫入到filename.PID的文件里,PID是運(yùn)行程序的進(jìn)行ID
            11. -log-file-exactly=<file> 輸出LOG信息到 file
            12. -log-file-qualifier=<VAR> 取得環(huán)境變量的值來做為輸出信息的文件名。 [none]
            13. -log-socket=ipaddr:port 輸出LOG到socket ,ipaddr:port

            LOG信息輸出

            1. -xml=yes 將信息以xml格式輸出,只有memcheck可用
            2. -num-callers=<number> show <number> callers in stack traces [12]
            3. -error-limit=no|yes 如果太多錯(cuò)誤,則停止顯示新錯(cuò)誤? [yes]
            4. -error-exitcode=<number> 如果發(fā)現(xiàn)錯(cuò)誤則返回錯(cuò)誤代碼 [0=disable]
            5. -db-attach=no|yes 當(dāng)出現(xiàn)錯(cuò)誤,valgrind會(huì)自動(dòng)啟動(dòng)調(diào)試器gdb。[no]
            6. -db-command=<command> 啟動(dòng)調(diào)試器的命令行選項(xiàng)[gdb -nw %f %p]

            適用于Memcheck工具的相關(guān)選項(xiàng):

            1. -leak-check=no|summary|full 要求對(duì)leak給出詳細(xì)信息? [summary]
            2. -leak-resolution=low|med|high how much bt merging in leak check [low]
            3. -show-reachable=no|yes show reachable blocks in leak check? [no]

            Valgrind 使用舉例

            下面是一段有問題的C程序代碼test.c

            #include <stdlib.h>
            void f(void)
            {
            int* x = malloc(10 * sizeof(int));
            x[10] = 0; //問題1: 數(shù)組下標(biāo)越界
            } //問題2: 內(nèi)存沒有釋放
            int main(void)
            {
            f();
            return 0;
            }
            1、 編譯程序test.c
            gcc -Wall test.c -g -o test
            2、 使用Valgrind檢查程序BUG
            valgrind --tool=memcheck --leak-check=full ./test
            3、 分析輸出的調(diào)試信息
            ==3908== Memcheck, a memory error detector.
            ==3908== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
            ==3908== Using LibVEX rev 1732, a library for dynamic binary translation.
            ==3908== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
            ==3908== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
            ==3908== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
            ==3908== For more details, rerun with: -v
            ==3908==
            --3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50
            --3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50
            /*數(shù)組越界錯(cuò)誤*/
            ==3908== Invalid write of size 4
            ==3908== at 0x8048384: f (test.c:6)
            ==3908== by 0x80483AC: main (test.c:11)
            ==3908== Address 0x400C050 is 0 bytes after a block of size 40 alloc'd
            ==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
            ==3908== by 0x8048377: f (test.c:5)
            ==3908== by 0x80483AC: main (test.c:11)
            ==3908==
            ==3908== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 14 from 1)
            ==3908== malloc/free: in use at exit: 40 bytes in 1 blocks.
            ==3908== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
            ==3908== For counts of detected errors, rerun with: -v
            ==3908== searching for pointers to 1 not-freed blocks.
            ==3908== checked 59,124 bytes.
            ==3908==
            ==3908==
            /*有內(nèi)存空間沒有釋放*/
            ==3908== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
            ==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
            ==3908== by 0x8048377: f (test.c:5)
            ==3908== by 0x80483AC: main (test.c:11)
            ==3908==
            ==3908== LEAK SUMMARY:
            ==3908== definitely lost: 40 bytes in 1 blocks.
            ==3908== possibly lost: 0 bytes in 0 blocks.
            ==3908== still reachable: 0 bytes in 0 blocks.
            ==3908== suppressed: 0 bytes in 0 blocks.

            posted on 2011-08-05 09:30 Roger 閱讀(530) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            欧美精品福利视频一区二区三区久久久精品 | 国产精品久久久久久一区二区三区| 五月丁香综合激情六月久久| 精品熟女少妇a∨免费久久| 青青青伊人色综合久久| 久久久久久曰本AV免费免费| 国产精品欧美久久久久天天影视 | 一级女性全黄久久生活片免费| 99久久综合狠狠综合久久止| 伊人久久大香线蕉无码麻豆| 久久久WWW成人免费毛片| 国产欧美久久一区二区| 久久精品中文字幕一区| 亚洲&#228;v永久无码精品天堂久久| 久久香蕉超碰97国产精品| 一本大道久久香蕉成人网| 久久er国产精品免费观看8| 久久亚洲精品无码观看不卡| 久久伊人精品一区二区三区| 亚洲精品无码久久久久去q| 男女久久久国产一区二区三区| 精品久久人妻av中文字幕| 久久国产精品免费| 综合人妻久久一区二区精品| 久久精品国产福利国产秒| 国内精品伊人久久久久网站| 一本一道久久a久久精品综合 | 国产亚洲欧美成人久久片| 精品国际久久久久999波多野| 国产成人无码精品久久久免费| 久久久久亚洲AV无码去区首| 国产美女亚洲精品久久久综合| .精品久久久麻豆国产精品| 精品久久久久国产免费| 中文字幕日本人妻久久久免费| 久久久久夜夜夜精品国产| 久久午夜免费视频| 精品久久久久久中文字幕| 久久天天躁狠狠躁夜夜不卡| 99久久精品免费观看国产| 久久久久亚洲精品天堂|