]> git.cworth.org Git - apitrace/blob - os_win32.cpp
Catch exceptions on Windows.
[apitrace] / 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 #include <signal.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 #include "os.hpp"
32
33
34 namespace OS {
35
36
37 /* 
38  * Trick from http://locklessinc.com/articles/pthreads_on_windows/
39  */
40 static CRITICAL_SECTION
41 CriticalSection = {
42     (PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0
43 };
44
45
46 void
47 AcquireMutex(void)
48 {
49     EnterCriticalSection(&CriticalSection); 
50 }
51
52
53 void
54 ReleaseMutex(void)
55 {
56     LeaveCriticalSection(&CriticalSection); 
57 }
58
59
60 bool
61 GetProcessName(char *str, size_t size)
62 {
63     char szProcessPath[PATH_MAX];
64     char *lpProcessName;
65     char *lpProcessExt;
66
67     GetModuleFileNameA(NULL, szProcessPath, sizeof(szProcessPath)/sizeof(szProcessPath[0]));
68
69     lpProcessName = strrchr(szProcessPath, '\\');
70     lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
71
72     lpProcessExt = strrchr(lpProcessName, '.');
73     if (lpProcessExt) {
74         *lpProcessExt = '\0';
75     }
76
77     strncpy(str, lpProcessName, size);
78
79     return true;
80 }
81
82 bool
83 GetCurrentDir(char *str, size_t size)
84 {
85     DWORD ret;
86     ret = GetCurrentDirectoryA(size, str);
87     str[size - 1] = 0;
88     return ret == 0 ? false : true;
89 }
90
91 void
92 DebugMessage(const char *format, ...)
93 {
94     char buf[4096];
95
96     va_list ap;
97     va_start(ap, format);
98     fflush(stdout);
99     vsnprintf(buf, sizeof buf, format, ap);
100     va_end(ap);
101
102     OutputDebugStringA(buf);
103
104     /*
105      * Also write the message to stderr, when a debugger is not present (to
106      * avoid duplicate messages in command line debuggers).
107      */
108 #if _WIN32_WINNT > 0x0400
109     if (!IsDebuggerPresent()) {
110         fflush(stdout);
111         fputs(buf, stderr);
112         fflush(stderr);
113     }
114 #endif
115 }
116
117 long long GetTime(void)
118 {
119     static LARGE_INTEGER frequency;
120     LARGE_INTEGER counter;
121     if (!frequency.QuadPart)
122         QueryPerformanceFrequency(&frequency);
123     QueryPerformanceCounter(&counter);
124     return counter.QuadPart*1000000LL/frequency.QuadPart;
125 }
126
127 void
128 Abort(void)
129 {
130 #ifndef NDEBUG
131     DebugBreak();
132 #else
133     ExitProcess(0);
134 #endif
135 }
136
137
138 struct Interrupts
139 {
140     Interrupts()
141         : set(false),
142           prevfilter(NULL),
143           handler(NULL)
144     {}
145
146     bool set;
147     LPTOP_LEVEL_EXCEPTION_FILTER prevFilter;
148
149     void (*handler)(int);
150 };
151 static Interrupts interrupts;
152
153 LONG WINAPI InterruptHandler(EXCEPTION_POINTERS *exceptionInfo)
154 {
155     if (interrupts.handler) {
156         int exceptionCode = 0;
157         if (exceptionInfo) {
158             exceptionCode = exceptionInfo->ExceptionRecord.ExceptionCode;
159         }
160
161         interrupts.handler(exceptionCode);
162     }
163
164     if (interrupts.prevFilter) {
165         return interrupts.prevFilter(exceptionInfo);
166     } else {
167         return EXCEPTION_CONTINUE_SEARCH;
168     }
169 }
170
171 void
172 CatchInterrupts(void (*func)(int))
173 {
174     interrupts.handler = func;
175
176     if (!interrupts.set) {
177         interrupts.prevFilter =
178             SetUnhandledExceptionFilter(InterruptHandler);
179         interrupts.set = true;
180     }
181 }
182
183
184 } /* namespace OS */