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 9 #include "llvm/Support/InitLLVM.h" 10 #include "llvm/ADT/StringRef.h" 11 #include "llvm/Support/AutoConvert.h" 12 #include "llvm/Support/Error.h" 13 #include "llvm/Support/ErrorHandling.h" 14 #include "llvm/Support/ManagedStatic.h" 15 #include "llvm/Support/Signals.h" 16 17 #ifdef _WIN32 18 #include "llvm/Support/Windows/WindowsSupport.h" 19 #endif 20 21 #ifdef __MVS__ 22 #include <unistd.h> 23 24 void CleanupStdHandles(void *Cookie) { 25 llvm::raw_ostream *Outs = &llvm::outs(), *Errs = &llvm::errs(); 26 Outs->flush(); 27 Errs->flush(); 28 llvm::restorezOSStdHandleAutoConversion(STDIN_FILENO); 29 llvm::restorezOSStdHandleAutoConversion(STDOUT_FILENO); 30 llvm::restorezOSStdHandleAutoConversion(STDERR_FILENO); 31 } 32 #endif 33 34 using namespace llvm; 35 using namespace llvm::sys; 36 37 InitLLVM::InitLLVM(int &Argc, const char **&Argv, 38 bool InstallPipeSignalExitHandler) { 39 #ifndef NDEBUG 40 static std::atomic<bool> Initialized{false}; 41 assert(!Initialized && "InitLLVM was already initialized!"); 42 Initialized = true; 43 #endif 44 #ifdef __MVS__ 45 // Bring stdin/stdout/stderr into a known state. 46 sys::AddSignalHandler(CleanupStdHandles, nullptr); 47 #endif 48 if (InstallPipeSignalExitHandler) 49 // The pipe signal handler must be installed before any other handlers are 50 // registered. This is because the Unix \ref RegisterHandlers function does 51 // not perform a sigaction() for SIGPIPE unless a one-shot handler is 52 // present, to allow long-lived processes (like lldb) to fully opt-out of 53 // llvm's SIGPIPE handling and ignore the signal safely. 54 sys::SetOneShotPipeSignalFunction(sys::DefaultOneShotPipeSignalHandler); 55 // Initialize the stack printer after installing the one-shot pipe signal 56 // handler, so we can perform a sigaction() for SIGPIPE on Unix if requested. 57 StackPrinter.emplace(Argc, Argv); 58 sys::PrintStackTraceOnErrorSignal(Argv[0]); 59 install_out_of_memory_new_handler(); 60 61 #ifdef __MVS__ 62 63 // We use UTF-8 as the internal character encoding. On z/OS, all external 64 // output is encoded in EBCDIC. In order to be able to read all 65 // error messages, we turn conversion to EBCDIC on for stderr fd. 66 std::string Banner = std::string(Argv[0]) + ": "; 67 ExitOnError ExitOnErr(Banner); 68 69 // If turning on conversion for stderr fails then the error message 70 // may be garbled. There is no solution to this problem. 71 ExitOnErr(errorCodeToError(llvm::enablezOSAutoConversion(STDERR_FILENO))); 72 ExitOnErr(errorCodeToError(llvm::enablezOSAutoConversion(STDOUT_FILENO))); 73 #endif 74 75 #ifdef _WIN32 76 // We use UTF-8 as the internal character encoding. On Windows, 77 // arguments passed to main() may not be encoded in UTF-8. In order 78 // to reliably detect encoding of command line arguments, we use an 79 // Windows API to obtain arguments, convert them to UTF-8, and then 80 // write them back to the Argv vector. 81 // 82 // There's probably other way to do the same thing (e.g. using 83 // wmain() instead of main()), but this way seems less intrusive 84 // than that. 85 std::string Banner = std::string(Argv[0]) + ": "; 86 ExitOnError ExitOnErr(Banner); 87 88 ExitOnErr(errorCodeToError(windows::GetCommandLineArguments(Args, Alloc))); 89 90 // GetCommandLineArguments doesn't terminate the vector with a 91 // nullptr. Do it to make it compatible with the real argv. 92 Args.push_back(nullptr); 93 94 Argc = Args.size() - 1; 95 Argv = Args.data(); 96 #endif 97 } 98 99 InitLLVM::~InitLLVM() { 100 #ifdef __MVS__ 101 CleanupStdHandles(nullptr); 102 #endif 103 llvm_shutdown(); 104 } 105