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