23 #ifdef HAVE_SYS_TIME_H
26 #ifdef HAVE_SYS_RESOURCE_H
27 #include <sys/resource.h>
29 #ifdef HAVE_SYS_STAT_H
37 #if defined(HAVE_MALLOC_H) && !defined(__DragonFly__) && \
38 !defined(__OpenBSD__) && !defined(__Bitrig__)
41 #if defined(HAVE_MALLCTL)
42 #include <malloc_np.h>
44 #ifdef HAVE_MALLOC_MALLOC_H
45 #include <malloc/malloc.h>
47 #ifdef HAVE_SYS_IOCTL_H
48 # include <sys/ioctl.h>
62 static std::pair<std::chrono::microseconds, std::chrono::microseconds> getRUsageTimes() {
63 #if defined(HAVE_GETRUSAGE)
65 ::getrusage(RUSAGE_SELF, &RU);
68 #warning Cannot get usage times on this platform
69 return { std::chrono::microseconds::zero(), std::chrono::microseconds::zero() };
76 #if defined(HAVE_GETPAGESIZE)
77 static const int page_size = ::getpagesize();
78 #elif defined(HAVE_SYSCONF)
79 static long page_size = ::sysconf(_SC_PAGE_SIZE);
81 #warning Cannot get the page size on this machine
83 return static_cast<unsigned>(page_size);
87 #if defined(HAVE_MALLINFO)
91 #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)
92 malloc_statistics_t
Stats;
93 malloc_zone_statistics(malloc_default_zone(), &Stats);
94 return Stats.size_in_use;
95 #elif defined(HAVE_MALLCTL)
98 if (mallctl(
"stats.allocated", &alloc, &sz, NULL, 0) == 0)
101 #elif defined(HAVE_SBRK)
104 static char *StartOfMemory =
reinterpret_cast<char*
>(::sbrk(0));
105 char *EndOfMemory = (
char*)sbrk(0);
106 if (EndOfMemory != ((
char*)-1) && StartOfMemory != ((
char*)-1))
107 return EndOfMemory - StartOfMemory;
110 #warning Cannot get malloc info on this platform
116 std::chrono::nanoseconds &sys_time) {
118 std::tie(user_time, sys_time) = getRUsageTimes();
121 #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__)
122 #include <mach/mach.h>
131 rlim.rlim_cur = rlim.rlim_max = 0;
132 setrlimit(RLIMIT_CORE, &rlim);
135 #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__)
139 mach_msg_type_number_t Count = 0;
140 exception_mask_t OriginalMasks[EXC_TYPES_COUNT];
141 exception_port_t OriginalPorts[EXC_TYPES_COUNT];
142 exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT];
143 thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT];
145 task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks,
146 &Count, OriginalPorts, OriginalBehaviors,
148 if (err == KERN_SUCCESS) {
150 for (
unsigned i = 0; i != Count; ++i)
151 task_set_exception_ports(mach_task_self(), OriginalMasks[i],
152 MACH_PORT_NULL, OriginalBehaviors[i],
157 signal(SIGABRT, _exit);
158 signal(SIGILL, _exit);
159 signal(SIGFPE, _exit);
160 signal(SIGSEGV, _exit);
161 signal(SIGBUS, _exit);
168 std::string NameStr = Name.
str();
169 const char *Val = ::getenv(NameStr.c_str());
172 return std::string(Val);
181 return std::error_code();
187 FDCloser(
int &FD) : FD(FD), KeepOpen(
false) {}
188 void keepOpen() { KeepOpen =
true; }
190 if (!KeepOpen && FD >= 0)
195 FDCloser(
const FDCloser &) =
delete;
196 void operator=(
const FDCloser &) =
delete;
205 FDCloser FDC(NullFD);
206 const int StandardFDs[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
207 for (
int StandardFD : StandardFDs) {
211 assert(errno &&
"expected errno to be set if fstat failed!");
214 return std::error_code(errno, std::generic_category());
219 assert(errno == EBADF &&
"expected errno to have EBADF at this point!");
223 return std::error_code(errno, std::generic_category());
226 if (NullFD == StandardFD)
228 else if (dup2(NullFD, StandardFD) < 0)
229 return std::error_code(errno, std::generic_category());
231 return std::error_code();
237 if (sigfillset(&FullSet) < 0)
238 return std::error_code(errno, std::generic_category());
241 #if LLVM_ENABLE_THREADS
242 if (
int EC = pthread_sigmask(SIG_SETMASK, &FullSet, &SavedSet))
243 return std::error_code(EC, std::generic_category());
245 if (sigprocmask(SIG_SETMASK, &FullSet, &SavedSet) < 0)
246 return std::error_code(errno, std::generic_category());
251 int ErrnoFromClose = 0;
253 ErrnoFromClose = errno;
256 #if LLVM_ENABLE_THREADS
257 EC = pthread_sigmask(SIG_SETMASK, &SavedSet,
nullptr);
259 if (sigprocmask(SIG_SETMASK, &SavedSet,
nullptr) < 0)
265 return std::error_code(ErrnoFromClose, std::generic_category());
266 return std::error_code(EC, std::generic_category());
290 static unsigned getColumns(
int FileID) {
292 if (
const char *ColumnsStr = std::getenv(
"COLUMNS")) {
293 int Columns = std::atoi(ColumnsStr);
298 unsigned Columns = 0;
300 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H)
303 if (ioctl(FileID, TIOCGWINSZ, &ws) == 0)
314 return getColumns(1);
321 return getColumns(2);
328 extern "C" int setupterm(
char *term,
int filedes,
int *errret);
329 extern "C" struct term *set_curterm(
struct term *termp);
330 extern "C" int del_curterm(
struct term *termp);
331 extern "C" int tigetnum(
char *capname);
338 static bool terminalHasColors(
int fd) {
344 if (setupterm(
nullptr, fd, &errret) != 0)
362 bool HasColors = tigetnum(const_cast<char *>(
"colors")) > 0;
366 struct term *termp = set_curterm(
nullptr);
367 (void)del_curterm(termp);
417 #if !HAVE_DECL_ARC4RANDOM
418 static unsigned GetRandomNumberSeed() {
420 int urandomFD = open(
"/dev/urandom", O_RDONLY);
422 if (urandomFD != -1) {
426 int count =
read(urandomFD, (
void *)&seed,
sizeof(seed));
431 if (count ==
sizeof(seed))
438 return hash_combine(Now.time_since_epoch().count(), ::getpid());
443 #if HAVE_DECL_ARC4RANDOM
446 static int x = (
static_cast<void>(::srand(GetRandomNumberSeed())), 0);
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.
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...
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(std::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...
Function Alias Analysis false
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 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 ...
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
static bool coreFilesPrevented
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
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...
auto RetryAfterSignal(const FailT &Fail, const Fun &F, const Args &...As) -> decltype(F(As...))
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.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
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.
std::chrono::microseconds toDuration(const struct timeval &TV)
Convert a struct timeval to a duration.
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 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)