Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 1 | //===- Unix/Process.cpp - Unix Process Implementation --------- -*- C++ -*-===// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the generic Unix implementation of the Process class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 14 | #include "Unix.h" |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Hashing.h" |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringRef.h" |
Chris Bieneman | fa35e11 | 2014-09-22 22:39:20 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Mutex.h" |
| 19 | #include "llvm/Support/MutexGuard.h" |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 20 | #include "llvm/Support/TimeValue.h" |
David Majnemer | 121a174 | 2014-10-06 23:16:18 +0000 | [diff] [blame] | 21 | #if HAVE_FCNTL_H |
| 22 | #include <fcntl.h> |
| 23 | #endif |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 24 | #ifdef HAVE_SYS_TIME_H |
| 25 | #include <sys/time.h> |
| 26 | #endif |
| 27 | #ifdef HAVE_SYS_RESOURCE_H |
| 28 | #include <sys/resource.h> |
| 29 | #endif |
Eric Christopher | 22738d0 | 2012-08-06 20:52:18 +0000 | [diff] [blame] | 30 | // DragonFlyBSD, OpenBSD, and Bitrig have deprecated <malloc.h> for |
| 31 | // <stdlib.h> instead. Unix.h includes this for us already. |
| 32 | #if defined(HAVE_MALLOC_H) && !defined(__DragonFly__) && \ |
| 33 | !defined(__OpenBSD__) && !defined(__Bitrig__) |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 34 | #include <malloc.h> |
| 35 | #endif |
Chris Lattner | 698fa76 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 36 | #ifdef HAVE_MALLOC_MALLOC_H |
| 37 | #include <malloc/malloc.h> |
| 38 | #endif |
Douglas Gregor | 1543661 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 39 | #ifdef HAVE_SYS_IOCTL_H |
| 40 | # include <sys/ioctl.h> |
| 41 | #endif |
Douglas Gregor | b81294d | 2009-05-18 17:21:34 +0000 | [diff] [blame] | 42 | #ifdef HAVE_TERMIOS_H |
| 43 | # include <termios.h> |
| 44 | #endif |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 45 | |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 48 | //=== is guaranteed to work on *all* UNIX variants. |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
Chris Lattner | 2f107cf | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 51 | using namespace llvm; |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 52 | using namespace sys; |
Chandler Carruth | 97683aa | 2012-12-31 11:17:50 +0000 | [diff] [blame] | 53 | |
| 54 | process::id_type self_process::get_id() { |
| 55 | return getpid(); |
| 56 | } |
| 57 | |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 58 | static std::pair<TimeValue, TimeValue> getRUsageTimes() { |
| 59 | #if defined(HAVE_GETRUSAGE) |
| 60 | struct rusage RU; |
| 61 | ::getrusage(RUSAGE_SELF, &RU); |
| 62 | return std::make_pair( |
| 63 | TimeValue( |
| 64 | static_cast<TimeValue::SecondsType>(RU.ru_utime.tv_sec), |
| 65 | static_cast<TimeValue::NanoSecondsType>( |
| 66 | RU.ru_utime.tv_usec * TimeValue::NANOSECONDS_PER_MICROSECOND)), |
| 67 | TimeValue( |
| 68 | static_cast<TimeValue::SecondsType>(RU.ru_stime.tv_sec), |
| 69 | static_cast<TimeValue::NanoSecondsType>( |
| 70 | RU.ru_stime.tv_usec * TimeValue::NANOSECONDS_PER_MICROSECOND))); |
| 71 | #else |
| 72 | #warning Cannot get usage times on this platform |
| 73 | return std::make_pair(TimeValue(), TimeValue()); |
| 74 | #endif |
| 75 | } |
| 76 | |
| 77 | TimeValue self_process::get_user_time() const { |
Chandler Carruth | b5429f4 | 2013-01-05 00:42:50 +0000 | [diff] [blame] | 78 | #if _POSIX_TIMERS > 0 && _POSIX_CPUTIME > 0 |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 79 | // Try to get a high resolution CPU timer. |
| 80 | struct timespec TS; |
| 81 | if (::clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &TS) == 0) |
| 82 | return TimeValue(static_cast<TimeValue::SecondsType>(TS.tv_sec), |
| 83 | static_cast<TimeValue::NanoSecondsType>(TS.tv_nsec)); |
| 84 | #endif |
| 85 | |
| 86 | // Otherwise fall back to rusage based timing. |
| 87 | return getRUsageTimes().first; |
| 88 | } |
| 89 | |
| 90 | TimeValue self_process::get_system_time() const { |
| 91 | // We can only collect system time by inspecting the results of getrusage. |
| 92 | return getRUsageTimes().second; |
| 93 | } |
| 94 | |
NAKAMURA Takumi | 7a04234 | 2013-09-04 14:12:26 +0000 | [diff] [blame] | 95 | // On Cygwin, getpagesize() returns 64k(AllocationGranularity) and |
| 96 | // offset in mmap(3) should be aligned to the AllocationGranularity. |
Chandler Carruth | 15dcad9 | 2012-12-31 23:23:35 +0000 | [diff] [blame] | 97 | static unsigned getPageSize() { |
NAKAMURA Takumi | 3bbbe2e | 2013-08-21 13:47:12 +0000 | [diff] [blame] | 98 | #if defined(HAVE_GETPAGESIZE) |
Owen Anderson | a83e868 | 2009-08-19 21:48:34 +0000 | [diff] [blame] | 99 | const int page_size = ::getpagesize(); |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 100 | #elif defined(HAVE_SYSCONF) |
Owen Anderson | a83e868 | 2009-08-19 21:48:34 +0000 | [diff] [blame] | 101 | long page_size = ::sysconf(_SC_PAGE_SIZE); |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 102 | #else |
| 103 | #warning Cannot get the page size on this machine |
| 104 | #endif |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 105 | return static_cast<unsigned>(page_size); |
| 106 | } |
| 107 | |
Chandler Carruth | 15dcad9 | 2012-12-31 23:23:35 +0000 | [diff] [blame] | 108 | // This constructor guaranteed to be run exactly once on a single thread, and |
| 109 | // sets up various process invariants that can be queried cheaply from then on. |
| 110 | self_process::self_process() : PageSize(getPageSize()) { |
| 111 | } |
| 112 | |
| 113 | |
Chris Lattner | 698fa76 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 114 | size_t Process::GetMallocUsage() { |
Reid Spencer | 1cf74ce | 2004-12-20 16:06:44 +0000 | [diff] [blame] | 115 | #if defined(HAVE_MALLINFO) |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 116 | struct mallinfo mi; |
| 117 | mi = ::mallinfo(); |
| 118 | return mi.uordblks; |
Chris Lattner | 16cbc6a | 2005-11-14 07:27:56 +0000 | [diff] [blame] | 119 | #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H) |
| 120 | malloc_statistics_t Stats; |
| 121 | malloc_zone_statistics(malloc_default_zone(), &Stats); |
| 122 | return Stats.size_in_use; // darwin |
Reid Spencer | 1cf74ce | 2004-12-20 16:06:44 +0000 | [diff] [blame] | 123 | #elif defined(HAVE_SBRK) |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 124 | // Note this is only an approximation and more closely resembles |
| 125 | // the value returned by mallinfo in the arena field. |
Chris Lattner | 698fa76 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 126 | static char *StartOfMemory = reinterpret_cast<char*>(::sbrk(0)); |
| 127 | char *EndOfMemory = (char*)sbrk(0); |
| 128 | if (EndOfMemory != ((char*)-1) && StartOfMemory != ((char*)-1)) |
| 129 | return EndOfMemory - StartOfMemory; |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 130 | else |
| 131 | return 0; |
| 132 | #else |
| 133 | #warning Cannot get malloc info on this platform |
| 134 | return 0; |
| 135 | #endif |
| 136 | } |
| 137 | |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 138 | void Process::GetTimeUsage(TimeValue &elapsed, TimeValue &user_time, |
| 139 | TimeValue &sys_time) { |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 140 | elapsed = TimeValue::now(); |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 141 | std::tie(user_time, sys_time) = getRUsageTimes(); |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 144 | #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__) |
Nate Begeman | 4840515 | 2008-04-12 00:47:46 +0000 | [diff] [blame] | 145 | #include <mach/mach.h> |
| 146 | #endif |
| 147 | |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 148 | // Some LLVM programs such as bugpoint produce core files as a normal part of |
| 149 | // their operation. To prevent the disk from filling up, this function |
| 150 | // does what's necessary to prevent their generation. |
| 151 | void Process::PreventCoreFiles() { |
| 152 | #if HAVE_SETRLIMIT |
| 153 | struct rlimit rlim; |
| 154 | rlim.rlim_cur = rlim.rlim_max = 0; |
Chris Lattner | f64397b | 2006-05-14 18:53:09 +0000 | [diff] [blame] | 155 | setrlimit(RLIMIT_CORE, &rlim); |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 156 | #endif |
Chris Lattner | 2f107cf | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 157 | |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 158 | #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__) |
Nate Begeman | 4840515 | 2008-04-12 00:47:46 +0000 | [diff] [blame] | 159 | // Disable crash reporting on Mac OS X 10.0-10.4 |
| 160 | |
| 161 | // get information about the original set of exception ports for the task |
| 162 | mach_msg_type_number_t Count = 0; |
| 163 | exception_mask_t OriginalMasks[EXC_TYPES_COUNT]; |
| 164 | exception_port_t OriginalPorts[EXC_TYPES_COUNT]; |
| 165 | exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT]; |
| 166 | thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT]; |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 167 | kern_return_t err = |
Nate Begeman | 4840515 | 2008-04-12 00:47:46 +0000 | [diff] [blame] | 168 | task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks, |
| 169 | &Count, OriginalPorts, OriginalBehaviors, |
| 170 | OriginalFlavors); |
| 171 | if (err == KERN_SUCCESS) { |
| 172 | // replace each with MACH_PORT_NULL. |
| 173 | for (unsigned i = 0; i != Count; ++i) |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 174 | task_set_exception_ports(mach_task_self(), OriginalMasks[i], |
Nate Begeman | 4840515 | 2008-04-12 00:47:46 +0000 | [diff] [blame] | 175 | MACH_PORT_NULL, OriginalBehaviors[i], |
| 176 | OriginalFlavors[i]); |
| 177 | } |
| 178 | |
| 179 | // Disable crash reporting on Mac OS X 10.5 |
Nate Begeman | f8be383 | 2008-03-31 22:19:25 +0000 | [diff] [blame] | 180 | signal(SIGABRT, _exit); |
| 181 | signal(SIGILL, _exit); |
| 182 | signal(SIGFPE, _exit); |
| 183 | signal(SIGSEGV, _exit); |
| 184 | signal(SIGBUS, _exit); |
Chris Lattner | 2f107cf | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 185 | #endif |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 186 | } |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 187 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 188 | Optional<std::string> Process::GetEnv(StringRef Name) { |
| 189 | std::string NameStr = Name.str(); |
| 190 | const char *Val = ::getenv(NameStr.c_str()); |
| 191 | if (!Val) |
| 192 | return None; |
| 193 | return std::string(Val); |
| 194 | } |
| 195 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 196 | std::error_code |
| 197 | Process::GetArgumentVector(SmallVectorImpl<const char *> &ArgsOut, |
| 198 | ArrayRef<const char *> ArgsIn, |
| 199 | SpecificBumpPtrAllocator<char> &) { |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 200 | ArgsOut.append(ArgsIn.begin(), ArgsIn.end()); |
| 201 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 202 | return std::error_code(); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 203 | } |
| 204 | |
David Majnemer | 121a174 | 2014-10-06 23:16:18 +0000 | [diff] [blame] | 205 | namespace { |
| 206 | class FDCloser { |
| 207 | public: |
| 208 | FDCloser(int &FD) : FD(FD), KeepOpen(false) {} |
| 209 | void keepOpen() { KeepOpen = true; } |
| 210 | ~FDCloser() { |
| 211 | if (!KeepOpen && FD >= 0) |
| 212 | ::close(FD); |
| 213 | } |
| 214 | |
| 215 | private: |
| 216 | FDCloser(const FDCloser &) LLVM_DELETED_FUNCTION; |
| 217 | void operator=(const FDCloser &) LLVM_DELETED_FUNCTION; |
| 218 | |
| 219 | int &FD; |
| 220 | bool KeepOpen; |
| 221 | }; |
| 222 | } |
| 223 | |
| 224 | std::error_code Process::FixupStandardFileDescriptors() { |
| 225 | int NullFD = -1; |
| 226 | FDCloser FDC(NullFD); |
| 227 | const int StandardFDs[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}; |
| 228 | for (int StandardFD : StandardFDs) { |
| 229 | struct stat st; |
| 230 | errno = 0; |
| 231 | while (fstat(StandardFD, &st) < 0) { |
| 232 | assert(errno && "expected errno to be set if fstat failed!"); |
| 233 | // fstat should return EBADF if the file descriptor is closed. |
| 234 | if (errno == EBADF) |
| 235 | break; |
| 236 | // retry fstat if we got EINTR, otherwise bubble up the failure. |
| 237 | if (errno != EINTR) |
| 238 | return std::error_code(errno, std::generic_category()); |
| 239 | } |
| 240 | // if fstat succeeds, move on to the next FD. |
| 241 | if (!errno) |
| 242 | continue; |
| 243 | assert(errno == EBADF && "expected errno to have EBADF at this point!"); |
| 244 | |
| 245 | if (NullFD < 0) { |
| 246 | while ((NullFD = open("/dev/null", O_RDWR)) < 0) { |
| 247 | if (errno == EINTR) |
| 248 | continue; |
| 249 | return std::error_code(errno, std::generic_category()); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (NullFD == StandardFD) |
| 254 | FDC.keepOpen(); |
| 255 | else if (dup2(NullFD, StandardFD) < 0) |
| 256 | return std::error_code(errno, std::generic_category()); |
| 257 | } |
| 258 | return std::error_code(); |
| 259 | } |
| 260 | |
David Majnemer | 51c2afc | 2014-10-07 05:48:40 +0000 | [diff] [blame^] | 261 | std::error_code Process::SafelyCloseFileDescriptor(int FD) { |
| 262 | // Create a signal set filled with *all* signals. |
| 263 | sigset_t FullSet; |
| 264 | if (sigfillset(&FullSet) < 0) |
| 265 | return std::error_code(errno, std::generic_category()); |
| 266 | // Atomically swap our current signal mask with a full mask. |
| 267 | sigset_t SavedSet; |
| 268 | if (int EC = pthread_sigmask(SIG_SETMASK, &FullSet, &SavedSet)) |
| 269 | return std::error_code(EC, std::generic_category()); |
| 270 | // Attempt to close the file descriptor. |
| 271 | // We need to save the error, if one occurs, because our subsequent call to |
| 272 | // pthread_sigmask might tamper with errno. |
| 273 | int ErrnoFromClose = 0; |
| 274 | if (::close(FD) < 0) |
| 275 | ErrnoFromClose = errno; |
| 276 | // Restore the signal mask back to what we saved earlier. |
| 277 | int EC = pthread_sigmask(SIG_SETMASK, &SavedSet, nullptr); |
| 278 | // The error code from close takes precedence over the one from |
| 279 | // pthread_sigmask. |
| 280 | if (ErrnoFromClose) |
| 281 | return std::error_code(ErrnoFromClose, std::generic_category()); |
| 282 | return std::error_code(EC, std::generic_category()); |
| 283 | } |
| 284 | |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 285 | bool Process::StandardInIsUserInput() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 286 | return FileDescriptorIsDisplayed(STDIN_FILENO); |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | bool Process::StandardOutIsDisplayed() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 290 | return FileDescriptorIsDisplayed(STDOUT_FILENO); |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | bool Process::StandardErrIsDisplayed() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 294 | return FileDescriptorIsDisplayed(STDERR_FILENO); |
| 295 | } |
| 296 | |
| 297 | bool Process::FileDescriptorIsDisplayed(int fd) { |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 298 | #if HAVE_ISATTY |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 299 | return isatty(fd); |
Duncan Sands | 44d423a | 2009-09-06 10:53:22 +0000 | [diff] [blame] | 300 | #else |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 301 | // If we don't have isatty, just return false. |
| 302 | return false; |
Duncan Sands | 44d423a | 2009-09-06 10:53:22 +0000 | [diff] [blame] | 303 | #endif |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 304 | } |
Douglas Gregor | 1543661 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 305 | |
| 306 | static unsigned getColumns(int FileID) { |
| 307 | // If COLUMNS is defined in the environment, wrap to that many columns. |
| 308 | if (const char *ColumnsStr = std::getenv("COLUMNS")) { |
| 309 | int Columns = std::atoi(ColumnsStr); |
| 310 | if (Columns > 0) |
| 311 | return Columns; |
| 312 | } |
| 313 | |
| 314 | unsigned Columns = 0; |
| 315 | |
Douglas Gregor | b81294d | 2009-05-18 17:21:34 +0000 | [diff] [blame] | 316 | #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H) |
Douglas Gregor | 1543661 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 317 | // Try to determine the width of the terminal. |
| 318 | struct winsize ws; |
| 319 | if (ioctl(FileID, TIOCGWINSZ, &ws) == 0) |
| 320 | Columns = ws.ws_col; |
| 321 | #endif |
| 322 | |
| 323 | return Columns; |
| 324 | } |
| 325 | |
| 326 | unsigned Process::StandardOutColumns() { |
| 327 | if (!StandardOutIsDisplayed()) |
| 328 | return 0; |
| 329 | |
| 330 | return getColumns(1); |
| 331 | } |
| 332 | |
| 333 | unsigned Process::StandardErrColumns() { |
| 334 | if (!StandardErrIsDisplayed()) |
| 335 | return 0; |
| 336 | |
| 337 | return getColumns(2); |
| 338 | } |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 339 | |
Chandler Carruth | 9121985 | 2013-08-12 10:40:11 +0000 | [diff] [blame] | 340 | #ifdef HAVE_TERMINFO |
Chandler Carruth | 67ff8b7 | 2013-08-18 01:20:32 +0000 | [diff] [blame] | 341 | // We manually declare these extern functions because finding the correct |
Chandler Carruth | 9121985 | 2013-08-12 10:40:11 +0000 | [diff] [blame] | 342 | // headers from various terminfo, curses, or other sources is harder than |
| 343 | // writing their specs down. |
| 344 | extern "C" int setupterm(char *term, int filedes, int *errret); |
Chandler Carruth | 67ff8b7 | 2013-08-18 01:20:32 +0000 | [diff] [blame] | 345 | extern "C" struct term *set_curterm(struct term *termp); |
| 346 | extern "C" int del_curterm(struct term *termp); |
Chandler Carruth | 9121985 | 2013-08-12 10:40:11 +0000 | [diff] [blame] | 347 | extern "C" int tigetnum(char *capname); |
| 348 | #endif |
| 349 | |
Chris Bieneman | 7827217 | 2014-09-24 18:35:58 +0000 | [diff] [blame] | 350 | #ifdef HAVE_TERMINFO |
Chris Bieneman | fa35e11 | 2014-09-22 22:39:20 +0000 | [diff] [blame] | 351 | static ManagedStatic<sys::Mutex> TermColorMutex; |
Chris Bieneman | 7827217 | 2014-09-24 18:35:58 +0000 | [diff] [blame] | 352 | #endif |
Chris Bieneman | fa35e11 | 2014-09-22 22:39:20 +0000 | [diff] [blame] | 353 | |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 354 | static bool terminalHasColors(int fd) { |
Chandler Carruth | f11f1e4 | 2013-08-12 09:49:17 +0000 | [diff] [blame] | 355 | #ifdef HAVE_TERMINFO |
| 356 | // First, acquire a global lock because these C routines are thread hostile. |
Chris Bieneman | fa35e11 | 2014-09-22 22:39:20 +0000 | [diff] [blame] | 357 | MutexGuard G(*TermColorMutex); |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 358 | |
| 359 | int errret = 0; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 360 | if (setupterm((char *)nullptr, fd, &errret) != 0) |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 361 | // Regardless of why, if we can't get terminfo, we shouldn't try to print |
| 362 | // colors. |
| 363 | return false; |
| 364 | |
Chandler Carruth | f11f1e4 | 2013-08-12 09:49:17 +0000 | [diff] [blame] | 365 | // Test whether the terminal as set up supports color output. How to do this |
| 366 | // isn't entirely obvious. We can use the curses routine 'has_colors' but it |
| 367 | // would be nice to avoid a dependency on curses proper when we can make do |
| 368 | // with a minimal terminfo parsing library. Also, we don't really care whether |
| 369 | // the terminal supports the curses-specific color changing routines, merely |
| 370 | // if it will interpret ANSI color escape codes in a reasonable way. Thus, the |
| 371 | // strategy here is just to query the baseline colors capability and if it |
| 372 | // supports colors at all to assume it will translate the escape codes into |
| 373 | // whatever range of colors it does support. We can add more detailed tests |
| 374 | // here if users report them as necessary. |
| 375 | // |
| 376 | // The 'tigetnum' routine returns -2 or -1 on errors, and might return 0 if |
| 377 | // the terminfo says that no colors are supported. |
Chandler Carruth | 67ff8b7 | 2013-08-18 01:20:32 +0000 | [diff] [blame] | 378 | bool HasColors = tigetnum(const_cast<char *>("colors")) > 0; |
| 379 | |
| 380 | // Now extract the structure allocated by setupterm and free its memory |
| 381 | // through a really silly dance. |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 382 | struct term *termp = set_curterm((struct term *)nullptr); |
Chandler Carruth | 67ff8b7 | 2013-08-18 01:20:32 +0000 | [diff] [blame] | 383 | (void)del_curterm(termp); // Drop any errors here. |
| 384 | |
| 385 | // Return true if we found a color capabilities for the current terminal. |
| 386 | if (HasColors) |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 387 | return true; |
| 388 | #endif |
| 389 | |
| 390 | // Otherwise, be conservative. |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 391 | return false; |
| 392 | } |
| 393 | |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 394 | bool Process::FileDescriptorHasColors(int fd) { |
| 395 | // A file descriptor has colors if it is displayed and the terminal has |
| 396 | // colors. |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 397 | return FileDescriptorIsDisplayed(fd) && terminalHasColors(fd); |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 400 | bool Process::StandardOutHasColors() { |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 401 | return FileDescriptorHasColors(STDOUT_FILENO); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | bool Process::StandardErrHasColors() { |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 405 | return FileDescriptorHasColors(STDERR_FILENO); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 408 | void Process::UseANSIEscapeCodes(bool /*enable*/) { |
| 409 | // No effect. |
| 410 | } |
| 411 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 412 | bool Process::ColorNeedsFlush() { |
| 413 | // No, we use ANSI escape sequences. |
| 414 | return false; |
| 415 | } |
| 416 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 417 | const char *Process::OutputColor(char code, bool bold, bool bg) { |
| 418 | return colorcodes[bg?1:0][bold?1:0][code&7]; |
| 419 | } |
| 420 | |
| 421 | const char *Process::OutputBold(bool bg) { |
| 422 | return "\033[1m"; |
| 423 | } |
| 424 | |
Benjamin Kramer | 13d16f3 | 2012-04-16 08:56:50 +0000 | [diff] [blame] | 425 | const char *Process::OutputReverse() { |
| 426 | return "\033[7m"; |
| 427 | } |
| 428 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 429 | const char *Process::ResetColor() { |
| 430 | return "\033[0m"; |
| 431 | } |
NAKAMURA Takumi | 54acb28 | 2012-05-06 08:24:18 +0000 | [diff] [blame] | 432 | |
Todd Fiala | 4ccfe39 | 2014-02-05 05:04:36 +0000 | [diff] [blame] | 433 | #if !defined(HAVE_DECL_ARC4RANDOM) || !HAVE_DECL_ARC4RANDOM |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 434 | static unsigned GetRandomNumberSeed() { |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 435 | // Attempt to get the initial seed from /dev/urandom, if possible. |
| 436 | if (FILE *RandomSource = ::fopen("/dev/urandom", "r")) { |
| 437 | unsigned seed; |
| 438 | int count = ::fread((void *)&seed, sizeof(seed), 1, RandomSource); |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 439 | ::fclose(RandomSource); |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 440 | |
| 441 | // Return the seed if the read was successful. |
| 442 | if (count == 1) |
| 443 | return seed; |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 444 | } |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 445 | |
| 446 | // Otherwise, swizzle the current time and the process ID to form a reasonable |
| 447 | // seed. |
Chandler Carruth | 5473dfb | 2012-12-31 11:45:20 +0000 | [diff] [blame] | 448 | TimeValue Now = TimeValue::now(); |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 449 | return hash_combine(Now.seconds(), Now.nanoseconds(), ::getpid()); |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 450 | } |
| 451 | #endif |
| 452 | |
NAKAMURA Takumi | 54acb28 | 2012-05-06 08:24:18 +0000 | [diff] [blame] | 453 | unsigned llvm::sys::Process::GetRandomNumber() { |
Todd Fiala | 4ccfe39 | 2014-02-05 05:04:36 +0000 | [diff] [blame] | 454 | #if defined(HAVE_DECL_ARC4RANDOM) && HAVE_DECL_ARC4RANDOM |
NAKAMURA Takumi | 54acb28 | 2012-05-06 08:24:18 +0000 | [diff] [blame] | 455 | return arc4random(); |
| 456 | #else |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 457 | static int x = (::srand(GetRandomNumberSeed()), 0); |
| 458 | (void)x; |
NAKAMURA Takumi | 54acb28 | 2012-05-06 08:24:18 +0000 | [diff] [blame] | 459 | return ::rand(); |
| 460 | #endif |
| 461 | } |