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 createDirectory(const String &path)
75 return CreateDirectoryA(path, NULL);
79 String::exists(void) const
81 DWORD attrs = GetFileAttributesA(str());
82 return attrs != INVALID_FILE_ATTRIBUTES;
86 copyFile(const String &srcFileName, const String &dstFileName, bool override)
88 return CopyFileA(srcFileName, dstFileName, !override);
92 removeFile(const String &srcFilename)
94 return DeleteFileA(srcFilename);
98 * Determine whether an argument should be quoted.
101 needsQuote(const char *arg)
109 if (c == ' ' || c == '\t' || c == '\"') {
126 quoteArg(std::string &s, const char *arg)
129 unsigned backslashes = 0;
136 } else if (c == '"') {
137 while (backslashes) {
154 int execute(char * const * args)
156 std::string commandLine;
158 const char *arg0 = *args;
161 while ((arg = *args++) != NULL) {
163 commandLine.push_back(sep);
166 if (needsQuote(arg)) {
167 quoteArg(commandLine, arg);
169 commandLine.append(arg);
175 STARTUPINFO startupInfo;
176 memset(&startupInfo, 0, sizeof(startupInfo));
177 startupInfo.cb = sizeof(startupInfo);
179 PROCESS_INFORMATION processInformation;
181 if (!CreateProcessA(NULL,
182 const_cast<char *>(commandLine.c_str()), // only modified by CreateProcessW
183 0, // process attributes
184 0, // thread attributes
185 FALSE, // inherit handles
186 0, // creation flags,
188 NULL, // current directory
192 log("error: failed to execute %s\n", arg0);
196 WaitForSingleObject(processInformation.hProcess, INFINITE);
198 DWORD exitCode = ~0UL;
199 GetExitCodeProcess(processInformation.hProcess, &exitCode);
201 CloseHandle(processInformation.hProcess);
202 CloseHandle(processInformation.hThread);
204 return (int)exitCode;
208 log(const char *format, ...)
213 va_start(ap, format);
215 vsnprintf(buf, sizeof buf, format, ap);
218 OutputDebugStringA(buf);
221 * Also write the message to stderr, when a debugger is not present (to
222 * avoid duplicate messages in command line debuggers).
224 #if _WIN32_WINNT > 0x0400
225 if (!IsDebuggerPresent()) {
233 long long timeFrequency = 0LL;
238 TerminateProcess(GetCurrentProcess(), 1);
242 #ifndef DBG_PRINTEXCEPTION_C
243 #define DBG_PRINTEXCEPTION_C 0x40010006
246 static PVOID prevExceptionFilter = NULL;
247 static void (*gCallback)(void) = NULL;
250 unhandledExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
252 PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord;
255 * Ignore OutputDebugStringA exception.
257 if (pExceptionRecord->ExceptionCode == DBG_PRINTEXCEPTION_C) {
258 return EXCEPTION_CONTINUE_SEARCH;
262 * Ignore C++ exceptions
264 * http://support.microsoft.com/kb/185294
265 * http://blogs.msdn.com/b/oldnewthing/archive/2010/07/30/10044061.aspx
267 if (pExceptionRecord->ExceptionCode == 0xe06d7363) {
268 return EXCEPTION_CONTINUE_SEARCH;
272 * Ignore thread naming exception.
274 * http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
276 if (pExceptionRecord->ExceptionCode == 0x406d1388) {
277 return EXCEPTION_CONTINUE_SEARCH;
281 * Ignore .NET exception.
283 * http://ig2600.blogspot.co.uk/2011/01/why-do-i-keep-getting-exception-code.html
285 if (pExceptionRecord->ExceptionCode == 0xe0434352) {
286 return EXCEPTION_CONTINUE_SEARCH;
289 // Clear direction flag
300 log("apitrace: warning: caught exception 0x%08lx\n", pExceptionRecord->ExceptionCode);
302 static int recursion_count = 0;
303 if (recursion_count) {
304 fputs("apitrace: warning: recursion handling exception\n", stderr);
313 return EXCEPTION_CONTINUE_SEARCH;
317 setExceptionCallback(void (*callback)(void))
322 gCallback = callback;
324 assert(!prevExceptionFilter);
326 prevExceptionFilter = AddVectoredExceptionHandler(0, unhandledExceptionHandler);
331 resetExceptionCallback(void)
334 RemoveVectoredExceptionHandler(prevExceptionFilter);