]> git.cworth.org Git - apitrace/blobdiff - os_win32.cpp
Catch exceptions on Windows.
[apitrace] / os_win32.cpp
index 6cddc552681db55863e1eaf01f1f1baac070c2ed..4bfdf5e00d96373c1a7e003f42c2dfd521ef935c 100644 (file)
@@ -24,6 +24,7 @@
  **************************************************************************/
 
 #include <windows.h>
+#include <signal.h>
 #include <string.h>
 #include <stdio.h>
 
@@ -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 */