[PATCH] D119009: [Support] Ensure handlers are set up before printing the stacktrace
Andy Yankovsky via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 4 08:59:39 PST 2022
werat created this revision.
Herald added subscribers: dexonsmith, hiraditya.
werat requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
On Windows `lldbassert()`[1] will segfault[2] if `RegisterHandler()` from `lib/Support/Windows/Signals.inc` wasn't called earlier at some point (it loads `Dbghelp.dll` and initializes the function pointers [3]). The `RegisterHandler()` function is called from `RemoveFileOnSignal`, `DontRemoveFileOnSignal`, `SetInterruptFunction`, `AddSignalHandler` and `PrintStackTraceOnErrorSignal`.
For LLDB `PrintStackTraceOnErrorSignal()` is called from `InitLLVM` ctor [4], which is created in the LLDB driver [5]. However in case of using the SB API via `liblldb`, none of these functions is called and thus any `lldbassert()` will crash the process.
At first glance it seems `SBDebugger::Initialize()` can do the initialization, but `PrintStackTraceOnErrorSignal` requires `argv`, which is not available there at the moment (and I'm not sure whether this is desirable behavior for the _library_).
It's also possible to just call `RegisterHandler()` in `llvm::sys::PrintStackTrace()` to ensure the `Dbghelp.dll` is always loaded. This patch does that.
Please, let me know if this approach is OK or a different solution should be implemented.
[1] https://github.com/llvm/llvm-project/blob/0236c571810dea1b72d99ee0af124a7a09976e00/lldb/source/Utility/LLDBAssert.cpp#L45
[2] https://github.com/llvm/llvm-project/blob/0236c571810dea1b72d99ee0af124a7a09976e00/llvm/lib/Support/Windows/Signals.inc#L300
[3] https://github.com/llvm/llvm-project/blob/0236c571810dea1b72d99ee0af124a7a09976e00/llvm/lib/Support/Windows/Signals.inc#L163
[4] https://github.com/llvm/llvm-project/blob/0236c571810dea1b72d99ee0af124a7a09976e00/llvm/lib/Support/InitLLVM.cpp#L37
[5] https://github.com/llvm/llvm-project/blob/0236c571810dea1b72d99ee0af124a7a09976e00/lldb/tools/driver/Driver.cpp#L778
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D119009
Files:
llvm/lib/Support/Windows/Signals.inc
Index: llvm/lib/Support/Windows/Signals.inc
===================================================================
--- llvm/lib/Support/Windows/Signals.inc
+++ llvm/lib/Support/Windows/Signals.inc
@@ -554,7 +554,9 @@
void llvm::sys::PrintStackTrace(raw_ostream &OS, int Depth) {
// FIXME: Handle "Depth" parameter to print stack trace upto specified Depth
+ RegisterHandler();
LocalPrintStackTrace(OS, nullptr);
+ LeaveCriticalSection(&CriticalSection);
}
void llvm::sys::SetInterruptFunction(void (*IF)()) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119009.405988.patch
Type: text/x-patch
Size: 521 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220204/b590e367/attachment.bin>
More information about the llvm-commits
mailing list