Tweak CrashRecoveryContextCleanup to provide an easy method for clients to select...
[oota-llvm.git] / include / llvm / Support / CrashRecoveryContext.h
1 //===--- CrashRecoveryContext.h - Crash Recovery ----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
11 #define LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
12
13 #include <string>
14
15 namespace llvm {
16 class StringRef;
17
18 class CrashRecoveryContextCleanup;
19   
20 /// \brief Crash recovery helper object.
21 ///
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.
27 ///
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:
31 ///
32 ///    void actual_work(void *);
33 ///
34 ///    void foo() {
35 ///      CrashRecoveryContext CRC;
36 ///
37 ///      if (!CRC.RunSafely(actual_work, 0)) {
38 ///         ... a crash was detected, report error to user ...
39 ///      }
40 ///
41 ///      ... no crash was detected ...
42 ///    }
43 ///
44 /// Crash recovery contexts may not be nested.
45 class CrashRecoveryContext {
46   void *Impl;
47   CrashRecoveryContextCleanup *head;
48
49 public:
50   CrashRecoveryContext() : Impl(0), head(0) {}
51   ~CrashRecoveryContext();
52   
53   void registerCleanup(CrashRecoveryContextCleanup *cleanup);
54   void unregisterCleanup(CrashRecoveryContextCleanup *cleanup);
55
56   /// \brief Enable crash recovery.
57   static void Enable();
58
59   /// \brief Disable crash recovery.
60   static void Disable();
61
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();
65
66   /// \brief Execute the provide callback function (with the given arguments) in
67   /// a protected context.
68   ///
69   /// \return True if the function completed successfully, and false if the
70   /// function crashed (or HandleCrash was called explicitly). Clients should
71   /// make as little assumptions as possible about the program state when
72   /// RunSafely has returned false. Clients can use getBacktrace() to retrieve
73   /// the backtrace of the crash on failures.
74   bool RunSafely(void (*Fn)(void*), void *UserData);
75
76   /// \brief Execute the provide callback function (with the given arguments) in
77   /// a protected context which is run in another thread (optionally with a
78   /// requested stack size).
79   ///
80   /// See RunSafely() and llvm_execute_on_thread().
81   bool RunSafelyOnThread(void (*Fn)(void*), void *UserData,
82                          unsigned RequestedStackSize = 0);
83
84   /// \brief Explicitly trigger a crash recovery in the current process, and
85   /// return failure from RunSafely(). This function does not return.
86   void HandleCrash();
87
88   /// \brief Return a string containing the backtrace where the crash was
89   /// detected; or empty if the backtrace wasn't recovered.
90   ///
91   /// This function is only valid when a crash has been detected (i.e.,
92   /// RunSafely() has returned false.
93   const std::string &getBacktrace() const;
94 };
95
96 class CrashRecoveryContextCleanup {
97 public:
98   bool cleanupFired;
99   enum ProvidedCleanups { DeleteCleanup, DestructorCleanup };
100   
101   CrashRecoveryContextCleanup() : cleanupFired(false) {}
102   virtual ~CrashRecoveryContextCleanup();
103   virtual void recoverResources() = 0;
104   
105   template <typename T> static CrashRecoveryContextCleanup *create(T *,
106                           ProvidedCleanups cleanupKind =
107                             CrashRecoveryContextCleanup::DeleteCleanup);
108   
109 private:
110   friend class CrashRecoveryContext;
111   CrashRecoveryContextCleanup *prev, *next;
112 };
113
114 template <typename T>
115 class CrashRecoveryContextDestructorCleanup 
116   : public CrashRecoveryContextCleanup
117 {
118   T *resource;
119 public:
120   CrashRecoveryContextDestructorCleanup(T *resource) : resource(resource) {}
121   virtual void recoverResources() {
122     resource->~T();
123   }
124 };
125
126 template <typename T>
127 class CrashRecoveryContextDeleteCleanup
128   : public CrashRecoveryContextCleanup
129 {
130   T *resource;
131 public:
132   CrashRecoveryContextDeleteCleanup(T *resource) : resource(resource) {}
133   virtual void recoverResources() {
134     delete resource;
135   }
136 };
137
138 template <typename T>
139 struct CrashRecoveryContextTrait {
140   static inline CrashRecoveryContextCleanup *
141   createCleanup(T *resource,
142                 CrashRecoveryContextCleanup::ProvidedCleanups cleanup) {
143     switch (cleanup) {
144       case CrashRecoveryContextCleanup::DeleteCleanup:
145         return new CrashRecoveryContextDeleteCleanup<T>(resource);
146       case CrashRecoveryContextCleanup::DestructorCleanup:
147         return new CrashRecoveryContextDestructorCleanup<T>(resource);
148     }
149     return 0;
150   }
151 };
152
153 template<typename T>
154 inline CrashRecoveryContextCleanup*
155 CrashRecoveryContextCleanup::create(T *x,
156           CrashRecoveryContextCleanup::ProvidedCleanups cleanupKind) {
157   return CrashRecoveryContext::GetCurrent() ?
158           CrashRecoveryContextTrait<T>::createCleanup(x, cleanupKind) : 
159           0;
160 }
161
162 class CrashRecoveryContextCleanupRegistrar {
163   CrashRecoveryContext *context;
164   CrashRecoveryContextCleanup *cleanup;
165 public:
166   CrashRecoveryContextCleanupRegistrar(CrashRecoveryContextCleanup *cleanup)
167     : context(CrashRecoveryContext::GetCurrent()),
168       cleanup(cleanup) 
169   {
170     if (context && cleanup)
171       context->registerCleanup(cleanup);
172   }
173   ~CrashRecoveryContextCleanupRegistrar() {
174     if (cleanup && !cleanup->cleanupFired) {
175       if (context)
176         context->unregisterCleanup(cleanup);
177       else
178         delete cleanup;
179     }
180   }
181 };
182 }
183
184 #endif