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
22#include <optional>
23#include <stdlib.h> // for _Exit
24
25using namespace llvm;
26using namespace sys;
27
28//===----------------------------------------------------------------------===//
29//=== WARNING: Implementation here must contain only TRULY operating system
30//=== independent code.
31//===----------------------------------------------------------------------===//
32
33std::optional<std::string>
34Process::FindInEnvPath(StringRef EnvName, StringRef FileName, char Separator) {
35 return FindInEnvPath(EnvName, FileName, IgnoreList: {}, Separator);
36}
37
38std::optional<std::string>
39Process::FindInEnvPath(StringRef EnvName, StringRef FileName,
40 ArrayRef<std::string> IgnoreList, char Separator) {
41 assert(!path::is_absolute(FileName));
42 std::optional<std::string> FoundPath;
43 std::optional<std::string> OptPath = Process::GetEnv(name: EnvName);
44 if (!OptPath)
45 return FoundPath;
46
47 const char EnvPathSeparatorStr[] = {Separator, '\0'};
48 SmallVector<StringRef, 8> Dirs;
49 SplitString(Source: *OptPath, OutFragments&: Dirs, Delimiters: EnvPathSeparatorStr);
50
51 for (StringRef Dir : Dirs) {
52 if (Dir.empty())
53 continue;
54
55 if (any_of(Range&: IgnoreList, P: [&](StringRef S) { return fs::equivalent(A: S, B: Dir); }))
56 continue;
57
58 SmallString<128> FilePath(Dir);
59 path::append(path&: FilePath, a: FileName);
60 if (fs::exists(Path: Twine(FilePath))) {
61 FoundPath = std::string(FilePath);
62 break;
63 }
64 }
65
66 return FoundPath;
67}
68
69// clang-format off
70#define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
71
72#define ALLCOLORS(FGBG, BRIGHT, BOLD) \
73 { \
74 COLOR(FGBG, "0", BOLD), \
75 COLOR(FGBG, "1", BOLD), \
76 COLOR(FGBG, "2", BOLD), \
77 COLOR(FGBG, "3", BOLD), \
78 COLOR(FGBG, "4", BOLD), \
79 COLOR(FGBG, "5", BOLD), \
80 COLOR(FGBG, "6", BOLD), \
81 COLOR(FGBG, "7", BOLD), \
82 COLOR(BRIGHT, "0", BOLD), \
83 COLOR(BRIGHT, "1", BOLD), \
84 COLOR(BRIGHT, "2", BOLD), \
85 COLOR(BRIGHT, "3", BOLD), \
86 COLOR(BRIGHT, "4", BOLD), \
87 COLOR(BRIGHT, "5", BOLD), \
88 COLOR(BRIGHT, "6", BOLD), \
89 COLOR(BRIGHT, "7", BOLD), \
90 }
91
92// bg
93// | bold
94// | |
95// | | codes
96// | | |
97// | | |
98static const char colorcodes[2][2][16][11] = {
99 { ALLCOLORS("3", "9", ""), ALLCOLORS("3", "9", "1;"),},
100 { ALLCOLORS("4", "10", ""), ALLCOLORS("4", "10", "1;")}
101};
102// clang-format on
103
104// A CMake option controls wheter we emit core dumps by default. An application
105// may disable core dumps by calling Process::PreventCoreFiles().
106static bool coreFilesPrevented = !LLVM_ENABLE_CRASH_DUMPS;
107
108bool Process::AreCoreFilesPrevented() { return coreFilesPrevented; }
109
110[[noreturn]] void Process::Exit(int RetCode, bool NoCleanup) {
111 if (CrashRecoveryContext *CRC = CrashRecoveryContext::GetCurrent())
112 CRC->HandleExit(RetCode);
113
114 if (NoCleanup)
115 ExitNoCleanup(RetCode);
116 else
117 ::exit(status: RetCode);
118}
119
120// Include the platform-specific parts of this class.
121#ifdef LLVM_ON_UNIX
122#include "Unix/Process.inc"
123#endif
124#ifdef _WIN32
125#include "Windows/Process.inc"
126#endif
127