dmalloc检测程序内存泄漏

一、生成dmalloc静态库

1、从网上下载dmalloc源码

编译方法:

./configure --prefix=/home/xxx/program/dmalloc-5.5.2/out --enable-threads

make

make install


注:如果使用C++,那么

make threadscxx

make install

2、把生成的bin和lib copy到板子对应的bin和lib目录下。

3、重新编译需要检测的程序:

makefile里面增加:-DDMALLOC -DDMALLOC_FUNC_CHECK -ldmallocthcxx。如果是C程序把-ldmallocthcxx修改为-ldmalloc

编译好重新放进板子后,设置dmalloc的环境变量

执行./dmalloc -b -l logfile -i 100 low

会看到有两行输出:

DMALLOC_OPTIONS=debug=0x4e48503,inter=100,log=logfile

export DMALLOC_OPTIONS

把它设置为环境变量,然后运行自己的程序,会在同一个目录下生成一个logfile文件,里面就是内存泄漏的信息。

[/usr/local/app1]## cat logfile 
1488479607: 5932: Dmalloc version '5.5.2' from 'http://dmalloc.com/'
1488479607: 5932: flags = 0x4000503, logfile 'logfile'
1488479607: 5932: interval = 0, addr = 0, seen # = 0, limit = 0
1488479607: 5932: threads enabled, lock-on = 0, lock-init = 2
1488479607: 5932: starting time = 1488479606
1488479607: 5932: process pid = 1740
1488479607: 5932: WARNING: tried to free(0) from 'ra=0xb6a4e6d7'
1488479607: 5936: WARNING: tried to free(0) from 'ra=0xb6a4eb15'
1488479607: 5937: WARNING: tried to free(0) from 'ra=0xb6a4eb1d'
1488479607: 5944: WARNING: tried to free(0) from 'ra=0xb6a0f0bf'
1488479607: 5945: WARNING: tried to free(0) from 'ra=0xb6a0f071'
1488479607: 6040: WARNING: tried to free(0) from 'ra=0xb6a0f0bf'
1488479607: 6041: WARNING: tried to free(0) from 'ra=0xb6a0f071'
1488479607: 6052: WARNING: tried to free(0) from 'ra=0xb6a0f0bf'
1488479607: 6053: WARNING: tried to free(0) from 'ra=0xb6a0f071'
1488479607: 6066: WARNING: tried to free(0) from 'ra=0xb6a0f0bf'
1488479607: 6067: WARNING: tried to free(0) from 'ra=0xb6a0f071'
1488479607: 6076: WARNING: tried to free(0) from 'ra=0xb6a0f0bf'
1488479607: 6077: WARNING: tried to free(0) from 'ra=0xb6a0f071'
1488479607: 6164: WARNING: tried to free(0) from 'ra=0xb6a0f0bf'
1488479607: 6165: WARNING: tried to free(0) from 'ra=0xb6a0f071'
1488479608: 6773: WARNING: tried to free(0) from 'ra=0xb6a0f0bf'
1488479608: 6774: WARNING: tried to free(0) from 'ra=0xb6a0f071'
1488479608: 6783: WARNING: tried to free(0) from 'ra=0xb6a0f0bf'

1488479621: 9438: WARNING: tried to free(0) from 'ra=0xb6a0f071'
1488479621: 9439: WARNING: tried to free(0) from 'ra=0xb6a0f0bf'
1488479621: 9440: WARNING: tried to free(0) from 'ra=0xb6a0f071'
1488479621: 9440: Dumping Chunk Statistics:
1488479621: 9440: basic-block 4096 bytes, alignment 8 bytes
1488479621: 9440: heap address range: 0xa742a000 to 0xb69e6000, 257671168 bytes
1488479621: 9440:     user blocks: 6808 blocks, 27601086 bytes (98%)
1488479621: 9440:    admin blocks: 27 blocks, 110592 bytes (0%)
1488479621: 9440:    total blocks: 6835 blocks, 27996160 bytes
1488479621: 9440: heap checked 0
1488479621: 9440: alloc calls: malloc 4264, calloc 621, realloc 133, free 4422
1488479621: 9440: alloc calls: recalloc 0, memalign 0, valloc 0
1488479621: 9440: alloc calls: new 0, delete 0
1488479621: 9440:   current memory in use: 26596574 bytes (1409 pnts)
1488479621: 9440:  total memory allocated: 28508463 bytes (5018 pnts)
1488479621: 9440:  max in use at one time: 26729359 bytes (1584 pnts)
1488479621: 9440: max alloced with 1 call: 8345216 bytes
1488479621: 9440: max unused memory space: 303713 bytes (1%)
1488479621: 9440: top 10 allocations:

4、提示


dmalloc支持两种使用方式。

一种是调试程序只进行dmalloc静态库的编译链接(上面介绍的就是这种方式),这种方式的好处是对于代码量比较大的现有程序,无需要关心每个.c文件的编译细节,只需要将dmalloc静态库放在程序依赖的最后面即可。但缺点是针对不同CPU体系需要了解基本的函数返回地址汇编实现,同时即使得到了泄露源的地址变量,但也需要借助gdb、objdump或map文件等手段得到该泄露源的真正文件/行号或函数范围。



还有一种方式除了使用dmalloc静态库编译链接外,还需要在每个要调试的.c程序文件中加入dmalloc.h文件的引用,这种方式的原理是使用dmalloc.h头文件的宏定义将需要调试的.c程序文件中的malloc替换为dmalloc_malloc,这种方式的好处和上面方式正好相反,不需要了解不同CPU体系的汇编,不需要借助gdb等工具,因为编译宏的替换可以直接使用__FILE__、__LINE__宏,所以得到的调试信息直接是可以看到的文件名/行号。但缺点也很明显,需要保证所有需要调试的.c程序文件,都要引用了dmalloc.h头文件。

如:

[/usr/local/app1]##  cat logfile | grep pointer
1488476724: 10554:   error details: checking pointer admin
1488476724: 10554:   pointer '0xa5bbb008' from './src/seeker_sync.c:664' prev access './src/seeker_sync.c:606'
1488476731: 11487:   error details: checking pointer admin
1488476731: 11487:   pointer '0xa5bad008' from './src/seeker_sync.c:478' prev access './src/seeker_sync.c:427'

这样子生成的log里面直接打印有内存泄漏的文件名字和代码行

你可能感兴趣的:(嵌入式,Linux,内存泄露)