X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=os_win32.cpp;h=4bfdf5e00d96373c1a7e003f42c2dfd521ef935c;hb=1e0fc67981e101495c2e236efb37cb5db83af400;hp=6cddc552681db55863e1eaf01f1f1baac070c2ed;hpb=dbaae4936e6b647dd0157e5e6b37e668e1fad983;p=apitrace diff --git a/os_win32.cpp b/os_win32.cpp index 6cddc55..4bfdf5e 100644 --- a/os_win32.cpp +++ b/os_win32.cpp @@ -24,6 +24,7 @@ **************************************************************************/ #include +#include #include #include @@ -99,11 +100,18 @@ DebugMessage(const char *format, ...) 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) @@ -126,4 +134,51 @@ Abort(void) #endif } + +struct Interrupts +{ + Interrupts() + : set(false), + prevfilter(NULL), + handler(NULL) + {} + + bool set; + LPTOP_LEVEL_EXCEPTION_FILTER prevFilter; + + void (*handler)(int); +}; +static Interrupts interrupts; + +LONG WINAPI InterruptHandler(EXCEPTION_POINTERS *exceptionInfo) +{ + if (interrupts.handler) { + int exceptionCode = 0; + if (exceptionInfo) { + exceptionCode = exceptionInfo->ExceptionRecord.ExceptionCode; + } + + interrupts.handler(exceptionCode); + } + + if (interrupts.prevFilter) { + return interrupts.prevFilter(exceptionInfo); + } else { + return EXCEPTION_CONTINUE_SEARCH; + } +} + +void +CatchInterrupts(void (*func)(int)) +{ + interrupts.handler = func; + + if (!interrupts.set) { + interrupts.prevFilter = + SetUnhandledExceptionFilter(InterruptHandler); + interrupts.set = true; + } +} + + } /* namespace OS */