24 #ifdef HAVE_SYS_TIME_H
27 #ifdef HAVE_SYS_RESOURCE_H
28 #include <sys/resource.h>
30 #ifdef HAVE_SYS_STAT_H
38 #if defined(HAVE_MALLOC_H) && !defined(__DragonFly__) && \
39 !defined(__OpenBSD__) && !defined(__Bitrig__)
42 #if defined(HAVE_MALLCTL)
43 #include <malloc_np.h>
45 #ifdef HAVE_MALLOC_MALLOC_H
46 #include <malloc/malloc.h>
48 #ifdef HAVE_SYS_IOCTL_H
49 # include <sys/ioctl.h>
63 static std::pair<TimeValue, TimeValue> getRUsageTimes() {
64 #if defined(HAVE_GETRUSAGE)
66 ::getrusage(RUSAGE_SELF, &RU);
67 return std::make_pair(
69 static_cast<TimeValue::SecondsType>(RU.ru_utime.tv_sec),
70 static_cast<TimeValue::NanoSecondsType>(
73 static_cast<TimeValue::SecondsType>(RU.ru_stime.tv_sec),
74 static_cast<TimeValue::NanoSecondsType>(
77 #warning Cannot get usage times on this platform
78 return std::make_pair(TimeValue(), TimeValue());
85 #if defined(HAVE_GETPAGESIZE)
86 static const int page_size = ::getpagesize();
87 #elif defined(HAVE_SYSCONF)
88 static long page_size = ::sysconf(_SC_PAGE_SIZE);
90 #warning Cannot get the page size on this machine
92 return static_cast<unsigned>(page_size);
96 #if defined(HAVE_MALLINFO)
100 #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)
101 malloc_statistics_t
Stats;
102 malloc_zone_statistics(malloc_default_zone(), &Stats);
103 return Stats.size_in_use;
104 #elif defined(HAVE_MALLCTL)
107 if (mallctl(
"stats.allocated", &alloc, &sz, NULL, 0) == 0)
110 #elif defined(HAVE_SBRK)
113 static char *StartOfMemory =
reinterpret_cast<char*
>(::sbrk(0));
114 char *EndOfMemory = (
char*)sbrk(0);
115 if (EndOfMemory != ((
char*)-1) && StartOfMemory != ((
char*)-1))
116 return EndOfMemory - StartOfMemory;
119 #warning Cannot get malloc info on this platform
125 TimeValue &sys_time) {
127 std::tie(user_time, sys_time) = getRUsageTimes();
130 #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__)
131 #include <mach/mach.h>
140 rlim.rlim_cur = rlim.rlim_max = 0;
141 setrlimit(RLIMIT_CORE, &rlim);
144 #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__)
148 mach_msg_type_number_t Count = 0;
149 exception_mask_t OriginalMasks[EXC_TYPES_COUNT];
150 exception_port_t OriginalPorts[EXC_TYPES_COUNT];
151 exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT];
152 thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT];
154 task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks,
155 &Count, OriginalPorts, OriginalBehaviors,
157 if (err == KERN_SUCCESS) {
159 for (
unsigned i = 0; i != Count; ++i)
160 task_set_exception_ports(mach_task_self(), OriginalMasks[i],
161 MACH_PORT_NULL, OriginalBehaviors[i],
166 signal(SIGABRT, _exit);
167 signal(SIGILL, _exit);
168 signal(SIGFPE, _exit);
169 signal(SIGSEGV, _exit);
170 signal(SIGBUS, _exit);
175 std::string NameStr = Name.
str();
176 const char *Val = ::getenv(NameStr.c_str());
179 return std::string(Val);
188 return std::error_code();
194 FDCloser(
int &FD) : FD(FD), KeepOpen(
false) {}
195 void keepOpen() { KeepOpen =
true; }
197 if (!KeepOpen && FD >= 0)
202 FDCloser(
const FDCloser &) =
delete;
203 void operator=(
const FDCloser &) =
delete;
212 FDCloser FDC(NullFD);
213 const int StandardFDs[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
214 for (
int StandardFD : StandardFDs) {
217 while (fstat(StandardFD, &st) < 0) {
218 assert(errno &&
"expected errno to be set if fstat failed!");
224 return std::error_code(errno, std::generic_category());
229 assert(errno == EBADF &&
"expected errno to have EBADF at this point!");
232 while ((NullFD = open(
"/dev/null", O_RDWR)) < 0) {
235 return std::error_code(errno, std::generic_category());
239 if (NullFD == StandardFD)
241 else if (dup2(NullFD, StandardFD) < 0)
242 return std::error_code(errno, std::generic_category());
244 return std::error_code();
250 if (sigfillset(&FullSet) < 0)
251 return std::error_code(errno, std::generic_category());
254 #if LLVM_ENABLE_THREADS
255 if (
int EC = pthread_sigmask(SIG_SETMASK, &FullSet, &SavedSet))
256 return std::error_code(EC, std::generic_category());
258 if (sigprocmask(SIG_SETMASK, &FullSet, &SavedSet) < 0)
259 return std::error_code(errno, std::generic_category());
264 int ErrnoFromClose = 0;
266 ErrnoFromClose = errno;
269 #if LLVM_ENABLE_THREADS
270 EC = pthread_sigmask(SIG_SETMASK, &SavedSet,
nullptr);
272 if (sigprocmask(SIG_SETMASK, &SavedSet,
nullptr) < 0)
278 return std::error_code(ErrnoFromClose, std::generic_category());
279 return std::error_code(EC, std::generic_category());
303 static unsigned getColumns(
int FileID) {
305 if (
const char *ColumnsStr = std::getenv(
"COLUMNS")) {
306 int Columns = std::atoi(ColumnsStr);
311 unsigned Columns = 0;
313 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H)
316 if (ioctl(FileID, TIOCGWINSZ, &ws) == 0)
327 return getColumns(1);
334 return getColumns(2);
341 extern "C" int setupterm(
char *term,
int filedes,
int *errret);
342 extern "C" struct term *set_curterm(
struct term *termp);
343 extern "C" int del_curterm(
struct term *termp);
344 extern "C" int tigetnum(
char *capname);
351 static bool terminalHasColors(
int fd) {
357 if (setupterm((
char *)
nullptr, fd, &errret) != 0)
375 bool HasColors = tigetnum(const_cast<char *>(
"colors")) > 0;
379 struct term *termp = set_curterm((
struct term *)
nullptr);
380 (void)del_curterm(termp);
430 #if !defined(HAVE_DECL_ARC4RANDOM) || !HAVE_DECL_ARC4RANDOM
431 static unsigned GetRandomNumberSeed() {
433 if (FILE *RandomSource = ::fopen(
"/dev/urandom",
"r")) {
435 int count = ::fread((
void *)&seed,
sizeof(seed), 1, RandomSource);
436 ::fclose(RandomSource);
446 return hash_combine(Now.seconds(), Now.nanoseconds(), ::getpid());
451 #if defined(HAVE_DECL_ARC4RANDOM) && HAVE_DECL_ARC4RANDOM
454 static int x = (::srand(GetRandomNumberSeed()), 0);
static TimeValue now()
This is a static constructor that returns a TimeValue that represents the current time...
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.
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.
std::string str() const
str - Get the contents as an std::string.
static bool FileDescriptorIsDisplayed(int fd)
This function determines if the given file descriptor is connected to a "tty" or "console" window...
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.
static const char colorcodes[2][2][8][10]
block placement Basic Block Placement Stats
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
static bool StandardInIsUserInput()
This function determines if the standard input is connected directly to a user's input (keyboard prob...
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.
Instances of this class acquire a given Mutex Lock when constructed and hold that lock until destruct...
static unsigned StandardOutColumns()
This function determines the number of columns in the window if standard output is connected to a "tt...
static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time, TimeValue &sys_time)
This static function will set user_time to the amount of CPU time spent in user (non-kernel) mode and...
static std::error_code GetArgumentVector(SmallVectorImpl< const char * > &Args, ArrayRef< const char * > ArgsFromMain, SpecificBumpPtrAllocator< char > &ArgAllocator)
This function returns a SmallVector containing the arguments passed from the operating system to the ...
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
static size_t GetMallocUsage()
Return process memory usage.
static unsigned getPageSize()
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...
A BumpPtrAllocator that allows only elements of a specific type to be allocated.
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.
static const char * OutputBold(bool bg)
Same as OutputColor, but only enables the bold attribute.
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...
static Optional< std::string > GetEnv(StringRef name)