分享

使用GDB检测内存泄漏

 心不留意外尘 2016-09-06

http://blog.csdn.net/zoomdy/article/details/51594228

2016


使用GDB在malloc和free函数设置断点,并打印分配和释放时的内存地址以及调用栈,然后通过脚本分析分配和释放是否配对来检测是否存在内存泄露以及发生内存泄漏的位置。

http://blog.csdn.net/zoomdy/article/details/51594228
mingdu.zheng at gmail dot com

malloc和free的包装,为了方便地设置断点。

#include <stdlib>

void _wrapmalloc_print_addr(const void* addr)
{
    (void)addr;
}

void _wrapfree_print_addr(const void* addr)
{
    (void)addr;
}

void *wrapmalloc(size_t size)
{
    void *p;
    p = malloc(size);

    if(p)
    {
        _wrapmalloc_print_addr(p);
    }

    return p;
}

void wrapfree(const void *p)
{
    if(p)
    {
        _wrapfree_print_addr(p);
    }
    free((void*)p);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

自动化脚本,调用GDB,自动输入命令,报告输出到leak.log。

#!/usr/bin/expect -f
set gdb [lindex $argv 0]
set exec [lindex $argv 1]
set target [lindex $argv 2]
set timeout 10000

spawn $gdb $exec -ex $target

expect "(gdb)"
send "monitor reset\r"

expect "(gdb)"
send "load\r"

#expect "(gdb)"
#send "monitor reset\r"

expect "(gdb)"
send "set pagination off\r"

expect "(gdb)"
send "b _wrapmalloc_print_addr\r"

expect "(gdb)"
send "commands\r"
expect ">"
send "silent\r"
expect ">"
send "printf \"malloc,%p\\n\", addr\r"
expect ">"
send "bt 5\r"
expect ">"
send "c\r"
expect ">"
send "end\r"

expect "(gdb)"
send "b _wrapfree_print_addr\r"

expect "(gdb)"
send "commands\r"
expect ">"
send "silent\r"
expect ">"
send "printf \"free,%p\\n\", addr\r"
expect ">"
send "bt 5\r"
expect ">"
send "c\r"
expect ">"
send "end\r"

expect "(gdb)"
send "c\r"

expect eof
exit
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

内存泄漏分析脚本,检测malloc释放有配对的free,如果没有那么打印该malloc的调用栈,通过调用栈可以找到分配内存的位置。

#!/usr/bin/python

path="leak.log"

t = {}
f = file(path)
l = f.readline()
malloc = False
a = ""
while l != "":
    if l.startswith("malloc"):
        malloc = True
        a = l.split(",")[1]
        t[a] = ""
    elif l.startswith("free"):
        malloc = False
        del t[l.split(",")[1]]
    else:
        if malloc:
            t[a] += l

    l = f.readline()

for i in t:
    print i
    print t[i]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多