有没有办法在运行时识别可执行文件正在valgrind中运行?我有一组C++单元测试,其中一个期望std::vector::reserve
抛出std::bad_alloc
.当我在valgrind下运行它时,它完全挽救,阻止我测试内存泄漏(使用valgrind)和行为(期望抛出异常).
这是一个重现它的最小例子:
#includeint main() { size_t uint_max = static_cast (-1); std::vector 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>::reserve(unsigned) new_allocator.h:92) by 0x804874D: main (vg.cxx:6)
我想修改我的单元测试,以便在从valgrind中运行时简单地跳过有问题的代码.这可能吗?
您应该从Valgrind手册中查看此页面,它包含一个RUNNING_ON_VALGRIND
宏(包含在valgrind.h中),可以满足您的需要.