一,Valgrind工具简介
Valgrind
工具可以检出动态分配的内存的访问越界问题。
二,举例
$gcc -g –o array array.c
$ valgrind --tool=memcheck ./array (程序源代码略)
……
==17990==
Invalid write of size 4
==17990== at 0x80483FA: main (
array.c:15
)
==17990== Address 0x402A1B8 is 0 bytes after a block of size 400 alloc'd
==17990== at 0x4004405: malloc (vg_replace_malloc.c:149)
==17990== by 0x80483D6: main (array.c:12)
公司常用的命令:
valgrind -v --tool=memcheck --error-limit=no --leak-check=yes --show-reachable=no --run-libc-freeres=no --log-file=valgrind.log $programname
valgrind -v --tool=memcheck --error-limit=no --leak-check=full --leak-resolution=high --show-reachable=yes --run-libc-freeres=no --track-fds=yes --log-file=/home/zebra/valgrind.log $programname
上边写的有点粗略,下边是引用的一篇关于vargrind的文章。
原文地址: http://hi.baidu.com/zzcqh/blog/item/67d619d954c2f52b11df9b45.html
Vargrind用于Linux程序的内存调试和代码剖析。在它的环境中运行你的代码来检测内存使用情况以自动形成报告,预防和发现BUG。
一,工具简介
Vargrind主要由Memcheck,Cachegrind,Helgrind, Callgrind,Massif等工具组成。下面介绍其中几个。
1,Memcheck
Memcheck 工具主要检查下面的程序错误:
-
使用
未初始化的内存 (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 [])
-
mem操作函数中,src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)
2,Callgrind
Callgrind收集程序运行时的一些数据,函数调用关系等信息,还可以有选择地进行cache 模拟。在运行结束时,它会把分析数据写入一个文件。callgrind_annotate可以把这个文件的内容转化成可读的形式。
3,Cachegrind
它模拟 CPU中的一级缓存I1,D1和L2二级缓存,能够精确地指出程序中cache的丢失和命中。如果需要,它还能够为我们提供cache丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数。这对优化程序有很大的帮助。
4,Helgrind
它主要用来检查多线程程序中出现的竞争问题。Helgrind
寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方,而且会导致难以发掘的错误。Helgrind实现了名为“Eraser”的竞争检测算法,并做了进一步改进,减少了报告错误的次数。
5,Massif
堆栈分析器,它能测量程序在堆栈中使用
了多少内存,告诉我们堆块,堆管理块和栈的大小。Massif能帮助我们减少内存的使用
,在带有虚拟内存的现代系统中,它还能够加速我们程序的运行,减少程序停留在交换区中的几率。
二,软件下载
1、 到www.valgrind
.org下载最新版valgrind
-3.2.3.tar.bz2
2、 解压安装包:tar –jxvf valgrind
-3.2.3.tar.bz2
3、 解压后生成目录valgrind
-3.2.3
4、 cd valgrind
-3.2.3
5、 ./configure
6、 make;make install
三,使用和选项
用法: valgrind
[options] prog-and-args [options]:
1,常用选项,适用于所有Valgrind
工具
-
-tool=<name> 使用valgrind
中名为toolname的工具。默认为memcheck。
-
q –quiet 安静运行,只打印错误信息。
-
v –verbose 打印详细的信息, 增加错误数统计。
-
-trace-children=no|yes 是否跟踪子线程
-
-track-fds=no|yes 是否跟踪打开的文件描述
-
-time-stamp=no|yes 是否增加时间戳到LOG信息
-
-log-fd=<number> 输出LOG到描述符文件 [2=stderr]
-
-log-file=<file> 将输出的信息写入到filename.PID的文件里,PID是运行程序的进行ID
-
-log-file-exactly=<file> 输出LOG信息到 file
-
-log-file-qualifier=<VAR> 取得环境变量的值来做为输出信息的文件名。 [none]
-
-log-socket=ipaddr:port 输出LOG到socket,ipaddr:port
2,LOG信息输出选项
-
-xml=yes 将信息以xml格式输出,只有memcheck可用
-
-num-callers=<number> show <number> callers in stack traces [12]
-
-error-limit=no|yes 如果太多错误,则停止显示新错误? [yes]
-
-error-exitcode=<number> 如果发现错误则返回错误代码 [0=disable]
-
-db-attach=no|yes 当出现错误,valgrind
会自动启动调试器gdb。[no]
-
-db-command=<command> 启动调试器的命令行选项[gdb -nw %f %p]
3,适用于Memcheck工具的相关选项:
-
-leak-check=no|summary|full 要求对leak给出详细信息? [summary]
-
-leak-resolution=low|med|high how much bt merging in leak check [low]
-
-show-reachable=no|yes show reachable blocks in leak check? [no]
四,举例
下面是一段有问题的C程序代码test.c
#include <stdlib.h>
void f(void)
{
int* x = malloc(10 * sizeof(int));
x[10] = 0; //问题1: 数组下标越界
} //问题2: 内存没有释放
int main(void)
{
f();
return 0;
}
1、 编译程序test.c
gcc -Wall test.c -g -o test
2、 使用
Valgrind
检查程序BUG
valgrind
--tool=memcheck --leak-check=full ./test
3、 分析输出的调试信息
==3908== Memcheck, a memory error detector.
==3908== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al
==3908== Using LibVEX rev 1732, a library for dynamic binary translation
==3908== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==3908== Using valgrind
-3.2.3, a dynamic binary instrumentation framework.
==3908== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==3908== For more details, rerun with: -v
==3908== --3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50--3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50
/*数组越界错误*/
==3908== Invalid write of size 4
==3908== at 0x8048384: f (test.c:6)
==3908== by 0x80483AC: main (test.c:11)
==3908== Address 0x400C050 is 0 bytes after a block of size 40 alloc'd
==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
==3908== by 0x8048377: f (test.c:5)
==3908== by 0x80483AC: main (test.c:11)
==3908==
==3908== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 14 from 1)
==3908== malloc/free: in use at exit: 40 bytes in 1 blocks.
==3908== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
==3908== For counts of detected errors, rerun with: -v
==3908== searching for pointers to 1 not-freed blocks.
==3908== checked 59,124 bytes.
==3908==
==3908==
/*有内存空间没有释放*/
==3908== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
==3908== by 0x8048377: f (test.c:5)
==3908== by 0x80483AC: main (test.c:11)
==3908==
==3908== LEAK SUMMARY:
==3908== definitely lost: 40 bytes in 1 blocks.
==3908== possibly lost: 0 bytes in 0 blocks.
==3908== still reachable: 0 bytes in 0 blocks.
==3908== suppressed: 0 bytes in 0 blocks.
分享到:
相关推荐
由于上传文件大小限制,将工具链分为2个压缩文件,arm-linux-gcc-4.3.2-1.tgz和arm-linux-gcc-4.3.2-2.tgz 请务必下载完整,分别进行安装。 安装方法: 1.ubuntu 终端输入 tar -zxvf arm-linux-gcc-4.3.2-1.tgz -C ...
04.1.1 Linux常用命令-文件处理命令-命令格式与目录处理命令ls.mp4 04.1.2 Linux常用命令-文件处理命令-目录处理命令.mp4 04.1.3 Linux常用命令-文件处理命令-文件处理命令.mp4 04.1.4 Linux常用命令-文件处理...
GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具。或许,各位比较喜欢那种图形界面方式的,像VC、BCB等 IDE的调试,但如果你是在UNIX平台下做软件,你会发现GDB这个调试工具有比VC、BCB的图形化调试器更强大...
交叉编译工具:gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux.tar.bz2 解压命令:tar jxvf gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux.tar.bz2
Linux常用命令-常用函数-核心手册-C一站式学习
arm-2014.05-29-arm-none-linux-gnueabi-i686-pc-linux-gnu 是由 CodeSourcery 公司基于GCC推出的的ARM交叉编译工具。可用于交叉编译ARM系统中所有环节的代码,包括裸机程序、u-boot、Linux kernel、filesystem和App...
p13390677_112040_Linux-x86-64_1of7.zip p13390677_112040_Linux-x86-64_2of7.zip p13390677_112040_Linux-x86-64_3of7.zip p13390677_112040_Linux-x86-64_4of7.zip p13390677_112040_Linux-x86-64_5of7.zip p...
arm-none-linux-gnueabi-gcc是 Codesourcery 公司(目前已经被Mentor收购)基于GCC推出的的ARM交叉编译工具。可用于交叉编译ARM系统中所有环节的代码,包括裸机程序、u-boot、Linux kernel、filesystem和App应用程序...
ARM Linux交叉编译工具链,arm-2014.05-29-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2
Linux常用操作-个人总结的 希望对你有用!!!
交叉编译工具:gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux.tar.bz2 解压命令:tar jxvf gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux.tar.bz2
android linux交叉编译工具aarch64-linux-android-4.9版本!需要的可以下载!
Ubuntu安装arm-linux-gcc交叉编译工具 arm-linux-gcc工具包arm-linux-gcc-5.4.0.tar.gz
util-linux-ng-2.17源码(含fdisk) Here is a list of all documented files with brief descriptions: util-linux-ng-2.17.2/disk-utils/blockdev.c [code] util-linux-ng-2.17.2/disk-utils/cramfs.h [code] ...
android linux交叉编译工具aarch64-linux-android-4.9版本。
linux-arm-gcc-4.9.2交叉编译工具,已验证,决对可以使用
找了好久才找到的这个,以前使用低版本的arm-linux-gcc交叉编译工具时,编译QT出错,这里找了arm-linux-gcc-5.4.0,放在这里存下
带硬件浮点的ARM9使用的交叉编译工具链arm-vfp-linux-gnu-4.3.2的源码包和安装方法说明。可以用于对LPC3250的交叉编译
arm-none-linux-gnueabi-gcc是 Codesourcery 公司(目前已经被Mentor收购)基于GCC推出的的ARM交叉编译工具。可用于交叉编译ARM系统中所有环节的代码,包括裸机程序、u-boot、Linux kernel、filesystem和App应用程序...
由于上传文件大小限制,将工具链分为2个压缩文件,arm-linux-gcc-4.3.2-1.tgz和arm-linux-gcc-4.3.2-2.tgz 请务必下载完整,分别进行安装。 安装方法: 1.ubuntu 终端输入 tar -zxvf arm-linux-gcc-4.3.2-1.tgz -C ...