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 * Determine whether an argument should be quoted.
83 needsQuote(const char *arg)
91 if (c == ' ' || c == '\t' || c == '\"') {
108 quoteArg(std::string &s, const char *arg)
111 unsigned backslashes = 0;
119 } else if (c == '"') {
120 while (backslashes) {
137 int execute(char * const * args)
139 std::string commandLine;
141 const char *arg0 = *args;
144 while ((arg = *args++) != NULL) {
146 commandLine.push_back(sep);
149 if (needsQuote(arg)) {
150 quoteArg(commandLine, arg);
152 commandLine.append(arg);
158 STARTUPINFO startupInfo;
159 memset(&startupInfo, 0, sizeof(startupInfo));
160 startupInfo.cb = sizeof(startupInfo);
162 PROCESS_INFORMATION processInformation;
164 if (!CreateProcessA(NULL,
165 const_cast<char *>(commandLine.c_str()), // only modified by CreateProcessW
166 0, // process attributes
167 0, // thread attributes
168 FALSE, // inherit handles
169 0, // creation flags,
171 NULL, // current directory
175 log("error: failed to execute %s\n", arg0);
179 WaitForSingleObject(processInformation.hProcess, INFINITE);
182 GetExitCodeProcess(processInformation.hProcess, &exitCode);
184 CloseHandle(processInformation.hProcess);
185 CloseHandle(processInformation.hThread);
187 return (int)exitCode;
191 log(const char *format, ...)
196 va_start(ap, format);
198 vsnprintf(buf, sizeof buf, format, ap);
201 OutputDebugStringA(buf);
204 * Also write the message to stderr, when a debugger is not present (to
205 * avoid duplicate messages in command line debuggers).
207 #if _WIN32_WINNT > 0x0400
208 if (!IsDebuggerPresent()) {
216 long long timeFrequency = 0LL;
229 #ifndef DBG_PRINTEXCEPTION_C
230 #define DBG_PRINTEXCEPTION_C 0x40010006
233 static PVOID prevExceptionFilter = NULL;
234 static void (*gCallback)(void) = NULL;
237 unhandledExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
239 PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord;
242 * Ignore OutputDebugStringA exception.
244 if (pExceptionRecord->ExceptionCode == DBG_PRINTEXCEPTION_C) {
245 return EXCEPTION_CONTINUE_SEARCH;
249 * Ignore C++ exceptions
251 * http://support.microsoft.com/kb/185294
252 * http://blogs.msdn.com/b/oldnewthing/archive/2010/07/30/10044061.aspx
254 if (pExceptionRecord->ExceptionCode == 0xe06d7363) {
255 return EXCEPTION_CONTINUE_SEARCH;
259 * Ignore thread naming exception.
261 * http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
263 if (pExceptionRecord->ExceptionCode == 0x406d1388) {
264 return EXCEPTION_CONTINUE_SEARCH;
267 // Clear direction flag
278 log("apitrace: warning: caught exception 0x%08lx\n", pExceptionRecord->ExceptionCode);
280 static int recursion_count = 0;
281 if (recursion_count) {
282 fprintf(stderr, "apitrace: warning: recursion handling exception\n");
291 return EXCEPTION_CONTINUE_SEARCH;
295 setExceptionCallback(void (*callback)(void))
300 gCallback = callback;
302 assert(!prevExceptionFilter);
304 prevExceptionFilter = AddVectoredExceptionHandler(0, unhandledExceptionHandler);
309 resetExceptionCallback(void)
312 RemoveVectoredExceptionHandler(prevExceptionFilter);