Support: Add CrashRecoveryContext helper object.
[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 /// \brief Crash recovery helper object.
19 ///
20 /// This class implements support for running operations in a safe context so
21 /// that crashes (memory errors, stack overflow, assertion violations) can be
22 /// detected and control restored to the crashing thread. Crash detection is
23 /// purely "best effort", the exact set of failures which can be recovered from
24 /// is platform dependent.
25 ///
26 /// Clients make use of this code by first calling
27 /// CrashRecoveryContext::Enable(), and then executing unsafe operations via a
28 /// CrashRecoveryContext object. For example:
29 ///
30 ///    void actual_work(void *);
31 ///
32 ///    void foo() {
33 ///      CrashRecoveryContext CRC;
34 ///
35 ///      if (!CRC.RunSafely(actual_work, 0)) {
36 ///         ... a crash was detected, report error to user ...
37 ///      }
38 ///
39 ///      ... no crash was detected ...
40 ///    }
41 ///
42 /// Crash recovery contexts may not be nested.
43 class CrashRecoveryContext {
44   void *Impl;
45
46 public:
47   CrashRecoveryContext() : Impl(0) {}
48   ~CrashRecoveryContext();
49
50   /// \brief Enable crash recovery. This function is not thread safe, clients
51   /// should call it during startup or with a lock held.
52   static void Enable();
53
54   /// \brief Disable crash recovery. This function is not thread safe, clients
55   /// should call it during startup or with a lock held.
56   static void Disable();
57
58   /// \brief Execute the provide callback function (with the given arguments) in
59   /// a protected context.
60   ///
61   /// \return True if the function completed successfully, and false if the
62   /// function crashed (or HandleCrash was called explicitly). Clients should
63   /// make as little assumptions as possible about the program state when
64   /// RunSafely has returned false. Clients can use getBacktrace() to retrieve
65   /// the backtrace of the crash on failures.
66   bool RunSafely(void (*Fn)(void*), void *UserData);
67
68   /// \brief Explicitly trigger a crash recovery in the current process, and
69   /// return failure from RunSafely(). This function does not return.
70   void HandleCrash();
71
72   /// \brief Return a string containing the backtrace where the crash was
73   /// detected; or empty if the backtrace wasn't recovered.
74   ///
75   /// This function is only valid when a crash has been detected (i.e.,
76   /// RunSafely() has returned false.
77   const std::string &getBacktrace() const;
78 };
79
80 }
81
82 #endif