diff options
author | Andreas Nilsson <andreas.nilsson@mongodb.com> | 2016-07-07 09:02:09 -0400 |
---|---|---|
committer | Andreas Nilsson <andreas.nilsson@mongodb.com> | 2016-07-07 16:39:52 -0400 |
commit | 744750da3a7173def0c37025e6d21f2c358363b5 (patch) | |
tree | 5650b08801e2ad92ff197e265608c5e23e7ef041 /src/mongo/util | |
parent | 5aa7c8a0b64bcd7e6781d335e6c786483fadee8e (diff) | |
download | mongo-744750da3a7173def0c37025e6d21f2c358363b5.tar.gz |
SERVER-24523 Include file and line numbers in assertions
Diffstat (limited to 'src/mongo/util')
-rw-r--r-- | src/mongo/util/assert_util.cpp | 51 | ||||
-rw-r--r-- | src/mongo/util/assert_util.h | 134 |
2 files changed, 129 insertions, 56 deletions
diff --git a/src/mongo/util/assert_util.cpp b/src/mongo/util/assert_util.cpp index 99b4ec6f5a0..0148969aaff 100644 --- a/src/mongo/util/assert_util.cpp +++ b/src/mongo/util/assert_util.cpp @@ -165,39 +165,42 @@ NOINLINE_DECL void invariantOKFailed(const char* expr, std::abort(); } -NOINLINE_DECL void fassertFailed(int msgid) noexcept { - log() << "Fatal Assertion " << msgid << endl; +NOINLINE_DECL void fassertFailedWithLocation(int msgid, const char* file, unsigned line) noexcept { + log() << "Fatal Assertion " << msgid << " at " << file << " " << dec << line; breakpoint(); log() << "\n\n***aborting after fassert() failure\n\n" << endl; std::abort(); } -NOINLINE_DECL void fassertFailedNoTrace(int msgid) noexcept { - log() << "Fatal Assertion " << msgid << endl; +NOINLINE_DECL void fassertFailedNoTraceWithLocation(int msgid, + const char* file, + unsigned line) noexcept { + log() << "Fatal Assertion " << msgid << " at " << file << " " << dec << line; breakpoint(); log() << "\n\n***aborting after fassert() failure\n\n" << endl; quickExit(EXIT_ABRUPT); } -MONGO_COMPILER_NORETURN void fassertFailedWithStatus(int msgid, const Status& status) noexcept { - log() << "Fatal assertion " << msgid << " " << status; +MONGO_COMPILER_NORETURN void fassertFailedWithStatusWithLocation(int msgid, + const Status& status, + const char* file, + unsigned line) noexcept { + log() << "Fatal assertion " << msgid << " " << status << " at " << file << " " << dec << line; breakpoint(); log() << "\n\n***aborting after fassert() failure\n\n" << endl; std::abort(); } -MONGO_COMPILER_NORETURN void fassertFailedWithStatusNoTrace(int msgid, - const Status& status) noexcept { - log() << "Fatal assertion " << msgid << " " << status; +MONGO_COMPILER_NORETURN void fassertFailedWithStatusNoTraceWithLocation(int msgid, + const Status& status, + const char* file, + unsigned line) noexcept { + log() << "Fatal assertion " << msgid << " " << status << " at " << file << " " << dec << line; breakpoint(); log() << "\n\n***aborting after fassert() failure\n\n" << endl; quickExit(EXIT_ABRUPT); } -void uasserted(int msgid, const string& msg) { - uasserted(msgid, msg.c_str()); -} - void UserException::appendPrefix(stringstream& ss) const { ss << "userassert:"; } @@ -205,19 +208,29 @@ void MsgAssertionException::appendPrefix(stringstream& ss) const { ss << "massert:"; } -NOINLINE_DECL void uasserted(int msgid, const char* msg) { +void uassertedWithLocation(int msgid, const string& msg, const char* file, unsigned line) { + uassertedWithLocation(msgid, msg.c_str(), file, line); +} + +NOINLINE_DECL void uassertedWithLocation(int msgid, + const char* msg, + const char* file, + unsigned line) { assertionCount.condrollover(++assertionCount.user); - LOG(1) << "User Assertion: " << msgid << ":" << msg << endl; + log() << "User Assertion: " << msgid << ":" << msg << ' ' << file << ' ' << dec << line << endl; throw UserException(msgid, msg); } -void msgasserted(int msgid, const string& msg) { - msgasserted(msgid, msg.c_str()); +void msgassertedWithLocation(int msgid, const string& msg, const char* file, unsigned line) { + msgassertedWithLocation(msgid, msg.c_str(), file, line); } -NOINLINE_DECL void msgasserted(int msgid, const char* msg) { +NOINLINE_DECL void msgassertedWithLocation(int msgid, + const char* msg, + const char* file, + unsigned line) { assertionCount.condrollover(++assertionCount.warning); - log() << "Assertion: " << msgid << ":" << msg << endl; + log() << "Assertion: " << msgid << ":" << msg << ' ' << file << ' ' << dec << line << endl; logContext(); throw MsgAssertionException(msgid, msg); } diff --git a/src/mongo/util/assert_util.h b/src/mongo/util/assert_util.h index 3e6afbb5eaa..bb11227db1a 100644 --- a/src/mongo/util/assert_util.h +++ b/src/mongo/util/assert_util.h @@ -179,17 +179,47 @@ MONGO_COMPILER_NORETURN void invariantOKFailed(const char* expr, const char* file, unsigned line) noexcept; void wasserted(const char* expr, const char* file, unsigned line); -MONGO_COMPILER_NORETURN void fassertFailed(int msgid) noexcept; -MONGO_COMPILER_NORETURN void fassertFailedNoTrace(int msgid) noexcept; -MONGO_COMPILER_NORETURN void fassertFailedWithStatus(int msgid, const Status& status) noexcept; -MONGO_COMPILER_NORETURN void fassertFailedWithStatusNoTrace(int msgid, - const Status& status) noexcept; + +#define fassertFailed MONGO_fassertFailed +#define MONGO_fassertFailed(...) ::mongo::fassertFailedWithLocation(__VA_ARGS__, __FILE__, __LINE__) +MONGO_COMPILER_NORETURN void fassertFailedWithLocation(int msgid, + const char* file, + unsigned line) noexcept; + +#define fassertFailedNoTrace MONGO_fassertFailedNoTrace +#define MONGO_fassertFailedNoTrace(...) \ + ::mongo::fassertFailedNoTraceWithLocation(__VA_ARGS__, __FILE__, __LINE__) +MONGO_COMPILER_NORETURN void fassertFailedNoTraceWithLocation(int msgid, + const char* file, + unsigned line) noexcept; + +#define fassertFailedWithStatus MONGO_fassertFailedWithStatus +#define MONGO_fassertFailedWithStatus(...) \ + ::mongo::fassertFailedWithStatusWithLocation(__VA_ARGS__, __FILE__, __LINE__) +MONGO_COMPILER_NORETURN void fassertFailedWithStatusWithLocation(int msgid, + const Status& status, + const char* file, + unsigned line) noexcept; + +#define fassertFailedWithStatusNoTrace MONGO_fassertFailedWithStatusNoTrace +#define MONGO_fassertFailedWithStatusNoTrace(...) \ + ::mongo::fassertFailedWithStatusNoTraceWithLocation(__VA_ARGS__, __FILE__, __LINE__) +MONGO_COMPILER_NORETURN void fassertFailedWithStatusNoTraceWithLocation(int msgid, + const Status& status, + const char* file, + unsigned line) noexcept; /** a "user assertion". throws UserAssertion. logs. typically used for errors that a user could cause, such as duplicate key, disk full, etc. */ -MONGO_COMPILER_NORETURN void uasserted(int msgid, const char* msg); -MONGO_COMPILER_NORETURN void uasserted(int msgid, const std::string& msg); +MONGO_COMPILER_NORETURN void uassertedWithLocation(int msgid, + const char* msg, + const char* file, + unsigned line); +MONGO_COMPILER_NORETURN void uassertedWithLocation(int msgid, + const std::string& msg, + const char* file, + unsigned line); /** msgassert and massert are for errors that are internal but have a well defined error text std::string. a stack trace is logged. @@ -197,8 +227,14 @@ MONGO_COMPILER_NORETURN void uasserted(int msgid, const std::string& msg); MONGO_COMPILER_NORETURN void msgassertedNoTrace(int msgid, const char* msg); MONGO_COMPILER_NORETURN void msgassertedNoTrace(int msgid, const std::string& msg); MONGO_COMPILER_NORETURN void msgassertedNoTraceWithStatus(int msgid, const Status& status); -MONGO_COMPILER_NORETURN void msgasserted(int msgid, const char* msg); -MONGO_COMPILER_NORETURN void msgasserted(int msgid, const std::string& msg); +MONGO_COMPILER_NORETURN void msgassertedWithLocation(int msgid, + const char* msg, + const char* file, + unsigned line); +MONGO_COMPILER_NORETURN void msgassertedWithLocation(int msgid, + const std::string& msg, + const char* file, + unsigned line); /* convert various types of exceptions to strings */ std::string causedBy(const char* e); @@ -208,21 +244,31 @@ std::string causedBy(const std::string& e); std::string causedBy(const std::string* e); std::string causedBy(const Status& e); +#define fassert MONGO_fassert +#define MONGO_fassert(...) ::mongo::fassertWithLocation(__VA_ARGS__, __FILE__, __LINE__) + /** aborts on condition failure */ -inline void fassert(int msgid, bool testOK) { - if (MONGO_unlikely(!testOK)) - fassertFailed(msgid); +inline void fassertWithLocation(int msgid, bool testOK, const char* file, unsigned line) { + if (MONGO_unlikely(!testOK)) { + fassertFailedWithLocation(msgid, file, line); + } } -inline void fassert(int msgid, const Status& status) { +inline void fassertWithLocation(int msgid, const Status& status, const char* file, unsigned line) { if (MONGO_unlikely(!status.isOK())) { - fassertFailedWithStatus(msgid, status); + fassertFailedWithStatusWithLocation(msgid, status, file, line); } } -inline void fassertNoTrace(int msgid, const Status& status) { +#define fassertNoTrace MONGO_fassertNoTrace +#define MONGO_fassertNoTrace(...) \ + ::mongo::fassertNoTraceWithLocation(__VA_ARGS__, __FILE__, __LINE__) +inline void fassertNoTraceWithLocation(int msgid, + const Status& status, + const char* file, + unsigned line) { if (MONGO_unlikely(!status.isOK())) { - fassertFailedWithStatusNoTrace(msgid, status); + fassertNoTraceWithLocation(msgid, status, file, line); } } @@ -235,23 +281,32 @@ inline void fassertNoTrace(int msgid, const Status& status) { * MONGO_COMPILER_UNREACHABLE as it is impossible to mark a lambda noreturn. */ #define uassert MONGO_uassert -#define MONGO_uassert(msgid, msg, expr) \ - do { \ - if (MONGO_unlikely(!(expr))) { \ - [&]() MONGO_COMPILER_COLD_FUNCTION { ::mongo::uasserted(msgid, msg); }(); \ - MONGO_COMPILER_UNREACHABLE; \ - } \ +#define MONGO_uassert(msgid, msg, expr) \ + do { \ + if (MONGO_unlikely(!(expr))) { \ + [&]() MONGO_COMPILER_COLD_FUNCTION { \ + ::mongo::uassertedWithLocation(msgid, msg, __FILE__, __LINE__); \ + }(); \ + MONGO_COMPILER_UNREACHABLE; \ + } \ } while (false) -inline void uassertStatusOK(const Status& status) { - uassert((status.location() != 0 ? status.location() : status.code()), - status.reason(), - status.isOK()); +#define uasserted MONGO_uasserted +#define MONGO_uasserted(...) ::mongo::uassertedWithLocation(__VA_ARGS__, __FILE__, __LINE__) + +#define uassertStatusOK MONGO_uassertStatusOK +#define MONGO_uassertStatusOK(...) \ + ::mongo::uassertStatusOKWithLocation(__VA_ARGS__, __FILE__, __LINE__) + +inline void uassertStatusOKWithLocation(const Status& status, const char* file, unsigned line) { + if (MONGO_unlikely(!status.isOK())) { + uasserted((status.location() != 0 ? status.location() : status.code()), status.reason()); + } } template <typename T> -inline T uassertStatusOK(StatusWith<T> sw) { - uassertStatusOK(sw.getStatus()); +inline T uassertStatusOKWithLocation(StatusWith<T> sw, const char* file, unsigned line) { + uassertStatusOKWithLocation(sw.getStatus(), file, line); return std::move(sw.getValue()); } @@ -284,18 +339,23 @@ inline void fassertStatusOK(int msgid, const Status& s) { display happening. */ #define massert MONGO_massert -#define MONGO_massert(msgid, msg, expr) \ - do { \ - if (MONGO_unlikely(!(expr))) { \ - [&]() MONGO_COMPILER_COLD_FUNCTION { ::mongo::msgasserted(msgid, msg); }(); \ - MONGO_COMPILER_UNREACHABLE; \ - } \ +#define MONGO_massert(msgid, msg, expr) \ + do { \ + if (MONGO_unlikely(!(expr))) { \ + [&]() MONGO_COMPILER_COLD_FUNCTION { \ + ::mongo::msgassertedWithLocation(msgid, msg, __FILE__, __LINE__); \ + }(); \ + MONGO_COMPILER_UNREACHABLE; \ + } \ } while (false) +#define msgasserted MONGO_msgasserted +#define MONGO_msgasserted(...) ::mongo::msgassertedWithLocation(__VA_ARGS__, __FILE__, __LINE__) + inline void massertStatusOK(const Status& status) { - massert((status.location() != 0 ? status.location() : status.code()), - status.reason(), - status.isOK()); + if (MONGO_unlikely(!status.isOK())) { + msgasserted((status.location() != 0 ? status.location() : status.code()), status.reason()); + } } inline void massertNoTraceStatusOK(const Status& status) { |