1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
39 * Trick from http://locklessinc.com/articles/pthreads_on_windows/
41 static CRITICAL_SECTION
43 (PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0
50 EnterCriticalSection(&criticalSection);
57 LeaveCriticalSection(&criticalSection);
66 char *szProcessPath = path.buf(PATH_MAX);
68 DWORD nWritten = GetModuleFileNameA(NULL, szProcessPath, PATH_MAX);
78 getCurrentDir(char *str, size_t size)
81 ret = GetCurrentDirectoryA(size, str);
83 return ret == 0 ? false : true;
87 log(const char *format, ...)
94 vsnprintf(buf, sizeof buf, format, ap);
97 OutputDebugStringA(buf);
100 * Also write the message to stderr, when a debugger is not present (to
101 * avoid duplicate messages in command line debuggers).
103 #if _WIN32_WINNT > 0x0400
104 if (!IsDebuggerPresent()) {
115 static LARGE_INTEGER frequency;
116 LARGE_INTEGER counter;
117 if (!frequency.QuadPart)
118 QueryPerformanceFrequency(&frequency);
119 QueryPerformanceCounter(&counter);
120 return counter.QuadPart*1000000LL/frequency.QuadPart;
134 static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
135 static void (*gCallback)(void) = NULL;
138 unhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
144 if (prevExceptionFilter) {
145 return prevExceptionFilter(pExceptionInfo);
147 return EXCEPTION_CONTINUE_SEARCH;
152 setExceptionCallback(void (*callback)(void))
157 gCallback = callback;
159 assert(!prevExceptionFilter);
162 * TODO: Unfortunately it seems that the CRT will reset the exception
163 * handler in certain circumnstances. See
164 * http://www.codeproject.com/KB/winsdk/crash_hook.aspx
166 prevExceptionFilter = SetUnhandledExceptionFilter(unhandledExceptionFilter);
171 resetExceptionCallback(void)