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