Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1833843
  • 博文数量: 286
  • 博客积分: 3713
  • 博客等级: 少校
  • 技术积分: 2275
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-11 09:47
个人简介

http://blog.chinaunix.net/uid/16979052.html

文章分类

全部博文(286)

文章存档

2018年(1)

2017年(16)

2016年(9)

2015年(17)

2014年(15)

2013年(112)

2012年(116)

分类: LINUX

2013-03-04 08:24:55

TD P { direction: ltr; color: rgb(0, 0, 0); text-align: left; }TD P.western { font-family: "Liberation Serif","Times New Roman",serif; font-size: 12pt; }TD P.cjk { font-family: "宋体"; font-size: 12pt; }TD P.ctl { font-family: "宋体"; font-size: 12pt; }H2 { margin-bottom: 0.21cm; direction: ltr; color: rgb(0, 0, 0); text-align: left; page-break-after: avoid; }H2.western { font-family: "Liberation Sans","Arial",sans-serif; font-size: 14pt; font-style: italic; font-weight: bold; }H2.cjk { font-family: "宋体"; font-size: 14pt; font-style: italic; font-weight: bold; }H2.ctl { font-family: "宋体"; font-size: 14pt; font-style: italic; font-weight: bold; }H1 { margin-bottom: 0.21cm; direction: ltr; color: rgb(0, 0, 0); text-align: left; page-break-after: avoid; }H1.western { font-family: "Liberation Sans","Arial",sans-serif; font-size: 16pt; font-weight: bold; }H1.cjk { font-family: "宋体"; font-size: 16pt; font-weight: bold; }H1.ctl { font-family: "宋体"; font-size: 16pt; font-weight: bold; }P { margin-bottom: 0.21cm; direction: ltr; color: rgb(0, 0, 0); text-align: left; }P.western { font-family: "Liberation Serif","Times New Roman",serif; font-size: 12pt; }P.cjk { font-family: "宋体"; font-size: 12pt; }P.ctl { font-family: "宋体"; font-size: 12pt; }

C/C++开发的时候常常遇到内存泄漏或内存访问越界问题,valgrindLinux平台上的一款免费的内存检测工具。Valgrind是一款基于模拟linux下的程序调试器和剖析器的软件套件,可以运行于x86, amd64ppc32架构上。

1、介绍

valgrind包含几个工具:

1.memcheck

memcheck探测程序中内存管理存在的问题。它检查所有对内存的读/写操作,并截取所有的malloc/new/free/delete调用。

memcheck工具能够探测到以下问题:

  • 使用未初始化的内存

  • /写已经被释放的内存

  • /写内存越界

  • /写不恰当的内存栈空间

  • 内存泄漏

  • 使用malloc/new/new[]free/delete/delete[]不匹配。

2.cachegrind

cachegrind是一个cache剖析器。它模拟执行CPU中的L1, D1L2 cache,因此它能很精确的指出代码中的cache未命中。如果你需要,它可以打印出cache未命中的次数,内存引用和发生cache未命中的每一行代码,每一个函数,每一个模块和整个程序的摘要。如果你要求更细致的信息,它可以打印出每一行机器码的未命中次数。在x86amd64上,cachegrind通过CPUID自动探测机器的cache配置,所以在多数情况下它不再需要更多的配置信息了。

3.helgrind

helgrind查找多线程程序中的竞争数据。helgrind查找内存地址,那些被多于一条线程访问的内存地址,但是没有使用一致的锁就会被查出。这表示这些地址在多线程间访问的时候没有进行同步,很可能会引起很难查找的时序问题。

2、分析内存泄露和内存越界

例子 :

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)

从这份报告可以看出,进程2967main函数(test_mem.c:6)内存越界写了,引起写内存越界的是memcpy 函数,出现了10bytes的内存泄露。




阅读(1510) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~