X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=os_win32.cpp;h=587503c41553fa7cd013cf5ad28862fa34f938a7;hb=c9385be0ae27a206be571f342f299c8762627b56;hp=8077bd9a867a79d37bc6284dfa17603839af7940;hpb=df66a909dbd545cac08776da18b166ab847c08e9;p=apitrace diff --git a/os_win32.cpp b/os_win32.cpp index 8077bd9..587503c 100644 --- a/os_win32.cpp +++ b/os_win32.cpp @@ -24,6 +24,8 @@ **************************************************************************/ #include +#include +#include #include #include @@ -90,30 +92,37 @@ GetCurrentDir(char *str, size_t size) void DebugMessage(const char *format, ...) { - char buf[4096]; - - va_list ap; - va_start(ap, format); - fflush(stdout); - vsnprintf(buf, sizeof buf, format, ap); - va_end(ap); - - OutputDebugStringA(buf); - if (!IsDebuggerPresent()) { - fflush(stdout); - fputs(buf, stderr); - fflush(stderr); - } + char buf[4096]; + + va_list ap; + va_start(ap, format); + fflush(stdout); + vsnprintf(buf, sizeof buf, format, ap); + va_end(ap); + + OutputDebugStringA(buf); + + /* + * Also write the message to stderr, when a debugger is not present (to + * avoid duplicate messages in command line debuggers). + */ +#if _WIN32_WINNT > 0x0400 + if (!IsDebuggerPresent()) { + fflush(stdout); + fputs(buf, stderr); + fflush(stderr); + } +#endif } long long GetTime(void) { - static LARGE_INTEGER frequency; - LARGE_INTEGER counter; - if(!frequency.QuadPart) - QueryPerformanceFrequency(&frequency); - QueryPerformanceCounter(&counter); - return counter.QuadPart*1000000LL/frequency.QuadPart; + static LARGE_INTEGER frequency; + LARGE_INTEGER counter; + if (!frequency.QuadPart) + QueryPerformanceFrequency(&frequency); + QueryPerformanceCounter(&counter); + return counter.QuadPart*1000000LL/frequency.QuadPart; } void @@ -126,4 +135,41 @@ Abort(void) #endif } + +static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL; +static void (*gCallback)(void) = NULL; + +static LONG WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo) +{ + if (gCallback) { + gCallback(); + } + + if (prevExceptionFilter) { + return prevExceptionFilter(pExceptionInfo); + } else { + return EXCEPTION_CONTINUE_SEARCH; + } +} + +void +SetExceptionCallback(void (*callback)(void)) +{ + assert(!gCallback); + + if (!gCallback) { + gCallback = callback; + + assert(!prevExceptionFilter); + prevExceptionFilter = SetUnhandledExceptionFilter(UnhandledExceptionFilter); + } +} + +void +ResetExceptionCallback(void) +{ + gCallback = NULL; +} + + } /* namespace OS */