有没有一种方法可以在运行时识别正在从valgrind运行的可执行文件?我有一组C++单元测试,其中一个预期std::vector::reserve
抛出std::bad_alloc
。当我在valgrind下运行它时,它完全释放,阻止我测试内存泄漏(使用valgrind)和行为(期待异常被抛出)。如何检测程序是否在valgrind中运行?
这里的再现成为一个小例子:
#include <vector>
int main()
{
size_t uint_max = static_cast<size_t>(-1);
std::vector<char> v;
v.reserve(uint_max);
}
运行Valgrind的,我得到这样的输出:
Warning: silly arg (-1) to __builtin_new()
new/new[] failed and should throw an exception, but Valgrind
cannot throw exceptions and so is aborting instead. Sorry.
at 0x40192BC: VALGRIND_PRINTF_BACKTRACE (valgrind.h:319)
by 0x401C823: operator new(unsigned) (vg_replace_malloc.c:164)
by 0x80487BF: std::vector<char, std::allocator<char> >::reserve(unsigned) new_allocator.h:92)
by 0x804874D: main (vg.cxx:6)
我想修改我的单元测试来简单地跳过有问题的代码时它是从valgrind中运行的。这可能吗?
感谢这一点,我一直在使用*研磨多年,但几乎没有触及界面。在autoconf中包含这个宏很容易,当valgrind/valgrind.h存在时,它会在valrgind下运行时自动打开我的调试printfs()和断言:) 再次感谢!很有帮助 – 2009-01-09 07:40:14