1 //===-- sanitizer_stacktrace.h ----------------------------------*- 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 is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_STACKTRACE_H
14 #define SANITIZER_STACKTRACE_H
15
16 #include "sanitizer_internal_defs.h"
17
18 namespace __sanitizer {
19
20 static const u32 kStackTraceMax = 256;
21
22 #if SANITIZER_LINUX && (defined(__aarch64__) || defined(__powerpc__) || \
23 defined(__powerpc64__) || defined(__sparc__) || \
24 defined(__mips__))
25 # define SANITIZER_CAN_FAST_UNWIND 0
26 #elif SANITIZER_WINDOWS
27 # define SANITIZER_CAN_FAST_UNWIND 0
28 #else
29 # define SANITIZER_CAN_FAST_UNWIND 1
30 #endif
31
32 // Fast unwind is the only option on Mac for now; we will need to
33 // revisit this macro when slow unwind works on Mac, see
34 // https://code.google.com/p/address-sanitizer/issues/detail?id=137
35 #if SANITIZER_MAC
36 # define SANITIZER_CAN_SLOW_UNWIND 0
37 #else
38 # define SANITIZER_CAN_SLOW_UNWIND 1
39 #endif
40
41 struct StackTrace {
42 const uptr *trace;
43 u32 size;
44 u32 tag;
45
46 static const int TAG_UNKNOWN = 0;
47 static const int TAG_ALLOC = 1;
48 static const int TAG_DEALLOC = 2;
49 static const int TAG_CUSTOM = 100; // Tool specific tags start here.
50
StackTraceStackTrace51 StackTrace() : trace(nullptr), size(0), tag(0) {}
StackTraceStackTrace52 StackTrace(const uptr *trace, u32 size) : trace(trace), size(size), tag(0) {}
StackTraceStackTrace53 StackTrace(const uptr *trace, u32 size, u32 tag)
54 : trace(trace), size(size), tag(tag) {}
55
56 // Prints a symbolized stacktrace, followed by an empty line.
57 void Print() const;
58
WillUseFastUnwindStackTrace59 static bool WillUseFastUnwind(bool request_fast_unwind) {
60 if (!SANITIZER_CAN_FAST_UNWIND)
61 return false;
62 else if (!SANITIZER_CAN_SLOW_UNWIND)
63 return true;
64 return request_fast_unwind;
65 }
66
67 static uptr GetCurrentPc();
68 static inline uptr GetPreviousInstructionPc(uptr pc);
69 static uptr GetNextInstructionPc(uptr pc);
70 typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer,
71 int out_size);
72 };
73
74 // Performance-critical, must be in the header.
75 ALWAYS_INLINE
GetPreviousInstructionPc(uptr pc)76 uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
77 #if defined(__arm__)
78 // Cancel Thumb bit.
79 pc = pc & (~1);
80 #endif
81 #if defined(__powerpc__) || defined(__powerpc64__)
82 // PCs are always 4 byte aligned.
83 return pc - 4;
84 #elif defined(__sparc__) || defined(__mips__)
85 return pc - 8;
86 #else
87 return pc - 1;
88 #endif
89 }
90
91 // StackTrace that owns the buffer used to store the addresses.
92 struct BufferedStackTrace : public StackTrace {
93 uptr trace_buffer[kStackTraceMax];
94 uptr top_frame_bp; // Optional bp of a top frame.
95
BufferedStackTraceBufferedStackTrace96 BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
97
98 void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
99 void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
100 uptr stack_bottom, bool request_fast_unwind);
101
102 private:
103 void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
104 u32 max_depth);
105 void SlowUnwindStack(uptr pc, u32 max_depth);
106 void SlowUnwindStackWithContext(uptr pc, void *context,
107 u32 max_depth);
108 void PopStackFrames(uptr count);
109 uptr LocatePcInTrace(uptr pc);
110
111 BufferedStackTrace(const BufferedStackTrace &);
112 void operator=(const BufferedStackTrace &);
113 };
114
115 } // namespace __sanitizer
116
117 // Use this macro if you want to print stack trace with the caller
118 // of the current function in the top frame.
119 #define GET_CALLER_PC_BP_SP \
120 uptr bp = GET_CURRENT_FRAME(); \
121 uptr pc = GET_CALLER_PC(); \
122 uptr local_stack; \
123 uptr sp = (uptr)&local_stack
124
125 #define GET_CALLER_PC_BP \
126 uptr bp = GET_CURRENT_FRAME(); \
127 uptr pc = GET_CALLER_PC();
128
129 // Use this macro if you want to print stack trace with the current
130 // function in the top frame.
131 #define GET_CURRENT_PC_BP_SP \
132 uptr bp = GET_CURRENT_FRAME(); \
133 uptr pc = StackTrace::GetCurrentPc(); \
134 uptr local_stack; \
135 uptr sp = (uptr)&local_stack
136
137
138 #endif // SANITIZER_STACKTRACE_H
139