免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1771 | 回复: 0
打印 上一主题 下一主题

dmalloc使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-22 17:07 |只看该作者 |倒序浏览

dmalloc用法快速入门
Submitted by liuguangzhao on Tue, 2008-07-01 14:43
dmalloc是一个简单易用的C/C++内存leak检查工具,以一个运行库的方式发布。
dmalloc能够检查出直到程序运行结束还没有释放的内存,并且能够精确指出在
哪个源文件的第几行。
dmalloc 主页:
http://dmalloc.com
支持的平台:AIX, BSD/OS, DG/UX, Free/Net/OpenBSD, GNU/Hurd, HPUX, Irix, Linux, MS-DOG, NeXT, OSF, SCO, Solaris, SunOS, Ultrix, Unixware, Windoze, and even Unicos on a Cray T3E
最新版本: 5.5.2
安装:下载
http://dmalloc.com/releases/dmalloc-5.5.2.tgz

tar zxvf dmalloc-5.5.2.tgz
cd dmalloc-5.5.2
./configure
make
make install
设置环境变量:
对于 Bash, ksh, and zsh (
http://www.zsh.org/
), 在 `.bashrc', `.profile', or `.zshrc'
文件中加入一行 ( -b 参数表示针对Bash的输出):
function dmalloc { eval `command dmalloc -b $*`; }
然后重新登陆用户,或者执行: source ~/.bashrc 或 source ~/.profile
接下面执行:
dmalloc -l logfile -i 100 low
在源文件中添加下面的C代码:
#ifdef DMALLOC
#include "dmalloc.h"
#endif
编译使用的CFLAGS: -DDMALLOC -DDMALLOC_FUNC_CHECK
如: gcc -DDMALLOC -DDMALLOC_FUNC_CHECK dm_test.c
执行:
./a.out
运行上面的命令会在当前目录下生成 logfile文件,查看logfile的内容如下:
cat logfile
1214894489: 2: Dmalloc version '5.5.2' from 'http://dmalloc.com/'
1214894489: 2: flags = 0x4e48503, logfile 'logfile'
1214894489: 2: interval = 100, addr = 0, seen # = 0, limit = 0
1214894489: 2: starting time = 1214894489
1214894489: 2: process pid = 9560
1214894489: 2: Dumping Chunk Statistics:
1214894489: 2: basic-block 4096 bytes, alignment 8 bytes
1214894489: 2: heap address range: 0xb8020000 to 0xb8029000, 36864 bytes
1214894489: 2:     user blocks: 1 blocks, 4043 bytes (10%)
1214894489: 2:    admin blocks: 8 blocks, 32768 bytes (89%)
1214894489: 2:    total blocks: 9 blocks, 36864 bytes
1214894489: 2: heap checked 1
1214894489: 2: alloc calls: malloc 2, calloc 0, realloc 0, free 0
1214894489: 2: alloc calls: recalloc 0, memalign 0, valloc 0
1214894489: 2: alloc calls: new 0, delete 0
1214894489: 2:   current memory in use: 11 bytes (2 pnts)
1214894489: 2:  total memory allocated: 11 bytes (2 pnts)
1214894489: 2:  max in use at one time: 11 bytes (2 pnts)
1214894489: 2: max alloced with 1 call: 6 bytes
1214894489: 2: max unused memory space: 53 bytes (82%)
1214894489: 2: top 10 allocations:
1214894489: 2:  total-size  count in-use-size  count  source
1214894489: 2:           6      1           6      1  dm_test.c:71
1214894489: 2:           5      1           5      1  dm_test.c:69
1214894489: 2:          11      2          11      2  Total of 2
1214894489: 2: Dumping Not-Freed Pointers Changed Since Start:
1214894489: 2:  not freed: '0xb8028fc8|s1' (6 bytes) from 'dm_test.c:71'
1214894489: 2:  not freed: '0xb8028fe8|s1' (5 bytes) from 'dm_test.c:69'
1214894489: 2:  total-size  count  source
1214894489: 2:           6      1  dm_test.c:71
1214894489: 2:           5      1  dm_test.c:69
1214894489: 2:          11      2  Total of 2
1214894489: 2: ending time = 1214894489, elapsed since start = 0:00:00
那么,哪个地方的内存leak就一目了然了。
//====== dm_test.c 源代码 =============
#include
#include
#include

#ifdef DMALLOC
#include
#endif

int main(int argc, char **argv)
{
   
    char *str;

    str = malloc(5);

    str = malloc(6);
   
    return 0;
}

[email=gliu@gliu-desktop:~/dmalloc-5.5.2/dmalloc-5.5$]gliu@gliu-desktop:~/dmalloc-5.5.2/dmalloc-5.5$[/email]
dmalloc -b  -l logfile all > tmp
[email=gliu@gliu-desktop:~/dmalloc-5.5.2/dmalloc-5.5$]gliu@gliu-desktop:~/dmalloc-5.5.2/dmalloc-5.5$[/email]
. tmp
[email=gliu@gliu-desktop:~/dmalloc-5.5.2/dmalloc-5.5$]gliu@gliu-desktop:~/dmalloc-5.5.2/dmalloc-5.5$[/email]
./a.out
hello, dmalloc
[email=gliu@gliu-desktop:~/dmalloc-5.5.2/dmalloc-5.5$]gliu@gliu-desktop:~/dmalloc-5.5.2/dmalloc-5.5$[/email]
vi logfile

      1. Download the latest release from
http://dmalloc.com/releases/
.
       2. Untar the package on host machine.
       3. Configure and build using arm-gcc, build result is 4 files: dmalloc, dmalloc.h, libdmalloc.a(Single-thread), libdmallocth.a(Multi-thread)
       4. Copy result file dmalloc to target board /usr/bin dir.
       5. Rebuild your project to link with libdmalloc.a or libdmallocth.a, add header file dmalloc.h to each file that use the functions malloc or calloc or free.
       6. Copy your rebuilt project to target board.
       7. On target board, type command:
              dmalloc –b –l logfile all > tmp
              . tmp
              ./a.out (rebuilt execution file)
      Then you can see dmlloc result in logfile, this file will give your memory leak information, if the log show memory leak by address, you can use arm-addr2line to convert the address to specific line:file.



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/19273/showart_1141492.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP