Valgrind是一个出色的内存调试器,它有--trace-malloc=yes
选项,它可以生成这样的东西:
--16301-- malloc(8) = 0x4EAD748
--16301-- free(0x4EAD748)
--16301-- free(0x4EAD498)
--16301-- malloc(21) = 0x4EAD780
--16301-- malloc(8) = 0x4EAD838
--16301-- free(0x4EAD6F8)
--16301-- calloc(1,88) = 0x4EAD870
--16301-- realloc(0x0,160)malloc(160) = 0x4EB1CF8
--16301-- realloc(0x4EB9F28,4) = 0x4EBA060
有没有一个工具可以解析这个输出,并为每个地址告诉它是否没有在匹配的对中正确分配和释放?
GCC与mtrace()
函数和mtrace
命令行工具有相似之处,但格式不同。
额外的问题:是否可以在"肯定丢失"的声明旁边输出实际地址?
(我将这两种最有可能与Valgrind一起使用的语言标记为"C"one_answers"C++"。)
输出似乎是部分输出(或者来自严重损坏的代码)。然而,这似乎是一个简单的perl脚本与地址匹配的任务。实际上,对于C++2011的正则表达式,即使是C++也应该能胜任这项任务,但我还没有使用这些输入:
#!/usr/bin/perl -w
use strict;
my %allocated;
while (<>)
{
chomp;
if (/(realloc(([^,]*),([^)]*))).* = (.*)/)
{
if ($2 ne "0x0")
{
if (!exists $allocated{$2})
{
print "spurious realloc($2, $3) = $4n";
}
else
{
delete $allocated{$2};
}
}
$allocated{$4} = "$1$;$3";
}
elsif (/(malloc((.*))) = (.*)/)
{
$allocated{$3} = "$1$;$2";
}
elsif (/ free((.*))/)
{
if ($1 ne "0x0")
{
if (!exists $allocated{$1})
{
print "spurious free($1)n";
}
else
{
delete $allocated{$1};
}
}
}
elsif (/(calloc((.*),(.*))) = (.*)/)
{
$allocated{$4} = "$1$;" . ($2 * $3);
}
}
my $total = 0;
foreach my $leak (keys %allocated)
{
my($call, $size) = split(/$;/, $allocated{$leak});
print "leak: address=$leak source=$call size=$sizen";
$total += $size;
}
if (0 < $total)
{
print "total leak=$totaln";
}
昨天的解决方案使用perl来分析输出。很明显,作为一名C++程序员,我应该用C++来做这件事。我以前没有使用过std::regex
,需要先了解一下。这里有一个C++解决方案:
#include "boost/regex.hpp"
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <stdexcept>
#include <string>
#include <vector>
namespace re = boost;
long to_long(std::string const& s)
{
return strtol(s.c_str(), 0, 10);
}
template <typename T>
static void insert(T& map, std::string const& address, std::string const& call, size_t size)
{
if (!map.insert(std::make_pair(address, std::make_pair(call, size))).second)
std::cout << "WARNING: duplicate address for " << call << ": " << address << "n";
}
template <typename T>
static void erase(T& map, std::string const& address, std::string const& call)
{
auto it(map.find(address));
if (it == map.end() && address != "0x0")
std::cout << "WARNING: spurious address in " << call << "n";
else
map.erase(it);
}
static void process(std::istream& in)
{
std::map<std::string, std::pair<std::string, size_t>> m;
std::vector<std::pair<re::regex, std::function<void(re::smatch&)>>> exps;
exps.emplace_back(re::regex(".*(malloc\((.*)\)) = (.*)"), [&](re::smatch& results){
::insert(m, results[3], results[1], ::to_long(results[2]));
});
exps.emplace_back(re::regex(".*(free\((.*)\))"), [&](re::smatch& results){
::erase(m, results[2], results[1]);
});
exps.emplace_back(re::regex(".*(calloc\((.*),(.*)\)) = (.*)"), [&](re::smatch& results){
::insert(m, results[4], results[1], ::to_long(results[2]) * ::to_long(results[3]));
});
exps.emplace_back(re::regex(".*(realloc\((.*),(.*)\)) = (.*)"), [&](re::smatch& results){
::erase(m, results[2], results[1]);
::insert(m, results[4], results[1], ::to_long(results[3]));
});
for (std::string line; std::getline(in, line); )
{
re::smatch results;
for (auto it(exps.begin()), end(exps.end()); it != end; ++it)
{
if (re::regex_match(line, results, it->first))
{
(it->second)(results);
break;
}
}
}
size_t total{0};
for (auto it(m.begin()), end(m.end()); it != end; ++it)
{
std::cout << "leaked memory at " << it->first << " " << "from " << it->second.first << "n";
total += it->second.second;
}
std::cout << "total leak: " << total << "n";
}
int main(int, char*[])
{
try
{
::process(std::cin);
}
catch (std::exception const &ex)
{
std::cerr << "ERROR: " << ex.what() << "n";
}
}
因为gcc当前版本的std::regex
似乎有缺陷,所以我使用了Boost的实现。切换版本应该很容易:只需将re
定义为std
的别名,而不是boost
。
我参加聚会有点晚了,但另一个答案没有考虑memalign。还有其他函数,如valloc、cfree或posix_meagn,但至少在linux上它们是别名的。无论如何,这是我的python版本,不能保证。
#!/usr/bin/python
import sys, re
memmap = {}
for line in sys.stdin:
tok = [x for x in re.split(' |(|)|,|=|n', line) if x][1:]
if tok and tok[0] in ['malloc', 'calloc', 'memalign', 'realloc', 'free']:
addr = int(tok[-1], 16)
if tok[0] == 'malloc':
memmap[addr] = int(tok[1])
elif tok[0] == 'calloc':
memmap[addr] = int(tok[1]) * int(tok[2])
elif tok[0] == 'memalign':
memmap[addr] = int(tok[-2])
elif tok[0] == 'realloc':
oldaddr = int(tok[1], 16)
if oldaddr != 0:
del memmap[oldaddr]
memmap[addr] = int(tok[2])
elif tok[0] == 'free' and addr != 0:
del memmap[addr]
for k, v in memmap.iteritems():
print 'leak at 0x%x, %d bytes' % (k, v)
print 'total %d bytes' % sum(memmap.itervalues())