]> git.cworth.org Git - apitrace/blobdiff - os_win32.cpp
Merge branch 'master' into on-demand-loading
[apitrace] / os_win32.cpp
index 4bfdf5e00d96373c1a7e003f42c2dfd521ef935c..587503c41553fa7cd013cf5ad28862fa34f938a7 100644 (file)
@@ -24,6 +24,7 @@
  **************************************************************************/
 
 #include <windows.h>
+#include <assert.h>
 #include <signal.h>
 #include <string.h>
 #include <stdio.h>
@@ -135,50 +136,40 @@ Abort(void)
 }
 
 
-struct Interrupts
-{
-    Interrupts()
-        : set(false),
-          prevfilter(NULL),
-          handler(NULL)
-    {}
-
-    bool set;
-    LPTOP_LEVEL_EXCEPTION_FILTER prevFilter;
+static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
+static void (*gCallback)(void) = NULL;
 
-    void (*handler)(int);
-};
-static Interrupts interrupts;
-
-LONG WINAPI InterruptHandler(EXCEPTION_POINTERS *exceptionInfo)
+static LONG WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
 {
-    if (interrupts.handler) {
-        int exceptionCode = 0;
-        if (exceptionInfo) {
-            exceptionCode = exceptionInfo->ExceptionRecord.ExceptionCode;
-        }
-
-        interrupts.handler(exceptionCode);
+    if (gCallback) {
+        gCallback();
     }
 
-    if (interrupts.prevFilter) {
-        return interrupts.prevFilter(exceptionInfo);
+       if (prevExceptionFilter) {
+               return prevExceptionFilter(pExceptionInfo);
     } else {
-        return EXCEPTION_CONTINUE_SEARCH;
+               return EXCEPTION_CONTINUE_SEARCH;
     }
 }
 
 void
-CatchInterrupts(void (*func)(int))
+SetExceptionCallback(void (*callback)(void))
 {
-    interrupts.handler = func;
+    assert(!gCallback);
 
-    if (!interrupts.set) {
-        interrupts.prevFilter =
-            SetUnhandledExceptionFilter(InterruptHandler);
-        interrupts.set = true;
+    if (!gCallback) {
+        gCallback = callback;
+
+        assert(!prevExceptionFilter);
+        prevExceptionFilter = SetUnhandledExceptionFilter(UnhandledExceptionFilter);
     }
 }
 
+void
+ResetExceptionCallback(void)
+{
+    gCallback = NULL;
+}
+
 
 } /* namespace OS */