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.
41 // This function retrieves the page size using GetSystemInfo and is present
42 // solely so it can be called once in Process::GetPageSize to initialize the
43 // static variable PageSize.
44 inline unsigned GetPageSizeOnce() {
45 // NOTE: A 32-bit application running under WOW64 is supposed to use
46 // GetNativeSystemInfo. However, this interface is not present prior
47 // to Windows XP so to use it requires dynamic linking. It is not clear
48 // how this affects the reported page size, if at all. One could argue
49 // that LLVM ought to run as 64-bits on a 64-bit system, anyway.
52 return static_cast<unsigned>(info.dwPageSize);
56 Process::GetPageSize() {
57 static const unsigned PageSize = GetPageSizeOnce();
62 Process::GetMallocUsage()
69 while (_heapwalk(&hinfo) == _HEAPOK)
76 Process::GetTotalMemoryUsage()
78 PROCESS_MEMORY_COUNTERS pmc;
79 GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
80 return pmc.PagefileUsage;
84 Process::GetTimeUsage(
85 TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
87 elapsed = TimeValue::now();
89 uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
90 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
91 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
92 (FILETIME*)&UserTime);
94 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
95 user_time.seconds( UserTime / 10000000 );
96 user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
97 sys_time.seconds( KernelTime / 10000000 );
98 sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
101 int Process::GetCurrentUserId()
106 int Process::GetCurrentGroupId()
111 // Some LLVM programs such as bugpoint produce core files as a normal part of
112 // their operation. To prevent the disk from filling up, this configuration item
113 // does what's necessary to prevent their generation.
114 void Process::PreventCoreFiles() {
115 // Windows doesn't do core files, but it does do modal pop-up message
116 // boxes. As this method is used by bugpoint, preventing these pop-ups
117 // is the moral equivalent of suppressing core files.
118 SetErrorMode(SEM_FAILCRITICALERRORS |
119 SEM_NOGPFAULTERRORBOX |
120 SEM_NOOPENFILEERRORBOX);
123 bool Process::StandardInIsUserInput() {
124 return FileDescriptorIsDisplayed(0);
127 bool Process::StandardOutIsDisplayed() {
128 return FileDescriptorIsDisplayed(1);
131 bool Process::StandardErrIsDisplayed() {
132 return FileDescriptorIsDisplayed(2);
135 bool Process::FileDescriptorIsDisplayed(int fd) {
136 DWORD Mode; // Unused
137 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
140 unsigned Process::StandardOutColumns() {
141 unsigned Columns = 0;
142 CONSOLE_SCREEN_BUFFER_INFO csbi;
143 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
144 Columns = csbi.dwSize.X;
148 unsigned Process::StandardErrColumns() {
149 unsigned Columns = 0;
150 CONSOLE_SCREEN_BUFFER_INFO csbi;
151 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
152 Columns = csbi.dwSize.X;
156 // The terminal always has colors.
157 bool Process::FileDescriptorHasColors(int fd) {
158 return FileDescriptorIsDisplayed(fd);
161 bool Process::StandardOutHasColors() {
162 return FileDescriptorHasColors(1);
165 bool Process::StandardErrHasColors() {
166 return FileDescriptorHasColors(2);
176 :defaultColor(GetCurrentColor()) {}
177 static unsigned GetCurrentColor() {
178 CONSOLE_SCREEN_BUFFER_INFO csbi;
179 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
180 return csbi.wAttributes;
183 WORD operator()() const { return defaultColor; }
186 DefaultColors defaultColors;
189 bool Process::ColorNeedsFlush() {
193 const char *Process::OutputBold(bool bg) {
194 WORD colors = DefaultColors::GetCurrentColor();
196 colors |= BACKGROUND_INTENSITY;
198 colors |= FOREGROUND_INTENSITY;
199 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
203 const char *Process::OutputColor(char code, bool bold, bool bg) {
206 colors = ((code&1) ? BACKGROUND_RED : 0) |
207 ((code&2) ? BACKGROUND_GREEN : 0 ) |
208 ((code&4) ? BACKGROUND_BLUE : 0);
210 colors |= BACKGROUND_INTENSITY;
212 colors = ((code&1) ? FOREGROUND_RED : 0) |
213 ((code&2) ? FOREGROUND_GREEN : 0 ) |
214 ((code&4) ? FOREGROUND_BLUE : 0);
216 colors |= FOREGROUND_INTENSITY;
218 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
222 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
223 CONSOLE_SCREEN_BUFFER_INFO info;
224 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
225 return info.wAttributes;
228 const char *Process::OutputReverse() {
229 const WORD attributes
230 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
232 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
233 FOREGROUND_RED | FOREGROUND_INTENSITY;
234 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
235 BACKGROUND_RED | BACKGROUND_INTENSITY;
236 const WORD color_mask = foreground_mask | background_mask;
238 WORD new_attributes =
239 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
240 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
241 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
242 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
243 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
244 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
245 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
246 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
248 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
250 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
254 const char *Process::ResetColor() {
255 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());