]> git.cworth.org Git - apitrace/blobdiff - os_win32.cpp
Catch exceptions on Windows.
[apitrace] / os_win32.cpp
index e96cc93d63431f9a74b5f5057313b113422986af..4bfdf5e00d96373c1a7e003f42c2dfd521ef935c 100644 (file)
  **************************************************************************/
 
 #include <windows.h>
+#include <signal.h>
 #include <string.h>
 #include <stdio.h>
 
 #include "os.hpp"
-#include "log.hpp"
 
 
 namespace OS {
@@ -46,14 +46,14 @@ CriticalSection = {
 void
 AcquireMutex(void)
 {
-    //EnterCriticalSection(&CriticalSection); 
+    EnterCriticalSection(&CriticalSection); 
 }
 
 
 void
 ReleaseMutex(void)
 {
-    //LeaveCriticalSection(&CriticalSection); 
+    LeaveCriticalSection(&CriticalSection); 
 }
 
 
@@ -89,14 +89,39 @@ GetCurrentDir(char *str, size_t size)
 }
 
 void
-DebugMessage(const char *message)
+DebugMessage(const char *format, ...)
 {
-   OutputDebugStringA(message);
-   if (!IsDebuggerPresent()) {
-      fflush(stdout);
-      fputs(message, 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;
 }
 
 void
@@ -110,24 +135,50 @@ Abort(void)
 }
 
 
+struct Interrupts
+{
+    Interrupts()
+        : set(false),
+          prevfilter(NULL),
+          handler(NULL)
+    {}
 
-} /* namespace OS */
+    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 0
-BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
-    switch(fdwReason) {
-    case DLL_PROCESS_ATTACH:
-    case DLL_THREAD_ATTACH:
-        return TRUE;
-    case DLL_THREAD_DETACH:
-        return TRUE;
-    case DLL_PROCESS_DETACH:
-        Log::Close();
-        return TRUE;
+    if (interrupts.prevFilter) {
+        return interrupts.prevFilter(exceptionInfo);
+    } else {
+        return EXCEPTION_CONTINUE_SEARCH;
     }
-    (void)hinstDLL;
-    (void)lpvReserved;
-    return TRUE;
 }
-#endif
+
+void
+CatchInterrupts(void (*func)(int))
+{
+    interrupts.handler = func;
+
+    if (!interrupts.set) {
+        interrupts.prevFilter =
+            SetUnhandledExceptionFilter(InterruptHandler);
+        interrupts.set = true;
+    }
+}
+
+
+} /* namespace OS */