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