PrettyStackTrace.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //===- llvm/Support/PrettyStackTrace.h - Pretty Crash Handling --*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the PrettyStackTraceEntry class, which is used to make
  10. // crashes give more contextual information about what the program was doing
  11. // when it crashed.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_SUPPORT_PRETTYSTACKTRACE_H
  15. #define LLVM_SUPPORT_PRETTYSTACKTRACE_H
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/Support/Compiler.h"
  18. namespace llvm {
  19. class raw_ostream;
  20. /// Enables dumping a "pretty" stack trace when the program crashes.
  21. ///
  22. /// \see PrettyStackTraceEntry
  23. void EnablePrettyStackTrace();
  24. /// Enables (or disables) dumping a "pretty" stack trace when the user sends
  25. /// SIGINFO or SIGUSR1 to the current process.
  26. ///
  27. /// This is a per-thread decision so that a program can choose to print stack
  28. /// traces only on a primary thread, or on all threads that use
  29. /// PrettyStackTraceEntry.
  30. ///
  31. /// \see EnablePrettyStackTrace
  32. /// \see PrettyStackTraceEntry
  33. void EnablePrettyStackTraceOnSigInfoForThisThread(bool ShouldEnable = true);
  34. /// Replaces the generic bug report message that is output upon
  35. /// a crash.
  36. void setBugReportMsg(const char *Msg);
  37. /// Get the bug report message that will be output upon a crash.
  38. const char *getBugReportMsg();
  39. /// PrettyStackTraceEntry - This class is used to represent a frame of the
  40. /// "pretty" stack trace that is dumped when a program crashes. You can define
  41. /// subclasses of this and declare them on the program stack: when they are
  42. /// constructed and destructed, they will add their symbolic frames to a
  43. /// virtual stack trace. This gets dumped out if the program crashes.
  44. class PrettyStackTraceEntry {
  45. friend PrettyStackTraceEntry *ReverseStackTrace(PrettyStackTraceEntry *);
  46. PrettyStackTraceEntry *NextEntry;
  47. PrettyStackTraceEntry(const PrettyStackTraceEntry &) = delete;
  48. void operator=(const PrettyStackTraceEntry &) = delete;
  49. public:
  50. PrettyStackTraceEntry();
  51. virtual ~PrettyStackTraceEntry();
  52. /// print - Emit information about this stack frame to OS.
  53. virtual void print(raw_ostream &OS) const = 0;
  54. /// getNextEntry - Return the next entry in the list of frames.
  55. const PrettyStackTraceEntry *getNextEntry() const { return NextEntry; }
  56. };
  57. /// PrettyStackTraceString - This object prints a specified string (which
  58. /// should not contain newlines) to the stream as the stack trace when a crash
  59. /// occurs.
  60. class PrettyStackTraceString : public PrettyStackTraceEntry {
  61. const char *Str;
  62. public:
  63. PrettyStackTraceString(const char *str) : Str(str) {}
  64. void print(raw_ostream &OS) const override;
  65. };
  66. /// PrettyStackTraceFormat - This object prints a string (which may use
  67. /// printf-style formatting but should not contain newlines) to the stream
  68. /// as the stack trace when a crash occurs.
  69. class PrettyStackTraceFormat : public PrettyStackTraceEntry {
  70. llvm::SmallVector<char, 32> Str;
  71. public:
  72. PrettyStackTraceFormat(const char *Format, ...);
  73. void print(raw_ostream &OS) const override;
  74. };
  75. /// PrettyStackTraceProgram - This object prints a specified program arguments
  76. /// to the stream as the stack trace when a crash occurs.
  77. class PrettyStackTraceProgram : public PrettyStackTraceEntry {
  78. int ArgC;
  79. const char *const *ArgV;
  80. public:
  81. PrettyStackTraceProgram(int argc, const char * const*argv)
  82. : ArgC(argc), ArgV(argv) {
  83. EnablePrettyStackTrace();
  84. }
  85. void print(raw_ostream &OS) const override;
  86. };
  87. /// Returns the topmost element of the "pretty" stack state.
  88. const void *SavePrettyStackState();
  89. /// Restores the topmost element of the "pretty" stack state to State, which
  90. /// should come from a previous call to SavePrettyStackState(). This is
  91. /// useful when using a CrashRecoveryContext in code that also uses
  92. /// PrettyStackTraceEntries, to make sure the stack that's printed if a crash
  93. /// happens after a crash that's been recovered by CrashRecoveryContext
  94. /// doesn't have frames on it that were added in code unwound by the
  95. /// CrashRecoveryContext.
  96. void RestorePrettyStackState(const void *State);
  97. } // end namespace llvm
  98. #endif