]> git.cworth.org Git - apitrace/blob - common/os_win32.cpp
3bee07012bf6bd8aca04529f57f29dcef055863f
[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
34
35 namespace os {
36
37
38 /* 
39  * Trick from http://locklessinc.com/articles/pthreads_on_windows/
40  */
41 static CRITICAL_SECTION
42 criticalSection = {
43     (PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0
44 };
45
46
47 void
48 acquireMutex(void)
49 {
50     EnterCriticalSection(&criticalSection);
51 }
52
53
54 void
55 releaseMutex(void)
56 {
57     LeaveCriticalSection(&criticalSection);
58 }
59
60
61 Path
62 getProcessName(void)
63 {
64     Path path;
65
66     char *szProcessPath = path.buf(PATH_MAX);
67
68     DWORD nWritten = GetModuleFileNameA(NULL, szProcessPath, PATH_MAX);
69
70     path.truncate();
71
72     return path;
73 }
74
75 bool
76 getCurrentDir(char *str, size_t size)
77 {
78     DWORD ret;
79     ret = GetCurrentDirectoryA(size, str);
80     str[size - 1] = 0;
81     return ret == 0 ? false : true;
82 }
83
84 void
85 log(const char *format, ...)
86 {
87     char buf[4096];
88
89     va_list ap;
90     va_start(ap, format);
91     fflush(stdout);
92     vsnprintf(buf, sizeof buf, format, ap);
93     va_end(ap);
94
95     OutputDebugStringA(buf);
96
97     /*
98      * Also write the message to stderr, when a debugger is not present (to
99      * avoid duplicate messages in command line debuggers).
100      */
101 #if _WIN32_WINNT > 0x0400
102     if (!IsDebuggerPresent()) {
103         fflush(stdout);
104         fputs(buf, stderr);
105         fflush(stderr);
106     }
107 #endif
108 }
109
110 long long
111 getTime(void)
112 {
113     static LARGE_INTEGER frequency;
114     LARGE_INTEGER counter;
115     if (!frequency.QuadPart)
116         QueryPerformanceFrequency(&frequency);
117     QueryPerformanceCounter(&counter);
118     return counter.QuadPart*1000000LL/frequency.QuadPart;
119 }
120
121 void
122 abort(void)
123 {
124 #ifndef NDEBUG
125     DebugBreak();
126 #else
127     ExitProcess(0);
128 #endif
129 }
130
131
132 static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
133 static void (*gCallback)(void) = NULL;
134
135 static LONG WINAPI
136 unhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
137 {
138     if (gCallback) {
139         gCallback();
140     }
141
142         if (prevExceptionFilter) {
143                 return prevExceptionFilter(pExceptionInfo);
144     } else {
145                 return EXCEPTION_CONTINUE_SEARCH;
146     }
147 }
148
149 void
150 setExceptionCallback(void (*callback)(void))
151 {
152     assert(!gCallback);
153
154     if (!gCallback) {
155         gCallback = callback;
156
157         assert(!prevExceptionFilter);
158
159         /*
160          * TODO: Unfortunately it seems that the CRT will reset the exception
161          * handler in certain circumnstances.  See
162          * http://www.codeproject.com/KB/winsdk/crash_hook.aspx
163          */
164         prevExceptionFilter = SetUnhandledExceptionFilter(unhandledExceptionFilter);
165     }
166 }
167
168 void
169 resetExceptionCallback(void)
170 {
171     gCallback = NULL;
172 }
173
174
175 } /* namespace os */