diff --git a/lldb/source/API/SystemInitializerFull.cpp b/lldb/source/API/SystemInitializerFull.cpp --- a/lldb/source/API/SystemInitializerFull.cpp +++ b/lldb/source/API/SystemInitializerFull.cpp @@ -18,6 +18,7 @@ #include "lldb/Utility/Reproducer.h" #include "lldb/Utility/Timer.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Signals.h" #include "llvm/Support/TargetSelect.h" #pragma clang diagnostic push @@ -53,6 +54,12 @@ if (error) return error; + // On Windows certain functions from Signals.h require that the global state + // is initialized. For LLDB driver the initialization happens in the `main` + // function via the `llvm::InitLLVM` helper, but for LLDB dynamic library + // (liblldb) that doesn't happen. + llvm::sys::PrintStackTraceOnErrorSignal(/*Argv0=*/{}); + // Initialize LLVM and Clang llvm::InitializeAllTargets(); llvm::InitializeAllAsmPrinters(); diff --git a/llvm/lib/Support/Unix/Signals.inc b/llvm/lib/Support/Unix/Signals.inc --- a/llvm/lib/Support/Unix/Signals.inc +++ b/llvm/lib/Support/Unix/Signals.inc @@ -637,6 +637,11 @@ /// process, print a stack trace and then exit. void llvm::sys::PrintStackTraceOnErrorSignal(StringRef Argv0, bool DisableCrashReporting) { + // If ::Argv0 is non-empty, then this function has already been called and + // there's no need to initialize the error handling again. + if (!::Argv0.empty()) + return; + ::Argv0 = Argv0; AddSignalHandler(PrintStackTraceSignalHandler, nullptr); diff --git a/llvm/lib/Support/Windows/Signals.inc b/llvm/lib/Support/Windows/Signals.inc --- a/llvm/lib/Support/Windows/Signals.inc +++ b/llvm/lib/Support/Windows/Signals.inc @@ -504,6 +504,11 @@ /// process, print a stack trace and then exit. void sys::PrintStackTraceOnErrorSignal(StringRef Argv0, bool DisableCrashReporting) { + // If ::Argv0 is non-empty, then this function has already been called and + // there's no need to initialize the error handling again. + if (!::Argv0.empty()) + return; + ::Argv0 = Argv0; if (DisableCrashReporting || getenv("LLVM_DISABLE_CRASH_REPORT"))