16 #include "llvm/Config/config.h" 22 #ifdef HAVE_SYS_TIME_H 25 #ifdef HAVE_SYS_RESOURCE_H 26 #include <sys/resource.h> 28 #ifdef HAVE_SYS_STAT_H 34 #if defined(HAVE_MALLINFO) 37 #if defined(HAVE_MALLCTL) 38 #include <malloc_np.h> 40 #ifdef HAVE_MALLOC_MALLOC_H 41 #include <malloc/malloc.h> 43 #ifdef HAVE_SYS_IOCTL_H 44 # include <sys/ioctl.h> 58 static std::pair<std::chrono::microseconds, std::chrono::microseconds> getRUsageTimes() {
59 #if defined(HAVE_GETRUSAGE) 61 ::getrusage(RUSAGE_SELF, &RU);
64 #warning Cannot get usage times on this platform 72 #if defined(HAVE_GETPAGESIZE) 73 static const int page_size = ::getpagesize();
74 #elif defined(HAVE_SYSCONF) 75 static long page_size = ::sysconf(_SC_PAGE_SIZE);
77 #error Cannot get the page size on this machine 82 return static_cast<unsigned>(page_size);
86 #if defined(HAVE_MALLINFO) 90 #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H) 91 malloc_statistics_t
Stats;
92 malloc_zone_statistics(malloc_default_zone(), &Stats);
93 return Stats.size_in_use;
94 #elif defined(HAVE_MALLCTL) 97 if (mallctl(
"stats.allocated", &alloc, &sz, NULL, 0) == 0)
100 #elif defined(HAVE_SBRK) 103 static char *StartOfMemory =
reinterpret_cast<char*
>(::sbrk(0));
104 char *EndOfMemory = (
char*)sbrk(0);
105 if (EndOfMemory != ((
char*)-1) && StartOfMemory != ((
char*)-1))
106 return EndOfMemory - StartOfMemory;
109 #warning Cannot get malloc info on this platform 115 std::chrono::nanoseconds &sys_time) {
117 std::tie(user_time, sys_time) = getRUsageTimes();
120 #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__) 121 #include <mach/mach.h> 130 rlim.rlim_cur = rlim.rlim_max = 0;
131 setrlimit(RLIMIT_CORE, &rlim);
134 #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__) 138 mach_msg_type_number_t Count = 0;
139 exception_mask_t OriginalMasks[EXC_TYPES_COUNT];
140 exception_port_t OriginalPorts[EXC_TYPES_COUNT];
141 exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT];
142 thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT];
144 task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks,
145 &Count, OriginalPorts, OriginalBehaviors,
147 if (err == KERN_SUCCESS) {
149 for (
unsigned i = 0; i != Count; ++i)
150 task_set_exception_ports(mach_task_self(), OriginalMasks[i],
151 MACH_PORT_NULL, OriginalBehaviors[i],
156 signal(SIGABRT, _exit);
157 signal(SIGILL, _exit);
158 signal(SIGFPE, _exit);
159 signal(SIGSEGV, _exit);
160 signal(SIGBUS, _exit);
167 std::string NameStr = Name.
str();
168 const char *Val = ::getenv(NameStr.c_str());
171 return std::string(Val);
177 FDCloser(
int &FD) : FD(FD), KeepOpen(
false) {}
178 void keepOpen() { KeepOpen =
true; }
180 if (!KeepOpen && FD >= 0)
185 FDCloser(
const FDCloser &) =
delete;
186 void operator=(
const FDCloser &) =
delete;
195 FDCloser FDC(NullFD);
196 const int StandardFDs[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
197 for (
int StandardFD : StandardFDs) {
201 assert(errno &&
"expected errno to be set if fstat failed!");
204 return std::error_code(errno, std::generic_category());
209 assert(errno == EBADF &&
"expected errno to have EBADF at this point!");
214 auto Open = [&]() { return ::open(
"/dev/null", O_RDWR); };
216 return std::error_code(errno, std::generic_category());
219 if (NullFD == StandardFD)
221 else if (dup2(NullFD, StandardFD) < 0)
222 return std::error_code(errno, std::generic_category());
224 return std::error_code();
230 if (sigfillset(&FullSet) < 0)
231 return std::error_code(errno, std::generic_category());
234 #if LLVM_ENABLE_THREADS 235 if (
int EC = pthread_sigmask(SIG_SETMASK, &FullSet, &SavedSet))
236 return std::error_code(EC, std::generic_category());
238 if (sigprocmask(SIG_SETMASK, &FullSet, &SavedSet) < 0)
239 return std::error_code(errno, std::generic_category());
244 int ErrnoFromClose = 0;
246 ErrnoFromClose = errno;
249 #if LLVM_ENABLE_THREADS 250 EC = pthread_sigmask(SIG_SETMASK, &SavedSet,
nullptr);
252 if (sigprocmask(SIG_SETMASK, &SavedSet,
nullptr) < 0)
258 return std::error_code(ErrnoFromClose, std::generic_category());
259 return std::error_code(EC, std::generic_category());
283 static unsigned getColumns(
int FileID) {
285 if (
const char *ColumnsStr = std::getenv(
"COLUMNS")) {
286 int Columns = std::atoi(ColumnsStr);
291 unsigned Columns = 0;
293 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H) \ 294 && !(defined(_XOPEN_SOURCE) || defined(_POSIX_C_SOURCE)) 297 if (ioctl(FileID, TIOCGWINSZ, &ws) == 0)
308 return getColumns(1);
315 return getColumns(2);
322 extern "C" int setupterm(
char *term,
int filedes,
int *errret);
323 extern "C" struct term *set_curterm(
struct term *termp);
324 extern "C" int del_curterm(
struct term *termp);
325 extern "C" int tigetnum(
char *capname);
332 static bool terminalHasColors(
int fd) {
335 std::lock_guard<std::mutex>
G(*TermColorMutex);
338 if (setupterm(
nullptr, fd, &errret) != 0)
356 bool HasColors = tigetnum(const_cast<char *>(
"colors")) > 0;
360 struct term *termp = set_curterm(
nullptr);
361 (void)del_curterm(termp);
369 if (
const char *TermStr = std::getenv(
"TERM")) {
372 .
Case(
"cygwin",
true)
426 #if !HAVE_DECL_ARC4RANDOM 427 static unsigned GetRandomNumberSeed() {
429 int urandomFD = open(
"/dev/urandom", O_RDONLY);
431 if (urandomFD != -1) {
435 int count =
read(urandomFD, (
void *)&seed,
sizeof(seed));
440 if (count ==
sizeof(seed))
447 return hash_combine(Now.time_since_epoch().count(), ::getpid());
452 #if HAVE_DECL_ARC4RANDOM 455 static int x = (
static_cast<void>(::srand(GetRandomNumberSeed())), 0);
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
This class represents lattice values for constants.
static const char * ResetColor()
Resets the terminals colors, or returns an escape sequence to do so.
static const char * OutputColor(char c, bool bold, bool bg)
This function returns the colorcode escape sequences.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static bool StandardErrHasColors()
This function determines whether the terminal connected to standard error supports colors...
static bool FileDescriptorHasColors(int fd)
This function determines if the given file descriptor is displayd and supports colors.
StringSwitch & StartsWith(StringLiteral S, T Value)
static bool FileDescriptorIsDisplayed(int fd)
This function determines if the given file descriptor is connected to a "tty" or "console" window...
StringSwitch & Case(StringLiteral S, T Value)
static std::error_code FixupStandardFileDescriptors()
static bool ColorNeedsFlush()
Whether changing colors requires the output to be flushed.
static const char * OutputReverse()
This function returns the escape sequence to reverse forground and background colors.
LLVM_NODISCARD R Default(T Value)
static const char colorcodes[2][2][8][10]
Tagged union holding either a T or a Error.
block placement Basic Block Placement Stats
static bool StandardInIsUserInput()
This function determines if the standard input is connected directly to a user's input (keyboard prob...
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
static bool StandardOutIsDisplayed()
This function determines if the standard output is connected to a "tty" or "console" window...
static void UseANSIEscapeCodes(bool enable)
Enables or disables whether ANSI escape sequences are used to output colors.
A switch()-like statement whose cases are string literals.
static unsigned StandardOutColumns()
This function determines the number of columns in the window if standard output is connected to a "tt...
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
StringSwitch & EndsWith(StringLiteral S, T Value)
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
static bool coreFilesPrevented
auto RetryAfterSignal(const FailT &Fail, const Fun &F, const Args &... As) -> decltype(F(As...))
static size_t GetMallocUsage()
Return process memory usage.
static bool StandardErrIsDisplayed()
This function determines if the standard error is connected to a "tty" or "console" window...
static void PreventCoreFiles()
This function makes the necessary calls to the operating system to prevent core files or any other ki...
static unsigned StandardErrColumns()
This function determines the number of columns in the window if standard error is connected to a "tty...
static bool StandardOutHasColors()
This function determines whether the terminal connected to standard output supports colors...
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
static unsigned GetRandomNumber()
Get the result of a process wide random number generator.
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
static void GetTimeUsage(TimePoint<> &elapsed, std::chrono::nanoseconds &user_time, std::chrono::nanoseconds &sys_time)
This static function will set user_time to the amount of CPU time spent in user (non-kernel) mode and...
static const char * OutputBold(bool bg)
Same as OutputColor, but only enables the bold attribute.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Expected< unsigned > getPageSize()
Get the process's page size.
static std::error_code SafelyCloseFileDescriptor(int FD)
StringRef - Represent a constant reference to a string, i.e.
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
std::chrono::nanoseconds toDuration(FILETIME Time)
static Optional< std::string > GetEnv(StringRef name)