clang-tools 19.0.0git
ThreadCrashReporter.cpp
Go to the documentation of this file.
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
10#include <atomic>
11
12namespace clang {
13namespace clangd {
14
15static thread_local ThreadCrashReporter *CurrentReporter = nullptr;
16
18 // No signal handling is done here on top of what AddSignalHandler() does:
19 // on Windows the signal handling is implmented via
20 // SetUnhandledExceptionFilter() which is thread-directed, and on Unix
21 // platforms the handlers are only called for KillSigs out of which only
22 // SIGQUIT seems to be process-directed and would be delivered to any thread
23 // that is not blocking it, but if the thread it gets delivered to has a
24 // ThreadCrashReporter installed during the interrupt — it seems reasonable to
25 // let it run and print the thread's context information.
26
27 // Call the reporters in LIFO order.
29 while (Reporter) {
30 Reporter->Callback();
31 Reporter = Reporter->Next;
32 }
33}
34
36 : Callback(std::move(ThreadLocalCallback)), Next(nullptr) {
37 this->Next = CurrentReporter;
38 CurrentReporter = this;
39 // Don't reorder subsequent operations: whatever comes after might crash and
40 // we want the crash handler to see the reporter values we just set.
41 std::atomic_signal_fence(std::memory_order_seq_cst);
42}
43
45 assert(CurrentReporter == this);
46 CurrentReporter = this->Next;
47 // Don't reorder subsequent operations: whatever comes after might crash and
48 // we want the crash handler to see the reporter values we just set.
49 std::atomic_signal_fence(std::memory_order_seq_cst);
50}
51
52} // namespace clangd
53} // namespace clang
Allows setting per-thread abort/kill signal callbacks, to print additional information about the cras...
static void runCrashHandlers()
Calls all currently-active ThreadCrashReporters for the current thread.
ThreadCrashReporter(SignalCallback ThreadLocalCallback)
Registers the callback as the first one in thread-local callback chain.
llvm::unique_function< void(void)> SignalCallback
~ThreadCrashReporter()
Resets the current thread's callback to nullptr.
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:28
static thread_local ThreadCrashReporter * CurrentReporter
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//