24 #if (HAVE_LIBPSAPI != 1)
25 #error "libpsapi.a should be present"
27 #if (HAVE_LIBSHELL32 != 1)
28 #error "libshell32.a should be present"
31 #pragma comment(lib, "psapi.lib")
32 #pragma comment(lib, "shell32.lib")
50 return GetCurrentProcessId();
53 static TimeValue getTimeValueFromFILETIME(FILETIME Time) {
54 ULARGE_INTEGER TimeInteger;
55 TimeInteger.LowPart = Time.dwLowDateTime;
56 TimeInteger.HighPart = Time.dwHighDateTime;
60 static_cast<TimeValue::SecondsType>(TimeInteger.QuadPart / 10000000),
61 static_cast<TimeValue::NanoSecondsType>(
62 (TimeInteger.QuadPart % 10000000) * 100));
66 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
67 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
71 return getTimeValueFromFILETIME(UserTime);
75 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
76 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
80 return getTimeValueFromFILETIME(KernelTime);
85 static unsigned getPageSize() {
95 return static_cast<unsigned>(info.dwPageSize);
100 self_process::self_process() : PageSize(getPageSize()) {
108 hinfo._pentry = NULL;
112 while (_heapwalk(&hinfo) == _HEAPOK)
119 TimeValue &sys_time) {
122 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
123 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
127 user_time = getTimeValueFromFILETIME(UserTime);
128 sys_time = getTimeValueFromFILETIME(KernelTime);
145 SetErrorMode(SEM_FAILCRITICALERRORS |
146 SEM_NOGPFAULTERRORBOX |
147 SEM_NOOPENFILEERRORBOX);
162 size_t Size = MAX_PATH;
178 return std::string(Res.
data());
188 wchar_t **UnicodeCommandLine = CommandLineToArgvW(GetCommandLineW(),
190 if (!UnicodeCommandLine)
195 for (
int i = 0; i < NewArgCount; ++i) {
198 wcslen(UnicodeCommandLine[i]),
203 char *Buffer = ArgAllocator.
Allocate(NewArgString.
size() + 1);
207 LocalFree(UnicodeCommandLine);
228 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
232 unsigned Columns = 0;
233 CONSOLE_SCREEN_BUFFER_INFO csbi;
234 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
235 Columns = csbi.dwSize.X;
240 unsigned Columns = 0;
241 CONSOLE_SCREEN_BUFFER_INFO csbi;
242 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
243 Columns = csbi.dwSize.X;
260 static bool UseANSI =
false;
272 :defaultColor(GetCurrentColor()) {}
273 static unsigned GetCurrentColor() {
274 CONSOLE_SCREEN_BUFFER_INFO csbi;
275 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
276 return csbi.wAttributes;
279 WORD operator()()
const {
return defaultColor; }
282 DefaultColors defaultColors;
290 if (UseANSI)
return "\033[1m";
292 WORD colors = DefaultColors::GetCurrentColor();
294 colors |= BACKGROUND_INTENSITY;
296 colors |= FOREGROUND_INTENSITY;
297 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
302 if (UseANSI)
return colorcodes[bg?1:0][bold?1:0][code&7];
306 colors = ((code&1) ? BACKGROUND_RED : 0) |
307 ((code&2) ? BACKGROUND_GREEN : 0 ) |
308 ((code&4) ? BACKGROUND_BLUE : 0);
310 colors |= BACKGROUND_INTENSITY;
312 colors = ((code&1) ? FOREGROUND_RED : 0) |
313 ((code&2) ? FOREGROUND_GREEN : 0 ) |
314 ((code&4) ? FOREGROUND_BLUE : 0);
316 colors |= FOREGROUND_INTENSITY;
318 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
322 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
323 CONSOLE_SCREEN_BUFFER_INFO
info;
324 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
325 return info.wAttributes;
329 if (UseANSI)
return "\033[7m";
332 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
334 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
335 FOREGROUND_RED | FOREGROUND_INTENSITY;
336 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
337 BACKGROUND_RED | BACKGROUND_INTENSITY;
338 const WORD color_mask = foreground_mask | background_mask;
340 WORD new_attributes =
341 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
342 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
343 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
344 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
345 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
346 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
347 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
348 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
350 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
352 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
357 if (UseANSI)
return "\033[0m";
358 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
void set_size(unsigned N)
void push_back(const T &Elt)
static TimeValue now()
Creates a TimeValue with the current time (UTC).
static const char * ResetColor()
Resets the terminals colors, or returns an escape sequence to do so.
virtual TimeValue get_system_time() const
Get the system time consumed by this process.
static const char * OutputColor(char c, bool bold, bool bg)
static bool StandardErrHasColors()
static bool FileDescriptorHasColors(int fd)
static bool FileDescriptorIsDisplayed(int fd)
virtual id_type get_id()
Get the operating system specific identifier for this process.
error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, SmallVectorImpl< char > &utf8)
error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl< wchar_t > &utf16)
static bool ColorNeedsFlush()
static const char * OutputReverse()
static const char colorcodes[2][2][8][10]
static bool StandardInIsUserInput()
static bool StandardOutIsDisplayed()
static void UseANSIEscapeCodes(bool enable)
static unsigned StandardOutColumns()
static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time, TimeValue &sys_time)
virtual TimeValue get_user_time() const
Get the user time consumed by this process.
pid_t id_type
Operating system specific type to identify a process.
T * Allocate(size_t num=1)
Allocate space for a specific count of elements.
static size_t GetMallocUsage()
Return process memory usage. This static function will return the total amount of memory allocated by...
static bool StandardErrIsDisplayed()
static void PreventCoreFiles()
Prevent core file generation.
static unsigned StandardErrColumns()
static bool StandardOutHasColors()
pointer data()
data - Return a pointer to the vector's buffer, even if empty().
static error_code success()
static error_code GetArgumentVector(SmallVectorImpl< const char * > &Args, ArrayRef< const char * > ArgsFromMain, SpecificBumpPtrAllocator< char > &ArgAllocator)
Deduce function attributes
static const char * OutputBold(bool bg)
Same as OutputColor, but only enables the bold attribute.
static Optional< std::string > GetEnv(StringRef name)