1 //===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of the Process class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/ConvertUTF.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/StringSaver.h"
19 #include "llvm/Support/WindowsError.h"
22 // The Windows.h header must be after LLVM and standard headers.
23 #include "WindowsSupport.h"
30 #if !defined(__MINGW32__)
31 #pragma comment(lib, "psapi.lib")
32 #pragma comment(lib, "shell32.lib")
35 //===----------------------------------------------------------------------===//
36 //=== WARNING: Implementation here must contain only Win32 specific code
37 //=== and must not be UNIX code
38 //===----------------------------------------------------------------------===//
41 // This ban should be lifted when MinGW 1.0+ has defined this value.
47 // This function retrieves the page size using GetNativeSystemInfo() and is
48 // present solely so it can be called once to initialize the self_process member
50 static unsigned computePageSize() {
51 // GetNativeSystemInfo() provides the physical page size which may differ
52 // from GetSystemInfo() in 32-bit applications running under WOW64.
54 GetNativeSystemInfo(&info);
55 // FIXME: FileOffset in MapViewOfFile() should be aligned to not dwPageSize,
56 // but dwAllocationGranularity.
57 return static_cast<unsigned>(info.dwPageSize);
60 unsigned Process::getPageSize() {
61 static unsigned Ret = computePageSize();
66 Process::GetMallocUsage()
73 while (_heapwalk(&hinfo) == _HEAPOK)
79 void Process::GetTimeUsage(TimePoint<> &elapsed, std::chrono::nanoseconds &user_time,
80 std::chrono::nanoseconds &sys_time) {
81 elapsed = std::chrono::system_clock::now();;
83 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
84 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
88 user_time = toDuration(UserTime);
89 sys_time = toDuration(KernelTime);
92 // Some LLVM programs such as bugpoint produce core files as a normal part of
93 // their operation. To prevent the disk from filling up, this configuration
94 // item does what's necessary to prevent their generation.
95 void Process::PreventCoreFiles() {
96 // Windows does have the concept of core files, called minidumps. However,
97 // disabling minidumps for a particular application extends past the lifetime
98 // of that application, which is the incorrect behavior for this API.
99 // Additionally, the APIs require elevated privileges to disable and re-
100 // enable minidumps, which makes this untenable. For more information, see
101 // WerAddExcludedApplication and WerRemoveExcludedApplication (Vista and
104 // Windows also has modal pop-up message boxes. As this method is used by
105 // bugpoint, preventing these pop-ups is additionally important.
106 SetErrorMode(SEM_FAILCRITICALERRORS |
107 SEM_NOGPFAULTERRORBOX |
108 SEM_NOOPENFILEERRORBOX);
110 coreFilesPrevented = true;
113 /// Returns the environment variable \arg Name's value as a string encoded in
114 /// UTF-8. \arg Name is assumed to be in UTF-8 encoding.
115 Optional<std::string> Process::GetEnv(StringRef Name) {
116 // Convert the argument to UTF-16 to pass it to _wgetenv().
117 SmallVector<wchar_t, 128> NameUTF16;
118 if (windows::UTF8ToUTF16(Name, NameUTF16))
121 // Environment variable can be encoded in non-UTF8 encoding, and there's no
122 // way to know what the encoding is. The only reliable way to look up
123 // multibyte environment variable is to use GetEnvironmentVariableW().
124 SmallVector<wchar_t, MAX_PATH> Buf;
125 size_t Size = MAX_PATH;
128 SetLastError(NO_ERROR);
130 GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.capacity());
131 if (Size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND)
134 // Try again with larger buffer.
135 } while (Size > Buf.capacity());
138 // Convert the result from UTF-16 to UTF-8.
139 SmallVector<char, MAX_PATH> Res;
140 if (windows::UTF16ToUTF8(Buf.data(), Size, Res))
142 return std::string(Res.data());
145 /// Perform wildcard expansion of Arg, or just push it into Args if it doesn't
146 /// have wildcards or doesn't match any files.
147 static std::error_code WildcardExpand(StringRef Arg,
148 SmallVectorImpl<const char *> &Args,
149 StringSaver &Saver) {
152 // Don't expand Arg if it does not contain any wildcard characters. This is
153 // the common case. Also don't wildcard expand /?. Always treat it as an
155 if (Arg.find_first_of("*?") == StringRef::npos || Arg == "/?" ||
157 Args.push_back(Arg.data());
161 // Convert back to UTF-16 so we can call FindFirstFileW.
162 SmallVector<wchar_t, MAX_PATH> ArgW;
163 EC = windows::UTF8ToUTF16(Arg, ArgW);
167 // Search for matching files.
168 // FIXME: This assumes the wildcard is only in the file name and not in the
169 // directory portion of the file path. For example, it doesn't handle
170 // "*\foo.c" nor "s?c\bar.cpp".
171 WIN32_FIND_DATAW FileData;
172 HANDLE FindHandle = FindFirstFileW(ArgW.data(), &FileData);
173 if (FindHandle == INVALID_HANDLE_VALUE) {
174 Args.push_back(Arg.data());
178 // Extract any directory part of the argument.
179 SmallString<MAX_PATH> Dir = Arg;
180 sys::path::remove_filename(Dir);
181 const int DirSize = Dir.size();
184 SmallString<MAX_PATH> FileName;
185 EC = windows::UTF16ToUTF8(FileData.cFileName, wcslen(FileData.cFileName),
190 // Append FileName to Dir, and remove it afterwards.
191 llvm::sys::path::append(Dir, FileName);
192 Args.push_back(Saver.save(StringRef(Dir)).data());
194 } while (FindNextFileW(FindHandle, &FileData));
196 FindClose(FindHandle);
200 static std::error_code GetExecutableName(SmallVectorImpl<char> &Filename) {
201 // The first argument may contain just the name of the executable (e.g.,
202 // "clang") rather than the full path, so swap it with the full path.
203 wchar_t ModuleName[MAX_PATH];
204 size_t Length = ::GetModuleFileNameW(NULL, ModuleName, MAX_PATH);
205 if (Length == 0 || Length == MAX_PATH) {
206 return mapWindowsError(GetLastError());
209 // If the first argument is a shortened (8.3) name (which is possible even
210 // if we got the module name), the driver will have trouble distinguishing it
211 // (e.g., clang.exe v. clang++.exe), so expand it now.
212 Length = GetLongPathNameW(ModuleName, ModuleName, MAX_PATH);
214 return mapWindowsError(GetLastError());
215 if (Length > MAX_PATH) {
216 // We're not going to try to deal with paths longer than MAX_PATH, so we'll
217 // treat this as an error. GetLastError() returns ERROR_SUCCESS, which
218 // isn't useful, so we'll hardcode an appropriate error value.
219 return mapWindowsError(ERROR_INSUFFICIENT_BUFFER);
222 std::error_code EC = windows::UTF16ToUTF8(ModuleName, Length, Filename);
226 StringRef Base = sys::path::filename(Filename.data());
227 Filename.assign(Base.begin(), Base.end());
228 return std::error_code();
232 windows::GetCommandLineArguments(SmallVectorImpl<const char *> &Args,
233 BumpPtrAllocator &Alloc) {
234 const wchar_t *CmdW = GetCommandLineW();
237 SmallString<MAX_PATH> Cmd;
238 EC = windows::UTF16ToUTF8(CmdW, wcslen(CmdW), Cmd);
242 SmallVector<const char *, 20> TmpArgs;
243 StringSaver Saver(Alloc);
244 cl::TokenizeWindowsCommandLine(Cmd, Saver, TmpArgs, /*MarkEOLs=*/false);
246 for (const char *Arg : TmpArgs) {
247 EC = WildcardExpand(Arg, Args, Saver);
252 SmallVector<char, MAX_PATH> Arg0(Args[0], Args[0] + strlen(Args[0]));
253 SmallVector<char, MAX_PATH> Filename;
254 sys::path::remove_filename(Arg0);
255 EC = GetExecutableName(Filename);
258 sys::path::append(Arg0, Filename);
259 Args[0] = Saver.save(Arg0).data();
260 return std::error_code();
263 std::error_code Process::FixupStandardFileDescriptors() {
264 return std::error_code();
267 std::error_code Process::SafelyCloseFileDescriptor(int FD) {
269 return std::error_code(errno, std::generic_category());
270 return std::error_code();
273 bool Process::StandardInIsUserInput() {
274 return FileDescriptorIsDisplayed(0);
277 bool Process::StandardOutIsDisplayed() {
278 return FileDescriptorIsDisplayed(1);
281 bool Process::StandardErrIsDisplayed() {
282 return FileDescriptorIsDisplayed(2);
285 bool Process::FileDescriptorIsDisplayed(int fd) {
286 DWORD Mode; // Unused
287 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
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;
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;
306 // The terminal always has colors.
307 bool Process::FileDescriptorHasColors(int fd) {
308 return FileDescriptorIsDisplayed(fd);
311 bool Process::StandardOutHasColors() {
312 return FileDescriptorHasColors(1);
315 bool Process::StandardErrHasColors() {
316 return FileDescriptorHasColors(2);
319 static bool UseANSI = false;
320 void Process::UseANSIEscapeCodes(bool enable) {
321 #if defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
323 HANDLE Console = GetStdHandle(STD_OUTPUT_HANDLE);
325 GetConsoleMode(Console, &Mode);
326 Mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
327 SetConsoleMode(Console, Mode);
340 :defaultColor(GetCurrentColor()) {}
341 static unsigned GetCurrentColor() {
342 CONSOLE_SCREEN_BUFFER_INFO csbi;
343 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
344 return csbi.wAttributes;
347 WORD operator()() const { return defaultColor; }
350 DefaultColors defaultColors;
352 WORD fg_color(WORD color) {
353 return color & (FOREGROUND_BLUE | FOREGROUND_GREEN |
354 FOREGROUND_INTENSITY | FOREGROUND_RED);
357 WORD bg_color(WORD color) {
358 return color & (BACKGROUND_BLUE | BACKGROUND_GREEN |
359 BACKGROUND_INTENSITY | BACKGROUND_RED);
363 bool Process::ColorNeedsFlush() {
367 const char *Process::OutputBold(bool bg) {
368 if (UseANSI) return "\033[1m";
370 WORD colors = DefaultColors::GetCurrentColor();
372 colors |= BACKGROUND_INTENSITY;
374 colors |= FOREGROUND_INTENSITY;
375 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
379 const char *Process::OutputColor(char code, bool bold, bool bg) {
380 if (UseANSI) return colorcodes[bg?1:0][bold?1:0][code&7];
382 WORD current = DefaultColors::GetCurrentColor();
385 colors = ((code&1) ? BACKGROUND_RED : 0) |
386 ((code&2) ? BACKGROUND_GREEN : 0 ) |
387 ((code&4) ? BACKGROUND_BLUE : 0);
389 colors |= BACKGROUND_INTENSITY;
390 colors |= fg_color(current);
392 colors = ((code&1) ? FOREGROUND_RED : 0) |
393 ((code&2) ? FOREGROUND_GREEN : 0 ) |
394 ((code&4) ? FOREGROUND_BLUE : 0);
396 colors |= FOREGROUND_INTENSITY;
397 colors |= bg_color(current);
399 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
403 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
404 CONSOLE_SCREEN_BUFFER_INFO info;
405 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
406 return info.wAttributes;
409 const char *Process::OutputReverse() {
410 if (UseANSI) return "\033[7m";
412 const WORD attributes
413 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
415 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
416 FOREGROUND_RED | FOREGROUND_INTENSITY;
417 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
418 BACKGROUND_RED | BACKGROUND_INTENSITY;
419 const WORD color_mask = foreground_mask | background_mask;
421 WORD new_attributes =
422 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
423 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
424 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
425 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
426 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
427 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
428 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
429 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
431 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
433 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
437 const char *Process::ResetColor() {
438 if (UseANSI) return "\033[0m";
439 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
443 // Include GetLastError() in a fatal error message.
444 static void ReportLastErrorFatal(const char *Msg) {
446 MakeErrMsg(&ErrMsg, Msg);
447 report_fatal_error(ErrMsg);
450 unsigned Process::GetRandomNumber() {
452 if (!::CryptAcquireContextW(&HCPC, NULL, NULL, PROV_RSA_FULL,
453 CRYPT_VERIFYCONTEXT))
454 ReportLastErrorFatal("Could not acquire a cryptographic context");
456 ScopedCryptContext CryptoProvider(HCPC);
458 if (!::CryptGenRandom(CryptoProvider, sizeof(Ret),
459 reinterpret_cast<BYTE *>(&Ret)))
460 ReportLastErrorFatal("Could not generate a random number");