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 /// \brief Crash recovery helper object.
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.
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:
30 /// void actual_work(void *);
33 /// CrashRecoveryContext CRC;
35 /// if (!CRC.RunSafely(actual_work, 0)) {
36 /// ... a crash was detected, report error to user ...
39 /// ... no crash was detected ...
42 /// Crash recovery contexts may not be nested.
43 class CrashRecoveryContext {
47 CrashRecoveryContext() : Impl(0) {}
48 ~CrashRecoveryContext();
50 /// \brief Enable crash recovery.
53 /// \brief Disable crash recovery.
54 static void Disable();
56 /// \brief Execute the provide callback function (with the given arguments) in
57 /// a protected context.
59 /// \return True if the function completed successfully, and false if the
60 /// function crashed (or HandleCrash was called explicitly). Clients should
61 /// make as little assumptions as possible about the program state when
62 /// RunSafely has returned false. Clients can use getBacktrace() to retrieve
63 /// the backtrace of the crash on failures.
64 bool RunSafely(void (*Fn)(void*), void *UserData);
66 /// \brief Explicitly trigger a crash recovery in the current process, and
67 /// return failure from RunSafely(). This function does not return.
70 /// \brief Return a string containing the backtrace where the crash was
71 /// detected; or empty if the backtrace wasn't recovered.
73 /// This function is only valid when a crash has been detected (i.e.,
74 /// RunSafely() has returned false.
75 const std::string &getBacktrace() const;