PrettyStackTrace.h 4.6 KB

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