解释Valgrind的trace-malloc输出

时间:2012-01-05 23:50:04

标签: c++ c malloc valgrind

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”和“C ++”。)

3 个答案:

答案 0 :(得分:3)

输出似乎是部分输出(或者来自可怕的破坏代码。但是,这似乎是一个简单的perl脚本匹配地址的工作。实际上,使用C ++ 2011的正则表达式甚至C ++应该但是我还没有使用过这些任务。所以,这里有一个简单的(尽管可能相当笨拙)perl脚本从标准输入读取valgrind的输出:

 #!/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) = $4\n";
              }
            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=$size\n";
    $total += $size;
  }

if (0 < $total)
  {
    print "total leak=$total\n";
  }

答案 1 :(得分:1)

昨天的解决方案使用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

答案 2 :(得分:1)

我参加派对有点晚了,但另一个答案没有考虑到memalign。还有其他功能,如valloc,cfree或posix_memalign,但至少在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())