diff --git a/kernel/src/arch/Serial.cpp b/kernel/src/arch/Serial.cpp index db860f2c..9a00a751 100644 --- a/kernel/src/arch/Serial.cpp +++ b/kernel/src/arch/Serial.cpp @@ -1,4 +1,5 @@ #include "arch/Serial.h" +#include "arch/CPU.h" #include namespace Serial @@ -33,4 +34,16 @@ namespace Serial va_end(ap); return rc; } +} + +static bool g_check_already_failed = false; + +_noreturn bool __check_failed(const char* file, const char* line, const char* func, const char* expr) +{ + if (!g_check_already_failed) + { // Avoid endlessly failing when trying to report a failed check. + g_check_already_failed = true; + Serial::printf("ERROR: Check failed at %s:%s, in %s: %s\n", file, line, func, expr); + } + CPU::efficient_halt(); } \ No newline at end of file diff --git a/luna/CMakeLists.txt b/luna/CMakeLists.txt index bfb5d3f7..54f2f78d 100644 --- a/luna/CMakeLists.txt +++ b/luna/CMakeLists.txt @@ -6,6 +6,7 @@ set(FREESTANDING_SOURCES set(SOURCES ${FREESTANDING_SOURCES} + Check.cpp ) add_library(luna-freestanding ${FREESTANDING_SOURCES}) diff --git a/luna/Check.cpp b/luna/Check.cpp new file mode 100644 index 00000000..e464ca86 --- /dev/null +++ b/luna/Check.cpp @@ -0,0 +1,6 @@ +#include + +_weak _noreturn bool __check_failed(const char*, const char*, const char*, const char*) +{ + __builtin_trap(); +} \ No newline at end of file diff --git a/luna/Check.h b/luna/Check.h index f8c4fd13..1e24fb66 100644 --- a/luna/Check.h +++ b/luna/Check.h @@ -1,22 +1,9 @@ #pragma once -#ifdef IN_MOON -#include "arch/Serial.h" +#include -[[noreturn]] inline bool __check_failed(const char* string) -{ - Serial::print("CHECK FAILED: "); - Serial::println(string); - for (;;) - ; -} -#else -[[noreturn]] inline bool __check_failed(const char*) -{ - __builtin_trap(); -} -#endif +extern _noreturn bool __check_failed(const char* file, const char* line, const char* func, const char* expr); #define STRINGIZE(x) #x #define STRINGIZE_VALUE_OF(x) STRINGIZE(x) -#define check(expr) (expr) || __check_failed("at " __FILE__ ":" STRINGIZE_VALUE_OF(__LINE__) ": " #expr) \ No newline at end of file +#define check(expr) (expr) || __check_failed(__FILE__, STRINGIZE_VALUE_OF(__LINE__), __PRETTY_FUNCTION__, #expr)