1 //===--- ThreadCrashReporter.cpp - Thread local signal handling --*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "support/ThreadCrashReporter.h"
15 static thread_local ThreadCrashReporter
*CurrentReporter
= nullptr;
17 void ThreadCrashReporter::runCrashHandlers() {
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.
27 // Call the reporters in LIFO order.
28 ThreadCrashReporter
*Reporter
= CurrentReporter
;
31 Reporter
= Reporter
->Next
;
35 ThreadCrashReporter::ThreadCrashReporter(SignalCallback ThreadLocalCallback
)
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
);
44 ThreadCrashReporter::~ThreadCrashReporter() {
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
);