blob: 89dbf2378d096d41a763c6f38a0dc21fd2d34fd1 [file] [log] [blame]
Reid Spencer91886b72004-09-15 05:47:40 +00001//===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Reid Spencer91886b72004-09-15 05:47:40 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Michael J. Spencer447762d2010-11-29 18:16:10 +00007//
Reid Spencer91886b72004-09-15 05:47:40 +00008//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of the Process class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer447762d2010-11-29 18:16:10 +000014#include "Windows.h"
Daniel Dunbar9b92e2b2011-09-23 23:23:36 +000015#include <direct.h>
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include <io.h>
17#include <malloc.h>
18#include <psapi.h>
Jeff Cohen7ae0bc72004-12-20 03:24:56 +000019
Reid Spencer187b4ad2006-06-01 19:03:21 +000020#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
Reid Spencer91886b72004-09-15 05:47:40 +000027
28//===----------------------------------------------------------------------===//
Michael J. Spencer447762d2010-11-29 18:16:10 +000029//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencer91886b72004-09-15 05:47:40 +000030//=== and must not be UNIX code
31//===----------------------------------------------------------------------===//
32
Jeff Cohen07e22ba2005-02-19 03:01:13 +000033#ifdef __MINGW32__
Jeff Cohen53fbecc2004-12-23 03:44:40 +000034// This ban should be lifted when MinGW 1.0+ has defined this value.
35# define _HEAPOK (-2)
36#endif
37
Reid Spencer91886b72004-09-15 05:47:40 +000038namespace llvm {
39using namespace sys;
40
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.
44inline 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.
50 SYSTEM_INFO info;
51 GetSystemInfo(&info);
52 return static_cast<unsigned>(info.dwPageSize);
53}
54
Michael J. Spencer447762d2010-11-29 18:16:10 +000055unsigned
Reid Spencer91886b72004-09-15 05:47:40 +000056Process::GetPageSize() {
57 static const unsigned PageSize = GetPageSizeOnce();
58 return PageSize;
59}
60
Michael J. Spencer447762d2010-11-29 18:16:10 +000061size_t
Reid Spencerac38f3a2004-12-20 00:59:28 +000062Process::GetMallocUsage()
63{
Jeff Cohen7ae0bc72004-12-20 03:24:56 +000064 _HEAPINFO hinfo;
65 hinfo._pentry = NULL;
66
67 size_t size = 0;
68
69 while (_heapwalk(&hinfo) == _HEAPOK)
70 size += hinfo._size;
71
72 return size;
Reid Spencerac38f3a2004-12-20 00:59:28 +000073}
74
Jeff Cohen1a26d152005-01-08 20:15:57 +000075size_t
Reid Spencerac38f3a2004-12-20 00:59:28 +000076Process::GetTotalMemoryUsage()
77{
Jeff Cohen7ae0bc72004-12-20 03:24:56 +000078 PROCESS_MEMORY_COUNTERS pmc;
79 GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
80 return pmc.PagefileUsage;
Reid Spencerac38f3a2004-12-20 00:59:28 +000081}
82
83void
84Process::GetTimeUsage(
85 TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
86{
87 elapsed = TimeValue::now();
88
Jeff Cohen7ae0bc72004-12-20 03:24:56 +000089 uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
Michael J. Spencer447762d2010-11-29 18:16:10 +000090 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
Jeff Cohen7ae0bc72004-12-20 03:24:56 +000091 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
92 (FILETIME*)&UserTime);
Reid Spencerac38f3a2004-12-20 00:59:28 +000093
94 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
95 user_time.seconds( UserTime / 10000000 );
Jeff Cohen7ae0bc72004-12-20 03:24:56 +000096 user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
Reid Spencerac38f3a2004-12-20 00:59:28 +000097 sys_time.seconds( KernelTime / 10000000 );
Jeff Cohen7ae0bc72004-12-20 03:24:56 +000098 sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
Reid Spencerac38f3a2004-12-20 00:59:28 +000099}
100
Reid Spencer2d45e252005-04-21 16:12:57 +0000101int Process::GetCurrentUserId()
102{
103 return 65536;
104}
105
106int Process::GetCurrentGroupId()
107{
108 return 65536;
109}
110
Reid Spencercf15b872004-12-27 06:17:27 +0000111// 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.
114void Process::PreventCoreFiles() {
Jeff Cohen81549a52005-02-18 07:05:18 +0000115 // 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);
Reid Spencercf15b872004-12-27 06:17:27 +0000121}
122
Jeff Cohenb90c31f2005-01-01 22:54:05 +0000123bool Process::StandardInIsUserInput() {
Dan Gohmane5929232009-09-11 20:46:33 +0000124 return FileDescriptorIsDisplayed(0);
Jeff Cohenb90c31f2005-01-01 22:54:05 +0000125}
126
127bool Process::StandardOutIsDisplayed() {
Dan Gohmane5929232009-09-11 20:46:33 +0000128 return FileDescriptorIsDisplayed(1);
Jeff Cohenb90c31f2005-01-01 22:54:05 +0000129}
130
131bool Process::StandardErrIsDisplayed() {
Dan Gohmane5929232009-09-11 20:46:33 +0000132 return FileDescriptorIsDisplayed(2);
133}
134
135bool Process::FileDescriptorIsDisplayed(int fd) {
Bill Wendling2b079652012-07-19 00:06:06 +0000136 DWORD Mode; // Unused
NAKAMURA Takumi23ebef12010-11-10 08:37:47 +0000137 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
Jeff Cohenb90c31f2005-01-01 22:54:05 +0000138}
139
Douglas Gregor15436612009-05-11 18:05:52 +0000140unsigned 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;
145 return Columns;
146}
147
148unsigned 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;
153 return Columns;
154}
155
Daniel Dunbar712de822012-07-20 18:29:38 +0000156// The terminal always has colors.
Benjamin Kramerdfaa0f32012-07-20 19:49:33 +0000157bool Process::FileDescriptorHasColors(int fd) {
Daniel Dunbar712de822012-07-20 18:29:38 +0000158 return FileDescriptorIsDisplayed(fd);
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000159}
160
161bool Process::StandardOutHasColors() {
Daniel Dunbar712de822012-07-20 18:29:38 +0000162 return FileDescriptorHasColors(1);
163}
164
165bool Process::StandardErrHasColors() {
166 return FileDescriptorHasColors(2);
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000167}
Torok Edwin63e44bb2009-06-04 08:18:25 +0000168
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000169namespace {
170class DefaultColors
171{
172 private:
173 WORD defaultColor;
174 public:
175 DefaultColors()
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;
181 return 0;
182 }
183 WORD operator()() const { return defaultColor; }
184};
185
186DefaultColors defaultColors;
187}
188
189bool Process::ColorNeedsFlush() {
190 return true;
191}
192
193const char *Process::OutputBold(bool bg) {
194 WORD colors = DefaultColors::GetCurrentColor();
195 if (bg)
196 colors |= BACKGROUND_INTENSITY;
197 else
198 colors |= FOREGROUND_INTENSITY;
199 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
200 return 0;
201}
202
203const char *Process::OutputColor(char code, bool bold, bool bg) {
204 WORD colors;
205 if (bg) {
206 colors = ((code&1) ? BACKGROUND_RED : 0) |
207 ((code&2) ? BACKGROUND_GREEN : 0 ) |
208 ((code&4) ? BACKGROUND_BLUE : 0);
209 if (bold)
210 colors |= BACKGROUND_INTENSITY;
211 } else {
212 colors = ((code&1) ? FOREGROUND_RED : 0) |
213 ((code&2) ? FOREGROUND_GREEN : 0 ) |
214 ((code&4) ? FOREGROUND_BLUE : 0);
215 if (bold)
216 colors |= FOREGROUND_INTENSITY;
217 }
218 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
219 return 0;
220}
221
Benjamin Kramer13d16f32012-04-16 08:56:50 +0000222static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
223 CONSOLE_SCREEN_BUFFER_INFO info;
224 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
225 return info.wAttributes;
226}
227
228const char *Process::OutputReverse() {
229 const WORD attributes
230 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
231
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;
237
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) |
247 0;
248 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
249
250 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
251 return 0;
252}
253
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000254const char *Process::ResetColor() {
255 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
256 return 0;
257}
258
Reid Spencer91886b72004-09-15 05:47:40 +0000259}