1 //===-- Process.cpp - Implement OS Process Concept --------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the operating system Process concept. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Support/Process.h" 14 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/ADT/StringExtras.h" 16 #include "llvm/Config/config.h" 17 #include "llvm/Config/llvm-config.h" 18 #include "llvm/Support/CrashRecoveryContext.h" 19 #include "llvm/Support/FileSystem.h" 20 #include "llvm/Support/Path.h" 21 #include "llvm/Support/Program.h" 22 23 using namespace llvm; 24 using namespace sys; 25 26 //===----------------------------------------------------------------------===// 27 //=== WARNING: Implementation here must contain only TRULY operating system 28 //=== independent code. 29 //===----------------------------------------------------------------------===// 30 31 Optional<std::string> 32 Process::FindInEnvPath(StringRef EnvName, StringRef FileName, char Separator) { 33 return FindInEnvPath(EnvName, FileName, {}, Separator); 34 } 35 36 Optional<std::string> Process::FindInEnvPath(StringRef EnvName, 37 StringRef FileName, 38 ArrayRef<std::string> IgnoreList, 39 char Separator) { 40 assert(!path::is_absolute(FileName)); 41 Optional<std::string> FoundPath; 42 Optional<std::string> OptPath = Process::GetEnv(EnvName); 43 if (!OptPath.hasValue()) 44 return FoundPath; 45 46 const char EnvPathSeparatorStr[] = {Separator, '\0'}; 47 SmallVector<StringRef, 8> Dirs; 48 SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr); 49 50 for (StringRef Dir : Dirs) { 51 if (Dir.empty()) 52 continue; 53 54 if (any_of(IgnoreList, [&](StringRef S) { return fs::equivalent(S, Dir); })) 55 continue; 56 57 SmallString<128> FilePath(Dir); 58 path::append(FilePath, FileName); 59 if (fs::exists(Twine(FilePath))) { 60 FoundPath = std::string(FilePath.str()); 61 break; 62 } 63 } 64 65 return FoundPath; 66 } 67 68 69 #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m" 70 71 #define ALLCOLORS(FGBG,BOLD) {\ 72 COLOR(FGBG, "0", BOLD),\ 73 COLOR(FGBG, "1", BOLD),\ 74 COLOR(FGBG, "2", BOLD),\ 75 COLOR(FGBG, "3", BOLD),\ 76 COLOR(FGBG, "4", BOLD),\ 77 COLOR(FGBG, "5", BOLD),\ 78 COLOR(FGBG, "6", BOLD),\ 79 COLOR(FGBG, "7", BOLD)\ 80 } 81 82 static const char colorcodes[2][2][8][10] = { 83 { ALLCOLORS("3",""), ALLCOLORS("3","1;") }, 84 { ALLCOLORS("4",""), ALLCOLORS("4","1;") } 85 }; 86 87 // A CMake option controls wheter we emit core dumps by default. An application 88 // may disable core dumps by calling Process::PreventCoreFiles(). 89 static bool coreFilesPrevented = !LLVM_ENABLE_CRASH_DUMPS; 90 91 bool Process::AreCoreFilesPrevented() { return coreFilesPrevented; } 92 93 LLVM_ATTRIBUTE_NORETURN 94 void Process::Exit(int RetCode) { 95 if (CrashRecoveryContext *CRC = CrashRecoveryContext::GetCurrent()) 96 CRC->HandleExit(RetCode); 97 ::exit(RetCode); 98 } 99 100 // Include the platform-specific parts of this class. 101 #ifdef LLVM_ON_UNIX 102 #include "Unix/Process.inc" 103 #endif 104 #ifdef _WIN32 105 #include "Windows/Process.inc" 106 #endif 107