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 GetCurrentProcess();
47 // This function retrieves the page size using GetSystemInfo and is present
48 // solely so it can be called once in Process::GetPageSize to initialize the
49 // static variable PageSize.
50 inline unsigned GetPageSizeOnce() {
51 // NOTE: A 32-bit application running under WOW64 is supposed to use
52 // GetNativeSystemInfo. However, this interface is not present prior
53 // to Windows XP so to use it requires dynamic linking. It is not clear
54 // how this affects the reported page size, if at all. One could argue
55 // that LLVM ought to run as 64-bits on a 64-bit system, anyway.
58 return static_cast<unsigned>(info.dwPageSize);
62 Process::GetPageSize() {
63 static const unsigned PageSize = GetPageSizeOnce();
68 Process::GetMallocUsage()
75 while (_heapwalk(&hinfo) == _HEAPOK)
82 Process::GetTotalMemoryUsage()
84 PROCESS_MEMORY_COUNTERS pmc;
85 GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
86 return pmc.PagefileUsage;
90 Process::GetTimeUsage(
91 TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
93 elapsed = TimeValue::now();
95 uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
96 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
97 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
98 (FILETIME*)&UserTime);
100 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
101 user_time.seconds( UserTime / 10000000 );
102 user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
103 sys_time.seconds( KernelTime / 10000000 );
104 sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
107 int Process::GetCurrentUserId()
112 int Process::GetCurrentGroupId()
117 // Some LLVM programs such as bugpoint produce core files as a normal part of
118 // their operation. To prevent the disk from filling up, this configuration item
119 // does what's necessary to prevent their generation.
120 void Process::PreventCoreFiles() {
121 // Windows doesn't do core files, but it does do modal pop-up message
122 // boxes. As this method is used by bugpoint, preventing these pop-ups
123 // is the moral equivalent of suppressing core files.
124 SetErrorMode(SEM_FAILCRITICALERRORS |
125 SEM_NOGPFAULTERRORBOX |
126 SEM_NOOPENFILEERRORBOX);
129 bool Process::StandardInIsUserInput() {
130 return FileDescriptorIsDisplayed(0);
133 bool Process::StandardOutIsDisplayed() {
134 return FileDescriptorIsDisplayed(1);
137 bool Process::StandardErrIsDisplayed() {
138 return FileDescriptorIsDisplayed(2);
141 bool Process::FileDescriptorIsDisplayed(int fd) {
142 DWORD Mode; // Unused
143 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
146 unsigned Process::StandardOutColumns() {
147 unsigned Columns = 0;
148 CONSOLE_SCREEN_BUFFER_INFO csbi;
149 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
150 Columns = csbi.dwSize.X;
154 unsigned Process::StandardErrColumns() {
155 unsigned Columns = 0;
156 CONSOLE_SCREEN_BUFFER_INFO csbi;
157 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
158 Columns = csbi.dwSize.X;
162 // The terminal always has colors.
163 bool Process::FileDescriptorHasColors(int fd) {
164 return FileDescriptorIsDisplayed(fd);
167 bool Process::StandardOutHasColors() {
168 return FileDescriptorHasColors(1);
171 bool Process::StandardErrHasColors() {
172 return FileDescriptorHasColors(2);
182 :defaultColor(GetCurrentColor()) {}
183 static unsigned GetCurrentColor() {
184 CONSOLE_SCREEN_BUFFER_INFO csbi;
185 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
186 return csbi.wAttributes;
189 WORD operator()() const { return defaultColor; }
192 DefaultColors defaultColors;
195 bool Process::ColorNeedsFlush() {
199 const char *Process::OutputBold(bool bg) {
200 WORD colors = DefaultColors::GetCurrentColor();
202 colors |= BACKGROUND_INTENSITY;
204 colors |= FOREGROUND_INTENSITY;
205 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
209 const char *Process::OutputColor(char code, bool bold, bool bg) {
212 colors = ((code&1) ? BACKGROUND_RED : 0) |
213 ((code&2) ? BACKGROUND_GREEN : 0 ) |
214 ((code&4) ? BACKGROUND_BLUE : 0);
216 colors |= BACKGROUND_INTENSITY;
218 colors = ((code&1) ? FOREGROUND_RED : 0) |
219 ((code&2) ? FOREGROUND_GREEN : 0 ) |
220 ((code&4) ? FOREGROUND_BLUE : 0);
222 colors |= FOREGROUND_INTENSITY;
224 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
228 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
229 CONSOLE_SCREEN_BUFFER_INFO info;
230 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
231 return info.wAttributes;
234 const char *Process::OutputReverse() {
235 const WORD attributes
236 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
238 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
239 FOREGROUND_RED | FOREGROUND_INTENSITY;
240 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
241 BACKGROUND_RED | BACKGROUND_INTENSITY;
242 const WORD color_mask = foreground_mask | background_mask;
244 WORD new_attributes =
245 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
246 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
247 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
248 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
249 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
250 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
251 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
252 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
254 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
256 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
260 const char *Process::ResetColor() {
261 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());