Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 1 | //===- Unix/Process.cpp - Unix Process Implementation --------- -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the generic Unix implementation of the Process class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 14 | #include "Unix.h" |
| 15 | #ifdef HAVE_SYS_TIME_H |
| 16 | #include <sys/time.h> |
| 17 | #endif |
| 18 | #ifdef HAVE_SYS_RESOURCE_H |
| 19 | #include <sys/resource.h> |
| 20 | #endif |
| 21 | #ifdef HAVE_MALLOC_H |
| 22 | #include <malloc.h> |
| 23 | #endif |
Chris Lattner | 513a954 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 24 | #ifdef HAVE_MALLOC_MALLOC_H |
| 25 | #include <malloc/malloc.h> |
| 26 | #endif |
Douglas Gregor | 0174674 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 27 | #ifdef HAVE_SYS_IOCTL_H |
| 28 | # include <sys/ioctl.h> |
| 29 | #endif |
Douglas Gregor | 071d73d | 2009-05-18 17:21:34 +0000 | [diff] [blame] | 30 | #ifdef HAVE_TERMIOS_H |
| 31 | # include <termios.h> |
| 32 | #endif |
Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 33 | |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 36 | //=== is guaranteed to work on *all* UNIX variants. |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Chris Lattner | 26b6c0b | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 40 | using namespace sys; |
| 41 | |
| 42 | unsigned |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 43 | Process::GetPageSize() |
| 44 | { |
Jay Foad | a14be3b | 2009-05-23 17:57:59 +0000 | [diff] [blame] | 45 | #if defined(__CYGWIN__) |
| 46 | // On Cygwin, getpagesize() returns 64k but the page size for the purposes of |
| 47 | // memory protection and mmap() is 4k. |
| 48 | // See http://www.cygwin.com/ml/cygwin/2009-01/threads.html#00492 |
Owen Anderson | 0d1ea25 | 2009-08-19 21:48:34 +0000 | [diff] [blame] | 49 | const int page_size = 0x1000; |
Jay Foad | a14be3b | 2009-05-23 17:57:59 +0000 | [diff] [blame] | 50 | #elif defined(HAVE_GETPAGESIZE) |
Owen Anderson | 0d1ea25 | 2009-08-19 21:48:34 +0000 | [diff] [blame] | 51 | const int page_size = ::getpagesize(); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 52 | #elif defined(HAVE_SYSCONF) |
Owen Anderson | 0d1ea25 | 2009-08-19 21:48:34 +0000 | [diff] [blame] | 53 | long page_size = ::sysconf(_SC_PAGE_SIZE); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 54 | #else |
| 55 | #warning Cannot get the page size on this machine |
| 56 | #endif |
Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 57 | return static_cast<unsigned>(page_size); |
| 58 | } |
| 59 | |
Chris Lattner | 513a954 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 60 | size_t Process::GetMallocUsage() { |
Reid Spencer | bc1ee84 | 2004-12-20 16:06:44 +0000 | [diff] [blame] | 61 | #if defined(HAVE_MALLINFO) |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 62 | struct mallinfo mi; |
| 63 | mi = ::mallinfo(); |
| 64 | return mi.uordblks; |
Chris Lattner | c590e9a | 2005-11-14 07:27:56 +0000 | [diff] [blame] | 65 | #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H) |
| 66 | malloc_statistics_t Stats; |
| 67 | malloc_zone_statistics(malloc_default_zone(), &Stats); |
| 68 | return Stats.size_in_use; // darwin |
Reid Spencer | bc1ee84 | 2004-12-20 16:06:44 +0000 | [diff] [blame] | 69 | #elif defined(HAVE_SBRK) |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 70 | // Note this is only an approximation and more closely resembles |
| 71 | // the value returned by mallinfo in the arena field. |
Chris Lattner | 513a954 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 72 | static char *StartOfMemory = reinterpret_cast<char*>(::sbrk(0)); |
| 73 | char *EndOfMemory = (char*)sbrk(0); |
| 74 | if (EndOfMemory != ((char*)-1) && StartOfMemory != ((char*)-1)) |
| 75 | return EndOfMemory - StartOfMemory; |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 76 | else |
| 77 | return 0; |
| 78 | #else |
| 79 | #warning Cannot get malloc info on this platform |
| 80 | return 0; |
| 81 | #endif |
| 82 | } |
| 83 | |
Jeff Cohen | e269a1a | 2005-01-08 20:15:57 +0000 | [diff] [blame] | 84 | size_t |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 85 | Process::GetTotalMemoryUsage() |
| 86 | { |
Reid Spencer | bc1ee84 | 2004-12-20 16:06:44 +0000 | [diff] [blame] | 87 | #if defined(HAVE_MALLINFO) |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 88 | struct mallinfo mi = ::mallinfo(); |
| 89 | return mi.uordblks + mi.hblkhd; |
Chris Lattner | c590e9a | 2005-11-14 07:27:56 +0000 | [diff] [blame] | 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_allocated; // darwin |
Reid Spencer | ed5e7bf | 2004-12-20 16:33:37 +0000 | [diff] [blame] | 94 | #elif defined(HAVE_GETRUSAGE) |
| 95 | struct rusage usage; |
| 96 | ::getrusage(RUSAGE_SELF, &usage); |
| 97 | return usage.ru_maxrss; |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 98 | #else |
| 99 | #warning Cannot get total memory size on this platform |
| 100 | return 0; |
| 101 | #endif |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | Process::GetTimeUsage(TimeValue& elapsed, TimeValue& user_time, |
| 106 | TimeValue& sys_time) |
| 107 | { |
| 108 | elapsed = TimeValue::now(); |
Chris Lattner | 095cc37 | 2006-05-12 18:20:39 +0000 | [diff] [blame] | 109 | #if defined(HAVE_GETRUSAGE) |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 110 | struct rusage usage; |
| 111 | ::getrusage(RUSAGE_SELF, &usage); |
Reid Spencer | 8b66289 | 2004-12-20 21:43:33 +0000 | [diff] [blame] | 112 | user_time = TimeValue( |
| 113 | static_cast<TimeValue::SecondsType>( usage.ru_utime.tv_sec ), |
| 114 | static_cast<TimeValue::NanoSecondsType>( usage.ru_utime.tv_usec * |
| 115 | TimeValue::NANOSECONDS_PER_MICROSECOND ) ); |
| 116 | sys_time = TimeValue( |
| 117 | static_cast<TimeValue::SecondsType>( usage.ru_stime.tv_sec ), |
| 118 | static_cast<TimeValue::NanoSecondsType>( usage.ru_stime.tv_usec * |
| 119 | TimeValue::NANOSECONDS_PER_MICROSECOND ) ); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 120 | #else |
| 121 | #warning Cannot get usage times on this platform |
| 122 | user_time.seconds(0); |
| 123 | user_time.microseconds(0); |
| 124 | sys_time.seconds(0); |
| 125 | sys_time.microseconds(0); |
| 126 | #endif |
| 127 | } |
| 128 | |
Chris Lattner | 26b6c0b | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 129 | int Process::GetCurrentUserId() { |
Reid Spencer | 93b3473 | 2005-04-21 16:12:57 +0000 | [diff] [blame] | 130 | return getuid(); |
| 131 | } |
| 132 | |
Chris Lattner | 26b6c0b | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 133 | int Process::GetCurrentGroupId() { |
Reid Spencer | 93b3473 | 2005-04-21 16:12:57 +0000 | [diff] [blame] | 134 | return getgid(); |
| 135 | } |
| 136 | |
Nate Begeman | e956398 | 2008-04-12 00:47:46 +0000 | [diff] [blame] | 137 | #ifdef HAVE_MACH_MACH_H |
| 138 | #include <mach/mach.h> |
| 139 | #endif |
| 140 | |
Reid Spencer | 68fdcc1 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 141 | // Some LLVM programs such as bugpoint produce core files as a normal part of |
| 142 | // their operation. To prevent the disk from filling up, this function |
| 143 | // does what's necessary to prevent their generation. |
| 144 | void Process::PreventCoreFiles() { |
| 145 | #if HAVE_SETRLIMIT |
| 146 | struct rlimit rlim; |
| 147 | rlim.rlim_cur = rlim.rlim_max = 0; |
Chris Lattner | 505f9c8 | 2006-05-14 18:53:09 +0000 | [diff] [blame] | 148 | setrlimit(RLIMIT_CORE, &rlim); |
Reid Spencer | 68fdcc1 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 149 | #endif |
Chris Lattner | 26b6c0b | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 5f1d013 | 2006-09-14 06:21:59 +0000 | [diff] [blame] | 151 | #ifdef HAVE_MACH_MACH_H |
Nate Begeman | e956398 | 2008-04-12 00:47:46 +0000 | [diff] [blame] | 152 | // Disable crash reporting on Mac OS X 10.0-10.4 |
| 153 | |
| 154 | // get information about the original set of exception ports for the task |
| 155 | mach_msg_type_number_t Count = 0; |
| 156 | exception_mask_t OriginalMasks[EXC_TYPES_COUNT]; |
| 157 | exception_port_t OriginalPorts[EXC_TYPES_COUNT]; |
| 158 | exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT]; |
| 159 | thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT]; |
| 160 | kern_return_t err = |
| 161 | task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks, |
| 162 | &Count, OriginalPorts, OriginalBehaviors, |
| 163 | OriginalFlavors); |
| 164 | if (err == KERN_SUCCESS) { |
| 165 | // replace each with MACH_PORT_NULL. |
| 166 | for (unsigned i = 0; i != Count; ++i) |
| 167 | task_set_exception_ports(mach_task_self(), OriginalMasks[i], |
| 168 | MACH_PORT_NULL, OriginalBehaviors[i], |
| 169 | OriginalFlavors[i]); |
| 170 | } |
| 171 | |
| 172 | // Disable crash reporting on Mac OS X 10.5 |
Nate Begeman | 82b53cd | 2008-03-31 22:19:25 +0000 | [diff] [blame] | 173 | signal(SIGABRT, _exit); |
| 174 | signal(SIGILL, _exit); |
| 175 | signal(SIGFPE, _exit); |
| 176 | signal(SIGSEGV, _exit); |
| 177 | signal(SIGBUS, _exit); |
Chris Lattner | 26b6c0b | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 178 | #endif |
Reid Spencer | 68fdcc1 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 179 | } |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 180 | |
Reid Spencer | a01aade | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 181 | bool Process::StandardInIsUserInput() { |
| 182 | #if HAVE_ISATTY |
| 183 | return isatty(0); |
Duncan Sands | 740eb53 | 2009-09-06 10:53:22 +0000 | [diff] [blame] | 184 | #else |
Reid Spencer | a01aade | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 185 | // If we don't have isatty, just return false. |
| 186 | return false; |
Duncan Sands | 740eb53 | 2009-09-06 10:53:22 +0000 | [diff] [blame] | 187 | #endif |
Reid Spencer | a01aade | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | bool Process::StandardOutIsDisplayed() { |
| 191 | #if HAVE_ISATTY |
| 192 | return isatty(1); |
Duncan Sands | 740eb53 | 2009-09-06 10:53:22 +0000 | [diff] [blame] | 193 | #else |
Reid Spencer | a01aade | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 194 | // If we don't have isatty, just return false. |
| 195 | return false; |
Duncan Sands | 740eb53 | 2009-09-06 10:53:22 +0000 | [diff] [blame] | 196 | #endif |
Reid Spencer | a01aade | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | bool Process::StandardErrIsDisplayed() { |
| 200 | #if HAVE_ISATTY |
| 201 | return isatty(2); |
Duncan Sands | 740eb53 | 2009-09-06 10:53:22 +0000 | [diff] [blame] | 202 | #else |
Reid Spencer | a01aade | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 203 | // If we don't have isatty, just return false. |
| 204 | return false; |
Duncan Sands | 740eb53 | 2009-09-06 10:53:22 +0000 | [diff] [blame] | 205 | #endif |
Reid Spencer | a01aade | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 206 | } |
Douglas Gregor | 0174674 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 207 | |
| 208 | static unsigned getColumns(int FileID) { |
| 209 | // If COLUMNS is defined in the environment, wrap to that many columns. |
| 210 | if (const char *ColumnsStr = std::getenv("COLUMNS")) { |
| 211 | int Columns = std::atoi(ColumnsStr); |
| 212 | if (Columns > 0) |
| 213 | return Columns; |
| 214 | } |
| 215 | |
| 216 | unsigned Columns = 0; |
| 217 | |
Douglas Gregor | 071d73d | 2009-05-18 17:21:34 +0000 | [diff] [blame] | 218 | #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H) |
Douglas Gregor | 0174674 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 219 | // Try to determine the width of the terminal. |
| 220 | struct winsize ws; |
| 221 | if (ioctl(FileID, TIOCGWINSZ, &ws) == 0) |
| 222 | Columns = ws.ws_col; |
| 223 | #endif |
| 224 | |
| 225 | return Columns; |
| 226 | } |
| 227 | |
| 228 | unsigned Process::StandardOutColumns() { |
| 229 | if (!StandardOutIsDisplayed()) |
| 230 | return 0; |
| 231 | |
| 232 | return getColumns(1); |
| 233 | } |
| 234 | |
| 235 | unsigned Process::StandardErrColumns() { |
| 236 | if (!StandardErrIsDisplayed()) |
| 237 | return 0; |
| 238 | |
| 239 | return getColumns(2); |
| 240 | } |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 241 | |
| 242 | static bool terminalHasColors() { |
| 243 | if (const char *term = std::getenv("TERM")) { |
| 244 | // Most modern terminals support ANSI escape sequences for colors. |
| 245 | // We could check terminfo, or have a list of known terms that support |
| 246 | // colors, but that would be overkill. |
| 247 | // The user can always ask for no colors by setting TERM to dumb, or |
| 248 | // using a commandline flag. |
| 249 | return strcmp(term, "dumb") != 0; |
| 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | bool Process::StandardOutHasColors() { |
| 255 | if (!StandardOutIsDisplayed()) |
| 256 | return false; |
| 257 | return terminalHasColors(); |
| 258 | } |
| 259 | |
| 260 | bool Process::StandardErrHasColors() { |
| 261 | if (!StandardErrIsDisplayed()) |
| 262 | return false; |
| 263 | return terminalHasColors(); |
| 264 | } |
| 265 | |
| 266 | bool Process::ColorNeedsFlush() { |
| 267 | // No, we use ANSI escape sequences. |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m" |
| 272 | |
| 273 | #define ALLCOLORS(FGBG,BOLD) {\ |
| 274 | COLOR(FGBG, "0", BOLD),\ |
| 275 | COLOR(FGBG, "1", BOLD),\ |
| 276 | COLOR(FGBG, "2", BOLD),\ |
| 277 | COLOR(FGBG, "3", BOLD),\ |
| 278 | COLOR(FGBG, "4", BOLD),\ |
| 279 | COLOR(FGBG, "5", BOLD),\ |
| 280 | COLOR(FGBG, "6", BOLD),\ |
| 281 | COLOR(FGBG, "7", BOLD)\ |
| 282 | } |
| 283 | |
| 284 | static const char* colorcodes[2][2][8] = { |
| 285 | { ALLCOLORS("3",""), ALLCOLORS("3","1;") }, |
| 286 | { ALLCOLORS("4",""), ALLCOLORS("4","1;") } |
| 287 | }; |
| 288 | |
| 289 | const char *Process::OutputColor(char code, bool bold, bool bg) { |
| 290 | return colorcodes[bg?1:0][bold?1:0][code&7]; |
| 291 | } |
| 292 | |
| 293 | const char *Process::OutputBold(bool bg) { |
| 294 | return "\033[1m"; |
| 295 | } |
| 296 | |
| 297 | const char *Process::ResetColor() { |
| 298 | return "\033[0m"; |
| 299 | } |