Reid Spencer | 91886b7 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 1 | //===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 91886b7 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 91886b7 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of the Process class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Allocator.h" |
Zachary Turner | b44d7a0 | 2018-06-01 22:23:46 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ConvertUTF.h" |
Alp Toker | 552f2f7 | 2014-06-03 03:01:03 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
Rafael Espindola | 5c4f829 | 2014-06-11 19:05:50 +0000 | [diff] [blame] | 17 | #include "llvm/Support/WindowsError.h" |
Chandler Carruth | 10b0915 | 2014-01-07 12:37:13 +0000 | [diff] [blame] | 18 | #include <malloc.h> |
| 19 | |
| 20 | // The Windows.h header must be after LLVM and standard headers. |
Reid Kleckner | d59e2fa | 2014-02-12 21:26:20 +0000 | [diff] [blame] | 21 | #include "WindowsSupport.h" |
Chandler Carruth | 10b0915 | 2014-01-07 12:37:13 +0000 | [diff] [blame] | 22 | |
Daniel Dunbar | 9b92e2b | 2011-09-23 23:23:36 +0000 | [diff] [blame] | 23 | #include <direct.h> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include <io.h> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include <psapi.h> |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 26 | #include <shellapi.h> |
Jeff Cohen | 7ae0bc7 | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 27 | |
Nico Weber | 8681121 | 2018-04-02 17:32:48 +0000 | [diff] [blame] | 28 | #if !defined(__MINGW32__) |
David Majnemer | f636cf4 | 2013-10-06 20:44:34 +0000 | [diff] [blame] | 29 | #pragma comment(lib, "psapi.lib") |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 30 | #pragma comment(lib, "shell32.lib") |
Reid Spencer | 187b4ad | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 31 | #endif |
Reid Spencer | 91886b7 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 32 | |
| 33 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 34 | //=== WARNING: Implementation here must contain only Win32 specific code |
Reid Spencer | 91886b7 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 35 | //=== and must not be UNIX code |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Jeff Cohen | 07e22ba | 2005-02-19 03:01:13 +0000 | [diff] [blame] | 38 | #ifdef __MINGW32__ |
Jeff Cohen | 53fbecc | 2004-12-23 03:44:40 +0000 | [diff] [blame] | 39 | // This ban should be lifted when MinGW 1.0+ has defined this value. |
| 40 | # define _HEAPOK (-2) |
| 41 | #endif |
| 42 | |
Chandler Carruth | 5473dfb | 2012-12-31 11:45:20 +0000 | [diff] [blame] | 43 | using namespace llvm; |
Chandler Carruth | 97683aa | 2012-12-31 11:17:50 +0000 | [diff] [blame] | 44 | |
Alp Toker | d71b6df | 2014-05-19 16:13:28 +0000 | [diff] [blame] | 45 | // This function retrieves the page size using GetNativeSystemInfo() and is |
| 46 | // present solely so it can be called once to initialize the self_process member |
| 47 | // below. |
Rafael Espindola | c0610bf | 2014-12-04 16:59:36 +0000 | [diff] [blame] | 48 | static unsigned computePageSize() { |
Alp Toker | d71b6df | 2014-05-19 16:13:28 +0000 | [diff] [blame] | 49 | // GetNativeSystemInfo() provides the physical page size which may differ |
| 50 | // from GetSystemInfo() in 32-bit applications running under WOW64. |
Reid Spencer | 91886b7 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 51 | SYSTEM_INFO info; |
Alp Toker | d71b6df | 2014-05-19 16:13:28 +0000 | [diff] [blame] | 52 | GetNativeSystemInfo(&info); |
NAKAMURA Takumi | 7a04234 | 2013-09-04 14:12:26 +0000 | [diff] [blame] | 53 | // FIXME: FileOffset in MapViewOfFile() should be aligned to not dwPageSize, |
| 54 | // but dwAllocationGranularity. |
Reid Spencer | 91886b7 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 55 | return static_cast<unsigned>(info.dwPageSize); |
| 56 | } |
| 57 | |
Rafael Espindola | c0610bf | 2014-12-04 16:59:36 +0000 | [diff] [blame] | 58 | unsigned Process::getPageSize() { |
| 59 | static unsigned Ret = computePageSize(); |
| 60 | return Ret; |
Reid Spencer | 91886b7 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 63 | size_t |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 64 | Process::GetMallocUsage() |
| 65 | { |
Jeff Cohen | 7ae0bc7 | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 66 | _HEAPINFO hinfo; |
| 67 | hinfo._pentry = NULL; |
| 68 | |
| 69 | size_t size = 0; |
| 70 | |
| 71 | while (_heapwalk(&hinfo) == _HEAPOK) |
| 72 | size += hinfo._size; |
| 73 | |
| 74 | return size; |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 77 | void Process::GetTimeUsage(TimePoint<> &elapsed, std::chrono::nanoseconds &user_time, |
| 78 | std::chrono::nanoseconds &sys_time) { |
| 79 | elapsed = std::chrono::system_clock::now();; |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 80 | |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 81 | FILETIME ProcCreate, ProcExit, KernelTime, UserTime; |
| 82 | if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime, |
| 83 | &UserTime) == 0) |
| 84 | return; |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 85 | |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 86 | user_time = toDuration(UserTime); |
| 87 | sys_time = toDuration(KernelTime); |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 90 | // Some LLVM programs such as bugpoint produce core files as a normal part of |
Aaron Ballman | dcd5757 | 2013-08-16 14:33:07 +0000 | [diff] [blame] | 91 | // their operation. To prevent the disk from filling up, this configuration |
| 92 | // item does what's necessary to prevent their generation. |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 93 | void Process::PreventCoreFiles() { |
Aaron Ballman | dcd5757 | 2013-08-16 14:33:07 +0000 | [diff] [blame] | 94 | // Windows does have the concept of core files, called minidumps. However, |
| 95 | // disabling minidumps for a particular application extends past the lifetime |
| 96 | // of that application, which is the incorrect behavior for this API. |
| 97 | // Additionally, the APIs require elevated privileges to disable and re- |
| 98 | // enable minidumps, which makes this untenable. For more information, see |
| 99 | // WerAddExcludedApplication and WerRemoveExcludedApplication (Vista and |
| 100 | // later). |
| 101 | // |
| 102 | // Windows also has modal pop-up message boxes. As this method is used by |
| 103 | // bugpoint, preventing these pop-ups is additionally important. |
Jeff Cohen | 81549a5 | 2005-02-18 07:05:18 +0000 | [diff] [blame] | 104 | SetErrorMode(SEM_FAILCRITICALERRORS | |
| 105 | SEM_NOGPFAULTERRORBOX | |
| 106 | SEM_NOOPENFILEERRORBOX); |
Leny Kholodov | 1b73e66 | 2016-05-04 16:56:51 +0000 | [diff] [blame] | 107 | |
| 108 | coreFilesPrevented = true; |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 111 | /// Returns the environment variable \arg Name's value as a string encoded in |
| 112 | /// UTF-8. \arg Name is assumed to be in UTF-8 encoding. |
| 113 | Optional<std::string> Process::GetEnv(StringRef Name) { |
| 114 | // Convert the argument to UTF-16 to pass it to _wgetenv(). |
| 115 | SmallVector<wchar_t, 128> NameUTF16; |
Ahmed Charles | ce30de9 | 2014-03-05 05:04:00 +0000 | [diff] [blame] | 116 | if (windows::UTF8ToUTF16(Name, NameUTF16)) |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 117 | return None; |
| 118 | |
| 119 | // Environment variable can be encoded in non-UTF8 encoding, and there's no |
| 120 | // way to know what the encoding is. The only reliable way to look up |
| 121 | // multibyte environment variable is to use GetEnvironmentVariableW(). |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 122 | SmallVector<wchar_t, MAX_PATH> Buf; |
| 123 | size_t Size = MAX_PATH; |
| 124 | do { |
David Majnemer | f07777c | 2013-10-07 21:57:07 +0000 | [diff] [blame] | 125 | Buf.reserve(Size); |
Ben Dunbobbin | ac6a5aa | 2017-08-18 16:55:44 +0000 | [diff] [blame] | 126 | SetLastError(NO_ERROR); |
David Majnemer | f07777c | 2013-10-07 21:57:07 +0000 | [diff] [blame] | 127 | Size = |
Ben Dunbobbin | ac6a5aa | 2017-08-18 16:55:44 +0000 | [diff] [blame] | 128 | GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.capacity()); |
| 129 | if (Size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 130 | return None; |
| 131 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 132 | // Try again with larger buffer. |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 133 | } while (Size > Buf.capacity()); |
| 134 | Buf.set_size(Size); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 135 | |
| 136 | // Convert the result from UTF-16 to UTF-8. |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 137 | SmallVector<char, MAX_PATH> Res; |
Ahmed Charles | ce30de9 | 2014-03-05 05:04:00 +0000 | [diff] [blame] | 138 | if (windows::UTF16ToUTF8(Buf.data(), Size, Res)) |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 139 | return None; |
David Majnemer | f07777c | 2013-10-07 21:57:07 +0000 | [diff] [blame] | 140 | return std::string(Res.data()); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 143 | static const char *AllocateString(const SmallVectorImpl<char> &S, |
| 144 | BumpPtrAllocator &Alloc) { |
| 145 | char *Buf = reinterpret_cast<char *>(Alloc.Allocate(S.size() + 1, 1)); |
| 146 | ::memcpy(Buf, S.data(), S.size()); |
| 147 | Buf[S.size()] = '\0'; |
| 148 | return Buf; |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /// Convert Arg from UTF-16 to UTF-8 and push it onto Args. |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 152 | static std::error_code ConvertAndPushArg(const wchar_t *Arg, |
| 153 | SmallVectorImpl<const char *> &Args, |
| 154 | BumpPtrAllocator &Alloc) { |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 155 | SmallVector<char, MAX_PATH> ArgString; |
| 156 | if (std::error_code ec = windows::UTF16ToUTF8(Arg, wcslen(Arg), ArgString)) |
| 157 | return ec; |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 158 | Args.push_back(AllocateString(ArgString, Alloc)); |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 159 | return std::error_code(); |
| 160 | } |
| 161 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 162 | /// Perform wildcard expansion of Arg, or just push it into Args if it |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 163 | /// doesn't have wildcards or doesn't match any files. |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 164 | static std::error_code WildcardExpand(const wchar_t *Arg, |
| 165 | SmallVectorImpl<const char *> &Args, |
| 166 | BumpPtrAllocator &Alloc) { |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 167 | if (!wcspbrk(Arg, L"*?")) { |
| 168 | // Arg does not contain any wildcard characters. This is the common case. |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 169 | return ConvertAndPushArg(Arg, Args, Alloc); |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Hans Wennborg | e34a71a | 2014-07-24 21:09:45 +0000 | [diff] [blame] | 172 | if (wcscmp(Arg, L"/?") == 0 || wcscmp(Arg, L"-?") == 0) { |
| 173 | // Don't wildcard expand /?. Always treat it as an option. |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 174 | return ConvertAndPushArg(Arg, Args, Alloc); |
Hans Wennborg | e34a71a | 2014-07-24 21:09:45 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 177 | // Extract any directory part of the argument. |
| 178 | SmallVector<char, MAX_PATH> Dir; |
| 179 | if (std::error_code ec = windows::UTF16ToUTF8(Arg, wcslen(Arg), Dir)) |
| 180 | return ec; |
| 181 | sys::path::remove_filename(Dir); |
| 182 | const int DirSize = Dir.size(); |
| 183 | |
| 184 | // Search for matching files. |
Adrian McCarthy | f833141 | 2016-06-20 17:51:27 +0000 | [diff] [blame] | 185 | // FIXME: This assumes the wildcard is only in the file name and not in the |
| 186 | // directory portion of the file path. For example, it doesn't handle |
| 187 | // "*\foo.c" nor "s?c\bar.cpp". |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 188 | WIN32_FIND_DATAW FileData; |
| 189 | HANDLE FindHandle = FindFirstFileW(Arg, &FileData); |
| 190 | if (FindHandle == INVALID_HANDLE_VALUE) { |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 191 | return ConvertAndPushArg(Arg, Args, Alloc); |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | std::error_code ec; |
| 195 | do { |
| 196 | SmallVector<char, MAX_PATH> FileName; |
| 197 | ec = windows::UTF16ToUTF8(FileData.cFileName, wcslen(FileData.cFileName), |
| 198 | FileName); |
| 199 | if (ec) |
| 200 | break; |
| 201 | |
Adrian McCarthy | f833141 | 2016-06-20 17:51:27 +0000 | [diff] [blame] | 202 | // Append FileName to Dir, and remove it afterwards. |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 203 | llvm::sys::path::append(Dir, StringRef(FileName.data(), FileName.size())); |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 204 | Args.push_back(AllocateString(Dir, Alloc)); |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 205 | Dir.resize(DirSize); |
| 206 | } while (FindNextFileW(FindHandle, &FileData)); |
| 207 | |
| 208 | FindClose(FindHandle); |
| 209 | return ec; |
| 210 | } |
| 211 | |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 212 | static std::error_code ExpandShortFileName(const wchar_t *Arg, |
| 213 | SmallVectorImpl<const char *> &Args, |
| 214 | BumpPtrAllocator &Alloc) { |
Adrian McCarthy | f833141 | 2016-06-20 17:51:27 +0000 | [diff] [blame] | 215 | SmallVector<wchar_t, MAX_PATH> LongPath; |
| 216 | DWORD Length = GetLongPathNameW(Arg, LongPath.data(), LongPath.capacity()); |
| 217 | if (Length == 0) |
| 218 | return mapWindowsError(GetLastError()); |
| 219 | if (Length > LongPath.capacity()) { |
| 220 | // We're not going to try to deal with paths longer than MAX_PATH, so we'll |
| 221 | // treat this as an error. GetLastError() returns ERROR_SUCCESS, which |
| 222 | // isn't useful, so we'll hardcode an appropriate error value. |
| 223 | return mapWindowsError(ERROR_INSUFFICIENT_BUFFER); |
| 224 | } |
| 225 | LongPath.set_size(Length); |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 226 | return ConvertAndPushArg(LongPath.data(), Args, Alloc); |
Adrian McCarthy | f833141 | 2016-06-20 17:51:27 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 229 | std::error_code |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 230 | windows::GetCommandLineArguments(SmallVectorImpl<const char *> &Args, |
| 231 | BumpPtrAllocator &Alloc) { |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 232 | int ArgCount; |
| 233 | wchar_t **UnicodeCommandLine = |
| 234 | CommandLineToArgvW(GetCommandLineW(), &ArgCount); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 235 | if (!UnicodeCommandLine) |
Yaron Keren | f8e6517 | 2015-05-04 04:48:10 +0000 | [diff] [blame] | 236 | return mapWindowsError(::GetLastError()); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 237 | |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 238 | Args.reserve(ArgCount); |
| 239 | std::error_code ec; |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 240 | |
Adrian McCarthy | f833141 | 2016-06-20 17:51:27 +0000 | [diff] [blame] | 241 | // The first argument may contain just the name of the executable (e.g., |
| 242 | // "clang") rather than the full path, so swap it with the full path. |
| 243 | wchar_t ModuleName[MAX_PATH]; |
| 244 | int Length = ::GetModuleFileNameW(NULL, ModuleName, MAX_PATH); |
| 245 | if (0 < Length && Length < MAX_PATH) |
| 246 | UnicodeCommandLine[0] = ModuleName; |
| 247 | |
| 248 | // If the first argument is a shortened (8.3) name (which is possible even |
| 249 | // if we got the module name), the driver will have trouble distinguishing it |
| 250 | // (e.g., clang.exe v. clang++.exe), so expand it now. |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 251 | ec = ExpandShortFileName(UnicodeCommandLine[0], Args, Alloc); |
Adrian McCarthy | f833141 | 2016-06-20 17:51:27 +0000 | [diff] [blame] | 252 | |
| 253 | for (int i = 1; i < ArgCount && !ec; ++i) { |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 254 | ec = WildcardExpand(UnicodeCommandLine[i], Args, Alloc); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 255 | if (ec) |
| 256 | break; |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 257 | } |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 258 | |
Hans Wennborg | 21f0f13 | 2014-07-16 00:52:11 +0000 | [diff] [blame] | 259 | LocalFree(UnicodeCommandLine); |
| 260 | return ec; |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 261 | } |
| 262 | |
David Majnemer | 121a174 | 2014-10-06 23:16:18 +0000 | [diff] [blame] | 263 | std::error_code Process::FixupStandardFileDescriptors() { |
| 264 | return std::error_code(); |
| 265 | } |
| 266 | |
David Majnemer | 51c2afc | 2014-10-07 05:48:40 +0000 | [diff] [blame] | 267 | std::error_code Process::SafelyCloseFileDescriptor(int FD) { |
| 268 | if (::close(FD) < 0) |
| 269 | return std::error_code(errno, std::generic_category()); |
| 270 | return std::error_code(); |
| 271 | } |
| 272 | |
Jeff Cohen | b90c31f | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 273 | bool Process::StandardInIsUserInput() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 274 | return FileDescriptorIsDisplayed(0); |
Jeff Cohen | b90c31f | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | bool Process::StandardOutIsDisplayed() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 278 | return FileDescriptorIsDisplayed(1); |
Jeff Cohen | b90c31f | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | bool Process::StandardErrIsDisplayed() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 282 | return FileDescriptorIsDisplayed(2); |
| 283 | } |
| 284 | |
| 285 | bool Process::FileDescriptorIsDisplayed(int fd) { |
Bill Wendling | 2b07965 | 2012-07-19 00:06:06 +0000 | [diff] [blame] | 286 | DWORD Mode; // Unused |
NAKAMURA Takumi | 23ebef1 | 2010-11-10 08:37:47 +0000 | [diff] [blame] | 287 | return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0); |
Jeff Cohen | b90c31f | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Douglas Gregor | 1543661 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 290 | unsigned Process::StandardOutColumns() { |
| 291 | unsigned Columns = 0; |
| 292 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 293 | if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) |
| 294 | Columns = csbi.dwSize.X; |
| 295 | return Columns; |
| 296 | } |
| 297 | |
| 298 | unsigned Process::StandardErrColumns() { |
| 299 | unsigned Columns = 0; |
| 300 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 301 | if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)) |
| 302 | Columns = csbi.dwSize.X; |
| 303 | return Columns; |
| 304 | } |
| 305 | |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 306 | // The terminal always has colors. |
Benjamin Kramer | dfaa0f3 | 2012-07-20 19:49:33 +0000 | [diff] [blame] | 307 | bool Process::FileDescriptorHasColors(int fd) { |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 308 | return FileDescriptorIsDisplayed(fd); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | bool Process::StandardOutHasColors() { |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 312 | return FileDescriptorHasColors(1); |
| 313 | } |
| 314 | |
| 315 | bool Process::StandardErrHasColors() { |
| 316 | return FileDescriptorHasColors(2); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 317 | } |
Torok Edwin | 63e44bb | 2009-06-04 08:18:25 +0000 | [diff] [blame] | 318 | |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 319 | static bool UseANSI = false; |
| 320 | void Process::UseANSIEscapeCodes(bool enable) { |
| 321 | UseANSI = enable; |
| 322 | } |
| 323 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 324 | namespace { |
| 325 | class DefaultColors |
| 326 | { |
| 327 | private: |
| 328 | WORD defaultColor; |
| 329 | public: |
| 330 | DefaultColors() |
| 331 | :defaultColor(GetCurrentColor()) {} |
| 332 | static unsigned GetCurrentColor() { |
| 333 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 334 | if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) |
| 335 | return csbi.wAttributes; |
| 336 | return 0; |
| 337 | } |
| 338 | WORD operator()() const { return defaultColor; } |
| 339 | }; |
| 340 | |
| 341 | DefaultColors defaultColors; |
Zachary Turner | 9e1ce99 | 2015-02-28 19:08:27 +0000 | [diff] [blame] | 342 | |
| 343 | WORD fg_color(WORD color) { |
| 344 | return color & (FOREGROUND_BLUE | FOREGROUND_GREEN | |
| 345 | FOREGROUND_INTENSITY | FOREGROUND_RED); |
| 346 | } |
| 347 | |
| 348 | WORD bg_color(WORD color) { |
| 349 | return color & (BACKGROUND_BLUE | BACKGROUND_GREEN | |
| 350 | BACKGROUND_INTENSITY | BACKGROUND_RED); |
| 351 | } |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | bool Process::ColorNeedsFlush() { |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 355 | return !UseANSI; |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | const char *Process::OutputBold(bool bg) { |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 359 | if (UseANSI) return "\033[1m"; |
| 360 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 361 | WORD colors = DefaultColors::GetCurrentColor(); |
| 362 | if (bg) |
| 363 | colors |= BACKGROUND_INTENSITY; |
| 364 | else |
| 365 | colors |= FOREGROUND_INTENSITY; |
| 366 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | const char *Process::OutputColor(char code, bool bold, bool bg) { |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 371 | if (UseANSI) return colorcodes[bg?1:0][bold?1:0][code&7]; |
| 372 | |
Zachary Turner | 9e1ce99 | 2015-02-28 19:08:27 +0000 | [diff] [blame] | 373 | WORD current = DefaultColors::GetCurrentColor(); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 374 | WORD colors; |
| 375 | if (bg) { |
| 376 | colors = ((code&1) ? BACKGROUND_RED : 0) | |
| 377 | ((code&2) ? BACKGROUND_GREEN : 0 ) | |
| 378 | ((code&4) ? BACKGROUND_BLUE : 0); |
| 379 | if (bold) |
| 380 | colors |= BACKGROUND_INTENSITY; |
Zachary Turner | 9e1ce99 | 2015-02-28 19:08:27 +0000 | [diff] [blame] | 381 | colors |= fg_color(current); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 382 | } else { |
| 383 | colors = ((code&1) ? FOREGROUND_RED : 0) | |
| 384 | ((code&2) ? FOREGROUND_GREEN : 0 ) | |
| 385 | ((code&4) ? FOREGROUND_BLUE : 0); |
| 386 | if (bold) |
| 387 | colors |= FOREGROUND_INTENSITY; |
Zachary Turner | 9e1ce99 | 2015-02-28 19:08:27 +0000 | [diff] [blame] | 388 | colors |= bg_color(current); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 389 | } |
| 390 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors); |
| 391 | return 0; |
| 392 | } |
| 393 | |
Benjamin Kramer | 13d16f3 | 2012-04-16 08:56:50 +0000 | [diff] [blame] | 394 | static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) { |
| 395 | CONSOLE_SCREEN_BUFFER_INFO info; |
| 396 | GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); |
| 397 | return info.wAttributes; |
| 398 | } |
| 399 | |
| 400 | const char *Process::OutputReverse() { |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 401 | if (UseANSI) return "\033[7m"; |
| 402 | |
Benjamin Kramer | 13d16f3 | 2012-04-16 08:56:50 +0000 | [diff] [blame] | 403 | const WORD attributes |
| 404 | = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)); |
| 405 | |
| 406 | const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN | |
| 407 | FOREGROUND_RED | FOREGROUND_INTENSITY; |
| 408 | const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN | |
| 409 | BACKGROUND_RED | BACKGROUND_INTENSITY; |
| 410 | const WORD color_mask = foreground_mask | background_mask; |
| 411 | |
| 412 | WORD new_attributes = |
| 413 | ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) | |
| 414 | ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) | |
| 415 | ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) | |
| 416 | ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) | |
| 417 | ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) | |
| 418 | ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) | |
| 419 | ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) | |
| 420 | ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) | |
| 421 | 0; |
| 422 | new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask); |
| 423 | |
| 424 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes); |
| 425 | return 0; |
| 426 | } |
| 427 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 428 | const char *Process::ResetColor() { |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 429 | if (UseANSI) return "\033[0m"; |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 430 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors()); |
| 431 | return 0; |
| 432 | } |
Aaron Ballman | 7844073 | 2014-02-04 14:49:21 +0000 | [diff] [blame] | 433 | |
Paul Robinson | 8ab79a1 | 2015-11-11 20:49:32 +0000 | [diff] [blame] | 434 | // Include GetLastError() in a fatal error message. |
| 435 | static void ReportLastErrorFatal(const char *Msg) { |
| 436 | std::string ErrMsg; |
| 437 | MakeErrMsg(&ErrMsg, Msg); |
| 438 | report_fatal_error(ErrMsg); |
| 439 | } |
| 440 | |
Aaron Ballman | 7844073 | 2014-02-04 14:49:21 +0000 | [diff] [blame] | 441 | unsigned Process::GetRandomNumber() { |
Aaron Ballman | 3f5e8b8 | 2014-02-11 02:47:33 +0000 | [diff] [blame] | 442 | HCRYPTPROV HCPC; |
| 443 | if (!::CryptAcquireContextW(&HCPC, NULL, NULL, PROV_RSA_FULL, |
| 444 | CRYPT_VERIFYCONTEXT)) |
Paul Robinson | 8ab79a1 | 2015-11-11 20:49:32 +0000 | [diff] [blame] | 445 | ReportLastErrorFatal("Could not acquire a cryptographic context"); |
Aaron Ballman | 3f5e8b8 | 2014-02-11 02:47:33 +0000 | [diff] [blame] | 446 | |
| 447 | ScopedCryptContext CryptoProvider(HCPC); |
| 448 | unsigned Ret; |
| 449 | if (!::CryptGenRandom(CryptoProvider, sizeof(Ret), |
| 450 | reinterpret_cast<BYTE *>(&Ret))) |
Paul Robinson | 8ab79a1 | 2015-11-11 20:49:32 +0000 | [diff] [blame] | 451 | ReportLastErrorFatal("Could not generate a random number"); |
Aaron Ballman | 3f5e8b8 | 2014-02-11 02:47:33 +0000 | [diff] [blame] | 452 | return Ret; |
Aaron Ballman | 7844073 | 2014-02-04 14:49:21 +0000 | [diff] [blame] | 453 | } |