Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:33

0001 //===- llvm/Support/Process.h -----------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 ///
0010 /// Provides a library for accessing information about this process and other
0011 /// processes on the operating system. Also provides means of spawning
0012 /// subprocess for commands. The design of this library is modeled after the
0013 /// proposed design of the Boost.Process library, and is design specifically to
0014 /// follow the style of standard libraries and potentially become a proposal
0015 /// for a standard library.
0016 ///
0017 /// This file declares the llvm::sys::Process class which contains a collection
0018 /// of legacy static interfaces for extracting various information about the
0019 /// current process. The goal is to migrate users of this API over to the new
0020 /// interfaces.
0021 ///
0022 //===----------------------------------------------------------------------===//
0023 
0024 #ifndef LLVM_SUPPORT_PROCESS_H
0025 #define LLVM_SUPPORT_PROCESS_H
0026 
0027 #include "llvm/Support/Chrono.h"
0028 #include "llvm/Support/DataTypes.h"
0029 #include "llvm/Support/Error.h"
0030 #include "llvm/Support/Program.h"
0031 #include <optional>
0032 #include <system_error>
0033 
0034 namespace llvm {
0035 template <typename T> class ArrayRef;
0036 class StringRef;
0037 
0038 namespace sys {
0039 
0040 
0041 /// A collection of legacy interfaces for querying information about the
0042 /// current executing process.
0043 class Process {
0044 public:
0045   using Pid = int32_t;
0046 
0047   /// Get the process's identifier.
0048   static Pid getProcessId();
0049 
0050   /// Get the process's page size.
0051   /// This may fail if the underlying syscall returns an error. In most cases,
0052   /// page size information is used for optimization, and this error can be
0053   /// safely discarded by calling consumeError, and an estimated page size
0054   /// substituted instead.
0055   static Expected<unsigned> getPageSize();
0056 
0057   /// Get the process's estimated page size.
0058   /// This function always succeeds, but if the underlying syscall to determine
0059   /// the page size fails then this will silently return an estimated page size.
0060   /// The estimated page size is guaranteed to be a power of 2.
0061   static unsigned getPageSizeEstimate() {
0062     if (auto PageSize = getPageSize())
0063       return *PageSize;
0064     else {
0065       consumeError(PageSize.takeError());
0066       return 4096;
0067     }
0068   }
0069 
0070   /// Return process memory usage.
0071   /// This static function will return the total amount of memory allocated
0072   /// by the process. This only counts the memory allocated via the malloc,
0073   /// calloc and realloc functions and includes any "free" holes in the
0074   /// allocated space.
0075   static size_t GetMallocUsage();
0076 
0077   /// This static function will set \p user_time to the amount of CPU time
0078   /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
0079   /// time spent in system (kernel) mode.  If the operating system does not
0080   /// support collection of these metrics, a zero duration will be for both
0081   /// values.
0082   /// \param elapsed Returns the system_clock::now() giving current time
0083   /// \param user_time Returns the current amount of user time for the process
0084   /// \param sys_time Returns the current amount of system time for the process
0085   static void GetTimeUsage(TimePoint<> &elapsed,
0086                            std::chrono::nanoseconds &user_time,
0087                            std::chrono::nanoseconds &sys_time);
0088 
0089   /// This function makes the necessary calls to the operating system to
0090   /// prevent core files or any other kind of large memory dumps that can
0091   /// occur when a program fails.
0092   /// Prevent core file generation.
0093   static void PreventCoreFiles();
0094 
0095   /// true if PreventCoreFiles has been called, false otherwise.
0096   static bool AreCoreFilesPrevented();
0097 
0098   // This function returns the environment variable \arg name's value as a UTF-8
0099   // string. \arg Name is assumed to be in UTF-8 encoding too.
0100   static std::optional<std::string> GetEnv(StringRef name);
0101 
0102   /// This function searches for an existing file in the list of directories
0103   /// in a PATH like environment variable, and returns the first file found,
0104   /// according to the order of the entries in the PATH like environment
0105   /// variable.  If an ignore list is specified, then any folder which is in
0106   /// the PATH like environment variable but is also in IgnoreList is not
0107   /// considered.
0108   static std::optional<std::string>
0109   FindInEnvPath(StringRef EnvName, StringRef FileName,
0110                 ArrayRef<std::string> IgnoreList,
0111                 char Separator = EnvPathSeparator);
0112 
0113   static std::optional<std::string>
0114   FindInEnvPath(StringRef EnvName, StringRef FileName,
0115                 char Separator = EnvPathSeparator);
0116 
0117   // This functions ensures that the standard file descriptors (input, output,
0118   // and error) are properly mapped to a file descriptor before we use any of
0119   // them.  This should only be called by standalone programs, library
0120   // components should not call this.
0121   static std::error_code FixupStandardFileDescriptors();
0122 
0123   // This function safely closes a file descriptor.  It is not safe to retry
0124   // close(2) when it returns with errno equivalent to EINTR; this is because
0125   // *nixen cannot agree if the file descriptor is, in fact, closed when this
0126   // occurs.
0127   //
0128   // N.B. Some operating systems, due to thread cancellation, cannot properly
0129   // guarantee that it will or will not be closed one way or the other!
0130   static std::error_code SafelyCloseFileDescriptor(int FD);
0131 
0132   /// This function determines if the standard input is connected directly
0133   /// to a user's input (keyboard probably), rather than coming from a file
0134   /// or pipe.
0135   static bool StandardInIsUserInput();
0136 
0137   /// This function determines if the standard output is connected to a
0138   /// "tty" or "console" window. That is, the output would be displayed to
0139   /// the user rather than being put on a pipe or stored in a file.
0140   static bool StandardOutIsDisplayed();
0141 
0142   /// This function determines if the standard error is connected to a
0143   /// "tty" or "console" window. That is, the output would be displayed to
0144   /// the user rather than being put on a pipe or stored in a file.
0145   static bool StandardErrIsDisplayed();
0146 
0147   /// This function determines if the given file descriptor is connected to
0148   /// a "tty" or "console" window. That is, the output would be displayed to
0149   /// the user rather than being put on a pipe or stored in a file.
0150   static bool FileDescriptorIsDisplayed(int fd);
0151 
0152   /// This function determines if the given file descriptor is displayd and
0153   /// supports colors.
0154   static bool FileDescriptorHasColors(int fd);
0155 
0156   /// This function determines the number of columns in the window
0157   /// if standard output is connected to a "tty" or "console"
0158   /// window. If standard output is not connected to a tty or
0159   /// console, or if the number of columns cannot be determined,
0160   /// this routine returns zero.
0161   static unsigned StandardOutColumns();
0162 
0163   /// This function determines the number of columns in the window
0164   /// if standard error is connected to a "tty" or "console"
0165   /// window. If standard error is not connected to a tty or
0166   /// console, or if the number of columns cannot be determined,
0167   /// this routine returns zero.
0168   static unsigned StandardErrColumns();
0169 
0170   /// This function determines whether the terminal connected to standard
0171   /// output supports colors. If standard output is not connected to a
0172   /// terminal, this function returns false.
0173   static bool StandardOutHasColors();
0174 
0175   /// This function determines whether the terminal connected to standard
0176   /// error supports colors. If standard error is not connected to a
0177   /// terminal, this function returns false.
0178   static bool StandardErrHasColors();
0179 
0180   /// Enables or disables whether ANSI escape sequences are used to output
0181   /// colors. This only has an effect on Windows.
0182   /// Note: Setting this option is not thread-safe and should only be done
0183   /// during initialization.
0184   static void UseANSIEscapeCodes(bool enable);
0185 
0186   /// Whether changing colors requires the output to be flushed.
0187   /// This is needed on systems that don't support escape sequences for
0188   /// changing colors.
0189   static bool ColorNeedsFlush();
0190 
0191   /// This function returns the colorcode escape sequences.
0192   /// If ColorNeedsFlush() is true then this function will change the colors
0193   /// and return an empty escape sequence. In that case it is the
0194   /// responsibility of the client to flush the output stream prior to
0195   /// calling this function.
0196   static const char *OutputColor(char c, bool bold, bool bg);
0197 
0198   /// Same as OutputColor, but only enables the bold attribute.
0199   static const char *OutputBold(bool bg);
0200 
0201   /// This function returns the escape sequence to reverse forground and
0202   /// background colors.
0203   static const char *OutputReverse();
0204 
0205   /// Resets the terminals colors, or returns an escape sequence to do so.
0206   static const char *ResetColor();
0207 
0208   /// Get the result of a process wide random number generator. The
0209   /// generator will be automatically seeded in non-deterministic fashion.
0210   static unsigned GetRandomNumber();
0211 
0212   /// Equivalent to ::exit(), except when running inside a CrashRecoveryContext.
0213   /// In that case, the control flow will resume after RunSafely(), like for a
0214   /// crash, rather than exiting the current process.
0215   /// Use \arg NoCleanup for calling _exit() instead of exit().
0216   [[noreturn]] static void Exit(int RetCode, bool NoCleanup = false);
0217 
0218 private:
0219   [[noreturn]] static void ExitNoCleanup(int RetCode);
0220 };
0221 
0222 }
0223 }
0224 
0225 #endif