1 //===-- InitLLVM.cpp -----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/InitLLVM.h"
10 #include "llvm/Support/Error.h"
11 #include "llvm/Support/ManagedStatic.h"
12 #include "llvm/Support/PrettyStackTrace.h"
13 #include "llvm/Support/Process.h"
14 #include "llvm/Support/Signals.h"
18 #include "llvm/Support/Windows/WindowsSupport.h"
22 using namespace llvm::sys
;
24 InitLLVM::InitLLVM(int &Argc
, const char **&Argv
,
25 bool InstallPipeSignalExitHandler
) {
26 if (InstallPipeSignalExitHandler
)
27 // The pipe signal handler must be installed before any other handlers are
28 // registered. This is because the Unix \ref RegisterHandlers function does
29 // not perform a sigaction() for SIGPIPE unless a one-shot handler is
30 // present, to allow long-lived processes (like lldb) to fully opt-out of
31 // llvm's SIGPIPE handling and ignore the signal safely.
32 sys::SetOneShotPipeSignalFunction(sys::DefaultOneShotPipeSignalHandler
);
33 // Initialize the stack printer after installing the one-shot pipe signal
34 // handler, so we can perform a sigaction() for SIGPIPE on Unix if requested.
35 StackPrinter
.emplace(Argc
, Argv
);
36 sys::PrintStackTraceOnErrorSignal(Argv
[0]);
37 install_out_of_memory_new_handler();
40 // We use UTF-8 as the internal character encoding. On Windows,
41 // arguments passed to main() may not be encoded in UTF-8. In order
42 // to reliably detect encoding of command line arguments, we use an
43 // Windows API to obtain arguments, convert them to UTF-8, and then
44 // write them back to the Argv vector.
46 // There's probably other way to do the same thing (e.g. using
47 // wmain() instead of main()), but this way seems less intrusive
49 std::string Banner
= std::string(Argv
[0]) + ": ";
50 ExitOnError
ExitOnErr(Banner
);
52 ExitOnErr(errorCodeToError(windows::GetCommandLineArguments(Args
, Alloc
)));
54 // GetCommandLineArguments doesn't terminate the vector with a
55 // nullptr. Do it to make it compatible with the real argv.
56 Args
.push_back(nullptr);
58 Argc
= Args
.size() - 1;
63 InitLLVM::~InitLLVM() { llvm_shutdown(); }