LLVM 20.0.0git
Process.cpp
Go to the documentation of this file.
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
14#include "llvm/ADT/STLExtras.h"
16#include "llvm/Config/config.h"
17#include "llvm/Config/llvm-config.h"
20#include "llvm/Support/Path.h"
22
23#include <optional>
24#include <stdlib.h> // for _Exit
25
26using namespace llvm;
27using namespace sys;
28
29//===----------------------------------------------------------------------===//
30//=== WARNING: Implementation here must contain only TRULY operating system
31//=== independent code.
32//===----------------------------------------------------------------------===//
33
34std::optional<std::string>
35Process::FindInEnvPath(StringRef EnvName, StringRef FileName, char Separator) {
36 return FindInEnvPath(EnvName, FileName, {}, Separator);
37}
38
39std::optional<std::string>
41 ArrayRef<std::string> IgnoreList, char Separator) {
42 assert(!path::is_absolute(FileName));
43 std::optional<std::string> FoundPath;
44 std::optional<std::string> OptPath = Process::GetEnv(EnvName);
45 if (!OptPath)
46 return FoundPath;
47
48 const char EnvPathSeparatorStr[] = {Separator, '\0'};
50 SplitString(*OptPath, Dirs, EnvPathSeparatorStr);
51
52 for (StringRef Dir : Dirs) {
53 if (Dir.empty())
54 continue;
55
56 if (any_of(IgnoreList, [&](StringRef S) { return fs::equivalent(S, Dir); }))
57 continue;
58
59 SmallString<128> FilePath(Dir);
60 path::append(FilePath, FileName);
61 if (fs::exists(Twine(FilePath))) {
62 FoundPath = std::string(FilePath);
63 break;
64 }
65 }
66
67 return FoundPath;
68}
69
70// clang-format off
71#define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
72
73#define ALLCOLORS(FGBG, BRIGHT, BOLD) \
74 { \
75 COLOR(FGBG, "0", BOLD), \
76 COLOR(FGBG, "1", BOLD), \
77 COLOR(FGBG, "2", BOLD), \
78 COLOR(FGBG, "3", BOLD), \
79 COLOR(FGBG, "4", BOLD), \
80 COLOR(FGBG, "5", BOLD), \
81 COLOR(FGBG, "6", BOLD), \
82 COLOR(FGBG, "7", BOLD), \
83 COLOR(BRIGHT, "0", BOLD), \
84 COLOR(BRIGHT, "1", BOLD), \
85 COLOR(BRIGHT, "2", BOLD), \
86 COLOR(BRIGHT, "3", BOLD), \
87 COLOR(BRIGHT, "4", BOLD), \
88 COLOR(BRIGHT, "5", BOLD), \
89 COLOR(BRIGHT, "6", BOLD), \
90 COLOR(BRIGHT, "7", BOLD), \
91 }
92
93// bg
94// | bold
95// | |
96// | | codes
97// | | |
98// | | |
99static const char colorcodes[2][2][16][11] = {
100 { ALLCOLORS("3", "9", ""), ALLCOLORS("3", "9", "1;"),},
101 { ALLCOLORS("4", "10", ""), ALLCOLORS("4", "10", "1;")}
102};
103// clang-format on
104
105// A CMake option controls wheter we emit core dumps by default. An application
106// may disable core dumps by calling Process::PreventCoreFiles().
107static bool coreFilesPrevented = !LLVM_ENABLE_CRASH_DUMPS;
108
110
111[[noreturn]] void Process::Exit(int RetCode, bool NoCleanup) {
113 CRC->HandleExit(RetCode);
114
115 if (NoCleanup)
116 ExitNoCleanup(RetCode);
117 else
118 ::exit(RetCode);
119}
120
121// Include the platform-specific parts of this class.
122#ifdef LLVM_ON_UNIX
123#include "Unix/Process.inc"
124#endif
125#ifdef _WIN32
126#include "Windows/Process.inc"
127#endif
static bool coreFilesPrevented
Definition: Process.cpp:107
#define ALLCOLORS(FGBG, BRIGHT, BOLD)
Definition: Process.cpp:73
static const char colorcodes[2][2][16][11]
Definition: Process.cpp:99
Provides a library for accessing information about this process and other processes on the operating ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file contains some functions that are useful when dealing with strings.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Crash recovery helper object.
static CrashRecoveryContext * GetCurrent()
Return the active context, if the code is currently executing in a thread which is in a protected con...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static std::optional< std::string > FindInEnvPath(StringRef EnvName, StringRef FileName, ArrayRef< std::string > IgnoreList, char Separator=EnvPathSeparator)
This function searches for an existing file in the list of directories in a PATH like environment var...
Definition: Process.cpp:40
static void Exit(int RetCode, bool NoCleanup=false)
Equivalent to ::exit(), except when running inside a CrashRecoveryContext.
Definition: Process.cpp:111
static std::optional< std::string > GetEnv(StringRef name)
static bool AreCoreFilesPrevented()
true if PreventCoreFiles has been called, false otherwise.
Definition: Process.cpp:109
bool equivalent(file_status A, file_status B)
Do file_status's represent the same thing?
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1078
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:672
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:457
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1729