**************************************************************************/
#include <windows.h>
+#include <signal.h>
#include <string.h>
#include <stdio.h>
#include "os.hpp"
-#include "trace_write.hpp"
namespace OS {
void
DebugMessage(const char *format, ...)
{
- char buf[4096];
-
- va_list ap;
- va_start(ap, format);
- fflush(stdout);
- vsnprintf(buf, sizeof buf, format, ap);
- va_end(ap);
-
- OutputDebugStringA(buf);
- if (!IsDebuggerPresent()) {
- fflush(stdout);
- fputs(buf, 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
}
+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:
- Trace::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 */