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();
46 // This function retrieves the page size using GetSystemInfo and is present
47 // solely so it can be called once to initialize the self_process member below.
48 static unsigned getPageSize() {
49 // NOTE: A 32-bit application running under WOW64 is supposed to use
50 // GetNativeSystemInfo. However, this interface is not present prior
51 // to Windows XP so to use it requires dynamic linking. It is not clear
52 // how this affects the reported page size, if at all. One could argue
53 // that LLVM ought to run as 64-bits on a 64-bit system, anyway.
56 return static_cast<unsigned>(info.dwPageSize);
59 // This constructor guaranteed to be run exactly once on a single thread, and
60 // sets up various process invariants that can be queried cheaply from then on.
61 self_process::self_process() : PageSize(getPageSize()) {
66 Process::GetMallocUsage()
73 while (_heapwalk(&hinfo) == _HEAPOK)
80 Process::GetTimeUsage(
81 TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
83 elapsed = TimeValue::now();
85 uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
86 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
87 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
88 (FILETIME*)&UserTime);
90 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
91 user_time.seconds( UserTime / 10000000 );
92 user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
93 sys_time.seconds( KernelTime / 10000000 );
94 sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
97 int Process::GetCurrentUserId()
102 int Process::GetCurrentGroupId()
107 // Some LLVM programs such as bugpoint produce core files as a normal part of
108 // their operation. To prevent the disk from filling up, this configuration item
109 // does what's necessary to prevent their generation.
110 void Process::PreventCoreFiles() {
111 // Windows doesn't do core files, but it does do modal pop-up message
112 // boxes. As this method is used by bugpoint, preventing these pop-ups
113 // is the moral equivalent of suppressing core files.
114 SetErrorMode(SEM_FAILCRITICALERRORS |
115 SEM_NOGPFAULTERRORBOX |
116 SEM_NOOPENFILEERRORBOX);
119 bool Process::StandardInIsUserInput() {
120 return FileDescriptorIsDisplayed(0);
123 bool Process::StandardOutIsDisplayed() {
124 return FileDescriptorIsDisplayed(1);
127 bool Process::StandardErrIsDisplayed() {
128 return FileDescriptorIsDisplayed(2);
131 bool Process::FileDescriptorIsDisplayed(int fd) {
132 DWORD Mode; // Unused
133 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
136 unsigned Process::StandardOutColumns() {
137 unsigned Columns = 0;
138 CONSOLE_SCREEN_BUFFER_INFO csbi;
139 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
140 Columns = csbi.dwSize.X;
144 unsigned Process::StandardErrColumns() {
145 unsigned Columns = 0;
146 CONSOLE_SCREEN_BUFFER_INFO csbi;
147 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
148 Columns = csbi.dwSize.X;
152 // The terminal always has colors.
153 bool Process::FileDescriptorHasColors(int fd) {
154 return FileDescriptorIsDisplayed(fd);
157 bool Process::StandardOutHasColors() {
158 return FileDescriptorHasColors(1);
161 bool Process::StandardErrHasColors() {
162 return FileDescriptorHasColors(2);
172 :defaultColor(GetCurrentColor()) {}
173 static unsigned GetCurrentColor() {
174 CONSOLE_SCREEN_BUFFER_INFO csbi;
175 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
176 return csbi.wAttributes;
179 WORD operator()() const { return defaultColor; }
182 DefaultColors defaultColors;
185 bool Process::ColorNeedsFlush() {
189 const char *Process::OutputBold(bool bg) {
190 WORD colors = DefaultColors::GetCurrentColor();
192 colors |= BACKGROUND_INTENSITY;
194 colors |= FOREGROUND_INTENSITY;
195 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
199 const char *Process::OutputColor(char code, bool bold, bool bg) {
202 colors = ((code&1) ? BACKGROUND_RED : 0) |
203 ((code&2) ? BACKGROUND_GREEN : 0 ) |
204 ((code&4) ? BACKGROUND_BLUE : 0);
206 colors |= BACKGROUND_INTENSITY;
208 colors = ((code&1) ? FOREGROUND_RED : 0) |
209 ((code&2) ? FOREGROUND_GREEN : 0 ) |
210 ((code&4) ? FOREGROUND_BLUE : 0);
212 colors |= FOREGROUND_INTENSITY;
214 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
218 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
219 CONSOLE_SCREEN_BUFFER_INFO info;
220 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
221 return info.wAttributes;
224 const char *Process::OutputReverse() {
225 const WORD attributes
226 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
228 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
229 FOREGROUND_RED | FOREGROUND_INTENSITY;
230 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
231 BACKGROUND_RED | BACKGROUND_INTENSITY;
232 const WORD color_mask = foreground_mask | background_mask;
234 WORD new_attributes =
235 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
236 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
237 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
238 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
239 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
240 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
241 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
242 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
244 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
246 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
250 const char *Process::ResetColor() {
251 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());