http://blog.chinaunix.net/uid/16979052.html
全部博文(286)
分类: LINUX
2013-03-04 08:24:55
作C/C++开发的时候常常遇到内存泄漏或内存访问越界问题,valgrind是Linux平台上的一款免费的内存检测工具。Valgrind是一款基于模拟linux下的程序调试器和剖析器的软件套件,可以运行于x86, amd64和ppc32架构上。
valgrind包含几个工具:
memcheck探测程序中内存管理存在的问题。它检查所有对内存的读/写操作,并截取所有的malloc/new/free/delete调用。
memcheck工具能够探测到以下问题:
使用未初始化的内存
读/写已经被释放的内存
读/写内存越界
读/写不恰当的内存栈空间
内存泄漏
使用malloc/new/new[]和free/delete/delete[]不匹配。
cachegrind是一个cache剖析器。它模拟执行CPU中的L1, D1和L2 cache,因此它能很精确的指出代码中的cache未命中。如果你需要,它可以打印出cache未命中的次数,内存引用和发生cache未命中的每一行代码,每一个函数,每一个模块和整个程序的摘要。如果你要求更细致的信息,它可以打印出每一行机器码的未命中次数。在x86和amd64上,cachegrind通过CPUID自动探测机器的cache配置,所以在多数情况下它不再需要更多的配置信息了。
helgrind查找多线程程序中的竞争数据。helgrind查找内存地址,那些被多于一条线程访问的内存地址,但是没有使用一致的锁就会被查出。这表示这些地址在多线程间访问的时候没有进行同步,很可能会引起很难查找的时序问题。
johnyin@johnyin:valgrind$cat -n test_mem.c |
1 #include
2 #include
3 int main(int argc, char *argv[]) 4 { 5 char *ptr = (char*)malloc(10); 6 strcpy(ptr, "0123456789abcd"); 7 return 0; 8 } 9 |
编译程序
gcc -g -o test test_mem.c
johnyin@johnyin:valgrind$valgrind --tool=memcheck --leak-check=yes ./test |
==2967== Memcheck, a memory error detector ==2967== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==2967== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info ==2967== Command: ./test ==2967== ==2967== Invalid write of size 1 ==2967== at 0x40259F5: memcpy (mc_replace_strmem.c:497) ==2967== by 0x8048429: main (test_mem.c:6) ==2967== Address 0x417b032 is 0 bytes after a block of size 10 alloc'd ==2967== at 0x4023F50: malloc (vg_replace_malloc.c:236) ==2967== by 0x8048408: main (test_mem.c:5) ==2967== ==2967== Invalid write of size 1 ==2967== at 0x40259FE: memcpy (mc_replace_strmem.c:497) ==2967== by 0x8048429: main (test_mem.c:6) ==2967== Address 0x417b033 is 1 bytes after a block of size 10 alloc'd ==2967== at 0x4023F50: malloc (vg_replace_malloc.c:236) ==2967== by 0x8048408: main (test_mem.c:5) ==2967== ==2967== Invalid write of size 1 ==2967== at 0x4025A34: memcpy (mc_replace_strmem.c:497) ==2967== by 0x8048429: main (test_mem.c:6) ==2967== Address 0x417b034 is 2 bytes after a block of size 10 alloc'd ==2967== at 0x4023F50: malloc (vg_replace_malloc.c:236) ==2967== by 0x8048408: main (test_mem.c:5) ==2967== ==2967== ==2967== HEAP SUMMARY: ==2967== in use at exit: 10 bytes in 1 blocks ==2967== total heap usage: 1 allocs, 0 frees, 10 bytes allocated ==2967== ==2967== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==2967== at 0x4023F50: malloc (vg_replace_malloc.c:236) ==2967== by 0x8048408: main (test_mem.c:5) ==2967== ==2967== LEAK SUMMARY: ==2967== definitely lost: 10 bytes in 1 blocks ==2967== indirectly lost: 0 bytes in 0 blocks ==2967== possibly lost: 0 bytes in 0 blocks ==2967== still reachable: 0 bytes in 0 blocks ==2967== suppressed: 0 bytes in 0 blocks ==2967== ==2967== For counts of detected and suppressed errors, rerun with: -v ==2967== ERROR SUMMARY: 6 errors from 4 contexts (suppressed: 12 from 7) |
从这份报告可以看出,进程2967,main函数(test_mem.c:6)内存越界写了,引起写内存越界的是memcpy 函数,出现了10bytes的内存泄露。