blob: 9eb611be8707b97f58b7d37a01b4b0c24a1096a2 [file] [log] [blame]
Reid Spencer437b0792004-09-15 05:47:40 +00001//===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===//
Michael J. Spencer1f6efa32010-11-29 18:16:10 +00002//
Reid Spencer437b0792004-09-15 05:47:40 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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. Spencer1f6efa32010-11-29 18:16:10 +00007//
Reid Spencer437b0792004-09-15 05:47:40 +00008//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of the Process class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000014#include "Windows.h"
Daniel Dunbarbb0a6122011-09-23 23:23:36 +000015#include <direct.h>
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include <io.h>
17#include <malloc.h>
18#include <psapi.h>
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000019
Reid Spencer48fdf912006-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 Spencer437b0792004-09-15 05:47:40 +000027
28//===----------------------------------------------------------------------===//
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000029//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencer437b0792004-09-15 05:47:40 +000030//=== and must not be UNIX code
31//===----------------------------------------------------------------------===//
32
Jeff Cohen23a1cf32005-02-19 03:01:13 +000033#ifdef __MINGW32__
Jeff Cohen2e5f4452004-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
Chandler Carruth9b4aba82012-12-31 11:45:20 +000038using namespace llvm;
Reid Spencer437b0792004-09-15 05:47:40 +000039using namespace sys;
40
Chandler Carruth0184a842012-12-31 11:17:50 +000041
42process::id_type self_process::get_id() {
43 return GetCurrentProcess();
44}
45
Reid Spencer437b0792004-09-15 05:47:40 +000046// This function retrieves the page size using GetSystemInfo and is present
Chandler Carruth814afe92012-12-31 23:23:35 +000047// solely so it can be called once to initialize the self_process member below.
48static unsigned getPageSize() {
Reid Spencer437b0792004-09-15 05:47:40 +000049 // 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.
54 SYSTEM_INFO info;
55 GetSystemInfo(&info);
56 return static_cast<unsigned>(info.dwPageSize);
57}
58
Chandler Carruth814afe92012-12-31 23:23:35 +000059// 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.
61self_process::self_process() : PageSize(getPageSize()) {
Reid Spencer437b0792004-09-15 05:47:40 +000062}
63
Chandler Carruth814afe92012-12-31 23:23:35 +000064
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000065size_t
Reid Spencer721d9aa2004-12-20 00:59:28 +000066Process::GetMallocUsage()
67{
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000068 _HEAPINFO hinfo;
69 hinfo._pentry = NULL;
70
71 size_t size = 0;
72
73 while (_heapwalk(&hinfo) == _HEAPOK)
74 size += hinfo._size;
75
76 return size;
Reid Spencer721d9aa2004-12-20 00:59:28 +000077}
78
Reid Spencer721d9aa2004-12-20 00:59:28 +000079void
80Process::GetTimeUsage(
81 TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time)
82{
83 elapsed = TimeValue::now();
84
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000085 uint64_t ProcCreate, ProcExit, KernelTime, UserTime;
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000086 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000087 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
88 (FILETIME*)&UserTime);
Reid Spencer721d9aa2004-12-20 00:59:28 +000089
90 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
91 user_time.seconds( UserTime / 10000000 );
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000092 user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 );
Reid Spencer721d9aa2004-12-20 00:59:28 +000093 sys_time.seconds( KernelTime / 10000000 );
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000094 sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 );
Reid Spencer721d9aa2004-12-20 00:59:28 +000095}
96
Reid Spencer93b34732005-04-21 16:12:57 +000097int Process::GetCurrentUserId()
98{
99 return 65536;
100}
101
102int Process::GetCurrentGroupId()
103{
104 return 65536;
105}
106
Reid Spencer68fdcc12004-12-27 06:17:27 +0000107// 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.
110void Process::PreventCoreFiles() {
Jeff Cohend79bcfd2005-02-18 07:05:18 +0000111 // 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);
Reid Spencer68fdcc12004-12-27 06:17:27 +0000117}
118
Jeff Cohenc6dffe02005-01-01 22:54:05 +0000119bool Process::StandardInIsUserInput() {
Dan Gohmanec080462009-09-11 20:46:33 +0000120 return FileDescriptorIsDisplayed(0);
Jeff Cohenc6dffe02005-01-01 22:54:05 +0000121}
122
123bool Process::StandardOutIsDisplayed() {
Dan Gohmanec080462009-09-11 20:46:33 +0000124 return FileDescriptorIsDisplayed(1);
Jeff Cohenc6dffe02005-01-01 22:54:05 +0000125}
126
127bool Process::StandardErrIsDisplayed() {
Dan Gohmanec080462009-09-11 20:46:33 +0000128 return FileDescriptorIsDisplayed(2);
129}
130
131bool Process::FileDescriptorIsDisplayed(int fd) {
Bill Wendling79223c32012-07-19 00:06:06 +0000132 DWORD Mode; // Unused
NAKAMURA Takumi46cf7c52010-11-10 08:37:47 +0000133 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
Jeff Cohenc6dffe02005-01-01 22:54:05 +0000134}
135
Douglas Gregor01746742009-05-11 18:05:52 +0000136unsigned 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;
141 return Columns;
142}
143
144unsigned 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;
149 return Columns;
150}
151
Daniel Dunbar7d836582012-07-20 18:29:38 +0000152// The terminal always has colors.
Benjamin Kramerbb3c4fa2012-07-20 19:49:33 +0000153bool Process::FileDescriptorHasColors(int fd) {
Daniel Dunbar7d836582012-07-20 18:29:38 +0000154 return FileDescriptorIsDisplayed(fd);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000155}
156
157bool Process::StandardOutHasColors() {
Daniel Dunbar7d836582012-07-20 18:29:38 +0000158 return FileDescriptorHasColors(1);
159}
160
161bool Process::StandardErrHasColors() {
162 return FileDescriptorHasColors(2);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000163}
Torok Edwin6c8db342009-06-04 08:18:25 +0000164
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000165namespace {
166class DefaultColors
167{
168 private:
169 WORD defaultColor;
170 public:
171 DefaultColors()
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;
177 return 0;
178 }
179 WORD operator()() const { return defaultColor; }
180};
181
182DefaultColors defaultColors;
183}
184
185bool Process::ColorNeedsFlush() {
186 return true;
187}
188
189const char *Process::OutputBold(bool bg) {
190 WORD colors = DefaultColors::GetCurrentColor();
191 if (bg)
192 colors |= BACKGROUND_INTENSITY;
193 else
194 colors |= FOREGROUND_INTENSITY;
195 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
196 return 0;
197}
198
199const char *Process::OutputColor(char code, bool bold, bool bg) {
200 WORD colors;
201 if (bg) {
202 colors = ((code&1) ? BACKGROUND_RED : 0) |
203 ((code&2) ? BACKGROUND_GREEN : 0 ) |
204 ((code&4) ? BACKGROUND_BLUE : 0);
205 if (bold)
206 colors |= BACKGROUND_INTENSITY;
207 } else {
208 colors = ((code&1) ? FOREGROUND_RED : 0) |
209 ((code&2) ? FOREGROUND_GREEN : 0 ) |
210 ((code&4) ? FOREGROUND_BLUE : 0);
211 if (bold)
212 colors |= FOREGROUND_INTENSITY;
213 }
214 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
215 return 0;
216}
217
Benjamin Kramer246de852012-04-16 08:56:50 +0000218static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
219 CONSOLE_SCREEN_BUFFER_INFO info;
220 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
221 return info.wAttributes;
222}
223
224const char *Process::OutputReverse() {
225 const WORD attributes
226 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
227
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;
233
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) |
243 0;
244 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
245
246 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
247 return 0;
248}
249
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000250const char *Process::ResetColor() {
251 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
252 return 0;
253}