AuroraRuntime/Source/Debug/Panic.cpp

138 lines
3.3 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Panic.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Debug.hpp"
#include "Panic.hpp"
//#include <intrin.h>
#include <Source/Grug/AuGrug.hpp>
#include <Source/Console/ConsoleTTY/ConsoleTTY.hpp>
#include <Source/Console/ConsoleStd/ConsoleStd.hpp>
#include <Source/Console/Console.hpp>
namespace Aurora::Debug
{
void PlatformHandleFatal();
static bool gHandlingFatal = false;
AUKN_SYM void DebugBreak()
{
#if defined(AU_CFG_ID_INTERNAL) || defined(AU_CFG_ID_DEBUG)
#if defined(AURORA_PLATFORM_WIN32)
#if defined(AU_CFG_ID_INTERNAL)
if (IsDebuggerPresent())
#endif
{
::DebugBreak();
}
#elif defined(AU_CFG_ID_DEBUG) // bc nix
#if defined(AURORA_COMPILER_MSVC)
__debugbreak();
#elif (defined(__has_builtin) && __has_builtin(__builtin_debugtrap))
__builtin_debugtrap();
#elif defined(AURORA_COMPILER_GCC) || defined(AURORA_COMPILER_CLANG)
// GCC is a terrible compiler, msvc is annoying at best, and LLVM + cpp frontend seems great, whats new?
// I refuse to chuck inline assembly here, not here, not in this project, not for freetards.
#endif
#endif
#endif
}
bool InPanic()
{
return gHandlingFatal;
}
AUKN_SYM void Panic2(AuUInt uLineHintInNonshipBinary)
{
// TODO: telemetry insert crashing from line uLineHintInNonshipBinary
Panic();
}
AUKN_SYM void Panic()
{
static bool gRunPlatformFastFailOnce {};
//
if (gHandlingFatal)
{
goto failFast;
}
DebugBreak();
// Edge case: internal builds should always check for a debugger before crashing
#if defined(AU_CFG_ID_INTERNAL) && defined(__has_builtin) && __has_builtin(__builtin_debugtrap)
__builtin_debugtrap();
#endif
//
if (AuExchange(gHandlingFatal, true))
{
goto failFast;
}
if (Aurora::RuntimeHasStarted())
{
try
{
Console::ConsoleTTY::PumpForce();
}
catch (...)
{
}
try
{
Grug::GrugFlushFlushs();
Grug::GrugFlushWrites();
}
catch (...)
{
}
try
{
Console::Pump();
}
catch (...)
{
}
}
else
{
Console::ConsoleStd::Pump();
gRunPlatformFastFailOnce = true;
}
failFast:
if (!AuExchange(gRunPlatformFastFailOnce, true))
{
try
{
Debug::PlatformHandleFatal(true);
}
catch (...)
{
}
}
#if defined(AURORA_IS_MODERNNT_DERIVED)
Win32Terminate();
#else
#if defined(AURORA_COMPILER_GCC) || defined(AURORA_COMPILER_CLANG)
__builtin_trap();
#else
std::terminate();
#endif
#endif
}
}