Valgrind用法(memory leak check)

1270阅读 0评论2013-09-17 joepayne
分类:C/C++

Valgrind 


  • 说明

Valgrind 是一个可以帮助检测linux下程序是否存在内存管理的问题,尤其是内存泄漏,及非法访问等.


valgrind [valgrind-option] program [program-args]
它的参数中可以用--tool=toolname指定使用什么工具如:
valgrind -tool=memcheck,如不指定,默认使用memcheck.

详细请看下面例子:

* Source

[fl@nwsso sessionvalue]$ cat test.c

#include
#include

int main(vold) {
        char *pcTmp = NULL;

        pcTmp = malloc(10);
        memset(pcTmp,'a',10);
        pcTmp[10]='\0';  //
Invalid write

        return 0;

}                        // Memory leak

* Compile

[fl@nwsso sessionvalue]$ gcc test.c -o test -g

 *Output

[fl@nwsso sessionvalue]$ valgrind -v --leak-check=full ./test
==30562== Memcheck, a memory error detector.
==30562== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==30562== Using LibVEX rev 1658, a library for dynamic binary translation.
==30562== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==30562== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
==30562== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==30562==
--30562-- Command line
--30562--    ./test
--30562-- Startup, with flags:
--30562--    -v
--30562--    --leak-check=full
--30562-- Contents of /proc/version:
--30562--   Linux version 2.4.20-8 (bhcompile@porky.devel.redhat.com) (gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) #1 Thu Mar 13 17:54:28 EST 2003
--30562-- Arch and hwcaps: X86, x86-sse1
--30562-- Valgrind library directory: /usr/local/lib/valgrind
--30562-- Reading syms from /lib/ld-2.3.2.so (0x4000000)
--30562-- Reading syms from /home/fl/C/session/sessionvalue/test (0x8048000)
--30562-- Reading syms from /usr/local/lib/valgrind/x86-linux/memcheck (0x38000000)
--30562--    object doesn't have a dynamic symbol table
--30562-- Reading suppressions file: /usr/local/lib/valgrind/default.supp
--30562-- REDIR: 0x40114A0 (index) redirected to 0x38020EEB (vgPlain_x86_linux_REDIR_FOR_index)
--30562-- Reading syms from /usr/local/lib/valgrind/x86-linux/vgpreload_core.so (0x4017000)
--30562-- Reading syms from /usr/local/lib/valgrind/x86-linux/vgpreload_memcheck.so (0x4019000)
==30562== WARNING: new redirection conflicts with existing -- ignoring it
--30562--     new: 0x040114A0 (index     ) R-> 0x0401C180 index
--30562-- REDIR: 0x4011640 (strlen) redirected to 0x401C3D4 (strlen)
--30562-- Reading syms from /lib/libc-2.3.2.so (0x403B000)
--30562-- REDIR: 0x40B5500 (rindex) redirected to 0x401C0A8 (rindex)
--30562-- REDIR: 0x40ADD80 (malloc) redirected to 0x401A64C (malloc)
--30562-- REDIR: 0x40B6B20 (memset) redirected to 0x401CCB4 (memset)
==30562== Invalid write of size 1
==30562==    at 0x804839B: main (in /home/fl/C/session/sessionvalue/test)
==30562==  Address 0x4174032 is 0 bytes after a block of size 10 alloc'd
==30562==    at 0x401A6CE: malloc (vg_replace_malloc.c:149)
==30562==    by 0x804837C: main (in /home/fl/C/session/sessionvalue/test)
--30562-- REDIR: 0x40ADF40 (free) redirected to 0x401B203 (free)
==30562==
==30562== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 1)
==30562==
==30562== 1 errors in context 1 of 1:
==30562== Invalid write of size 1
==30562==    at 0x804839B: main (in /home/fl/C/session/sessionvalue/test)
==30562==  Address 0x4174032 is 0 bytes after a block of size 10 alloc'd
==30562==    at 0x401A6CE: malloc (vg_replace_malloc.c:149)
==30562==    by 0x804837C: main (in /home/fl/C/session/sessionvalue/test)
--30562--
--30562-- supp:   11 Ubuntu-stripped-ld.so
==30562==
==30562== IN SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 1)
==30562==
==30562== malloc/free: in use at exit: 10 bytes in 1 blocks.
==30562== malloc/free: 1 allocs, 0 frees, 10 bytes allocated.
==30562==
==30562== searching for pointers to 1 not-freed blocks.
==30562== checked 50,976 bytes.
==30562==
==30562==
==30562== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==30562==    at 0x401A6CE: malloc (vg_replace_malloc.c:149)
==30562==    by 0x804837C: main (in /home/fl/C/session/sessionvalue/test)
==30562==
==30562== LEAK SUMMARY:
==30562==    definitely lost: 10 bytes in 1 blocks.
==30562==      possibly lost: 0 bytes in 0 blocks.
==30562==    still reachable: 0 bytes in 0 blocks.
==30562==         suppressed: 0 bytes in 0 blocks.
==30562== Reachable blocks (those to which a pointer was found) are not shown.
==30562== To see them, rerun with: --show-reachable=yes
--30562--  memcheck: sanity checks: 0 cheap, 1 expensive
--30562--  memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use
--30562--  memcheck: auxmaps: 0 searches, 0 comparisons
--30562--  memcheck: SMs: n_issued      = 6 (96k, 0M)
--30562--  memcheck: SMs: n_deissued    = 0 (0k, 0M)
--30562--  memcheck: SMs: max_noaccess  = 65535 (1048560k, 1023M)
--30562--  memcheck: SMs: max_undefined = 0 (0k, 0M)
--30562--  memcheck: SMs: max_defined   = 21 (336k, 0M)
--30562--  memcheck: SMs: max_non_DSM   = 6 (96k, 0M)
--30562--  memcheck: max sec V bit nodes:    0 (0k, 0M)
--30562--  memcheck: set_sec_vbits8 calls: 0 (new: 0, updates: 0)
--30562--  memcheck: max shadow mem size:   400k, 0M
--30562-- translate:            fast SP updates identified: 1,162 ( 87.4%)
--30562-- translate:   generic_known SP updates identified: 76 (  5.7%)
--30562-- translate: generic_unknown SP updates identified: 91 (  6.8%)
--30562--     tt/tc: 2,924 tt lookups requiring 2,940 probes
--30562--     tt/tc: 2,924 fast-cache updates, 3 flushes
--30562--  transtab: new        1,369 (28,827 -> 480,003; ratio 166:10) [0 scs]
--30562--  transtab: dumped     0 (0 -> ??)
--30562--  transtab: discarded  6 (153 -> ??)
--30562-- scheduler: 21,310 jumps (bb entries).
--30562-- scheduler: 0/1,588 major/minor sched events.
--30562--    sanity: 1 cheap, 1 expensive checks.
--30562--    exectx: 30,011 lists, 8 contexts (avg 0 per list)
--30562--    exectx: 13 searches, 5 full compares (384 per 1000)
--30562--    exectx: 0 cmp2, 26 cmp4, 0 cmpAll

* Output elucidata

检查内存错误要在上面的输出中看两个关键字
ERROR SUMMARY 和 LEAK SUMMARY
例子中存在两个错误:
 Invalid write
==30562== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 1)
==30562==
==30562== 1 errors in context 1 of 1:
==30562== Invalid write of size 1
==30562== ERROR SUMMARY: 1 errors from 1 contexts

 Memory leak
==30562== LEAK SUMMARY:
==30562==    definitely lost: 10 bytes in 1 blocks. 
==30562==      possibly lost: 0 bytes in 0 blocks.
==30562==    still reachable: 0 bytes in 0 blocks.
==30562==         suppressed: 0 bytes in 0 blocks.

可以查看valgrind manual,增加更多选项,如 --log-file --leak-resolution --show-reachable等等.


 

上一篇:同一进程中的线程究竟共享哪些资源
下一篇:Unix / Linux 文件系统解析