1 //===--- CrashRecoveryContext.h - Crash Recovery ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
11 #define LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
18 class CrashRecoveryContextCleanup;
20 /// \brief Crash recovery helper object.
22 /// This class implements support for running operations in a safe context so
23 /// that crashes (memory errors, stack overflow, assertion violations) can be
24 /// detected and control restored to the crashing thread. Crash detection is
25 /// purely "best effort", the exact set of failures which can be recovered from
26 /// is platform dependent.
28 /// Clients make use of this code by first calling
29 /// CrashRecoveryContext::Enable(), and then executing unsafe operations via a
30 /// CrashRecoveryContext object. For example:
32 /// void actual_work(void *);
35 /// CrashRecoveryContext CRC;
37 /// if (!CRC.RunSafely(actual_work, 0)) {
38 /// ... a crash was detected, report error to user ...
41 /// ... no crash was detected ...
44 /// Crash recovery contexts may not be nested.
45 class CrashRecoveryContext {
47 CrashRecoveryContextCleanup *head;
50 CrashRecoveryContext() : Impl(0), head(0) {}
51 ~CrashRecoveryContext();
53 void registerCleanup(CrashRecoveryContextCleanup *cleanup);
54 void unregisterCleanup(CrashRecoveryContextCleanup *cleanup);
56 /// \brief Enable crash recovery.
59 /// \brief Disable crash recovery.
60 static void Disable();
62 /// \brief Return the active context, if the code is currently executing in a
63 /// thread which is in a protected context.
64 static CrashRecoveryContext *GetCurrent();
66 /// \brief Return true if the current thread is recovering from a
68 static bool isRecoveringFromCrash();
70 /// \brief Execute the provide callback function (with the given arguments) in
71 /// a protected context.
73 /// \return True if the function completed successfully, and false if the
74 /// function crashed (or HandleCrash was called explicitly). Clients should
75 /// make as little assumptions as possible about the program state when
76 /// RunSafely has returned false. Clients can use getBacktrace() to retrieve
77 /// the backtrace of the crash on failures.
78 bool RunSafely(void (*Fn)(void*), void *UserData);
80 /// \brief Execute the provide callback function (with the given arguments) in
81 /// a protected context which is run in another thread (optionally with a
82 /// requested stack size).
84 /// See RunSafely() and llvm_execute_on_thread().
85 bool RunSafelyOnThread(void (*Fn)(void*), void *UserData,
86 unsigned RequestedStackSize = 0);
88 /// \brief Explicitly trigger a crash recovery in the current process, and
89 /// return failure from RunSafely(). This function does not return.
92 /// \brief Return a string containing the backtrace where the crash was
93 /// detected; or empty if the backtrace wasn't recovered.
95 /// This function is only valid when a crash has been detected (i.e.,
96 /// RunSafely() has returned false.
97 const std::string &getBacktrace() const;
100 class CrashRecoveryContextCleanup {
102 CrashRecoveryContext *context;
103 CrashRecoveryContextCleanup(CrashRecoveryContext *context)
104 : context(context) {}
108 CrashRecoveryContextCleanup() : cleanupFired(false) {}
109 virtual ~CrashRecoveryContextCleanup();
110 virtual void recoverResources() = 0;
112 CrashRecoveryContext *getContext() const {
117 friend class CrashRecoveryContext;
118 CrashRecoveryContextCleanup *prev, *next;
121 template<typename DERIVED, typename T>
122 class CrashRecoveryContextCleanupBase : public CrashRecoveryContextCleanup {
125 CrashRecoveryContextCleanupBase(CrashRecoveryContext *context, T* resource)
126 : CrashRecoveryContextCleanup(context), resource(resource) {}
128 static DERIVED *create(T *x) {
130 if (CrashRecoveryContext *context = CrashRecoveryContext::GetCurrent())
131 return new DERIVED(context, x);
137 template <typename T>
138 class CrashRecoveryContextDestructorCleanup : public
139 CrashRecoveryContextCleanupBase<CrashRecoveryContextDestructorCleanup<T>, T> {
141 CrashRecoveryContextDestructorCleanup(CrashRecoveryContext *context,
143 : CrashRecoveryContextCleanupBase<
144 CrashRecoveryContextDestructorCleanup<T>, T>(context, resource) {}
146 virtual void recoverResources() {
147 this->resource->~T();
151 template <typename T>
152 class CrashRecoveryContextDeleteCleanup : public
153 CrashRecoveryContextCleanupBase<CrashRecoveryContextDeleteCleanup<T>, T> {
155 CrashRecoveryContextDeleteCleanup(CrashRecoveryContext *context, T *resource)
156 : CrashRecoveryContextCleanupBase<
157 CrashRecoveryContextDeleteCleanup<T>, T>(context, resource) {}
159 virtual void recoverResources() {
160 delete this->resource;
164 template <typename T>
165 class CrashRecoveryContextReleaseRefCleanup : public
166 CrashRecoveryContextCleanupBase<CrashRecoveryContextReleaseRefCleanup<T>, T>
169 CrashRecoveryContextReleaseRefCleanup(CrashRecoveryContext *context,
171 : CrashRecoveryContextCleanupBase<CrashRecoveryContextReleaseRefCleanup<T>,
172 T>(context, resource) {}
174 virtual void recoverResources() {
175 this->resource->Release();
179 template <typename T, typename Cleanup = CrashRecoveryContextDeleteCleanup<T> >
180 class CrashRecoveryContextCleanupRegistrar {
181 CrashRecoveryContextCleanup *cleanup;
183 CrashRecoveryContextCleanupRegistrar(T *x)
184 : cleanup(Cleanup::create(x)) {
186 cleanup->getContext()->registerCleanup(cleanup);
189 ~CrashRecoveryContextCleanupRegistrar() {
190 if (cleanup && !cleanup->cleanupFired)
191 cleanup->getContext()->unregisterCleanup(cleanup);