Removing unused functionality.
[oota-llvm.git] / lib / Support / Windows / Process.inc
1 //===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides the Win32 specific implementation of the Process class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Windows.h"
15 #include <direct.h>
16 #include <io.h>
17 #include <malloc.h>
18 #include <psapi.h>
19
20 #ifdef __MINGW32__
21  #if (HAVE_LIBPSAPI != 1)
22   #error "libpsapi.a should be present"
23  #endif
24 #else
25  #pragma comment(lib, "psapi.lib")
26 #endif
27
28 //===----------------------------------------------------------------------===//
29 //=== WARNING: Implementation here must contain only Win32 specific code
30 //===          and must not be UNIX code
31 //===----------------------------------------------------------------------===//
32
33 #ifdef __MINGW32__
34 // This ban should be lifted when MinGW 1.0+ has defined this value.
35 #  define _HEAPOK (-2)
36 #endif
37
38 using namespace llvm;
39 using namespace sys;
40
41
42 process::id_type self_process::get_id() {
43   return GetCurrentProcessId();
44 }
45
46 static TimeValue getTimeValueFromFILETIME(FILETIME Time) {
47   ULARGE_INTEGER TimeInteger;
48   TimeInteger.LowPart = Time.dwLowDateTime;
49   TimeInteger.HighPart = Time.dwHighDateTime;
50
51   // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
52   return TimeValue(
53       static_cast<TimeValue::SecondsType>(TimeInteger.QuadPart / 10000000),
54       static_cast<TimeValue::NanoSecondsType>(
55           (TimeInteger.QuadPart % 10000000) * 100));
56 }
57
58 TimeValue self_process::get_user_time() const {
59   FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
60   if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
61                       &UserTime) == 0)
62     return TimeValue();
63
64   return getTimeValueFromFILETIME(UserTime);
65 }
66
67 TimeValue self_process::get_system_time() const {
68   FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
69   if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
70                       &UserTime) == 0)
71     return TimeValue();
72
73   return getTimeValueFromFILETIME(KernelTime);
74 }
75
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.
84   SYSTEM_INFO info;
85   GetSystemInfo(&info);
86   return static_cast<unsigned>(info.dwPageSize);
87 }
88
89 // This constructor guaranteed to be run exactly once on a single thread, and
90 // sets up various process invariants that can be queried cheaply from then on.
91 self_process::self_process() : PageSize(getPageSize()) {
92 }
93
94
95 size_t
96 Process::GetMallocUsage()
97 {
98   _HEAPINFO hinfo;
99   hinfo._pentry = NULL;
100
101   size_t size = 0;
102
103   while (_heapwalk(&hinfo) == _HEAPOK)
104     size += hinfo._size;
105
106   return size;
107 }
108
109 void Process::GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
110                            TimeValue &sys_time) {
111   elapsed = TimeValue::now();
112
113   FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
114   if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
115                       &UserTime) == 0)
116     return;
117
118   user_time = getTimeValueFromFILETIME(UserTime);
119   sys_time = getTimeValueFromFILETIME(KernelTime);
120 }
121
122 // Some LLVM programs such as bugpoint produce core files as a normal part of
123 // their operation. To prevent the disk from filling up, this configuration
124 // item does what's necessary to prevent their generation.
125 void Process::PreventCoreFiles() {
126   // Windows does have the concept of core files, called minidumps.  However,
127   // disabling minidumps for a particular application extends past the lifetime
128   // of that application, which is the incorrect behavior for this API.
129   // Additionally, the APIs require elevated privileges to disable and re-
130   // enable minidumps, which makes this untenable. For more information, see
131   // WerAddExcludedApplication and WerRemoveExcludedApplication (Vista and
132   // later).
133   //
134   // Windows also has modal pop-up message boxes.  As this method is used by
135   // bugpoint, preventing these pop-ups is additionally important.
136   SetErrorMode(SEM_FAILCRITICALERRORS |
137                SEM_NOGPFAULTERRORBOX |
138                SEM_NOOPENFILEERRORBOX);
139 }
140
141 bool Process::StandardInIsUserInput() {
142   return FileDescriptorIsDisplayed(0);
143 }
144
145 bool Process::StandardOutIsDisplayed() {
146   return FileDescriptorIsDisplayed(1);
147 }
148
149 bool Process::StandardErrIsDisplayed() {
150   return FileDescriptorIsDisplayed(2);
151 }
152
153 bool Process::FileDescriptorIsDisplayed(int fd) {
154   DWORD Mode;  // Unused
155   return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
156 }
157
158 unsigned Process::StandardOutColumns() {
159   unsigned Columns = 0;
160   CONSOLE_SCREEN_BUFFER_INFO csbi;
161   if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
162     Columns = csbi.dwSize.X;
163   return Columns;
164 }
165
166 unsigned Process::StandardErrColumns() {
167   unsigned Columns = 0;
168   CONSOLE_SCREEN_BUFFER_INFO csbi;
169   if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
170     Columns = csbi.dwSize.X;
171   return Columns;
172 }
173
174 // The terminal always has colors.
175 bool Process::FileDescriptorHasColors(int fd) {
176   return FileDescriptorIsDisplayed(fd);
177 }
178
179 bool Process::StandardOutHasColors() {
180   return FileDescriptorHasColors(1);
181 }
182
183 bool Process::StandardErrHasColors() {
184   return FileDescriptorHasColors(2);
185 }
186
187 namespace {
188 class DefaultColors
189 {
190   private:
191     WORD defaultColor;
192   public:
193     DefaultColors()
194      :defaultColor(GetCurrentColor()) {}
195     static unsigned GetCurrentColor() {
196       CONSOLE_SCREEN_BUFFER_INFO csbi;
197       if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
198         return csbi.wAttributes;
199       return 0;
200     }
201     WORD operator()() const { return defaultColor; }
202 };
203
204 DefaultColors defaultColors;
205 }
206
207 bool Process::ColorNeedsFlush() {
208   return true;
209 }
210
211 const char *Process::OutputBold(bool bg) {
212   WORD colors = DefaultColors::GetCurrentColor();
213   if (bg)
214     colors |= BACKGROUND_INTENSITY;
215   else
216     colors |= FOREGROUND_INTENSITY;
217   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
218   return 0;
219 }
220
221 const char *Process::OutputColor(char code, bool bold, bool bg) {
222   WORD colors;
223   if (bg) {
224     colors = ((code&1) ? BACKGROUND_RED : 0) |
225       ((code&2) ? BACKGROUND_GREEN : 0 ) |
226       ((code&4) ? BACKGROUND_BLUE : 0);
227     if (bold)
228       colors |= BACKGROUND_INTENSITY;
229   } else {
230     colors = ((code&1) ? FOREGROUND_RED : 0) |
231       ((code&2) ? FOREGROUND_GREEN : 0 ) |
232       ((code&4) ? FOREGROUND_BLUE : 0);
233     if (bold)
234       colors |= FOREGROUND_INTENSITY;
235   }
236   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
237   return 0;
238 }
239
240 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
241   CONSOLE_SCREEN_BUFFER_INFO info;
242   GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
243   return info.wAttributes;
244 }
245
246 const char *Process::OutputReverse() {
247   const WORD attributes
248    = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
249
250   const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
251     FOREGROUND_RED | FOREGROUND_INTENSITY;
252   const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
253     BACKGROUND_RED | BACKGROUND_INTENSITY;
254   const WORD color_mask = foreground_mask | background_mask;
255
256   WORD new_attributes =
257     ((attributes & FOREGROUND_BLUE     )?BACKGROUND_BLUE     :0) |
258     ((attributes & FOREGROUND_GREEN    )?BACKGROUND_GREEN    :0) |
259     ((attributes & FOREGROUND_RED      )?BACKGROUND_RED      :0) |
260     ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
261     ((attributes & BACKGROUND_BLUE     )?FOREGROUND_BLUE     :0) |
262     ((attributes & BACKGROUND_GREEN    )?FOREGROUND_GREEN    :0) |
263     ((attributes & BACKGROUND_RED      )?FOREGROUND_RED      :0) |
264     ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
265     0;
266   new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
267
268   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
269   return 0;
270 }
271
272 const char *Process::ResetColor() {
273   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
274   return 0;
275 }