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 **************************************************************************/
33 #include "os_path.hpp"
40 * Trick from http://locklessinc.com/articles/pthreads_on_windows/
42 static CRITICAL_SECTION
44 (PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0
51 EnterCriticalSection(&criticalSection);
58 LeaveCriticalSection(&criticalSection);
66 size_t size = MAX_PATH;
67 char *buf = path.buf(size);
69 DWORD nWritten = GetModuleFileNameA(NULL, buf, size);
81 size_t size = MAX_PATH;
82 char *buf = path.buf(size);
84 DWORD ret = GetCurrentDirectoryA(size, buf);
94 Path::exists(void) const
96 DWORD attrs = GetFileAttributesA(str());
97 return attrs != INVALID_FILE_ATTRIBUTES;
101 log(const char *format, ...)
106 va_start(ap, format);
108 vsnprintf(buf, sizeof buf, format, ap);
111 OutputDebugStringA(buf);
114 * Also write the message to stderr, when a debugger is not present (to
115 * avoid duplicate messages in command line debuggers).
117 #if _WIN32_WINNT > 0x0400
118 if (!IsDebuggerPresent()) {
129 static LARGE_INTEGER frequency;
130 LARGE_INTEGER counter;
131 if (!frequency.QuadPart)
132 QueryPerformanceFrequency(&frequency);
133 QueryPerformanceCounter(&counter);
134 return counter.QuadPart*1000000LL/frequency.QuadPart;
148 static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
149 static void (*gCallback)(void) = NULL;
152 unhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
158 if (prevExceptionFilter) {
159 return prevExceptionFilter(pExceptionInfo);
161 return EXCEPTION_CONTINUE_SEARCH;
166 setExceptionCallback(void (*callback)(void))
171 gCallback = callback;
173 assert(!prevExceptionFilter);
176 * TODO: Unfortunately it seems that the CRT will reset the exception
177 * handler in certain circumnstances. See
178 * http://www.codeproject.com/KB/winsdk/crash_hook.aspx
180 prevExceptionFilter = SetUnhandledExceptionFilter(unhandledExceptionFilter);
185 resetExceptionCallback(void)