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. This function is not thread safe, clients
51 /// should call it during startup or with a lock held.
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();
58 /// \brief Execute the provide callback function (with the given arguments) in
59 /// a protected context.
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);
68 /// \brief Explicitly trigger a crash recovery in the current process, and
69 /// return failure from RunSafely(). This function does not return.
72 /// \brief Return a string containing the backtrace where the crash was
73 /// detected; or empty if the backtrace wasn't recovered.
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;