为什么要进行Google Test segfaulting? [英] Why is Google Test segfaulting?

查看:123
本文介绍了为什么要进行Google Test segfaulting?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Google Test的新手,并且正在尝试提供的示例。我的问题是,当我引入故障并设置 GTEST_BREAK_ON_FAILURE = 1 (或使用命令行选项)时,GTest会出现段错误。

I'm new to Google Test and I'm playing around with the provided examples. My issue is, when I introduce a failure and set GTEST_BREAK_ON_FAILURE=1 (or use the command line option), GTest will segfault.

我正在考虑此示例。如果我在任何测试中插入类似的内容,就会开始出现段错误:

I am considering this example. If I insert something like this into any of the tests, I will start to get the segfault:

EXPECT_EQ(8,2 * 3);

仅重申一下,只有当我还设置了 GTEST_BREAK_ON_FAILURE = 1 时, 。我已经从命令行以及gdb运行了。如果未设置该环境变量,它将报告错误,但不会出现段错误。

Just to reiterate, that is only when I have also set GTEST_BREAK_ON_FAILURE=1. I have run from the command line and also with gdb. If that environment variable is not set, it reports the error but does not segfault.

有什么线索可能导致此问题/我在做什么错?我一直在寻找类似的问题,但是我还没有遇到任何问题。

Any clue to what could be causing this/what I am doing wrong? I've been looking for a similar issue, but I haven't run into anything yet.

仅供参考,我正在使用在64位CrunchBang上运行的Google Test 1.7.0版Linux 11 Waldorf。

FYI I am using Google Test version 1.7.0 running on 64 bit CrunchBang Linux 11 "Waldorf".

编辑代码示例:

// Tests factorial of positive numbers.
TEST(FactorialTest, Positive) {
  EXPECT_EQ(1, Factorial(1));
  EXPECT_EQ(2, Factorial(2));
  EXPECT_EQ(6, Factorial(3));
  EXPECT_EQ(40320, Factorial(8));
}

调试器输出:

(gdb) run
Starting program: /home/yourfavoriteprotein/bin/cpp_unit_test_frameworks/gtest-1.7.0/samples/mytest 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Running main() from test_main.cc
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative (0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
sample1_unittest.cc:112: Failure
Value of: 2*3
  Actual: 6
Expected: 8

Program received signal SIGSEGV, Segmentation fault.
0x0000000000413427 in testing::UnitTest::AddTestPartResult(testing::TestPartResult::Type,         char const*, int, std::string const&, std::string const&) ()
(gdb) quit


推荐答案

GTEST_BREAK_ON_FAILURE = 1 表示Google测试将您放入调试器如果测试失败。

GTEST_BREAK_ON_FAILURE=1 means that Google Test drops you into the debugger if a test fails.

碰巧,将您带到调试器中的一种简单,可移植的方式是触发段错误。

It so happens that an easy, portable way to drop you into the debugger is to trigger a segfault.

换句话说,这种行为是设计使然; Google Test故意触发段错误以使调试器运行。 (请参阅Google测试代码中的此处。 )

In other words, this behavior is by design; Google Test deliberately triggers a segfault to make the debugger run. (See here in the Google Test code.)

这篇关于为什么要进行Google Test segfaulting?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆