2004-09-15 05:47:40 +00:00
|
|
|
//===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===//
|
2010-11-29 18:16:10 +00:00
|
|
|
//
|
2004-09-15 05:47:40 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2010-11-29 18:16:10 +00:00
|
|
|
//
|
2004-09-15 05:47:40 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides the Win32 specific implementation of the Process class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "Windows.h"
|
2011-09-23 23:23:36 +00:00
|
|
|
#include <direct.h>
|
2012-12-03 16:50:05 +00:00
|
|
|
#include <io.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <psapi.h>
|
2004-12-20 03:24:56 +00:00
|
|
|
|
2006-06-01 19:03:21 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#if (HAVE_LIBPSAPI != 1)
|
|
|
|
#error "libpsapi.a should be present"
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#pragma comment(lib, "psapi.lib")
|
|
|
|
#endif
|
2004-09-15 05:47:40 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-11-29 18:16:10 +00:00
|
|
|
//=== WARNING: Implementation here must contain only Win32 specific code
|
2004-09-15 05:47:40 +00:00
|
|
|
//=== and must not be UNIX code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-02-19 03:01:13 +00:00
|
|
|
#ifdef __MINGW32__
|
2004-12-23 03:44:40 +00:00
|
|
|
// This ban should be lifted when MinGW 1.0+ has defined this value.
|
|
|
|
# define _HEAPOK (-2)
|
|
|
|
#endif
|
|
|
|
|
2012-12-31 11:45:20 +00:00
|
|
|
using namespace llvm;
|
2004-09-15 05:47:40 +00:00
|
|
|
using namespace sys;
|
|
|
|
|
2012-12-31 11:17:50 +00:00
|
|
|
|
|
|
|
process::id_type self_process::get_id() {
|
2013-06-08 20:29:03 +00:00
|
|
|
return GetCurrentProcessId();
|
2012-12-31 11:17:50 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 23:19:55 +00:00
|
|
|
static TimeValue getTimeValueFromFILETIME(FILETIME Time) {
|
|
|
|
ULARGE_INTEGER TimeInteger;
|
|
|
|
TimeInteger.LowPart = Time.dwLowDateTime;
|
|
|
|
TimeInteger.HighPart = Time.dwHighDateTime;
|
|
|
|
|
|
|
|
// FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
|
|
|
|
return TimeValue(
|
|
|
|
static_cast<TimeValue::SecondsType>(TimeInteger.QuadPart / 10000000),
|
|
|
|
static_cast<TimeValue::NanoSecondsType>(
|
|
|
|
(TimeInteger.QuadPart % 10000000) * 100));
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeValue self_process::get_user_time() const {
|
|
|
|
FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
|
|
|
|
if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
|
|
|
|
&UserTime) == 0)
|
|
|
|
return TimeValue();
|
|
|
|
|
|
|
|
return getTimeValueFromFILETIME(UserTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeValue self_process::get_system_time() const {
|
|
|
|
FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
|
|
|
|
if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
|
|
|
|
&UserTime) == 0)
|
|
|
|
return TimeValue();
|
|
|
|
|
|
|
|
return getTimeValueFromFILETIME(KernelTime);
|
|
|
|
}
|
|
|
|
|
2004-09-15 05:47:40 +00:00
|
|
|
// This function retrieves the page size using GetSystemInfo and is present
|
2012-12-31 23:23:35 +00:00
|
|
|
// solely so it can be called once to initialize the self_process member below.
|
|
|
|
static unsigned getPageSize() {
|
2004-09-15 05:47:40 +00:00
|
|
|
// NOTE: A 32-bit application running under WOW64 is supposed to use
|
|
|
|
// GetNativeSystemInfo. However, this interface is not present prior
|
|
|
|
// to Windows XP so to use it requires dynamic linking. It is not clear
|
|
|
|
// how this affects the reported page size, if at all. One could argue
|
|
|
|
// that LLVM ought to run as 64-bits on a 64-bit system, anyway.
|
|
|
|
SYSTEM_INFO info;
|
|
|
|
GetSystemInfo(&info);
|
|
|
|
return static_cast<unsigned>(info.dwPageSize);
|
|
|
|
}
|
|
|
|
|
2012-12-31 23:23:35 +00:00
|
|
|
// This constructor guaranteed to be run exactly once on a single thread, and
|
|
|
|
// sets up various process invariants that can be queried cheaply from then on.
|
|
|
|
self_process::self_process() : PageSize(getPageSize()) {
|
2004-09-15 05:47:40 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 23:23:35 +00:00
|
|
|
|
2010-11-29 18:16:10 +00:00
|
|
|
size_t
|
2004-12-20 00:59:28 +00:00
|
|
|
Process::GetMallocUsage()
|
|
|
|
{
|
2004-12-20 03:24:56 +00:00
|
|
|
_HEAPINFO hinfo;
|
|
|
|
hinfo._pentry = NULL;
|
|
|
|
|
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
while (_heapwalk(&hinfo) == _HEAPOK)
|
|
|
|
size += hinfo._size;
|
|
|
|
|
|
|
|
return size;
|
2004-12-20 00:59:28 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 23:19:55 +00:00
|
|
|
void Process::GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
|
|
|
|
TimeValue &sys_time) {
|
2004-12-20 00:59:28 +00:00
|
|
|
elapsed = TimeValue::now();
|
|
|
|
|
2013-01-04 23:19:55 +00:00
|
|
|
FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
|
|
|
|
if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
|
|
|
|
&UserTime) == 0)
|
|
|
|
return;
|
2004-12-20 00:59:28 +00:00
|
|
|
|
2013-01-04 23:19:55 +00:00
|
|
|
user_time = getTimeValueFromFILETIME(UserTime);
|
2013-01-04 23:46:04 +00:00
|
|
|
sys_time = getTimeValueFromFILETIME(KernelTime);
|
2004-12-20 00:59:28 +00:00
|
|
|
}
|
|
|
|
|
2004-12-27 06:17:27 +00:00
|
|
|
// Some LLVM programs such as bugpoint produce core files as a normal part of
|
2013-08-16 14:33:07 +00:00
|
|
|
// their operation. To prevent the disk from filling up, this configuration
|
|
|
|
// item does what's necessary to prevent their generation.
|
2004-12-27 06:17:27 +00:00
|
|
|
void Process::PreventCoreFiles() {
|
2013-08-16 14:33:07 +00:00
|
|
|
// Windows does have the concept of core files, called minidumps. However,
|
|
|
|
// disabling minidumps for a particular application extends past the lifetime
|
|
|
|
// of that application, which is the incorrect behavior for this API.
|
|
|
|
// Additionally, the APIs require elevated privileges to disable and re-
|
|
|
|
// enable minidumps, which makes this untenable. For more information, see
|
|
|
|
// WerAddExcludedApplication and WerRemoveExcludedApplication (Vista and
|
|
|
|
// later).
|
|
|
|
//
|
|
|
|
// Windows also has modal pop-up message boxes. As this method is used by
|
|
|
|
// bugpoint, preventing these pop-ups is additionally important.
|
2005-02-18 07:05:18 +00:00
|
|
|
SetErrorMode(SEM_FAILCRITICALERRORS |
|
|
|
|
SEM_NOGPFAULTERRORBOX |
|
|
|
|
SEM_NOOPENFILEERRORBOX);
|
2004-12-27 06:17:27 +00:00
|
|
|
}
|
|
|
|
|
2005-01-01 22:54:05 +00:00
|
|
|
bool Process::StandardInIsUserInput() {
|
2009-09-11 20:46:33 +00:00
|
|
|
return FileDescriptorIsDisplayed(0);
|
2005-01-01 22:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::StandardOutIsDisplayed() {
|
2009-09-11 20:46:33 +00:00
|
|
|
return FileDescriptorIsDisplayed(1);
|
2005-01-01 22:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::StandardErrIsDisplayed() {
|
2009-09-11 20:46:33 +00:00
|
|
|
return FileDescriptorIsDisplayed(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::FileDescriptorIsDisplayed(int fd) {
|
2012-07-19 00:06:06 +00:00
|
|
|
DWORD Mode; // Unused
|
2010-11-10 08:37:47 +00:00
|
|
|
return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
|
2005-01-01 22:54:05 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 18:05:52 +00:00
|
|
|
unsigned Process::StandardOutColumns() {
|
|
|
|
unsigned Columns = 0;
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
|
|
|
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
|
|
|
|
Columns = csbi.dwSize.X;
|
|
|
|
return Columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Process::StandardErrColumns() {
|
|
|
|
unsigned Columns = 0;
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
|
|
|
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
|
|
|
|
Columns = csbi.dwSize.X;
|
|
|
|
return Columns;
|
|
|
|
}
|
|
|
|
|
2012-07-20 18:29:38 +00:00
|
|
|
// The terminal always has colors.
|
2012-07-20 19:49:33 +00:00
|
|
|
bool Process::FileDescriptorHasColors(int fd) {
|
2012-07-20 18:29:38 +00:00
|
|
|
return FileDescriptorIsDisplayed(fd);
|
2009-06-04 07:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::StandardOutHasColors() {
|
2012-07-20 18:29:38 +00:00
|
|
|
return FileDescriptorHasColors(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::StandardErrHasColors() {
|
|
|
|
return FileDescriptorHasColors(2);
|
2009-06-04 07:09:50 +00:00
|
|
|
}
|
2009-06-04 08:18:25 +00:00
|
|
|
|
2009-06-04 07:09:50 +00:00
|
|
|
namespace {
|
|
|
|
class DefaultColors
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
WORD defaultColor;
|
|
|
|
public:
|
|
|
|
DefaultColors()
|
|
|
|
:defaultColor(GetCurrentColor()) {}
|
|
|
|
static unsigned GetCurrentColor() {
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
|
|
|
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
|
|
|
|
return csbi.wAttributes;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
WORD operator()() const { return defaultColor; }
|
|
|
|
};
|
|
|
|
|
|
|
|
DefaultColors defaultColors;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::ColorNeedsFlush() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Process::OutputBold(bool bg) {
|
|
|
|
WORD colors = DefaultColors::GetCurrentColor();
|
|
|
|
if (bg)
|
|
|
|
colors |= BACKGROUND_INTENSITY;
|
|
|
|
else
|
|
|
|
colors |= FOREGROUND_INTENSITY;
|
|
|
|
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Process::OutputColor(char code, bool bold, bool bg) {
|
|
|
|
WORD colors;
|
|
|
|
if (bg) {
|
|
|
|
colors = ((code&1) ? BACKGROUND_RED : 0) |
|
|
|
|
((code&2) ? BACKGROUND_GREEN : 0 ) |
|
|
|
|
((code&4) ? BACKGROUND_BLUE : 0);
|
|
|
|
if (bold)
|
|
|
|
colors |= BACKGROUND_INTENSITY;
|
|
|
|
} else {
|
|
|
|
colors = ((code&1) ? FOREGROUND_RED : 0) |
|
|
|
|
((code&2) ? FOREGROUND_GREEN : 0 ) |
|
|
|
|
((code&4) ? FOREGROUND_BLUE : 0);
|
|
|
|
if (bold)
|
|
|
|
colors |= FOREGROUND_INTENSITY;
|
|
|
|
}
|
|
|
|
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-16 08:56:50 +00:00
|
|
|
static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
|
|
|
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
|
|
|
|
return info.wAttributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Process::OutputReverse() {
|
|
|
|
const WORD attributes
|
|
|
|
= GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
|
|
|
|
|
|
|
|
const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
|
|
|
|
FOREGROUND_RED | FOREGROUND_INTENSITY;
|
|
|
|
const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
|
|
|
|
BACKGROUND_RED | BACKGROUND_INTENSITY;
|
|
|
|
const WORD color_mask = foreground_mask | background_mask;
|
|
|
|
|
|
|
|
WORD new_attributes =
|
|
|
|
((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
|
|
|
|
((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
|
|
|
|
((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
|
|
|
|
((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
|
|
|
|
((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
|
|
|
|
((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
|
|
|
|
((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
|
|
|
|
((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
|
|
|
|
0;
|
|
|
|
new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
|
|
|
|
|
|
|
|
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-04 07:09:50 +00:00
|
|
|
const char *Process::ResetColor() {
|
|
|
|
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
|
|
|
|
return 0;
|
|
|
|
}
|