1 //===- SystemUtils.h - Utilities to do low-level system stuff --*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains functions used to do a variety of low-level, often
11 // system-specific, tasks.
13 //===----------------------------------------------------------------------===//
15 #define _POSIX_MAPPED_FILES
16 #include "Support/SystemUtils.h"
17 #include "Config/sys/types.h"
18 #include "Config/sys/stat.h"
19 #include "Config/fcntl.h"
20 #include "Config/sys/wait.h"
21 #include "Config/sys/mman.h"
22 #include "Config/unistd.h"
30 /// isExecutableFile - This function returns true if the filename specified
31 /// exists and is executable.
33 bool llvm::isExecutableFile(const std::string &ExeFileName) {
35 if (stat(ExeFileName.c_str(), &Buf))
36 return false; // Must not be executable!
38 if (!(Buf.st_mode & S_IFREG))
39 return false; // Not a regular file?
41 if (Buf.st_uid == getuid()) // Owner of file?
42 return Buf.st_mode & S_IXUSR;
43 else if (Buf.st_gid == getgid()) // In group of file?
44 return Buf.st_mode & S_IXGRP;
45 else // Unrelated to file?
46 return Buf.st_mode & S_IXOTH;
49 /// isStandardOutAConsole - Return true if we can tell that the standard output
50 /// stream goes to a terminal window or console.
51 bool llvm::isStandardOutAConsole() {
55 // If we don't have isatty, just return false.
60 /// FindExecutable - Find a named executable, giving the argv[0] of program
61 /// being executed. This allows us to find another LLVM tool if it is built
62 /// into the same directory, but that directory is neither the current
63 /// directory, nor in the PATH. If the executable cannot be found, return an
66 std::string llvm::FindExecutable(const std::string &ExeName,
67 const std::string &ProgramPath) {
68 // First check the directory that bugpoint is in. We can do this if
69 // BugPointPath contains at least one / character, indicating that it is a
70 // relative path to bugpoint itself.
72 std::string Result = ProgramPath;
73 while (!Result.empty() && Result[Result.size()-1] != '/')
74 Result.erase(Result.size()-1, 1);
76 if (!Result.empty()) {
78 if (isExecutableFile(Result)) return Result; // Found it?
81 // Okay, if the path to the program didn't tell us anything, try using the
82 // PATH environment variable.
83 const char *PathStr = getenv("PATH");
84 if (PathStr == 0) return "";
86 // Now we have a colon separated list of directories to search... try them...
87 unsigned PathLen = strlen(PathStr);
89 // Find the first colon...
90 const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
92 // Check to see if this first directory contains the executable...
93 std::string FilePath = std::string(PathStr, Colon) + '/' + ExeName;
94 if (isExecutableFile(FilePath))
95 return FilePath; // Found the executable!
97 // Nope it wasn't in this directory, check the next range!
98 PathLen -= Colon-PathStr;
100 while (*PathStr == ':') { // Advance past colons
106 // If we fell out, we ran out of directories in PATH to search, return failure
110 static void RedirectFD(const std::string &File, int FD) {
111 if (File.empty()) return; // Noop
114 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
116 std::cerr << "Error opening file '" << File << "' for "
117 << (FD == 0 ? "input" : "output") << "!\n";
121 dup2(InFD, FD); // Install it as the requested FD
122 close(InFD); // Close the original FD
125 /// RunProgramWithTimeout - This function executes the specified program, with
126 /// the specified null-terminated argument array, with the stdin/out/err fd's
127 /// redirected, with a timeout specified on the command line. This terminates
128 /// the calling program if there is an error executing the specified program.
129 /// It returns the return value of the program, or -1 if a timeout is detected.
131 int llvm::RunProgramWithTimeout(const std::string &ProgramPath,
133 const std::string &StdInFile,
134 const std::string &StdOutFile,
135 const std::string &StdErrFile) {
136 // FIXME: install sigalarm handler here for timeout...
138 #ifdef HAVE_SYS_WAIT_H
142 std::cerr << "ERROR forking!\n";
145 RedirectFD(StdInFile, 0); // Redirect file descriptors...
146 RedirectFD(StdOutFile, 1);
147 if (StdOutFile != StdErrFile)
148 RedirectFD(StdErrFile, 2);
152 execv(ProgramPath.c_str(), (char *const *)Args);
153 std::cerr << "Error executing program: '" << ProgramPath;
154 for (; *Args; ++Args)
155 std::cerr << " " << *Args;
162 // Make sure all output has been written while waiting
163 std::cout << std::flush;
166 if (wait(&Status) != Child) {
167 if (errno == EINTR) {
168 static bool FirstTimeout = true;
171 "*** Program execution timed out! This mechanism is designed to handle\n"
172 " programs stuck in infinite loops gracefully. The -timeout option\n"
173 " can be used to change the timeout threshold or disable it completely\n"
174 " (with -timeout=0). This message is only displayed once.\n";
175 FirstTimeout = false;
177 return -1; // Timeout detected
180 std::cerr << "Error waiting for child process!\n";
186 std::cerr << "RunProgramWithTimeout not implemented on this platform!\n";
193 // Function: ExecWait ()
196 // This function executes a program with the specified arguments and
197 // environment. It then waits for the progarm to termiante and then returns
201 // argv - The arguments to the program as an array of C strings. The first
202 // argument should be the name of the program to execute, and the
203 // last argument should be a pointer to NULL.
205 // envp - The environment passes to the program as an array of C strings in
206 // the form of "name=value" pairs. The last element should be a
214 // 1 - The program could not be executed.
215 // 1 - The program returned a non-zero exit status.
216 // 1 - The program terminated abnormally.
219 // The program will inherit the stdin, stdout, and stderr file descriptors
220 // as well as other various configuration settings (umask).
222 // This function should not print anything to stdout/stderr on its own. It is
223 // a generic library function. The caller or executed program should report
224 // errors in the way it sees fit.
226 // This function does not use $PATH to find programs.
228 int llvm::ExecWait(const char * const old_argv[],
229 const char * const old_envp[]) {
230 #ifdef HAVE_SYS_WAIT_H
232 // Create local versions of the parameters that can be passed into execve()
233 // without creating const problems.
235 char ** const argv = (char ** const) old_argv;
236 char ** const envp = (char ** const) old_envp;
238 // Create a child process.
240 // An error occured: Return to the caller.
245 // Child process: Execute the program.
247 execve (argv[0], argv, envp);
248 // If the execve() failed, we should exit and let the parent pick up
249 // our non-zero exit status.
252 // Parent process: Break out of the switch to do our processing.
257 // Parent process: Wait for the child process to termiante.
259 if ((wait (&status)) == -1)
262 // If the program exited normally with a zero exit status, return success!
263 if (WIFEXITED (status) && (WEXITSTATUS(status) == 0))
266 std::cerr << "llvm::ExecWait not implemented on this platform!\n";
269 // Otherwise, return failure.
273 /// AllocateRWXMemory - Allocate a slab of memory with read/write/execute
274 /// permissions. This is typically used for JIT applications where we want
275 /// to emit code to the memory then jump to it. Getting this type of memory
276 /// is very OS specific.
278 void *llvm::AllocateRWXMemory(unsigned NumBytes) {
279 if (NumBytes == 0) return 0;
280 static const long pageSize = sysconf(_SC_PAGESIZE);
281 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
283 /* FIXME: This should use the proper autoconf flags */
284 #if defined(i386) || defined(__i386__) || defined(__x86__)
285 /* Linux and *BSD tend to have these flags named differently. */
286 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
287 # define MAP_ANONYMOUS MAP_ANON
288 #endif /* defined(MAP_ANON) && !defined(MAP_ANONYMOUS) */
289 #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
292 std::cerr << "This architecture is not supported by the JIT!\n";
299 #if defined(__linux__)
303 unsigned mmapFlags = MAP_PRIVATE|MAP_ANONYMOUS;
305 mmapFlags |= MAP_NORESERVE;
308 void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
310 if (pa == MAP_FAILED) {
316 std::cerr << "Do not know how to allocate mem for the JIT without mmap!\n";