]> git.cworth.org Git - apitrace/blob - common/os_win32.cpp
Add a path manipulating class.
[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     path.trimExtension();
72     path.trimDirectory();
73
74     return path;
75 }
76
77 bool
78 getCurrentDir(char *str, size_t size)
79 {
80     DWORD ret;
81     ret = GetCurrentDirectoryA(size, str);
82     str[size - 1] = 0;
83     return ret == 0 ? false : true;
84 }
85
86 void
87 log(const char *format, ...)
88 {
89     char buf[4096];
90
91     va_list ap;
92     va_start(ap, format);
93     fflush(stdout);
94     vsnprintf(buf, sizeof buf, format, ap);
95     va_end(ap);
96
97     OutputDebugStringA(buf);
98
99     /*
100      * Also write the message to stderr, when a debugger is not present (to
101      * avoid duplicate messages in command line debuggers).
102      */
103 #if _WIN32_WINNT > 0x0400
104     if (!IsDebuggerPresent()) {
105         fflush(stdout);
106         fputs(buf, stderr);
107         fflush(stderr);
108     }
109 #endif
110 }
111
112 long long
113 getTime(void)
114 {
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;
121 }
122
123 void
124 abort(void)
125 {
126 #ifndef NDEBUG
127     DebugBreak();
128 #else
129     ExitProcess(0);
130 #endif
131 }
132
133
134 static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
135 static void (*gCallback)(void) = NULL;
136
137 static LONG WINAPI
138 unhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
139 {
140     if (gCallback) {
141         gCallback();
142     }
143
144         if (prevExceptionFilter) {
145                 return prevExceptionFilter(pExceptionInfo);
146     } else {
147                 return EXCEPTION_CONTINUE_SEARCH;
148     }
149 }
150
151 void
152 setExceptionCallback(void (*callback)(void))
153 {
154     assert(!gCallback);
155
156     if (!gCallback) {
157         gCallback = callback;
158
159         assert(!prevExceptionFilter);
160
161         /*
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
165          */
166         prevExceptionFilter = SetUnhandledExceptionFilter(unhandledExceptionFilter);
167     }
168 }
169
170 void
171 resetExceptionCallback(void)
172 {
173     gCallback = NULL;
174 }
175
176
177 } /* namespace os */