1818#include < memory>
1919#include < atomic>
2020
21- #if defined( WIN32)
21+ #if WIN32
2222#include < windows.h>
2323#include < dbghelp.h>
2424#include < thread>
3030#include < csignal>
3131#endif
3232
33- #if !defined( WIN32)
33+ #if !WIN32
3434#include < dlfcn.h>
35- #if !defined( __APPLE__)
35+ #if !__APPLE__
3636#include < link.h>
3737#endif
3838#endif
3939
40- #if defined( __APPLE__)
40+ #if __APPLE__
4141#include < mach-o/dyld.h>
4242#include < mach/mach_init.h>
4343#include < sys/sysctl.h>
5252
5353std::string DemangleSymbol (const std::string& name)
5454{
55- #if defined( __GNUC__) || defined( __clang__)
55+ #if __GNUC__ || __clang__
5656 int status = -4 ; // some arbitrary value to eliminate the compiler warning
5757 char * str = abi::__cxa_demangle (name.c_str (), nullptr , nullptr , &status);
5858 if (status != 0 ) {
@@ -74,7 +74,7 @@ static std::atomic<bool> skipAbortSignal(false);
7474
7575static ssize_t GetExeFileNameImpl (char * buf, size_t bufSize)
7676{
77- #if defined( WIN32)
77+ #if WIN32
7878 std::vector<TCHAR> tmp (bufSize);
7979 DWORD len = GetModuleFileName (nullptr , tmp.data (), bufSize);
8080 if (len >= bufSize) {
@@ -84,7 +84,7 @@ static ssize_t GetExeFileNameImpl(char* buf, size_t bufSize)
8484 buf[i] = (char )tmp[i];
8585 }
8686 return len;
87- #elif defined( __APPLE__)
87+ #elif __APPLE__
8888 uint32_t bufSize2 = (uint32_t )bufSize;
8989 if (_NSGetExecutablePath (buf, &bufSize2) != 0 ) {
9090 // it's not entirely clear if the value returned by _NSGetExecutablePath includes the null character
@@ -127,7 +127,7 @@ static void my_backtrace_error_callback (void *data, const char *msg,
127127
128128static backtrace_state* GetLibBacktraceState ()
129129{
130- #if defined( WIN32)
130+ #if WIN32
131131 // libbacktrace is not able to handle the DWARF debuglink in the .exe
132132 // but luckily we can just specify the .dbg file here as it's a valid PE/XCOFF file
133133 static std::string debugFileName = g_exeFileName + " .dbg" ;
@@ -140,7 +140,7 @@ static backtrace_state* GetLibBacktraceState()
140140}
141141#endif // ENABLE_STACKTRACES
142142
143- #if defined( WIN32)
143+ #if WIN32
144144static uint64_t GetBaseAddress ()
145145{
146146 return 0 ;
@@ -188,15 +188,15 @@ static __attribute__((noinline)) std::vector<uint64_t> GetStackFrames(size_t ski
188188 STACKFRAME64 stackframe;
189189 ZeroMemory (&stackframe, sizeof (STACKFRAME64));
190190
191- #if defined( __i386__)
191+ #ifdef __i386__
192192 image = IMAGE_FILE_MACHINE_I386;
193193 stackframe.AddrPC .Offset = context.Eip ;
194194 stackframe.AddrPC .Mode = AddrModeFlat;
195195 stackframe.AddrFrame .Offset = context.Ebp ;
196196 stackframe.AddrFrame .Mode = AddrModeFlat;
197197 stackframe.AddrStack .Offset = context.Esp ;
198198 stackframe.AddrStack .Mode = AddrModeFlat;
199- #elif defined( __x86_64__)
199+ #elif __x86_64__
200200 image = IMAGE_FILE_MACHINE_AMD64;
201201 stackframe.AddrPC .Offset = context.Rip ;
202202 stackframe.AddrPC .Mode = AddrModeFlat;
@@ -240,7 +240,7 @@ static __attribute__((noinline)) std::vector<uint64_t> GetStackFrames(size_t ski
240240}
241241#else
242242
243- #if defined( __APPLE__)
243+ #if __APPLE__
244244static uint64_t GetBaseAddress ()
245245{
246246 mach_port_name_t target_task;
@@ -534,11 +534,14 @@ static void PrintCrashInfo(const crash_info& ci)
534534static StdMutex g_stacktraces_mutex;
535535static std::map<void *, std::shared_ptr<std::vector<uint64_t >>> g_stacktraces;
536536
537- #ifdef CRASH_HOOKS_WRAPPED_CXX_ABI
537+ #if CRASH_HOOKS_WRAPPED_CXX_ABI
538538// These come in through -Wl,-wrap
539+ // It only works on GCC
539540extern " C" void * __real___cxa_allocate_exception (size_t thrown_size);
540541extern " C" void __real___cxa_free_exception (void * thrown_exception);
541- #if defined(WIN32)
542+ #if __clang__
543+ #error not supported on WIN32 (no dlsym support)
544+ #elif WIN32
542545extern " C" void __real__assert (const char *assertion, const char *file, unsigned int line);
543546extern " C" void __real__wassert (const wchar_t *assertion, const wchar_t *file, unsigned int line);
544547#else
@@ -557,13 +560,13 @@ extern "C" void __real___cxa_free_exception(void * thrown_exception)
557560 static auto f = (void (*)(void *))dlsym (RTLD_NEXT, " __cxa_free_exception" );
558561 return f (thrown_exception);
559562}
560- #if defined( __clang__) && defined(__APPLE__)
563+ #if __clang__
561564extern " C" void __attribute__ ((noreturn)) __real___assert_rtn(const char *function, const char *file, int line, const char *assertion)
562565{
563566 static auto f = (void (__attribute__ ((noreturn)) *) (const char *, const char *, int , const char *))dlsym (RTLD_NEXT, " __assert_rtn" );
564567 f (function, file, line, assertion);
565568}
566- #elif defined( WIN32)
569+ #elif WIN32
567570#error not supported on WIN32 (no dlsym support)
568571#else
569572extern " C" void __real___assert_fail (const char *assertion, const char *file, unsigned int line, const char *function)
@@ -574,7 +577,7 @@ extern "C" void __real___assert_fail(const char *assertion, const char *file, un
574577#endif
575578#endif
576579
577- #ifdef CRASH_HOOKS_WRAPPED_CXX_ABI
580+ #if CRASH_HOOKS_WRAPPED_CXX_ABI
578581#define WRAPPED_NAME (x ) __wrap_##x
579582#else
580583#define WRAPPED_NAME (x ) x
@@ -622,15 +625,15 @@ static __attribute__((noinline)) crash_info GetCrashInfoFromAssertion(const char
622625 return ci;
623626}
624627
625- #if defined( __clang__) && defined(__APPLE__)
628+ #if __clang__
626629extern " C" void __attribute__ ((noinline)) WRAPPED_NAME(__assert_rtn)(const char *function, const char *file, int line, const char *assertion)
627630{
628631 auto ci = GetCrashInfoFromAssertion (assertion, file, line, function);
629632 PrintCrashInfo (ci);
630633 skipAbortSignal = true ;
631634 __real___assert_rtn (function, file, line, assertion);
632635}
633- #elif defined( WIN32)
636+ #elif WIN32
634637extern " C" void __attribute__ ((noinline)) WRAPPED_NAME(_assert)(const char *assertion, const char *file, unsigned int line)
635638{
636639 auto ci = GetCrashInfoFromAssertion (assertion, file, line, nullptr );
@@ -760,7 +763,7 @@ void RegisterPrettyTerminateHander()
760763 std::set_terminate (terminate_handler);
761764}
762765
763- #if !defined( WIN32)
766+ #if !WIN32
764767static void HandlePosixSignal (int s)
765768{
766769 if (s == SIGABRT && skipAbortSignal) {
@@ -837,7 +840,7 @@ LONG WINAPI HandleWindowsException(EXCEPTION_POINTERS * ExceptionInfo)
837840
838841void RegisterPrettySignalHandlers ()
839842{
840- #if defined( WIN32)
843+ #if WIN32
841844 SetUnhandledExceptionFilter (HandleWindowsException);
842845#else
843846 const std::vector<int > posix_signals = {
@@ -853,7 +856,7 @@ void RegisterPrettySignalHandlers()
853856 SIGTRAP, // Trace/breakpoint trap
854857 SIGXCPU, // CPU time limit exceeded (4.2BSD)
855858 SIGXFSZ, // File size limit exceeded (4.2BSD)
856- #if defined( __APPLE__)
859+ #if __APPLE__
857860 SIGEMT, // emulation instruction executed
858861#endif
859862 };
0 commit comments