Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 1 | //===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===// |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of the Process class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 14 | #include "Windows.h" |
Daniel Dunbar | bb0a612 | 2011-09-23 23:23:36 +0000 | [diff] [blame] | 15 | #include <direct.h> |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include <io.h> |
| 17 | #include <malloc.h> |
| 18 | #include <psapi.h> |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 19 | |
Reid Spencer | 48fdf91 | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 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 |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 27 | |
| 28 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 29 | //=== WARNING: Implementation here must contain only Win32 specific code |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 30 | //=== and must not be UNIX code |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Jeff Cohen | 23a1cf3 | 2005-02-19 03:01:13 +0000 | [diff] [blame] | 33 | #ifdef __MINGW32__ |
Jeff Cohen | 2e5f445 | 2004-12-23 03:44:40 +0000 | [diff] [blame] | 34 | // This ban should be lifted when MinGW 1.0+ has defined this value. |
| 35 | # define _HEAPOK (-2) |
| 36 | #endif |
| 37 | |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 38 | namespace llvm { |
| 39 | using namespace sys; |
| 40 | |
Chandler Carruth | 0184a84 | 2012-12-31 11:17:50 +0000 | [diff] [blame^] | 41 | |
| 42 | process::id_type self_process::get_id() { |
| 43 | return GetCurrentProcess(); |
| 44 | } |
| 45 | |
| 46 | |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 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. |
| 56 | SYSTEM_INFO info; |
| 57 | GetSystemInfo(&info); |
| 58 | return static_cast<unsigned>(info.dwPageSize); |
| 59 | } |
| 60 | |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 61 | unsigned |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 62 | Process::GetPageSize() { |
| 63 | static const unsigned PageSize = GetPageSizeOnce(); |
| 64 | return PageSize; |
| 65 | } |
| 66 | |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 67 | size_t |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 68 | Process::GetMallocUsage() |
| 69 | { |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 70 | _HEAPINFO hinfo; |
| 71 | hinfo._pentry = NULL; |
| 72 | |
| 73 | size_t size = 0; |
| 74 | |
| 75 | while (_heapwalk(&hinfo) == _HEAPOK) |
| 76 | size += hinfo._size; |
| 77 | |
| 78 | return size; |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Jeff Cohen | e269a1a | 2005-01-08 20:15:57 +0000 | [diff] [blame] | 81 | size_t |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 82 | Process::GetTotalMemoryUsage() |
| 83 | { |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 84 | PROCESS_MEMORY_COUNTERS pmc; |
| 85 | GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)); |
| 86 | return pmc.PagefileUsage; |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void |
| 90 | Process::GetTimeUsage( |
| 91 | TimeValue& elapsed, TimeValue& user_time, TimeValue& sys_time) |
| 92 | { |
| 93 | elapsed = TimeValue::now(); |
| 94 | |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 95 | uint64_t ProcCreate, ProcExit, KernelTime, UserTime; |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 96 | GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate, |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 97 | (FILETIME*)&ProcExit, (FILETIME*)&KernelTime, |
| 98 | (FILETIME*)&UserTime); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 99 | |
| 100 | // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond) |
| 101 | user_time.seconds( UserTime / 10000000 ); |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 102 | user_time.nanoseconds( unsigned(UserTime % 10000000) * 100 ); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 103 | sys_time.seconds( KernelTime / 10000000 ); |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 104 | sys_time.nanoseconds( unsigned(KernelTime % 10000000) * 100 ); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Reid Spencer | 93b3473 | 2005-04-21 16:12:57 +0000 | [diff] [blame] | 107 | int Process::GetCurrentUserId() |
| 108 | { |
| 109 | return 65536; |
| 110 | } |
| 111 | |
| 112 | int Process::GetCurrentGroupId() |
| 113 | { |
| 114 | return 65536; |
| 115 | } |
| 116 | |
Reid Spencer | 68fdcc1 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 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() { |
Jeff Cohen | d79bcfd | 2005-02-18 07:05:18 +0000 | [diff] [blame] | 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); |
Reid Spencer | 68fdcc1 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Jeff Cohen | c6dffe0 | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 129 | bool Process::StandardInIsUserInput() { |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 130 | return FileDescriptorIsDisplayed(0); |
Jeff Cohen | c6dffe0 | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | bool Process::StandardOutIsDisplayed() { |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 134 | return FileDescriptorIsDisplayed(1); |
Jeff Cohen | c6dffe0 | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | bool Process::StandardErrIsDisplayed() { |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 138 | return FileDescriptorIsDisplayed(2); |
| 139 | } |
| 140 | |
| 141 | bool Process::FileDescriptorIsDisplayed(int fd) { |
Bill Wendling | 79223c3 | 2012-07-19 00:06:06 +0000 | [diff] [blame] | 142 | DWORD Mode; // Unused |
NAKAMURA Takumi | 46cf7c5 | 2010-11-10 08:37:47 +0000 | [diff] [blame] | 143 | return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0); |
Jeff Cohen | c6dffe0 | 2005-01-01 22:54:05 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Douglas Gregor | 0174674 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 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; |
| 151 | return Columns; |
| 152 | } |
| 153 | |
| 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; |
| 159 | return Columns; |
| 160 | } |
| 161 | |
Daniel Dunbar | 7d83658 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 162 | // The terminal always has colors. |
Benjamin Kramer | bb3c4fa | 2012-07-20 19:49:33 +0000 | [diff] [blame] | 163 | bool Process::FileDescriptorHasColors(int fd) { |
Daniel Dunbar | 7d83658 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 164 | return FileDescriptorIsDisplayed(fd); |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | bool Process::StandardOutHasColors() { |
Daniel Dunbar | 7d83658 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 168 | return FileDescriptorHasColors(1); |
| 169 | } |
| 170 | |
| 171 | bool Process::StandardErrHasColors() { |
| 172 | return FileDescriptorHasColors(2); |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 173 | } |
Torok Edwin | 6c8db34 | 2009-06-04 08:18:25 +0000 | [diff] [blame] | 174 | |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 175 | namespace { |
| 176 | class DefaultColors |
| 177 | { |
| 178 | private: |
| 179 | WORD defaultColor; |
| 180 | public: |
| 181 | DefaultColors() |
| 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; |
| 187 | return 0; |
| 188 | } |
| 189 | WORD operator()() const { return defaultColor; } |
| 190 | }; |
| 191 | |
| 192 | DefaultColors defaultColors; |
| 193 | } |
| 194 | |
| 195 | bool Process::ColorNeedsFlush() { |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | const char *Process::OutputBold(bool bg) { |
| 200 | WORD colors = DefaultColors::GetCurrentColor(); |
| 201 | if (bg) |
| 202 | colors |= BACKGROUND_INTENSITY; |
| 203 | else |
| 204 | colors |= FOREGROUND_INTENSITY; |
| 205 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | const char *Process::OutputColor(char code, bool bold, bool bg) { |
| 210 | WORD colors; |
| 211 | if (bg) { |
| 212 | colors = ((code&1) ? BACKGROUND_RED : 0) | |
| 213 | ((code&2) ? BACKGROUND_GREEN : 0 ) | |
| 214 | ((code&4) ? BACKGROUND_BLUE : 0); |
| 215 | if (bold) |
| 216 | colors |= BACKGROUND_INTENSITY; |
| 217 | } else { |
| 218 | colors = ((code&1) ? FOREGROUND_RED : 0) | |
| 219 | ((code&2) ? FOREGROUND_GREEN : 0 ) | |
| 220 | ((code&4) ? FOREGROUND_BLUE : 0); |
| 221 | if (bold) |
| 222 | colors |= FOREGROUND_INTENSITY; |
| 223 | } |
| 224 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors); |
| 225 | return 0; |
| 226 | } |
| 227 | |
Benjamin Kramer | 246de85 | 2012-04-16 08:56:50 +0000 | [diff] [blame] | 228 | static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) { |
| 229 | CONSOLE_SCREEN_BUFFER_INFO info; |
| 230 | GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); |
| 231 | return info.wAttributes; |
| 232 | } |
| 233 | |
| 234 | const char *Process::OutputReverse() { |
| 235 | const WORD attributes |
| 236 | = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)); |
| 237 | |
| 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; |
| 243 | |
| 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) | |
| 253 | 0; |
| 254 | new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask); |
| 255 | |
| 256 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes); |
| 257 | return 0; |
| 258 | } |
| 259 | |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 260 | const char *Process::ResetColor() { |
| 261 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors()); |
| 262 | return 0; |
| 263 | } |
| 264 | |
Reid Spencer | 437b079 | 2004-09-15 05:47:40 +0000 | [diff] [blame] | 265 | } |