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 **************************************************************************/
37 #include "os_string.hpp"
47 DWORD size = MAX_PATH;
48 char *buf = path.buf(size);
50 DWORD nWritten = GetModuleFileNameA(NULL, buf, size);
62 DWORD size = MAX_PATH;
63 char *buf = path.buf(size);
65 DWORD ret = GetCurrentDirectoryA(size, buf);
75 createDirectory(const String &path)
77 return CreateDirectoryA(path, NULL);
81 String::exists(void) const
83 DWORD attrs = GetFileAttributesA(str());
84 return attrs != INVALID_FILE_ATTRIBUTES;
88 copyFile(const String &srcFileName, const String &dstFileName, bool override)
90 return CopyFileA(srcFileName, dstFileName, !override);
94 removeFile(const String &srcFilename)
96 return DeleteFileA(srcFilename);
100 * Determine whether an argument should be quoted.
103 needsQuote(const char *arg)
111 if (c == ' ' || c == '\t' || c == '\"') {
128 quoteArg(std::string &s, const char *arg)
131 unsigned backslashes = 0;
138 } else if (c == '"') {
139 while (backslashes) {
156 int execute(char * const * args)
158 std::string commandLine;
160 const char *arg0 = *args;
163 while ((arg = *args++) != NULL) {
165 commandLine.push_back(sep);
168 if (needsQuote(arg)) {
169 quoteArg(commandLine, arg);
171 commandLine.append(arg);
177 STARTUPINFOA startupInfo;
178 memset(&startupInfo, 0, sizeof(startupInfo));
179 startupInfo.cb = sizeof(startupInfo);
181 PROCESS_INFORMATION processInformation;
183 if (!CreateProcessA(NULL,
184 const_cast<char *>(commandLine.c_str()), // only modified by CreateProcessW
185 0, // process attributes
186 0, // thread attributes
187 FALSE, // inherit handles
188 0, // creation flags,
190 NULL, // current directory
194 log("error: failed to execute %s\n", arg0);
198 WaitForSingleObject(processInformation.hProcess, INFINITE);
200 DWORD exitCode = ~0UL;
201 GetExitCodeProcess(processInformation.hProcess, &exitCode);
203 CloseHandle(processInformation.hProcess);
204 CloseHandle(processInformation.hThread);
206 return (int)exitCode;
210 log(const char *format, ...)
215 va_start(ap, format);
217 vsnprintf(buf, sizeof buf, format, ap);
220 OutputDebugStringA(buf);
223 * Also write the message to stderr, when a debugger is not present (to
224 * avoid duplicate messages in command line debuggers).
226 #if _WIN32_WINNT > 0x0400
227 if (!IsDebuggerPresent()) {
235 long long timeFrequency = 0LL;
240 TerminateProcess(GetCurrentProcess(), 1);
244 #ifndef DBG_PRINTEXCEPTION_C
245 #define DBG_PRINTEXCEPTION_C 0x40010006
248 static PVOID prevExceptionFilter = NULL;
249 static void (*gCallback)(void) = NULL;
252 unhandledExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
254 PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord;
257 * Ignore OutputDebugStringA exception.
259 if (pExceptionRecord->ExceptionCode == DBG_PRINTEXCEPTION_C) {
260 return EXCEPTION_CONTINUE_SEARCH;
264 * Ignore C++ exceptions
266 * http://support.microsoft.com/kb/185294
267 * http://blogs.msdn.com/b/oldnewthing/archive/2010/07/30/10044061.aspx
269 if (pExceptionRecord->ExceptionCode == 0xe06d7363) {
270 return EXCEPTION_CONTINUE_SEARCH;
274 * Ignore thread naming exception.
276 * http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
278 if (pExceptionRecord->ExceptionCode == 0x406d1388) {
279 return EXCEPTION_CONTINUE_SEARCH;
283 * Ignore .NET exception.
285 * http://ig2600.blogspot.co.uk/2011/01/why-do-i-keep-getting-exception-code.html
287 if (pExceptionRecord->ExceptionCode == 0xe0434352) {
288 return EXCEPTION_CONTINUE_SEARCH;
291 // Clear direction flag
302 log("apitrace: warning: caught exception 0x%08lx\n", pExceptionRecord->ExceptionCode);
304 static int recursion_count = 0;
305 if (recursion_count) {
306 fputs("apitrace: warning: recursion handling exception\n", stderr);
315 return EXCEPTION_CONTINUE_SEARCH;
319 setExceptionCallback(void (*callback)(void))
324 gCallback = callback;
326 assert(!prevExceptionFilter);
328 prevExceptionFilter = AddVectoredExceptionHandler(0, unhandledExceptionHandler);
333 resetExceptionCallback(void)
336 RemoveVectoredExceptionHandler(prevExceptionFilter);
344 #endif // defined(_WIN32)