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,7 +637,10 @@ /// process, print a stack trace and then exit. void llvm::sys::PrintStackTraceOnErrorSignal(StringRef Argv0, bool DisableCrashReporting) { - ::Argv0 = Argv0; + // 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; 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,7 +504,10 @@ /// process, print a stack trace and then exit. void sys::PrintStackTraceOnErrorSignal(StringRef Argv0, bool DisableCrashReporting) { - ::Argv0 = Argv0; + // 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; if (DisableCrashReporting || getenv("LLVM_DISABLE_CRASH_REPORT")) Process::PreventCoreFiles();