Is there a way to identify at run-time of an executable is being run from within valgrind? I have a set of C++ unit tests, and one of them expects std::vector::reserve
to throw std::bad_alloc
. When I run this under valgrind, it bails out completely, preventing me from testing for both memory leaks (using valgrind) and behavior (expecting the exception to be thrown).
有没有办法在运行时识别可执行文件从valgrind中运行?我有一组C ++单元测试,其中一个期望std :: vector :: reserve抛出std :: bad_alloc。当我在valgrind下运行它时,它完全挽救,阻止我测试内存泄漏(使用valgrind)和行为(期望抛出异常)。
Here's a minimal example that reproduces it:
这是一个重现它的最小例子:
#include <vector>
int main()
{
size_t uint_max = static_cast<size_t>(-1);
std::vector<char> v;
v.reserve(uint_max);
}
Running valgrind, I get this output:
运行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)
I'd like to modify my unit test to simply skip the offending code when it's being run from within valgrind. Is this possible?
我想修改我的单元测试,以便在从valgrind中运行时简单地跳过有问题的代码。这可能吗?
2 个解决方案
#1
17
You should look at this page from the Valgrind manual, it contains a RUNNING_ON_VALGRIND
macro (included from valgrind.h) which does what you want.
您应该从Valgrind手册中查看此页面,它包含一个RUNNING_ON_VALGRIND宏(包含在valgrind.h中),它可以满足您的需要。
#2
0
I looked at the valgrind doucmentation and didn't find an easy answer. But here are a couple of things you can try:
我看着valgrind doucmentation,并没有找到一个简单的答案。但是您可以尝试以下几种方法:
-
Write your own wrapper around the offending new operation and raise the exception before valgrind gets its private new function going.
在有问题的新操作周围编写自己的包装器,并在valgrind获取其私有新函数之前引发异常。
-
Try as poster above suggested except that instead of a command-line option (which requires plumbing) use an environment variable:
尝试上面建议的海报,除了代替命令行选项(需要管道)使用环境变量:
MYAPP_UNIT_TESTS_DISABLED="NEW_MINUS_ONE,FLY_TO_MOON,DEREF_NULL" valgrind myapp
Then you can easily write a function
然后你可以轻松编写一个函数
bool unit_test_enabled(const char *testname);
to protect your unit test based on the value returned by getenv(3).
根据getenv(3)返回的值保护您的单元测试。
#1
17
You should look at this page from the Valgrind manual, it contains a RUNNING_ON_VALGRIND
macro (included from valgrind.h) which does what you want.
您应该从Valgrind手册中查看此页面,它包含一个RUNNING_ON_VALGRIND宏(包含在valgrind.h中),它可以满足您的需要。
#2
0
I looked at the valgrind doucmentation and didn't find an easy answer. But here are a couple of things you can try:
我看着valgrind doucmentation,并没有找到一个简单的答案。但是您可以尝试以下几种方法:
-
Write your own wrapper around the offending new operation and raise the exception before valgrind gets its private new function going.
在有问题的新操作周围编写自己的包装器,并在valgrind获取其私有新函数之前引发异常。
-
Try as poster above suggested except that instead of a command-line option (which requires plumbing) use an environment variable:
尝试上面建议的海报,除了代替命令行选项(需要管道)使用环境变量:
MYAPP_UNIT_TESTS_DISABLED="NEW_MINUS_ONE,FLY_TO_MOON,DEREF_NULL" valgrind myapp
Then you can easily write a function
然后你可以轻松编写一个函数
bool unit_test_enabled(const char *testname);
to protect your unit test based on the value returned by getenv(3).
根据getenv(3)返回的值保护您的单元测试。