1 //===--- ThreadCrashReporter.cpp - Thread local signal handling --*- C++-*-===// 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 "support/ThreadCrashReporter.h" 10 #include "llvm/Support/Signals.h" 11 #include "llvm/Support/ThreadLocal.h" 12 #include <atomic> 13 14 namespace clang { 15 namespace clangd { 16 17 static thread_local ThreadCrashReporter *CurrentReporter = nullptr; 18 19 void ThreadCrashReporter::runCrashHandlers() { 20 // No signal handling is done here on top of what AddSignalHandler() does: 21 // on Windows the signal handling is implmented via 22 // SetUnhandledExceptionFilter() which is thread-directed, and on Unix 23 // platforms the handlers are only called for KillSigs out of which only 24 // SIGQUIT seems to be process-directed and would be delivered to any thread 25 // that is not blocking it, but if the thread it gets delivered to has a 26 // ThreadCrashReporter installed during the interrupt — it seems reasonable to 27 // let it run and print the thread's context information. 28 29 // Call the reporters in LIFO order. 30 ThreadCrashReporter *Reporter = CurrentReporter; 31 while (Reporter) { 32 Reporter->Callback(); 33 Reporter = Reporter->Next; 34 } 35 } 36 37 ThreadCrashReporter::ThreadCrashReporter(SignalCallback ThreadLocalCallback) 38 : Callback(std::move(ThreadLocalCallback)), Next(nullptr) { 39 this->Next = CurrentReporter; 40 CurrentReporter = this; 41 // Don't reorder subsequent operations: whatever comes after might crash and 42 // we want the the crash handler to see the reporter values we just set. 43 std::atomic_signal_fence(std::memory_order_seq_cst); 44 } 45 46 ThreadCrashReporter::~ThreadCrashReporter() { 47 assert(CurrentReporter == this); 48 CurrentReporter = this->Next; 49 // Don't reorder subsequent operations: whatever comes after might crash and 50 // we want the the crash handler to see the reporter values we just set. 51 std::atomic_signal_fence(std::memory_order_seq_cst); 52 } 53 54 } // namespace clangd 55 } // namespace clang 56