Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/Support/PrettyStackTrace.h - Pretty Crash Handling --*- 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 | // This file defines the PrettyStackTraceEntry class, which is used to make |
| 11 | // crashes give more contextual information about what the program was doing |
| 12 | // when it crashed. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_SUPPORT_PRETTYSTACKTRACE_H |
| 17 | #define LLVM_SUPPORT_PRETTYSTACKTRACE_H |
| 18 | |
| 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/Support/Compiler.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | class raw_ostream; |
| 24 | |
| 25 | void EnablePrettyStackTrace(); |
| 26 | |
| 27 | /// PrettyStackTraceEntry - This class is used to represent a frame of the |
| 28 | /// "pretty" stack trace that is dumped when a program crashes. You can define |
| 29 | /// subclasses of this and declare them on the program stack: when they are |
| 30 | /// constructed and destructed, they will add their symbolic frames to a |
| 31 | /// virtual stack trace. This gets dumped out if the program crashes. |
| 32 | class PrettyStackTraceEntry { |
| 33 | friend PrettyStackTraceEntry *ReverseStackTrace(PrettyStackTraceEntry *); |
| 34 | |
| 35 | PrettyStackTraceEntry *NextEntry; |
| 36 | PrettyStackTraceEntry(const PrettyStackTraceEntry &) = delete; |
| 37 | void operator=(const PrettyStackTraceEntry &) = delete; |
| 38 | public: |
| 39 | PrettyStackTraceEntry(); |
| 40 | virtual ~PrettyStackTraceEntry(); |
| 41 | |
| 42 | /// print - Emit information about this stack frame to OS. |
| 43 | virtual void print(raw_ostream &OS) const = 0; |
| 44 | |
| 45 | /// getNextEntry - Return the next entry in the list of frames. |
| 46 | const PrettyStackTraceEntry *getNextEntry() const { return NextEntry; } |
| 47 | }; |
| 48 | |
| 49 | /// PrettyStackTraceString - This object prints a specified string (which |
| 50 | /// should not contain newlines) to the stream as the stack trace when a crash |
| 51 | /// occurs. |
| 52 | class PrettyStackTraceString : public PrettyStackTraceEntry { |
| 53 | const char *Str; |
| 54 | public: |
| 55 | PrettyStackTraceString(const char *str) : Str(str) {} |
| 56 | void print(raw_ostream &OS) const override; |
| 57 | }; |
| 58 | |
| 59 | /// PrettyStackTraceFormat - This object prints a string (which may use |
| 60 | /// printf-style formatting but should not contain newlines) to the stream |
| 61 | /// as the stack trace when a crash occurs. |
| 62 | class PrettyStackTraceFormat : public PrettyStackTraceEntry { |
| 63 | llvm::SmallVector<char, 32> Str; |
| 64 | public: |
| 65 | PrettyStackTraceFormat(const char *Format, ...); |
| 66 | void print(raw_ostream &OS) const override; |
| 67 | }; |
| 68 | |
| 69 | /// PrettyStackTraceProgram - This object prints a specified program arguments |
| 70 | /// to the stream as the stack trace when a crash occurs. |
| 71 | class PrettyStackTraceProgram : public PrettyStackTraceEntry { |
| 72 | int ArgC; |
| 73 | const char *const *ArgV; |
| 74 | public: |
| 75 | PrettyStackTraceProgram(int argc, const char * const*argv) |
| 76 | : ArgC(argc), ArgV(argv) { |
| 77 | EnablePrettyStackTrace(); |
| 78 | } |
| 79 | void print(raw_ostream &OS) const override; |
| 80 | }; |
| 81 | |
| 82 | /// Returns the topmost element of the "pretty" stack state. |
| 83 | const void *SavePrettyStackState(); |
| 84 | |
| 85 | /// Restores the topmost element of the "pretty" stack state to State, which |
| 86 | /// should come from a previous call to SavePrettyStackState(). This is |
| 87 | /// useful when using a CrashRecoveryContext in code that also uses |
| 88 | /// PrettyStackTraceEntries, to make sure the stack that's printed if a crash |
| 89 | /// happens after a crash that's been recovered by CrashRecoveryContext |
| 90 | /// doesn't have frames on it that were added in code unwound by the |
| 91 | /// CrashRecoveryContext. |
| 92 | void RestorePrettyStackState(const void *State); |
| 93 | |
| 94 | } // end namespace llvm |
| 95 | |
| 96 | #endif |