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 //===----------------------------------------------------------------------===//
21 #if (HAVE_LIBPSAPI != 1)
22 #error "libpsapi.a should be present"
25 #pragma comment(lib, "psapi.lib")
28 //===----------------------------------------------------------------------===//
29 //=== WARNING: Implementation here must contain only Win32 specific code
30 //=== and must not be UNIX code
31 //===----------------------------------------------------------------------===//
34 // This ban should be lifted when MinGW 1.0+ has defined this value.
42 process::id_type self_process::get_id() {
43 return GetCurrentProcessId();
46 static TimeValue getTimeValueFromFILETIME(FILETIME Time) {
47 ULARGE_INTEGER TimeInteger;
48 TimeInteger.LowPart = Time.dwLowDateTime;
49 TimeInteger.HighPart = Time.dwHighDateTime;
51 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
53 static_cast<TimeValue::SecondsType>(TimeInteger.QuadPart / 10000000),
54 static_cast<TimeValue::NanoSecondsType>(
55 (TimeInteger.QuadPart % 10000000) * 100));
58 TimeValue self_process::get_user_time() const {
59 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
60 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
64 return getTimeValueFromFILETIME(UserTime);
67 TimeValue self_process::get_system_time() const {
68 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
69 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
73 return getTimeValueFromFILETIME(KernelTime);
76 // This function retrieves the page size using GetSystemInfo and is present
77 // solely so it can be called once to initialize the self_process member below.
78 static unsigned getPageSize() {
79 // NOTE: A 32-bit application running under WOW64 is supposed to use
80 // GetNativeSystemInfo. However, this interface is not present prior
81 // to Windows XP so to use it requires dynamic linking. It is not clear
82 // how this affects the reported page size, if at all. One could argue
83 // that LLVM ought to run as 64-bits on a 64-bit system, anyway.
86 // FIXME: FileOffset in MapViewOfFile() should be aligned to not dwPageSize,
87 // but dwAllocationGranularity.
88 return static_cast<unsigned>(info.dwPageSize);
91 // This constructor guaranteed to be run exactly once on a single thread, and
92 // sets up various process invariants that can be queried cheaply from then on.
93 self_process::self_process() : PageSize(getPageSize()) {
98 Process::GetMallocUsage()
101 hinfo._pentry = NULL;
105 while (_heapwalk(&hinfo) == _HEAPOK)
111 void Process::GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
112 TimeValue &sys_time) {
113 elapsed = TimeValue::now();
115 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
116 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
120 user_time = getTimeValueFromFILETIME(UserTime);
121 sys_time = getTimeValueFromFILETIME(KernelTime);
124 // Some LLVM programs such as bugpoint produce core files as a normal part of
125 // their operation. To prevent the disk from filling up, this configuration
126 // item does what's necessary to prevent their generation.
127 void Process::PreventCoreFiles() {
128 // Windows does have the concept of core files, called minidumps. However,
129 // disabling minidumps for a particular application extends past the lifetime
130 // of that application, which is the incorrect behavior for this API.
131 // Additionally, the APIs require elevated privileges to disable and re-
132 // enable minidumps, which makes this untenable. For more information, see
133 // WerAddExcludedApplication and WerRemoveExcludedApplication (Vista and
136 // Windows also has modal pop-up message boxes. As this method is used by
137 // bugpoint, preventing these pop-ups is additionally important.
138 SetErrorMode(SEM_FAILCRITICALERRORS |
139 SEM_NOGPFAULTERRORBOX |
140 SEM_NOOPENFILEERRORBOX);
143 bool Process::StandardInIsUserInput() {
144 return FileDescriptorIsDisplayed(0);
147 bool Process::StandardOutIsDisplayed() {
148 return FileDescriptorIsDisplayed(1);
151 bool Process::StandardErrIsDisplayed() {
152 return FileDescriptorIsDisplayed(2);
155 bool Process::FileDescriptorIsDisplayed(int fd) {
156 DWORD Mode; // Unused
157 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
160 unsigned Process::StandardOutColumns() {
161 unsigned Columns = 0;
162 CONSOLE_SCREEN_BUFFER_INFO csbi;
163 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
164 Columns = csbi.dwSize.X;
168 unsigned Process::StandardErrColumns() {
169 unsigned Columns = 0;
170 CONSOLE_SCREEN_BUFFER_INFO csbi;
171 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
172 Columns = csbi.dwSize.X;
176 // The terminal always has colors.
177 bool Process::FileDescriptorHasColors(int fd) {
178 return FileDescriptorIsDisplayed(fd);
181 bool Process::StandardOutHasColors() {
182 return FileDescriptorHasColors(1);
185 bool Process::StandardErrHasColors() {
186 return FileDescriptorHasColors(2);
196 :defaultColor(GetCurrentColor()) {}
197 static unsigned GetCurrentColor() {
198 CONSOLE_SCREEN_BUFFER_INFO csbi;
199 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
200 return csbi.wAttributes;
203 WORD operator()() const { return defaultColor; }
206 DefaultColors defaultColors;
209 bool Process::ColorNeedsFlush() {
213 const char *Process::OutputBold(bool bg) {
214 WORD colors = DefaultColors::GetCurrentColor();
216 colors |= BACKGROUND_INTENSITY;
218 colors |= FOREGROUND_INTENSITY;
219 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
223 const char *Process::OutputColor(char code, bool bold, bool bg) {
226 colors = ((code&1) ? BACKGROUND_RED : 0) |
227 ((code&2) ? BACKGROUND_GREEN : 0 ) |
228 ((code&4) ? BACKGROUND_BLUE : 0);
230 colors |= BACKGROUND_INTENSITY;
232 colors = ((code&1) ? FOREGROUND_RED : 0) |
233 ((code&2) ? FOREGROUND_GREEN : 0 ) |
234 ((code&4) ? FOREGROUND_BLUE : 0);
236 colors |= FOREGROUND_INTENSITY;
238 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
242 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
243 CONSOLE_SCREEN_BUFFER_INFO info;
244 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
245 return info.wAttributes;
248 const char *Process::OutputReverse() {
249 const WORD attributes
250 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
252 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
253 FOREGROUND_RED | FOREGROUND_INTENSITY;
254 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
255 BACKGROUND_RED | BACKGROUND_INTENSITY;
256 const WORD color_mask = foreground_mask | background_mask;
258 WORD new_attributes =
259 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
260 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
261 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
262 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
263 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
264 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
265 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
266 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
268 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
270 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
274 const char *Process::ResetColor() {
275 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());