Misha Brukman | ebb0faa | 2004-06-18 15:38:49 +0000 | [diff] [blame] | 1 | //===- SystemUtils.cpp - Utilities for low-level system tasks -------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file contains functions used to do a variety of low-level, often |
| 11 | // system-specific, tasks. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | c89fe6d | 2004-05-28 00:59:40 +0000 | [diff] [blame] | 15 | #define _POSIX_MAPPED_FILES |
Misha Brukman | 3d1b0c7 | 2003-08-07 21:28:50 +0000 | [diff] [blame] | 16 | #include "Support/SystemUtils.h" |
John Criswell | 7a73b80 | 2003-06-30 21:59:07 +0000 | [diff] [blame] | 17 | #include "Config/fcntl.h" |
Misha Brukman | b487303 | 2004-06-18 15:34:07 +0000 | [diff] [blame] | 18 | #include "Config/pagesize.h" |
John Criswell | 7a73b80 | 2003-06-30 21:59:07 +0000 | [diff] [blame] | 19 | #include "Config/unistd.h" |
Misha Brukman | ebb0faa | 2004-06-18 15:38:49 +0000 | [diff] [blame] | 20 | #include "Config/windows.h" |
| 21 | #include "Config/sys/mman.h" |
| 22 | #include "Config/sys/stat.h" |
| 23 | #include "Config/sys/types.h" |
| 24 | #include "Config/sys/wait.h" |
Chris Lattner | 74b1f45 | 2004-01-10 19:15:14 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Misha Brukman | ebb0faa | 2004-06-18 15:38:49 +0000 | [diff] [blame] | 26 | #include <cerrno> |
| 27 | #include <cstdlib> |
Chris Lattner | 74b1f45 | 2004-01-10 19:15:14 +0000 | [diff] [blame] | 28 | #include <fstream> |
| 29 | #include <iostream> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 32 | /// isExecutableFile - This function returns true if the filename specified |
| 33 | /// exists and is executable. |
| 34 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 35 | bool llvm::isExecutableFile(const std::string &ExeFileName) { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 36 | struct stat Buf; |
| 37 | if (stat(ExeFileName.c_str(), &Buf)) |
| 38 | return false; // Must not be executable! |
| 39 | |
| 40 | if (!(Buf.st_mode & S_IFREG)) |
| 41 | return false; // Not a regular file? |
| 42 | |
| 43 | if (Buf.st_uid == getuid()) // Owner of file? |
| 44 | return Buf.st_mode & S_IXUSR; |
| 45 | else if (Buf.st_gid == getgid()) // In group of file? |
| 46 | return Buf.st_mode & S_IXGRP; |
| 47 | else // Unrelated to file? |
| 48 | return Buf.st_mode & S_IXOTH; |
| 49 | } |
| 50 | |
Chris Lattner | b234d46 | 2004-04-02 05:04:03 +0000 | [diff] [blame] | 51 | /// isStandardOutAConsole - Return true if we can tell that the standard output |
| 52 | /// stream goes to a terminal window or console. |
| 53 | bool llvm::isStandardOutAConsole() { |
Brian Gaeke | 8507ecb | 2004-04-02 21:26:04 +0000 | [diff] [blame] | 54 | #if HAVE_ISATTY |
Chris Lattner | b234d46 | 2004-04-02 05:04:03 +0000 | [diff] [blame] | 55 | return isatty(1); |
Brian Gaeke | 8507ecb | 2004-04-02 21:26:04 +0000 | [diff] [blame] | 56 | #endif |
| 57 | // If we don't have isatty, just return false. |
| 58 | return false; |
Chris Lattner | b234d46 | 2004-04-02 05:04:03 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
Misha Brukman | f7066c7 | 2003-08-07 21:34:25 +0000 | [diff] [blame] | 62 | /// FindExecutable - Find a named executable, giving the argv[0] of program |
Misha Brukman | 44f8a34 | 2003-09-29 22:40:07 +0000 | [diff] [blame] | 63 | /// being executed. This allows us to find another LLVM tool if it is built |
| 64 | /// into the same directory, but that directory is neither the current |
| 65 | /// directory, nor in the PATH. If the executable cannot be found, return an |
| 66 | /// empty string. |
Misha Brukman | f7066c7 | 2003-08-07 21:34:25 +0000 | [diff] [blame] | 67 | /// |
Chris Lattner | 49f61c4 | 2004-05-28 01:20:58 +0000 | [diff] [blame] | 68 | #undef FindExecutable // needed on windows :( |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 69 | std::string llvm::FindExecutable(const std::string &ExeName, |
| 70 | const std::string &ProgramPath) { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 71 | // First check the directory that bugpoint is in. We can do this if |
| 72 | // BugPointPath contains at least one / character, indicating that it is a |
| 73 | // relative path to bugpoint itself. |
| 74 | // |
Misha Brukman | 35d402f | 2003-08-07 21:33:33 +0000 | [diff] [blame] | 75 | std::string Result = ProgramPath; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 76 | while (!Result.empty() && Result[Result.size()-1] != '/') |
| 77 | Result.erase(Result.size()-1, 1); |
| 78 | |
| 79 | if (!Result.empty()) { |
| 80 | Result += ExeName; |
| 81 | if (isExecutableFile(Result)) return Result; // Found it? |
| 82 | } |
| 83 | |
Misha Brukman | 35d402f | 2003-08-07 21:33:33 +0000 | [diff] [blame] | 84 | // Okay, if the path to the program didn't tell us anything, try using the |
| 85 | // PATH environment variable. |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 86 | const char *PathStr = getenv("PATH"); |
| 87 | if (PathStr == 0) return ""; |
| 88 | |
Misha Brukman | bc0e998 | 2003-07-14 17:20:40 +0000 | [diff] [blame] | 89 | // Now we have a colon separated list of directories to search... try them... |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 90 | unsigned PathLen = strlen(PathStr); |
| 91 | while (PathLen) { |
| 92 | // Find the first colon... |
| 93 | const char *Colon = std::find(PathStr, PathStr+PathLen, ':'); |
| 94 | |
| 95 | // Check to see if this first directory contains the executable... |
| 96 | std::string FilePath = std::string(PathStr, Colon) + '/' + ExeName; |
| 97 | if (isExecutableFile(FilePath)) |
| 98 | return FilePath; // Found the executable! |
| 99 | |
| 100 | // Nope it wasn't in this directory, check the next range! |
| 101 | PathLen -= Colon-PathStr; |
| 102 | PathStr = Colon; |
| 103 | while (*PathStr == ':') { // Advance past colons |
| 104 | PathStr++; |
| 105 | PathLen--; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // If we fell out, we ran out of directories in PATH to search, return failure |
| 110 | return ""; |
| 111 | } |
| 112 | |
| 113 | static void RedirectFD(const std::string &File, int FD) { |
| 114 | if (File.empty()) return; // Noop |
| 115 | |
| 116 | // Open the file |
| 117 | int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666); |
| 118 | if (InFD == -1) { |
| 119 | std::cerr << "Error opening file '" << File << "' for " |
Misha Brukman | 44f8a34 | 2003-09-29 22:40:07 +0000 | [diff] [blame] | 120 | << (FD == 0 ? "input" : "output") << "!\n"; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 121 | exit(1); |
| 122 | } |
| 123 | |
| 124 | dup2(InFD, FD); // Install it as the requested FD |
| 125 | close(InFD); // Close the original FD |
| 126 | } |
| 127 | |
| 128 | /// RunProgramWithTimeout - This function executes the specified program, with |
| 129 | /// the specified null-terminated argument array, with the stdin/out/err fd's |
Misha Brukman | 950971d | 2003-09-16 15:31:46 +0000 | [diff] [blame] | 130 | /// redirected, with a timeout specified on the command line. This terminates |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 131 | /// the calling program if there is an error executing the specified program. |
| 132 | /// It returns the return value of the program, or -1 if a timeout is detected. |
| 133 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 134 | int llvm::RunProgramWithTimeout(const std::string &ProgramPath, |
| 135 | const char **Args, |
| 136 | const std::string &StdInFile, |
| 137 | const std::string &StdOutFile, |
| 138 | const std::string &StdErrFile) { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 139 | // FIXME: install sigalarm handler here for timeout... |
| 140 | |
Chris Lattner | d895ffe | 2004-05-27 01:20:55 +0000 | [diff] [blame] | 141 | #ifdef HAVE_SYS_WAIT_H |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 142 | int Child = fork(); |
| 143 | switch (Child) { |
| 144 | case -1: |
| 145 | std::cerr << "ERROR forking!\n"; |
| 146 | exit(1); |
| 147 | case 0: // Child |
| 148 | RedirectFD(StdInFile, 0); // Redirect file descriptors... |
| 149 | RedirectFD(StdOutFile, 1); |
Chris Lattner | bf3d2e2 | 2004-04-16 05:35:58 +0000 | [diff] [blame] | 150 | if (StdOutFile != StdErrFile) |
| 151 | RedirectFD(StdErrFile, 2); |
| 152 | else |
| 153 | dup2(1, 2); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 154 | |
| 155 | execv(ProgramPath.c_str(), (char *const *)Args); |
Brian Gaeke | 53e557d | 2003-10-15 20:46:58 +0000 | [diff] [blame] | 156 | std::cerr << "Error executing program: '" << ProgramPath; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 157 | for (; *Args; ++Args) |
| 158 | std::cerr << " " << *Args; |
Brian Gaeke | 53e557d | 2003-10-15 20:46:58 +0000 | [diff] [blame] | 159 | std::cerr << "'\n"; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 160 | exit(1); |
| 161 | |
| 162 | default: break; |
| 163 | } |
| 164 | |
| 165 | // Make sure all output has been written while waiting |
| 166 | std::cout << std::flush; |
| 167 | |
| 168 | int Status; |
| 169 | if (wait(&Status) != Child) { |
| 170 | if (errno == EINTR) { |
| 171 | static bool FirstTimeout = true; |
| 172 | if (FirstTimeout) { |
Misha Brukman | 44f8a34 | 2003-09-29 22:40:07 +0000 | [diff] [blame] | 173 | std::cout << |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 174 | "*** Program execution timed out! This mechanism is designed to handle\n" |
| 175 | " programs stuck in infinite loops gracefully. The -timeout option\n" |
| 176 | " can be used to change the timeout threshold or disable it completely\n" |
| 177 | " (with -timeout=0). This message is only displayed once.\n"; |
Misha Brukman | 44f8a34 | 2003-09-29 22:40:07 +0000 | [diff] [blame] | 178 | FirstTimeout = false; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 179 | } |
| 180 | return -1; // Timeout detected |
| 181 | } |
| 182 | |
| 183 | std::cerr << "Error waiting for child process!\n"; |
| 184 | exit(1); |
| 185 | } |
| 186 | return Status; |
Chris Lattner | d895ffe | 2004-05-27 01:20:55 +0000 | [diff] [blame] | 187 | |
| 188 | #else |
| 189 | std::cerr << "RunProgramWithTimeout not implemented on this platform!\n"; |
| 190 | return -1; |
| 191 | #endif |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 192 | } |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 193 | |
| 194 | |
Misha Brukman | d6af686 | 2004-06-02 00:09:46 +0000 | [diff] [blame] | 195 | // ExecWait - executes a program with the specified arguments and environment. |
| 196 | // It then waits for the progarm to termiante and then returns to the caller. |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 197 | // |
| 198 | // Inputs: |
| 199 | // argv - The arguments to the program as an array of C strings. The first |
| 200 | // argument should be the name of the program to execute, and the |
| 201 | // last argument should be a pointer to NULL. |
| 202 | // |
| 203 | // envp - The environment passes to the program as an array of C strings in |
| 204 | // the form of "name=value" pairs. The last element should be a |
| 205 | // pointer to NULL. |
| 206 | // |
| 207 | // Outputs: |
| 208 | // None. |
| 209 | // |
| 210 | // Return value: |
| 211 | // 0 - No errors. |
| 212 | // 1 - The program could not be executed. |
| 213 | // 1 - The program returned a non-zero exit status. |
| 214 | // 1 - The program terminated abnormally. |
| 215 | // |
| 216 | // Notes: |
| 217 | // The program will inherit the stdin, stdout, and stderr file descriptors |
| 218 | // as well as other various configuration settings (umask). |
| 219 | // |
| 220 | // This function should not print anything to stdout/stderr on its own. It is |
| 221 | // a generic library function. The caller or executed program should report |
| 222 | // errors in the way it sees fit. |
| 223 | // |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 224 | // This function does not use $PATH to find programs. |
| 225 | // |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 226 | int llvm::ExecWait(const char * const old_argv[], |
| 227 | const char * const old_envp[]) { |
Chris Lattner | d895ffe | 2004-05-27 01:20:55 +0000 | [diff] [blame] | 228 | #ifdef HAVE_SYS_WAIT_H |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 229 | // Create local versions of the parameters that can be passed into execve() |
| 230 | // without creating const problems. |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 231 | char ** const argv = (char ** const) old_argv; |
| 232 | char ** const envp = (char ** const) old_envp; |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 233 | |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 234 | // Create a child process. |
Chris Lattner | d895ffe | 2004-05-27 01:20:55 +0000 | [diff] [blame] | 235 | switch (fork()) { |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 236 | // An error occured: Return to the caller. |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 237 | case -1: |
| 238 | return 1; |
| 239 | break; |
| 240 | |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 241 | // Child process: Execute the program. |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 242 | case 0: |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 243 | execve (argv[0], argv, envp); |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 244 | // If the execve() failed, we should exit and let the parent pick up |
| 245 | // our non-zero exit status. |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 246 | exit (1); |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 247 | |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 248 | // Parent process: Break out of the switch to do our processing. |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 249 | default: |
| 250 | break; |
| 251 | } |
| 252 | |
Misha Brukman | d6af686 | 2004-06-02 00:09:46 +0000 | [diff] [blame] | 253 | // Parent process: Wait for the child process to terminate. |
Chris Lattner | d895ffe | 2004-05-27 01:20:55 +0000 | [diff] [blame] | 254 | int status; |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 255 | if ((wait (&status)) == -1) |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 256 | return 1; |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 257 | |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 258 | // If the program exited normally with a zero exit status, return success! |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 259 | if (WIFEXITED (status) && (WEXITSTATUS(status) == 0)) |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 260 | return 0; |
Chris Lattner | d895ffe | 2004-05-27 01:20:55 +0000 | [diff] [blame] | 261 | #else |
| 262 | std::cerr << "llvm::ExecWait not implemented on this platform!\n"; |
| 263 | #endif |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 264 | |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 265 | // Otherwise, return failure. |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 266 | return 1; |
| 267 | } |
Chris Lattner | c89fe6d | 2004-05-28 00:59:40 +0000 | [diff] [blame] | 268 | |
| 269 | /// AllocateRWXMemory - Allocate a slab of memory with read/write/execute |
| 270 | /// permissions. This is typically used for JIT applications where we want |
| 271 | /// to emit code to the memory then jump to it. Getting this type of memory |
| 272 | /// is very OS specific. |
| 273 | /// |
| 274 | void *llvm::AllocateRWXMemory(unsigned NumBytes) { |
| 275 | if (NumBytes == 0) return 0; |
Chris Lattner | 49f61c4 | 2004-05-28 01:20:58 +0000 | [diff] [blame] | 276 | |
| 277 | #if defined(HAVE_WINDOWS_H) |
| 278 | // On windows we use VirtualAlloc. |
| 279 | void *P = VirtualAlloc(0, NumBytes, MEM_COMMIT, PAGE_EXECUTE_READWRITE); |
| 280 | if (P == 0) { |
| 281 | std::cerr << "Error allocating executable memory!\n"; |
| 282 | abort(); |
| 283 | } |
| 284 | return P; |
| 285 | |
| 286 | #elif defined(HAVE_MMAP) |
Misha Brukman | b487303 | 2004-06-18 15:34:07 +0000 | [diff] [blame] | 287 | static const long pageSize = GetPageSize(); |
Chris Lattner | c89fe6d | 2004-05-28 00:59:40 +0000 | [diff] [blame] | 288 | unsigned NumPages = (NumBytes+pageSize-1)/pageSize; |
| 289 | |
| 290 | /* FIXME: This should use the proper autoconf flags */ |
| 291 | #if defined(i386) || defined(__i386__) || defined(__x86__) |
| 292 | /* Linux and *BSD tend to have these flags named differently. */ |
| 293 | #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS) |
| 294 | # define MAP_ANONYMOUS MAP_ANON |
| 295 | #endif /* defined(MAP_ANON) && !defined(MAP_ANONYMOUS) */ |
| 296 | #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
| 297 | /* nothing */ |
| 298 | #else |
Chris Lattner | 49f61c4 | 2004-05-28 01:20:58 +0000 | [diff] [blame] | 299 | std::cerr << "This architecture has an unknown MMAP implementation!\n"; |
Chris Lattner | c89fe6d | 2004-05-28 00:59:40 +0000 | [diff] [blame] | 300 | abort(); |
| 301 | return 0; |
| 302 | #endif |
| 303 | |
Chris Lattner | c89fe6d | 2004-05-28 00:59:40 +0000 | [diff] [blame] | 304 | int fd = -1; |
| 305 | #if defined(__linux__) |
| 306 | fd = 0; |
| 307 | #endif |
| 308 | |
| 309 | unsigned mmapFlags = MAP_PRIVATE|MAP_ANONYMOUS; |
| 310 | #ifdef MAP_NORESERVE |
| 311 | mmapFlags |= MAP_NORESERVE; |
| 312 | #endif |
| 313 | |
| 314 | void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC, |
| 315 | mmapFlags, fd, 0); |
| 316 | if (pa == MAP_FAILED) { |
| 317 | perror("mmap"); |
| 318 | abort(); |
| 319 | } |
| 320 | return pa; |
| 321 | #else |
| 322 | std::cerr << "Do not know how to allocate mem for the JIT without mmap!\n"; |
| 323 | abort(); |
| 324 | return 0; |
| 325 | #endif |
| 326 | } |
| 327 | |
| 328 | |