关于Valgrind

valgrind 包含多个工具 Memcheck、Cachegrind、Helgrind、Callgrind、massif

valgrind是在命令行中通过程序指定 tool 命令行参数为具体的工具名称来启动的。

一、安装

yum 直接安装 valgrind 后,在使用时,可能会提示

“failed to start tool 'memcheck' for platform 'amd64-linux'”

推荐使用编译安装
1、下载valgrind安装包

wget http://www.valgrind.org/downloads/valgrind-3.8.1.tar.bz2

2、解压安装包

tar -xvf valgrind-3.8.1.tar.bz2

3、执行 autogen.sh

cd valgrind-3.8.1 && ./autogen.sh

这一步可能会提示

“run aclocal error”

手动安装 automake 即可

 yum install automake

4、执行

./configure --prefix=/usr/bin/webserver/valgrind

这一步可能会提示

   “checking the GLIBC_VERSION version... unsupported version 2.17
    configure: errror: Valgrind requires glibc version 2.2-2.16”

这是由于valgrind 需要 glibc 版本在 2.2-2.16 之间,系统 glibc 版本为 2.17。不能为了安装 valgrind 回退 glibc 的版本,需要修改 configure 文件,增加对 glibc 2.17 版本的支持.

在这里插入图片描述
在 case “${GLIBC_VERSION}” in 中增加 2.17 相关信息

在这里插入图片描述
可以直接拷贝 2.16,版本信息改为 2.17 即可

5、重新执行

 ./configure --prefix=/usr/bin/webserver/valgrind

6、make
7、make install

二、功能

1、Memcheck
用来检测程序中出现的内存问题,所有对内存的读写都会被检测到,一切 malloc()/free()/new/delete 的调用都会被捕获.

使用未初始化的内存(Use of uninitialised memory)/使用已经释放了的内存(Reading/writing memory after it has been free’d)/使用超过 malloc 分配的内存空间(Reading/writing off the end of malloc’d blocks)/对堆栈的非法访问(Reading/writing inappropriate areas on the stack)/申请的空间是否有释放(Memory leaks-where pointers to malloc’d blocks are lost forever)/malloc free new delete 申请和释放内存的匹配(Mismatched use of malloc/new/new[] vs free/delete/delete[])/src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)

valgrind --tool=memcheck --leak-check=full ./mem_leak
–leak-check=full 完全检查内存泄漏
–show-reachable=yes 显示内存泄漏的地点
–trace-children=yes 跟入子进程
–log-file=log.txt 将调试信息输出到 log.txt

2、Callgrind
和 gprof 类似的分析工具,它不需要在编辑源代码时附加特殊选项。

3、Cachegrind
Cache分析器,模拟 CPU 中的一级缓存 I1,DI 和二级缓存,能够精确地指出程序中 cache 的丢失和命中。如果需要,它还可以提供 cache 丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数。

4、Helgrind
检查多线程程序中出现的竞争问题。Helgrind 寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方。Helgrind 实现了名为“Eraser”的竞争检测算法。

5、Massif
堆栈分析器,它能测量程序在堆栈中使用了多少内存,告诉我们堆块,管理堆块和栈的大小。

PS:1) Valgrind 不检查静态分配数组的使用情况
2)Valgrind 占用了更多的内存——两倍于程序的正常使用量。

参考:https://blog.csdn.net/m_buddy/article/details/74999118
https://blog.csdn.net/Linsoft1994/article/details/79562518

猜你喜欢

转载自blog.csdn.net/if9600/article/details/111299700