LLVM 23.0.0git
InitLLVM.cpp
Go to the documentation of this file.
1//===-- InitLLVM.cpp -----------------------------------------------------===//
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
10#include "llvm/ADT/StringRef.h"
12#include "llvm/Support/Error.h"
16
17#ifdef _WIN32
19#endif
20
21#if defined(HAVE_UNISTD_H)
22#include <unistd.h>
23#else
24#ifndef STDIN_FILENO
25#define STDIN_FILENO 0
26#endif
27#ifndef STDOUT_FILENO
28#define STDOUT_FILENO 1
29#endif
30#ifndef STDERR_FILENO
31#define STDERR_FILENO 2
32#endif
33#endif
34
35static void RaiseLimits() {
36#ifdef _AIX
37 // AIX has restrictive memory soft-limits out-of-box, so raise them if needed.
38 auto RaiseLimit = [](int resource) {
39 struct rlimit r;
40 getrlimit(resource, &r);
41
42 // Increase the soft limit to the hard limit, if necessary and
43 // possible.
44 if (r.rlim_cur != RLIM_INFINITY && r.rlim_cur != r.rlim_max) {
45 r.rlim_cur = r.rlim_max;
46 setrlimit(resource, &r);
47 }
48 };
49
50 // Address space size.
51 RaiseLimit(RLIMIT_AS);
52 // Heap size.
53 RaiseLimit(RLIMIT_DATA);
54 // Stack size.
55 RaiseLimit(RLIMIT_STACK);
56#ifdef RLIMIT_RSS
57 // Resident set size.
58 RaiseLimit(RLIMIT_RSS);
59#endif
60#endif
61}
62
63void CleanupStdHandles(void *Cookie) {
64 llvm::raw_ostream *Outs = &llvm::outs(), *Errs = &llvm::errs();
65 Outs->flush();
66 Errs->flush();
67 llvm::restoreStdHandleAutoConversion(STDIN_FILENO);
68 llvm::restoreStdHandleAutoConversion(STDOUT_FILENO);
69 llvm::restoreStdHandleAutoConversion(STDERR_FILENO);
70}
71
72using namespace llvm;
73using namespace llvm::sys;
74
75InitLLVM::InitLLVM(int &Argc, const char **&Argv,
76 bool InstallPipeSignalExitHandler,
77 bool NeedsPOSIXUtilitySignalHandling) {
78#ifndef NDEBUG
79 static std::atomic<bool> Initialized{false};
80 assert(!Initialized && "InitLLVM was already initialized!");
81 Initialized = true;
82#endif
83
84 // Bring stdin/stdout/stderr into a known state.
85#ifdef _WIN32
87#else
89 NeedsPOSIXUtilitySignalHandling);
90#endif
91
92 if (InstallPipeSignalExitHandler)
93 // The pipe signal handler must be installed before any other handlers are
94 // registered. This is because the Unix \ref RegisterHandlers function does
95 // not perform a sigaction() for SIGPIPE unless a one-shot handler is
96 // present, to allow long-lived processes (like lldb) to fully opt-out of
97 // llvm's SIGPIPE handling and ignore the signal safely.
99 // Initialize the stack printer after installing the one-shot pipe signal
100 // handler, so we can perform a sigaction() for SIGPIPE on Unix if requested.
101 StackPrinter.emplace(Argc, Argv);
104 RaiseLimits();
105
106#ifdef __MVS__
107
108 // We use UTF-8 as the internal character encoding. On z/OS, all external
109 // output is encoded in EBCDIC. In order to be able to read all
110 // error messages, we turn conversion to EBCDIC on for stderr fd.
111 std::string Banner = std::string(Argv[0]) + ": ";
112 ExitOnError ExitOnErr(Banner);
113
114 // If turning on conversion for stderr fails then the error message
115 // may be garbled. There is no solution to this problem.
116 ExitOnErr(errorCodeToError(llvm::enableAutoConversion(STDERR_FILENO)));
117 ExitOnErr(errorCodeToError(llvm::enableAutoConversion(STDOUT_FILENO)));
118#endif
119
120#ifdef _WIN32
121 // We use UTF-8 as the internal character encoding. On Windows,
122 // arguments passed to main() may not be encoded in UTF-8. In order
123 // to reliably detect encoding of command line arguments, we use an
124 // Windows API to obtain arguments, convert them to UTF-8, and then
125 // write them back to the Argv vector.
126 //
127 // There's probably other way to do the same thing (e.g. using
128 // wmain() instead of main()), but this way seems less intrusive
129 // than that.
130 std::string Banner = std::string(Argv[0]) + ": ";
131 ExitOnError ExitOnErr(Banner);
132
133 ExitOnErr(errorCodeToError(windows::GetCommandLineArguments(Args, Alloc)));
134
135 // GetCommandLineArguments doesn't terminate the vector with a
136 // nullptr. Do it to make it compatible with the real argv.
137 Args.push_back(nullptr);
138
139 Argc = Args.size() - 1;
140 Argv = Args.data();
141#endif
142}
143
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void RaiseLimits()
Definition InitLLVM.cpp:35
#define STDOUT_FILENO
Definition InitLLVM.cpp:28
#define STDERR_FILENO
Definition InitLLVM.cpp:31
void CleanupStdHandles(void *Cookie)
Definition InitLLVM.cpp:63
#define STDIN_FILENO
Definition InitLLVM.cpp:25
Helper for check-and-exit error handling.
Definition Error.h:1444
LLVM_ABI ~InitLLVM()
Definition InitLLVM.cpp:144
LLVM_ABI InitLLVM(int &Argc, const char **&Argv, bool InstallPipeSignalExitHandler=true, bool NeedsPOSIXUtilitySignalHandling=false)
Definition InitLLVM.cpp:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI std::error_code GetCommandLineArguments(SmallVectorImpl< const char * > &Args, BumpPtrAllocator &Alloc)
LLVM_ABI void DefaultOneShotPipeSignalHandler()
On Unix systems and Windows, this function exits with an "IO error" exit code.
LLVM_ABI void AddSignalHandler(SignalHandlerCallback FnPtr, void *Cookie, bool NeedsPOSIXUtilitySignalHandling=false)
Add a function to be called when an abort/kill signal is delivered to the process.
LLVM_ABI void SetOneShotPipeSignalFunction(void(*Handler)())
Registers a function to be called in a "one-shot" manner when a pipe signal is delivered to the proce...
LLVM_ABI void PrintStackTraceOnErrorSignal(StringRef Argv0, bool DisableCrashReporting=false)
When an error signal (such as SIGABRT or SIGSEGV) is delivered to the process, print a stack trace an...
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
LLVM_ABI void install_out_of_memory_new_handler()
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI void llvm_shutdown()
llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition Error.cpp:107