]> git.cworth.org Git - apitrace/commitdiff
Catch exceptions on Windows.
authorZack Rusin <zack@kde.org>
Wed, 24 Aug 2011 01:17:35 +0000 (21:17 -0400)
committerZack Rusin <zack@kde.org>
Wed, 24 Aug 2011 01:17:35 +0000 (21:17 -0400)
os_win32.cpp

index 90ce8d3a74a41a5a660cc71547d27898d0d834e1..4bfdf5e00d96373c1a7e003f42c2dfd521ef935c 100644 (file)
@@ -134,12 +134,50 @@ 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))
 {
-    signal(SIGINT, func);
-    signal(SIGHUP, func);
-    signal(SIGTERM, func);
+    interrupts.handler = func;
+
+    if (!interrupts.set) {
+        interrupts.prevFilter =
+            SetUnhandledExceptionFilter(InterruptHandler);
+        interrupts.set = true;
+    }
 }