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 **************************************************************************/
35 #include "os_string.hpp"
45 size_t size = MAX_PATH;
46 char *buf = path.buf(size);
48 DWORD nWritten = GetModuleFileNameA(NULL, buf, size);
60 size_t size = MAX_PATH;
61 char *buf = path.buf(size);
63 DWORD ret = GetCurrentDirectoryA(size, buf);
73 String::exists(void) const
75 DWORD attrs = GetFileAttributesA(str());
76 return attrs != INVALID_FILE_ATTRIBUTES;
80 copyFile(const String &srcFileName, const String &dstFileName, bool override)
82 return CopyFileA(srcFileName, dstFileName, !override);
86 removeFile(const String &srcFilename)
88 return DeleteFileA(srcFilename);
92 * Determine whether an argument should be quoted.
95 needsQuote(const char *arg)
103 if (c == ' ' || c == '\t' || c == '\"') {
120 quoteArg(std::string &s, const char *arg)
123 unsigned backslashes = 0;
130 } else if (c == '"') {
131 while (backslashes) {
148 int execute(char * const * args)
150 std::string commandLine;
152 const char *arg0 = *args;
155 while ((arg = *args++) != NULL) {
157 commandLine.push_back(sep);
160 if (needsQuote(arg)) {
161 quoteArg(commandLine, arg);
163 commandLine.append(arg);
169 STARTUPINFO startupInfo;
170 memset(&startupInfo, 0, sizeof(startupInfo));
171 startupInfo.cb = sizeof(startupInfo);
173 PROCESS_INFORMATION processInformation;
175 if (!CreateProcessA(NULL,
176 const_cast<char *>(commandLine.c_str()), // only modified by CreateProcessW
177 0, // process attributes
178 0, // thread attributes
179 FALSE, // inherit handles
180 0, // creation flags,
182 NULL, // current directory
186 log("error: failed to execute %s\n", arg0);
190 WaitForSingleObject(processInformation.hProcess, INFINITE);
192 DWORD exitCode = ~0UL;
193 GetExitCodeProcess(processInformation.hProcess, &exitCode);
195 CloseHandle(processInformation.hProcess);
196 CloseHandle(processInformation.hThread);
198 return (int)exitCode;
202 log(const char *format, ...)
207 va_start(ap, format);
209 vsnprintf(buf, sizeof buf, format, ap);
212 OutputDebugStringA(buf);
215 * Also write the message to stderr, when a debugger is not present (to
216 * avoid duplicate messages in command line debuggers).
218 #if _WIN32_WINNT > 0x0400
219 if (!IsDebuggerPresent()) {
227 long long timeFrequency = 0LL;
240 #ifndef DBG_PRINTEXCEPTION_C
241 #define DBG_PRINTEXCEPTION_C 0x40010006
244 static PVOID prevExceptionFilter = NULL;
245 static void (*gCallback)(void) = NULL;
248 unhandledExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
250 PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord;
253 * Ignore OutputDebugStringA exception.
255 if (pExceptionRecord->ExceptionCode == DBG_PRINTEXCEPTION_C) {
256 return EXCEPTION_CONTINUE_SEARCH;
260 * Ignore C++ exceptions
262 * http://support.microsoft.com/kb/185294
263 * http://blogs.msdn.com/b/oldnewthing/archive/2010/07/30/10044061.aspx
265 if (pExceptionRecord->ExceptionCode == 0xe06d7363) {
266 return EXCEPTION_CONTINUE_SEARCH;
270 * Ignore thread naming exception.
272 * http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
274 if (pExceptionRecord->ExceptionCode == 0x406d1388) {
275 return EXCEPTION_CONTINUE_SEARCH;
279 * Ignore .NET exception.
281 * http://ig2600.blogspot.co.uk/2011/01/why-do-i-keep-getting-exception-code.html
283 if (pExceptionRecord->ExceptionCode == 0xe0434352) {
284 return EXCEPTION_CONTINUE_SEARCH;
287 // Clear direction flag
298 log("apitrace: warning: caught exception 0x%08lx\n", pExceptionRecord->ExceptionCode);
300 static int recursion_count = 0;
301 if (recursion_count) {
302 fprintf(stderr, "apitrace: warning: recursion handling exception\n");
311 return EXCEPTION_CONTINUE_SEARCH;
315 setExceptionCallback(void (*callback)(void))
320 gCallback = callback;
322 assert(!prevExceptionFilter);
324 prevExceptionFilter = AddVectoredExceptionHandler(0, unhandledExceptionHandler);
329 resetExceptionCallback(void)
332 RemoveVectoredExceptionHandler(prevExceptionFilter);