LLVM API Documentation

Process.cpp
Go to the documentation of this file.
00001 //===-- Process.cpp - Implement OS Process Concept --------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements the operating system Process concept.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/ADT/StringExtras.h"
00015 #include "llvm/Config/config.h"
00016 #include "llvm/Support/ErrorHandling.h"
00017 #include "llvm/Support/FileSystem.h"
00018 #include "llvm/Support/Process.h"
00019 #include "llvm/Support/Program.h"
00020 
00021 using namespace llvm;
00022 using namespace sys;
00023 
00024 //===----------------------------------------------------------------------===//
00025 //=== WARNING: Implementation here must contain only TRULY operating system
00026 //===          independent code.
00027 //===----------------------------------------------------------------------===//
00028 
00029 // Empty virtual destructor to anchor the vtable for the process class.
00030 process::~process() {}
00031 
00032 self_process *process::get_self() {
00033   // Use a function local static for thread safe initialization and allocate it
00034   // as a raw pointer to ensure it is never destroyed.
00035   static self_process *SP = new self_process();
00036 
00037   return SP;
00038 }
00039 
00040 // The destructor for the self_process subclass must never actually be
00041 // executed. There should be at most one instance of this class, and that
00042 // instance should live until the process terminates to avoid the potential for
00043 // racy accesses during shutdown.
00044 self_process::~self_process() {
00045   llvm_unreachable("This destructor must never be executed!");
00046 }
00047 
00048 /// \brief A helper function to compute the elapsed wall-time since the program
00049 /// started.
00050 ///
00051 /// Note that this routine actually computes the elapsed wall time since the
00052 /// first time it was called. However, we arrange to have it called during the
00053 /// startup of the process to get approximately correct results.
00054 static TimeValue getElapsedWallTime() {
00055   static TimeValue &StartTime = *new TimeValue(TimeValue::now());
00056   return TimeValue::now() - StartTime;
00057 }
00058 
00059 /// \brief A special global variable to ensure we call \c getElapsedWallTime
00060 /// during global initialization of the program.
00061 ///
00062 /// Note that this variable is never referenced elsewhere. Doing so could
00063 /// create race conditions during program startup or shutdown.
00064 static volatile TimeValue DummyTimeValue = getElapsedWallTime();
00065 
00066 // Implement this routine by using the static helpers above. They're already
00067 // portable.
00068 TimeValue self_process::get_wall_time() const {
00069   return getElapsedWallTime();
00070 }
00071 
00072 Optional<std::string> Process::FindInEnvPath(const std::string& EnvName,
00073                                              const std::string& FileName)
00074 {
00075   Optional<std::string> FoundPath;
00076   Optional<std::string> OptPath = Process::GetEnv(EnvName);
00077   if (!OptPath.hasValue())
00078     return FoundPath;
00079 
00080   const char EnvPathSeparatorStr[] = {EnvPathSeparator, '\0'};
00081   SmallVector<StringRef, 8> Dirs;
00082   SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr);
00083 
00084   for (const auto &Dir : Dirs) {
00085     if (Dir.empty())
00086       continue;
00087 
00088     SmallString<128> FilePath(Dir);
00089     path::append(FilePath, FileName);
00090     if (fs::exists(Twine(FilePath))) {
00091       FoundPath = FilePath.str();
00092       break;
00093     }
00094   }
00095 
00096   return FoundPath;
00097 }
00098 
00099 
00100 #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
00101 
00102 #define ALLCOLORS(FGBG,BOLD) {\
00103     COLOR(FGBG, "0", BOLD),\
00104     COLOR(FGBG, "1", BOLD),\
00105     COLOR(FGBG, "2", BOLD),\
00106     COLOR(FGBG, "3", BOLD),\
00107     COLOR(FGBG, "4", BOLD),\
00108     COLOR(FGBG, "5", BOLD),\
00109     COLOR(FGBG, "6", BOLD),\
00110     COLOR(FGBG, "7", BOLD)\
00111   }
00112 
00113 static const char colorcodes[2][2][8][10] = {
00114  { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
00115  { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
00116 };
00117 
00118 // Include the platform-specific parts of this class.
00119 #ifdef LLVM_ON_UNIX
00120 #include "Unix/Process.inc"
00121 #endif
00122 #ifdef LLVM_ON_WIN32
00123 #include "Windows/Process.inc"
00124 #endif