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