如何检测程序是否在valgrind内部运行?

问题描述 投票:11回答:3

是否有一种方法可以在valgrind内部运行可执行文件时进行标识?我有一组C ++单元测试,其中一个期望std::vector::reserve抛出std::bad_alloc。当我在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内部运行时仅跳过有问题的代码。这可能吗?

是否有一种方法可以在valgrind内部运行可执行文件时进行标识?我有一组C ++单元测试,其中一个期望std :: vector :: reserve抛出std :: bad_alloc。当...

c++ unit-testing valgrind
3个回答
21
投票

1
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.