xref: /aosp_15_r20/external/llvm/include/llvm/Support/Process.h (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- llvm/Support/Process.h -----------------------------------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker /// \file
10*9880d681SAndroid Build Coastguard Worker ///
11*9880d681SAndroid Build Coastguard Worker /// Provides a library for accessing information about this process and other
12*9880d681SAndroid Build Coastguard Worker /// processes on the operating system. Also provides means of spawning
13*9880d681SAndroid Build Coastguard Worker /// subprocess for commands. The design of this library is modeled after the
14*9880d681SAndroid Build Coastguard Worker /// proposed design of the Boost.Process library, and is design specifically to
15*9880d681SAndroid Build Coastguard Worker /// follow the style of standard libraries and potentially become a proposal
16*9880d681SAndroid Build Coastguard Worker /// for a standard library.
17*9880d681SAndroid Build Coastguard Worker ///
18*9880d681SAndroid Build Coastguard Worker /// This file declares the llvm::sys::Process class which contains a collection
19*9880d681SAndroid Build Coastguard Worker /// of legacy static interfaces for extracting various information about the
20*9880d681SAndroid Build Coastguard Worker /// current process. The goal is to migrate users of this API over to the new
21*9880d681SAndroid Build Coastguard Worker /// interfaces.
22*9880d681SAndroid Build Coastguard Worker ///
23*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
24*9880d681SAndroid Build Coastguard Worker 
25*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_SUPPORT_PROCESS_H
26*9880d681SAndroid Build Coastguard Worker #define LLVM_SUPPORT_PROCESS_H
27*9880d681SAndroid Build Coastguard Worker 
28*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Optional.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Config/llvm-config.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Allocator.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/DataTypes.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/TimeValue.h"
33*9880d681SAndroid Build Coastguard Worker #include <system_error>
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker namespace llvm {
36*9880d681SAndroid Build Coastguard Worker template <typename T> class ArrayRef;
37*9880d681SAndroid Build Coastguard Worker class StringRef;
38*9880d681SAndroid Build Coastguard Worker 
39*9880d681SAndroid Build Coastguard Worker namespace sys {
40*9880d681SAndroid Build Coastguard Worker 
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker /// \brief A collection of legacy interfaces for querying information about the
43*9880d681SAndroid Build Coastguard Worker /// current executing process.
44*9880d681SAndroid Build Coastguard Worker class Process {
45*9880d681SAndroid Build Coastguard Worker public:
46*9880d681SAndroid Build Coastguard Worker   static unsigned getPageSize();
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker   /// \brief Return process memory usage.
49*9880d681SAndroid Build Coastguard Worker   /// This static function will return the total amount of memory allocated
50*9880d681SAndroid Build Coastguard Worker   /// by the process. This only counts the memory allocated via the malloc,
51*9880d681SAndroid Build Coastguard Worker   /// calloc and realloc functions and includes any "free" holes in the
52*9880d681SAndroid Build Coastguard Worker   /// allocated space.
53*9880d681SAndroid Build Coastguard Worker   static size_t GetMallocUsage();
54*9880d681SAndroid Build Coastguard Worker 
55*9880d681SAndroid Build Coastguard Worker   /// This static function will set \p user_time to the amount of CPU time
56*9880d681SAndroid Build Coastguard Worker   /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
57*9880d681SAndroid Build Coastguard Worker   /// time spent in system (kernel) mode.  If the operating system does not
58*9880d681SAndroid Build Coastguard Worker   /// support collection of these metrics, a zero TimeValue will be for both
59*9880d681SAndroid Build Coastguard Worker   /// values.
60*9880d681SAndroid Build Coastguard Worker   /// \param elapsed Returns the TimeValue::now() giving current time
61*9880d681SAndroid Build Coastguard Worker   /// \param user_time Returns the current amount of user time for the process
62*9880d681SAndroid Build Coastguard Worker   /// \param sys_time Returns the current amount of system time for the process
63*9880d681SAndroid Build Coastguard Worker   static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
64*9880d681SAndroid Build Coastguard Worker                            TimeValue &sys_time);
65*9880d681SAndroid Build Coastguard Worker 
66*9880d681SAndroid Build Coastguard Worker   /// This function makes the necessary calls to the operating system to
67*9880d681SAndroid Build Coastguard Worker   /// prevent core files or any other kind of large memory dumps that can
68*9880d681SAndroid Build Coastguard Worker   /// occur when a program fails.
69*9880d681SAndroid Build Coastguard Worker   /// @brief Prevent core file generation.
70*9880d681SAndroid Build Coastguard Worker   static void PreventCoreFiles();
71*9880d681SAndroid Build Coastguard Worker 
72*9880d681SAndroid Build Coastguard Worker   /// \brief true if PreventCoreFiles has been called, false otherwise.
73*9880d681SAndroid Build Coastguard Worker   static bool AreCoreFilesPrevented();
74*9880d681SAndroid Build Coastguard Worker 
75*9880d681SAndroid Build Coastguard Worker   // This function returns the environment variable \arg name's value as a UTF-8
76*9880d681SAndroid Build Coastguard Worker   // string. \arg Name is assumed to be in UTF-8 encoding too.
77*9880d681SAndroid Build Coastguard Worker   static Optional<std::string> GetEnv(StringRef name);
78*9880d681SAndroid Build Coastguard Worker 
79*9880d681SAndroid Build Coastguard Worker   /// This function searches for an existing file in the list of directories
80*9880d681SAndroid Build Coastguard Worker   /// in a PATH like environment variable, and returns the first file found,
81*9880d681SAndroid Build Coastguard Worker   /// according to the order of the entries in the PATH like environment
82*9880d681SAndroid Build Coastguard Worker   /// variable.
83*9880d681SAndroid Build Coastguard Worker   static Optional<std::string> FindInEnvPath(const std::string& EnvName,
84*9880d681SAndroid Build Coastguard Worker                                              const std::string& FileName);
85*9880d681SAndroid Build Coastguard Worker 
86*9880d681SAndroid Build Coastguard Worker   /// This function returns a SmallVector containing the arguments passed from
87*9880d681SAndroid Build Coastguard Worker   /// the operating system to the program.  This function expects to be handed
88*9880d681SAndroid Build Coastguard Worker   /// the vector passed in from main.
89*9880d681SAndroid Build Coastguard Worker   static std::error_code
90*9880d681SAndroid Build Coastguard Worker   GetArgumentVector(SmallVectorImpl<const char *> &Args,
91*9880d681SAndroid Build Coastguard Worker                     ArrayRef<const char *> ArgsFromMain,
92*9880d681SAndroid Build Coastguard Worker                     SpecificBumpPtrAllocator<char> &ArgAllocator);
93*9880d681SAndroid Build Coastguard Worker 
94*9880d681SAndroid Build Coastguard Worker   // This functions ensures that the standard file descriptors (input, output,
95*9880d681SAndroid Build Coastguard Worker   // and error) are properly mapped to a file descriptor before we use any of
96*9880d681SAndroid Build Coastguard Worker   // them.  This should only be called by standalone programs, library
97*9880d681SAndroid Build Coastguard Worker   // components should not call this.
98*9880d681SAndroid Build Coastguard Worker   static std::error_code FixupStandardFileDescriptors();
99*9880d681SAndroid Build Coastguard Worker 
100*9880d681SAndroid Build Coastguard Worker   // This function safely closes a file descriptor.  It is not safe to retry
101*9880d681SAndroid Build Coastguard Worker   // close(2) when it returns with errno equivalent to EINTR; this is because
102*9880d681SAndroid Build Coastguard Worker   // *nixen cannot agree if the file descriptor is, in fact, closed when this
103*9880d681SAndroid Build Coastguard Worker   // occurs.
104*9880d681SAndroid Build Coastguard Worker   //
105*9880d681SAndroid Build Coastguard Worker   // N.B. Some operating systems, due to thread cancellation, cannot properly
106*9880d681SAndroid Build Coastguard Worker   // guarantee that it will or will not be closed one way or the other!
107*9880d681SAndroid Build Coastguard Worker   static std::error_code SafelyCloseFileDescriptor(int FD);
108*9880d681SAndroid Build Coastguard Worker 
109*9880d681SAndroid Build Coastguard Worker   /// This function determines if the standard input is connected directly
110*9880d681SAndroid Build Coastguard Worker   /// to a user's input (keyboard probably), rather than coming from a file
111*9880d681SAndroid Build Coastguard Worker   /// or pipe.
112*9880d681SAndroid Build Coastguard Worker   static bool StandardInIsUserInput();
113*9880d681SAndroid Build Coastguard Worker 
114*9880d681SAndroid Build Coastguard Worker   /// This function determines if the standard output is connected to a
115*9880d681SAndroid Build Coastguard Worker   /// "tty" or "console" window. That is, the output would be displayed to
116*9880d681SAndroid Build Coastguard Worker   /// the user rather than being put on a pipe or stored in a file.
117*9880d681SAndroid Build Coastguard Worker   static bool StandardOutIsDisplayed();
118*9880d681SAndroid Build Coastguard Worker 
119*9880d681SAndroid Build Coastguard Worker   /// This function determines if the standard error is connected to a
120*9880d681SAndroid Build Coastguard Worker   /// "tty" or "console" window. That is, the output would be displayed to
121*9880d681SAndroid Build Coastguard Worker   /// the user rather than being put on a pipe or stored in a file.
122*9880d681SAndroid Build Coastguard Worker   static bool StandardErrIsDisplayed();
123*9880d681SAndroid Build Coastguard Worker 
124*9880d681SAndroid Build Coastguard Worker   /// This function determines if the given file descriptor is connected to
125*9880d681SAndroid Build Coastguard Worker   /// a "tty" or "console" window. That is, the output would be displayed to
126*9880d681SAndroid Build Coastguard Worker   /// the user rather than being put on a pipe or stored in a file.
127*9880d681SAndroid Build Coastguard Worker   static bool FileDescriptorIsDisplayed(int fd);
128*9880d681SAndroid Build Coastguard Worker 
129*9880d681SAndroid Build Coastguard Worker   /// This function determines if the given file descriptor is displayd and
130*9880d681SAndroid Build Coastguard Worker   /// supports colors.
131*9880d681SAndroid Build Coastguard Worker   static bool FileDescriptorHasColors(int fd);
132*9880d681SAndroid Build Coastguard Worker 
133*9880d681SAndroid Build Coastguard Worker   /// This function determines the number of columns in the window
134*9880d681SAndroid Build Coastguard Worker   /// if standard output is connected to a "tty" or "console"
135*9880d681SAndroid Build Coastguard Worker   /// window. If standard output is not connected to a tty or
136*9880d681SAndroid Build Coastguard Worker   /// console, or if the number of columns cannot be determined,
137*9880d681SAndroid Build Coastguard Worker   /// this routine returns zero.
138*9880d681SAndroid Build Coastguard Worker   static unsigned StandardOutColumns();
139*9880d681SAndroid Build Coastguard Worker 
140*9880d681SAndroid Build Coastguard Worker   /// This function determines the number of columns in the window
141*9880d681SAndroid Build Coastguard Worker   /// if standard error is connected to a "tty" or "console"
142*9880d681SAndroid Build Coastguard Worker   /// window. If standard error is not connected to a tty or
143*9880d681SAndroid Build Coastguard Worker   /// console, or if the number of columns cannot be determined,
144*9880d681SAndroid Build Coastguard Worker   /// this routine returns zero.
145*9880d681SAndroid Build Coastguard Worker   static unsigned StandardErrColumns();
146*9880d681SAndroid Build Coastguard Worker 
147*9880d681SAndroid Build Coastguard Worker   /// This function determines whether the terminal connected to standard
148*9880d681SAndroid Build Coastguard Worker   /// output supports colors. If standard output is not connected to a
149*9880d681SAndroid Build Coastguard Worker   /// terminal, this function returns false.
150*9880d681SAndroid Build Coastguard Worker   static bool StandardOutHasColors();
151*9880d681SAndroid Build Coastguard Worker 
152*9880d681SAndroid Build Coastguard Worker   /// This function determines whether the terminal connected to standard
153*9880d681SAndroid Build Coastguard Worker   /// error supports colors. If standard error is not connected to a
154*9880d681SAndroid Build Coastguard Worker   /// terminal, this function returns false.
155*9880d681SAndroid Build Coastguard Worker   static bool StandardErrHasColors();
156*9880d681SAndroid Build Coastguard Worker 
157*9880d681SAndroid Build Coastguard Worker   /// Enables or disables whether ANSI escape sequences are used to output
158*9880d681SAndroid Build Coastguard Worker   /// colors. This only has an effect on Windows.
159*9880d681SAndroid Build Coastguard Worker   /// Note: Setting this option is not thread-safe and should only be done
160*9880d681SAndroid Build Coastguard Worker   /// during initialization.
161*9880d681SAndroid Build Coastguard Worker   static void UseANSIEscapeCodes(bool enable);
162*9880d681SAndroid Build Coastguard Worker 
163*9880d681SAndroid Build Coastguard Worker   /// Whether changing colors requires the output to be flushed.
164*9880d681SAndroid Build Coastguard Worker   /// This is needed on systems that don't support escape sequences for
165*9880d681SAndroid Build Coastguard Worker   /// changing colors.
166*9880d681SAndroid Build Coastguard Worker   static bool ColorNeedsFlush();
167*9880d681SAndroid Build Coastguard Worker 
168*9880d681SAndroid Build Coastguard Worker   /// This function returns the colorcode escape sequences.
169*9880d681SAndroid Build Coastguard Worker   /// If ColorNeedsFlush() is true then this function will change the colors
170*9880d681SAndroid Build Coastguard Worker   /// and return an empty escape sequence. In that case it is the
171*9880d681SAndroid Build Coastguard Worker   /// responsibility of the client to flush the output stream prior to
172*9880d681SAndroid Build Coastguard Worker   /// calling this function.
173*9880d681SAndroid Build Coastguard Worker   static const char *OutputColor(char c, bool bold, bool bg);
174*9880d681SAndroid Build Coastguard Worker 
175*9880d681SAndroid Build Coastguard Worker   /// Same as OutputColor, but only enables the bold attribute.
176*9880d681SAndroid Build Coastguard Worker   static const char *OutputBold(bool bg);
177*9880d681SAndroid Build Coastguard Worker 
178*9880d681SAndroid Build Coastguard Worker   /// This function returns the escape sequence to reverse forground and
179*9880d681SAndroid Build Coastguard Worker   /// background colors.
180*9880d681SAndroid Build Coastguard Worker   static const char *OutputReverse();
181*9880d681SAndroid Build Coastguard Worker 
182*9880d681SAndroid Build Coastguard Worker   /// Resets the terminals colors, or returns an escape sequence to do so.
183*9880d681SAndroid Build Coastguard Worker   static const char *ResetColor();
184*9880d681SAndroid Build Coastguard Worker 
185*9880d681SAndroid Build Coastguard Worker   /// Get the result of a process wide random number generator. The
186*9880d681SAndroid Build Coastguard Worker   /// generator will be automatically seeded in non-deterministic fashion.
187*9880d681SAndroid Build Coastguard Worker   static unsigned GetRandomNumber();
188*9880d681SAndroid Build Coastguard Worker };
189*9880d681SAndroid Build Coastguard Worker 
190*9880d681SAndroid Build Coastguard Worker }
191*9880d681SAndroid Build Coastguard Worker }
192*9880d681SAndroid Build Coastguard Worker 
193*9880d681SAndroid Build Coastguard Worker #endif
194