diff --git a/src/base/logging.hh b/src/base/logging.hh index f00fa201a9..f2d28228fd 100644 --- a/src/base/logging.hh +++ b/src/base/logging.hh @@ -309,6 +309,24 @@ class Logger } while (0) /** @} */ // end of api_logger -} // namespace gem5 +/** + * The assert macro will function like a normal assert, but will use panic + * instead of straight abort(). This allows to perform some cleaning up in + * ExitLogger::exit() before calling abort(). This macro will not check its + * condition in fast builds, but it must still be valid code. + * + * @param cond Condition that is checked; if false -> panic + * + * \def gem5_assert(cond) + * + * @ingroup api_logger + */ +#define gem5_assert(cond) \ + do { \ + if (GEM5_UNLIKELY(!NDEBUG_DEFINED && !static_cast(cond))) \ + panic("assert(" # cond ") failed"); \ + } while (0) +/** @} */ // end of api_logger +} // namespace gem5 #endif // __BASE_LOGGING_HH__ diff --git a/src/base/logging.test.cc b/src/base/logging.test.cc index 31b363d6ec..da8068232e 100644 --- a/src/base/logging.test.cc +++ b/src/base/logging.test.cc @@ -554,3 +554,15 @@ TEST(LoggingDeathTest, ChattyAssert) ASSERT_DEATH(chatty_assert(false, "message\n"), ::testing::HasSubstr( "panic: assert(false) failed: message\nMemory Usage:")); } + +/** Test macro gem5_assert. */ +TEST(LoggingDeathTest, gem5Assert) +{ +#ifdef NDEBUG + GTEST_SKIP() << "Skipping as assertions are " + "stripped out of fast builds"; +#endif + gem5_assert(true); + ASSERT_DEATH(gem5_assert(false), ::testing::HasSubstr( + "panic: assert(false) failed\nMemory Usage:")); +}