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; |
| 51 | |
Chandler Carruth | 97683aa | 2012-12-31 11:17:50 +0000 | [diff] [blame] | 52 | |
| 53 | process::id_type self_process::get_id() { |
Aaron Ballman | f0b5384 | 2013-06-08 20:29:03 +0000 | [diff] [blame] | 54 | return GetCurrentProcessId(); |
Chandler Carruth | 97683aa | 2012-12-31 11:17:50 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 57 | static TimeValue getTimeValueFromFILETIME(FILETIME Time) { |
| 58 | ULARGE_INTEGER TimeInteger; |
| 59 | TimeInteger.LowPart = Time.dwLowDateTime; |
| 60 | TimeInteger.HighPart = Time.dwHighDateTime; |
| 61 | |
| 62 | // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond) |
| 63 | return TimeValue( |
| 64 | static_cast<TimeValue::SecondsType>(TimeInteger.QuadPart / 10000000), |
| 65 | static_cast<TimeValue::NanoSecondsType>( |
| 66 | (TimeInteger.QuadPart % 10000000) * 100)); |
| 67 | } |
| 68 | |
| 69 | TimeValue self_process::get_user_time() const { |
| 70 | FILETIME ProcCreate, ProcExit, KernelTime, UserTime; |
| 71 | if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime, |
| 72 | &UserTime) == 0) |
| 73 | return TimeValue(); |
| 74 | |
| 75 | return getTimeValueFromFILETIME(UserTime); |
| 76 | } |
| 77 | |
| 78 | TimeValue self_process::get_system_time() const { |
| 79 | FILETIME ProcCreate, ProcExit, KernelTime, UserTime; |
| 80 | if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime, |
| 81 | &UserTime) == 0) |
| 82 | return TimeValue(); |
| 83 | |
| 84 | return getTimeValueFromFILETIME(KernelTime); |
| 85 | } |
| 86 | |
Alp Toker | d71b6df | 2014-05-19 16:13:28 +0000 | [diff] [blame] | 87 | // This function retrieves the page size using GetNativeSystemInfo() and is |
| 88 | // present solely so it can be called once to initialize the self_process member |
| 89 | // below. |
Chandler Carruth | 15dcad9 | 2012-12-31 23:23:35 +0000 | [diff] [blame] | 90 | static unsigned getPageSize() { |
Alp Toker | d71b6df | 2014-05-19 16:13:28 +0000 | [diff] [blame] | 91 | // GetNativeSystemInfo() provides the physical page size which may differ |
| 92 | // from GetSystemInfo() in 32-bit applications running under WOW64. |
Reid Spencer | 91886b7 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 93 | SYSTEM_INFO info; |
Alp Toker | d71b6df | 2014-05-19 16:13:28 +0000 | [diff] [blame] | 94 | GetNativeSystemInfo(&info); |
NAKAMURA Takumi | 7a04234 | 2013-09-04 14:12:26 +0000 | [diff] [blame] | 95 | // FIXME: FileOffset in MapViewOfFile() should be aligned to not dwPageSize, |
| 96 | // but dwAllocationGranularity. |
Reid Spencer | 91886b7 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 97 | return static_cast<unsigned>(info.dwPageSize); |
| 98 | } |
| 99 | |
Chandler Carruth | 15dcad9 | 2012-12-31 23:23:35 +0000 | [diff] [blame] | 100 | // This constructor guaranteed to be run exactly once on a single thread, and |
| 101 | // sets up various process invariants that can be queried cheaply from then on. |
| 102 | self_process::self_process() : PageSize(getPageSize()) { |
Reid Spencer | 91886b7 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Chandler Carruth | 15dcad9 | 2012-12-31 23:23:35 +0000 | [diff] [blame] | 105 | |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 106 | size_t |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 107 | Process::GetMallocUsage() |
| 108 | { |
Jeff Cohen | 7ae0bc7 | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 109 | _HEAPINFO hinfo; |
| 110 | hinfo._pentry = NULL; |
| 111 | |
| 112 | size_t size = 0; |
| 113 | |
| 114 | while (_heapwalk(&hinfo) == _HEAPOK) |
| 115 | size += hinfo._size; |
| 116 | |
| 117 | return size; |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 120 | void Process::GetTimeUsage(TimeValue &elapsed, TimeValue &user_time, |
| 121 | TimeValue &sys_time) { |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 122 | elapsed = TimeValue::now(); |
| 123 | |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 124 | FILETIME ProcCreate, ProcExit, KernelTime, UserTime; |
| 125 | if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime, |
| 126 | &UserTime) == 0) |
| 127 | return; |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 128 | |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 129 | user_time = getTimeValueFromFILETIME(UserTime); |
Chandler Carruth | b79a7aa | 2013-01-04 23:46:04 +0000 | [diff] [blame] | 130 | sys_time = getTimeValueFromFILETIME(KernelTime); |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 133 | // 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] | 134 | // their operation. To prevent the disk from filling up, this configuration |
| 135 | // item does what's necessary to prevent their generation. |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 136 | void Process::PreventCoreFiles() { |
Aaron Ballman | dcd5757 | 2013-08-16 14:33:07 +0000 | [diff] [blame] | 137 | // Windows does have the concept of core files, called minidumps. However, |
| 138 | // disabling minidumps for a particular application extends past the lifetime |
| 139 | // of that application, which is the incorrect behavior for this API. |
| 140 | // Additionally, the APIs require elevated privileges to disable and re- |
| 141 | // enable minidumps, which makes this untenable. For more information, see |
| 142 | // WerAddExcludedApplication and WerRemoveExcludedApplication (Vista and |
| 143 | // later). |
| 144 | // |
| 145 | // Windows also has modal pop-up message boxes. As this method is used by |
| 146 | // bugpoint, preventing these pop-ups is additionally important. |
Jeff Cohen | 81549a5 | 2005-02-18 07:05:18 +0000 | [diff] [blame] | 147 | SetErrorMode(SEM_FAILCRITICALERRORS | |
| 148 | SEM_NOGPFAULTERRORBOX | |
| 149 | SEM_NOOPENFILEERRORBOX); |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 152 | /// Returns the environment variable \arg Name's value as a string encoded in |
| 153 | /// UTF-8. \arg Name is assumed to be in UTF-8 encoding. |
| 154 | Optional<std::string> Process::GetEnv(StringRef Name) { |
| 155 | // Convert the argument to UTF-16 to pass it to _wgetenv(). |
| 156 | SmallVector<wchar_t, 128> NameUTF16; |
Ahmed Charles | ce30de9 | 2014-03-05 05:04:00 +0000 | [diff] [blame] | 157 | if (windows::UTF8ToUTF16(Name, NameUTF16)) |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 158 | return None; |
| 159 | |
| 160 | // Environment variable can be encoded in non-UTF8 encoding, and there's no |
| 161 | // way to know what the encoding is. The only reliable way to look up |
| 162 | // multibyte environment variable is to use GetEnvironmentVariableW(). |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 163 | SmallVector<wchar_t, MAX_PATH> Buf; |
| 164 | size_t Size = MAX_PATH; |
| 165 | do { |
David Majnemer | f07777c | 2013-10-07 21:57:07 +0000 | [diff] [blame] | 166 | Buf.reserve(Size); |
| 167 | Size = |
| 168 | GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.capacity()); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 169 | if (Size == 0) |
| 170 | return None; |
| 171 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 172 | // Try again with larger buffer. |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 173 | } while (Size > Buf.capacity()); |
| 174 | Buf.set_size(Size); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 175 | |
| 176 | // Convert the result from UTF-16 to UTF-8. |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 177 | SmallVector<char, MAX_PATH> Res; |
Ahmed Charles | ce30de9 | 2014-03-05 05:04:00 +0000 | [diff] [blame] | 178 | if (windows::UTF16ToUTF8(Buf.data(), Size, Res)) |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 179 | return None; |
David Majnemer | f07777c | 2013-10-07 21:57:07 +0000 | [diff] [blame] | 180 | return std::string(Res.data()); |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 183 | static error_code windows_error(DWORD E) { |
Rafael Espindola | 5c4f829 | 2014-06-11 19:05:50 +0000 | [diff] [blame^] | 184 | return mapWindowsError(E); |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 185 | } |
| 186 | |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 187 | error_code |
| 188 | Process::GetArgumentVector(SmallVectorImpl<const char *> &Args, |
| 189 | ArrayRef<const char *>, |
| 190 | SpecificBumpPtrAllocator<char> &ArgAllocator) { |
| 191 | int NewArgCount; |
| 192 | error_code ec; |
| 193 | |
| 194 | wchar_t **UnicodeCommandLine = CommandLineToArgvW(GetCommandLineW(), |
| 195 | &NewArgCount); |
| 196 | if (!UnicodeCommandLine) |
| 197 | return windows_error(::GetLastError()); |
| 198 | |
| 199 | Args.reserve(NewArgCount); |
| 200 | |
| 201 | for (int i = 0; i < NewArgCount; ++i) { |
| 202 | SmallVector<char, MAX_PATH> NewArgString; |
| 203 | ec = windows::UTF16ToUTF8(UnicodeCommandLine[i], |
| 204 | wcslen(UnicodeCommandLine[i]), |
| 205 | NewArgString); |
| 206 | if (ec) |
| 207 | break; |
| 208 | |
| 209 | char *Buffer = ArgAllocator.Allocate(NewArgString.size() + 1); |
| 210 | ::memcpy(Buffer, NewArgString.data(), NewArgString.size() + 1); |
| 211 | Args.push_back(Buffer); |
| 212 | } |
| 213 | LocalFree(UnicodeCommandLine); |
| 214 | if (ec) |
| 215 | return ec; |
| 216 | |
Rafael Espindola | 03bddfe | 2014-05-31 01:37:45 +0000 | [diff] [blame] | 217 | return error_code(); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Jeff Cohen | b90c31f | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 220 | bool Process::StandardInIsUserInput() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 221 | return FileDescriptorIsDisplayed(0); |
Jeff Cohen | b90c31f | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | bool Process::StandardOutIsDisplayed() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 225 | return FileDescriptorIsDisplayed(1); |
Jeff Cohen | b90c31f | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | bool Process::StandardErrIsDisplayed() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 229 | return FileDescriptorIsDisplayed(2); |
| 230 | } |
| 231 | |
| 232 | bool Process::FileDescriptorIsDisplayed(int fd) { |
Bill Wendling | 2b07965 | 2012-07-19 00:06:06 +0000 | [diff] [blame] | 233 | DWORD Mode; // Unused |
NAKAMURA Takumi | 23ebef1 | 2010-11-10 08:37:47 +0000 | [diff] [blame] | 234 | return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0); |
Jeff Cohen | b90c31f | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Douglas Gregor | 1543661 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 237 | unsigned Process::StandardOutColumns() { |
| 238 | unsigned Columns = 0; |
| 239 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 240 | if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) |
| 241 | Columns = csbi.dwSize.X; |
| 242 | return Columns; |
| 243 | } |
| 244 | |
| 245 | unsigned Process::StandardErrColumns() { |
| 246 | unsigned Columns = 0; |
| 247 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 248 | if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)) |
| 249 | Columns = csbi.dwSize.X; |
| 250 | return Columns; |
| 251 | } |
| 252 | |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 253 | // The terminal always has colors. |
Benjamin Kramer | dfaa0f3 | 2012-07-20 19:49:33 +0000 | [diff] [blame] | 254 | bool Process::FileDescriptorHasColors(int fd) { |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 255 | return FileDescriptorIsDisplayed(fd); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | bool Process::StandardOutHasColors() { |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 259 | return FileDescriptorHasColors(1); |
| 260 | } |
| 261 | |
| 262 | bool Process::StandardErrHasColors() { |
| 263 | return FileDescriptorHasColors(2); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 264 | } |
Torok Edwin | 63e44bb | 2009-06-04 08:18:25 +0000 | [diff] [blame] | 265 | |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 266 | static bool UseANSI = false; |
| 267 | void Process::UseANSIEscapeCodes(bool enable) { |
| 268 | UseANSI = enable; |
| 269 | } |
| 270 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 271 | namespace { |
| 272 | class DefaultColors |
| 273 | { |
| 274 | private: |
| 275 | WORD defaultColor; |
| 276 | public: |
| 277 | DefaultColors() |
| 278 | :defaultColor(GetCurrentColor()) {} |
| 279 | static unsigned GetCurrentColor() { |
| 280 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 281 | if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) |
| 282 | return csbi.wAttributes; |
| 283 | return 0; |
| 284 | } |
| 285 | WORD operator()() const { return defaultColor; } |
| 286 | }; |
| 287 | |
| 288 | DefaultColors defaultColors; |
| 289 | } |
| 290 | |
| 291 | bool Process::ColorNeedsFlush() { |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 292 | return !UseANSI; |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | const char *Process::OutputBold(bool bg) { |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 296 | if (UseANSI) return "\033[1m"; |
| 297 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 298 | WORD colors = DefaultColors::GetCurrentColor(); |
| 299 | if (bg) |
| 300 | colors |= BACKGROUND_INTENSITY; |
| 301 | else |
| 302 | colors |= FOREGROUND_INTENSITY; |
| 303 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors); |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | const char *Process::OutputColor(char code, bool bold, bool bg) { |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 308 | if (UseANSI) return colorcodes[bg?1:0][bold?1:0][code&7]; |
| 309 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 310 | WORD colors; |
| 311 | if (bg) { |
| 312 | colors = ((code&1) ? BACKGROUND_RED : 0) | |
| 313 | ((code&2) ? BACKGROUND_GREEN : 0 ) | |
| 314 | ((code&4) ? BACKGROUND_BLUE : 0); |
| 315 | if (bold) |
| 316 | colors |= BACKGROUND_INTENSITY; |
| 317 | } else { |
| 318 | colors = ((code&1) ? FOREGROUND_RED : 0) | |
| 319 | ((code&2) ? FOREGROUND_GREEN : 0 ) | |
| 320 | ((code&4) ? FOREGROUND_BLUE : 0); |
| 321 | if (bold) |
| 322 | colors |= FOREGROUND_INTENSITY; |
| 323 | } |
| 324 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors); |
| 325 | return 0; |
| 326 | } |
| 327 | |
Benjamin Kramer | 13d16f3 | 2012-04-16 08:56:50 +0000 | [diff] [blame] | 328 | static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) { |
| 329 | CONSOLE_SCREEN_BUFFER_INFO info; |
| 330 | GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); |
| 331 | return info.wAttributes; |
| 332 | } |
| 333 | |
| 334 | const char *Process::OutputReverse() { |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 335 | if (UseANSI) return "\033[7m"; |
| 336 | |
Benjamin Kramer | 13d16f3 | 2012-04-16 08:56:50 +0000 | [diff] [blame] | 337 | const WORD attributes |
| 338 | = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)); |
| 339 | |
| 340 | const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN | |
| 341 | FOREGROUND_RED | FOREGROUND_INTENSITY; |
| 342 | const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN | |
| 343 | BACKGROUND_RED | BACKGROUND_INTENSITY; |
| 344 | const WORD color_mask = foreground_mask | background_mask; |
| 345 | |
| 346 | WORD new_attributes = |
| 347 | ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) | |
| 348 | ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) | |
| 349 | ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) | |
| 350 | ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) | |
| 351 | ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) | |
| 352 | ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) | |
| 353 | ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) | |
| 354 | ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) | |
| 355 | 0; |
| 356 | new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask); |
| 357 | |
| 358 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes); |
| 359 | return 0; |
| 360 | } |
| 361 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 362 | const char *Process::ResetColor() { |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 363 | if (UseANSI) return "\033[0m"; |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 364 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors()); |
| 365 | return 0; |
| 366 | } |
Aaron Ballman | 7844073 | 2014-02-04 14:49:21 +0000 | [diff] [blame] | 367 | |
| 368 | unsigned Process::GetRandomNumber() { |
Aaron Ballman | 3f5e8b8 | 2014-02-11 02:47:33 +0000 | [diff] [blame] | 369 | HCRYPTPROV HCPC; |
| 370 | if (!::CryptAcquireContextW(&HCPC, NULL, NULL, PROV_RSA_FULL, |
| 371 | CRYPT_VERIFYCONTEXT)) |
Alp Toker | 552f2f7 | 2014-06-03 03:01:03 +0000 | [diff] [blame] | 372 | report_fatal_error("Could not acquire a cryptographic context"); |
Aaron Ballman | 3f5e8b8 | 2014-02-11 02:47:33 +0000 | [diff] [blame] | 373 | |
| 374 | ScopedCryptContext CryptoProvider(HCPC); |
| 375 | unsigned Ret; |
| 376 | if (!::CryptGenRandom(CryptoProvider, sizeof(Ret), |
| 377 | reinterpret_cast<BYTE *>(&Ret))) |
Alp Toker | 552f2f7 | 2014-06-03 03:01:03 +0000 | [diff] [blame] | 378 | report_fatal_error("Could not generate a random number"); |
Aaron Ballman | 3f5e8b8 | 2014-02-11 02:47:33 +0000 | [diff] [blame] | 379 | return Ret; |
Aaron Ballman | 7844073 | 2014-02-04 14:49:21 +0000 | [diff] [blame] | 380 | } |