From d9483ccb087011469052e7ba2ea4aaf912ae2b6b Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Wed, 28 Jul 2021 12:43:02 +0300 Subject: [PATCH] Enable stacktraces for RuntimeAssert, TODO --- .../runtime/src/main/cpp/KAssert.cpp | 15 ++++++-------- kotlin-native/runtime/src/main/cpp/KAssert.h | 20 +++++++++---------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/kotlin-native/runtime/src/main/cpp/KAssert.cpp b/kotlin-native/runtime/src/main/cpp/KAssert.cpp index 3a5348b2bc3..5f7c2235cdd 100644 --- a/kotlin-native/runtime/src/main/cpp/KAssert.cpp +++ b/kotlin-native/runtime/src/main/cpp/KAssert.cpp @@ -12,10 +12,7 @@ using namespace kotlin; namespace { -// TODO: Enable stacktraces for asserts when stacktrace printing is more mature. -inline constexpr bool kEnableStacktraces = false; - -void PrintAssert(const char* location, const char* format, std::va_list args) noexcept { +void PrintAssert(bool allowStacktrace, const char* location, const char* format, std::va_list args) noexcept { char buf[1024]; int written = -1; @@ -33,24 +30,24 @@ void PrintAssert(const char* location, const char* format, std::va_list args) no konan::consoleErrorUtf8(buf, konan::strnlen(buf, sizeof(buf))); konan::consoleErrorf("\n"); - if constexpr (kEnableStacktraces) { + if (allowStacktrace) { kotlin::PrintStackTraceStderr(); } } } // namespace -void internal::RuntimeAssertFailedLog(const char* location, const char* format, ...) { +void internal::RuntimeAssertFailedLog(bool allowStacktrace, const char* location, const char* format, ...) { std::va_list args; va_start(args, format); - PrintAssert(location, format, args); + PrintAssert(allowStacktrace, location, format, args); va_end(args); } -RUNTIME_NORETURN void internal::RuntimeAssertFailedPanic(const char* location, const char* format, ...) { +RUNTIME_NORETURN void internal::RuntimeAssertFailedPanic(bool allowStacktrace, const char* location, const char* format, ...) { std::va_list args; va_start(args, format); - PrintAssert(location, format, args); + PrintAssert(allowStacktrace, location, format, args); va_end(args); konan::abort(); } diff --git a/kotlin-native/runtime/src/main/cpp/KAssert.h b/kotlin-native/runtime/src/main/cpp/KAssert.h index 4809174321e..9da933d256d 100644 --- a/kotlin-native/runtime/src/main/cpp/KAssert.h +++ b/kotlin-native/runtime/src/main/cpp/KAssert.h @@ -28,16 +28,16 @@ namespace kotlin { namespace internal { -void RuntimeAssertFailedLog(const char* location, const char* format, ...) __attribute__((format(printf, 2, 3))); -RUNTIME_NORETURN void RuntimeAssertFailedPanic(const char* location, const char* format, ...) __attribute__((format(printf, 2, 3))); +void RuntimeAssertFailedLog(bool allowStacktrace, const char* location, const char* format, ...) __attribute__((format(printf, 3, 4))); +RUNTIME_NORETURN void RuntimeAssertFailedPanic(bool allowStacktrace, const char* location, const char* format, ...) __attribute__((format(printf, 3, 4))); inline RUNTIME_NORETURN void TODOImpl(const char* location) { - RuntimeAssertFailedPanic(location, "Unimplemented"); + RuntimeAssertFailedPanic(true, location, "Unimplemented"); } // TODO: Support format string when `RuntimeAssertFailed` supports it. inline RUNTIME_NORETURN void TODOImpl(const char* location, const char* message) { - RuntimeAssertFailedPanic(location, "%s", message); + RuntimeAssertFailedPanic(true, location, "%s", message); } } // namespace internal @@ -50,12 +50,12 @@ inline RUNTIME_NORETURN void TODOImpl(const char* location, const char* message) case ::kotlin::compiler::RuntimeAssertsMode::kIgnore: break; \ case ::kotlin::compiler::RuntimeAssertsMode::kLog: \ if (!(condition)) { \ - ::kotlin::internal::RuntimeAssertFailedLog(CURRENT_SOURCE_LOCATION, format, ##__VA_ARGS__); \ + ::kotlin::internal::RuntimeAssertFailedLog(true, CURRENT_SOURCE_LOCATION, format, ##__VA_ARGS__); \ } \ break; \ case ::kotlin::compiler::RuntimeAssertsMode::kPanic: \ if (!(condition)) { \ - ::kotlin::internal::RuntimeAssertFailedPanic(CURRENT_SOURCE_LOCATION, format, ##__VA_ARGS__); \ + ::kotlin::internal::RuntimeAssertFailedPanic(true, CURRENT_SOURCE_LOCATION, format, ##__VA_ARGS__); \ } \ break; \ } \ @@ -63,11 +63,11 @@ inline RUNTIME_NORETURN void TODOImpl(const char* location, const char* message) // Use RuntimeCheck() in runtime checks that could fail due to external condition and shall lead // to program termination. Never compiled out. -// TODO: Consider using `CURRENT_SOURCE_LOCATION` when `kotlin::compiler::runtimeAssertsMode()` is not `kIgnore`. +// TODO: Consider using `CURRENT_SOURCE_LOCATION` and stacktraces when `kotlin::compiler::runtimeAssertsMode()` is not `kIgnore`. #define RuntimeCheck(condition, format, ...) \ do { \ if (!(condition)) { \ - ::kotlin::internal::RuntimeAssertFailedPanic(nullptr, format, ##__VA_ARGS__); \ + ::kotlin::internal::RuntimeAssertFailedPanic(false, nullptr, format, ##__VA_ARGS__); \ } \ } while (false) @@ -77,10 +77,10 @@ inline RUNTIME_NORETURN void TODOImpl(const char* location, const char* message) } while (false) // Use RuntimeFail() to unconditionally fail, signifying compiler/runtime bug. -// TODO: Consider using `CURRENT_SOURCE_LOCATION` when `kotlin::compiler::runtimeAssertsMode()` is not `kIgnore`. +// TODO: Consider using `CURRENT_SOURCE_LOCATION` and stacktraces when `kotlin::compiler::runtimeAssertsMode()` is not `kIgnore`. #define RuntimeFail(format, ...) \ do { \ - ::kotlin::internal::RuntimeAssertFailedPanic(nullptr, format, ##__VA_ARGS__); \ + ::kotlin::internal::RuntimeAssertFailedPanic(false, nullptr, format, ##__VA_ARGS__); \ } while (false) #endif // RUNTIME_ASSERT_H