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

            Linux 內存調試工具- Valgrind 使用初探

            Posted on 2008-07-01 10:02 T.S Liu 閱讀(886) 評論(0)  編輯 收藏 引用 所屬分類: linux_bug_tool
            Valgrind 是在linux系統下開發應用程序時用于調試內存問題的工具。它尤其擅長發現內存管理的問題,它可以檢查程序運行時的內存泄漏問題。

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

               下載最新版本的Valgrind,目前是3.2.0。 wget http://www.valgrind.org/downloads/valkyrie-1.2.0.tar.bz2

               執行常規的安裝步驟:./confgure && make && make install。注意: 系統必須安裝QT的開發包。即便這樣在make 時還是出現qplatformdefs.h這個文件找不到的情況,導致make失敗。查找系統中的qplatformdefs.h 之后,發現沒有存在于qt的標準頭文件目錄/usr/lib/qt-3.3/include。如是將/usr/lib/qt-3.3/mkspecs/linux-g++/ 目錄下該頭文件復制標準頭文件目錄,重新make ,后面一切OK。

            初次使用
                編譯如下代碼:  gcc -Wall example.c -g -o example 

            #include <stdlib.h>
                        void f(void)
                        {
                        int* x = malloc(10 * sizeof(int));
                        x[10] = 0;        // problem 1: heap block overrun
                        }                    // problem 2: memory leak -- x not freed
                        int main(void)
                        {
                        f();
                        return 0;
                        }

                 注意:gcc 的-g 選項讓Valgrind調試輸出時指出相應信息的代碼所在的行號。

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

            ==6742== Memcheck, a memory error detector for x86-linux.
            ==6742== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward et al.
            ==6742== Using valgrind-2.2.0, a program supervision framework for x86-linux.
            ==6742== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
            ==6742== For more details, rerun with: -v
            ==6742==
            ==6742== Invalid write of size 4
            ==6742==    at 0x8048384: f (example.c:6)
            ==6742==    by 0x80483AC: main (example.c:12)
            ==6742==  Address 0x1B908050 is 0 bytes after a block of size 40 alloc'd
            ==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
            ==6742==    by 0x8048377: f (example.c:5)
            ==6742==    by 0x80483AC: main (example.c:12)
            ==6742==
            ==6742== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 1)
            ==6742== malloc/free: in use at exit: 40 bytes in 1 blocks.
            ==6742== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
            ==6742== For counts of detected errors, rerun with: -v
            ==6742== searching for pointers to 1 not-freed blocks.
            ==6742== checked 1360800 bytes.
            ==6742==
            ==6742==
            ==6742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
            ==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
            ==6742==    by 0x8048377: f (example.c:5)
            ==6742==    by 0x80483AC: main (example.c:12)
            ==6742==
            ==6742== LEAK SUMMARY:
            ==6742==    definitely lost: 40 bytes in 1 blocks.
            ==6742==    possibly lost:   0 bytes in 0 blocks.
            ==6742==    still reachable: 0 bytes in 0 blocks.
            ==6742==         suppressed: 0 bytes in 0 blocks.
            ==6742== Reachable blocks (those to which a pointer was found) are not shown.
            ==6742== To see them, rerun with: --show-reachable=yes

               上面的C程序存在兩個錯誤:1. 數組下標越界;2. 分配的內存沒有釋放,存在內存泄露的問題。對于錯誤1,看Valgrind的調試信息片斷
            ==6742== Invalid write of size 4
            ==6742==    at 0x8048384: f (example.c:6)
            ==6742==    by 0x80483AC: main (example.c:12)
            ==6742==  Address 0x1B908050 is 0 bytes after a block of size 40 alloc'd
            ==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
            ==6742==    by 0x8048377: f (example.c:5)

            對于錯誤2,看這個

            ==6742== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.

            ......

            ==6742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
            ==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
            ==6742==    by 0x8048377: f (example.c:5)
            ==6742==    by 0x80483AC: main (example.c:12)

            久久久久久免费一区二区三区| 久久狠狠爱亚洲综合影院| 久久精品蜜芽亚洲国产AV| 精品久久久久久中文字幕人妻最新 | 伊人久久大香线蕉AV一区二区| 久久久精品视频免费观看 | 韩国三级中文字幕hd久久精品| 亚洲一区精品伊人久久伊人| 久久精品国产亚洲av麻豆色欲| 99久久精品无码一区二区毛片| 亚洲国产精品综合久久一线| 久久精品一区二区国产| 四虎国产精品成人免费久久| 精品久久香蕉国产线看观看亚洲 | 99国产欧美精品久久久蜜芽| 欧洲国产伦久久久久久久| 久久久久久亚洲精品成人 | 日韩精品久久久久久| 免费精品久久天干天干| 99久久综合狠狠综合久久| 久久无码人妻一区二区三区| 久久久久无码专区亚洲av| 国产精品一区二区久久| 久久久噜噜噜久久中文字幕色伊伊 | 国内精品久久久久久麻豆| 久久人人爽人人爽人人片AV不 | 久久精品国产亚洲7777| 国产V亚洲V天堂无码久久久| 无遮挡粉嫩小泬久久久久久久| 久久一本综合| 亚洲精品高清一二区久久| 99久久精品免费国产大片| 久久福利青草精品资源站| 久久精品无码午夜福利理论片| 亚洲中文字幕无码久久2017| 久久久久久免费视频| 热99RE久久精品这里都是精品免费| 色综合合久久天天给综看| 合区精品久久久中文字幕一区| 亚洲国产成人精品女人久久久| 久久这里只有精品首页|