]> git.cworth.org Git - apitrace/blob - common/os_win32.cpp
74d911cadc6e294049408db6ea3cafc88e5cf151
[apitrace] / common / os_win32.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 #include <windows.h>
27
28 #include <assert.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "os.hpp"
33 #include "os_path.hpp"
34
35
36 namespace os {
37
38
39 /* 
40  * Trick from http://locklessinc.com/articles/pthreads_on_windows/
41  */
42 static CRITICAL_SECTION
43 criticalSection = {
44     (PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0
45 };
46
47
48 void
49 acquireMutex(void)
50 {
51     EnterCriticalSection(&criticalSection);
52 }
53
54
55 void
56 releaseMutex(void)
57 {
58     LeaveCriticalSection(&criticalSection);
59 }
60
61
62 Path
63 getProcessName(void)
64 {
65     Path path;
66     size_t size = MAX_PATH;
67     char *buf = path.buf(size);
68
69     DWORD nWritten = GetModuleFileNameA(NULL, buf, size);
70     (void)nWritten;
71
72     path.truncate();
73
74     return path;
75 }
76
77 Path
78 getCurrentDir(void)
79 {
80     Path path;
81     size_t size = MAX_PATH;
82     char *buf = path.buf(size);
83     
84     DWORD ret = GetCurrentDirectoryA(size, buf);
85     (void)ret;
86     
87     buf[size - 1] = 0;
88     path.truncate();
89
90     return path;
91 }
92
93 bool
94 Path::exists(void) const
95 {
96     DWORD attrs = GetFileAttributesA(str());
97     return attrs != INVALID_FILE_ATTRIBUTES;
98 }
99
100 void
101 log(const char *format, ...)
102 {
103     char buf[4096];
104
105     va_list ap;
106     va_start(ap, format);
107     fflush(stdout);
108     vsnprintf(buf, sizeof buf, format, ap);
109     va_end(ap);
110
111     OutputDebugStringA(buf);
112
113     /*
114      * Also write the message to stderr, when a debugger is not present (to
115      * avoid duplicate messages in command line debuggers).
116      */
117 #if _WIN32_WINNT > 0x0400
118     if (!IsDebuggerPresent()) {
119         fflush(stdout);
120         fputs(buf, stderr);
121         fflush(stderr);
122     }
123 #endif
124 }
125
126 long long
127 getTime(void)
128 {
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;
135 }
136
137 void
138 abort(void)
139 {
140 #ifndef NDEBUG
141     DebugBreak();
142 #else
143     ExitProcess(0);
144 #endif
145 }
146
147
148 static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
149 static void (*gCallback)(void) = NULL;
150
151 static LONG WINAPI
152 unhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
153 {
154     if (gCallback) {
155         gCallback();
156     }
157
158         if (prevExceptionFilter) {
159                 return prevExceptionFilter(pExceptionInfo);
160     } else {
161                 return EXCEPTION_CONTINUE_SEARCH;
162     }
163 }
164
165 void
166 setExceptionCallback(void (*callback)(void))
167 {
168     assert(!gCallback);
169
170     if (!gCallback) {
171         gCallback = callback;
172
173         assert(!prevExceptionFilter);
174
175         /*
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
179          */
180         prevExceptionFilter = SetUnhandledExceptionFilter(unhandledExceptionFilter);
181     }
182 }
183
184 void
185 resetExceptionCallback(void)
186 {
187     gCallback = NULL;
188 }
189
190
191 } /* namespace os */