1 //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_STACKTRACE_H
14 #define SANITIZER_STACKTRACE_H
16 #include "sanitizer_internal_defs.h"
18 namespace __sanitizer {
20 static const u32 kStackTraceMax = 256;
22 #if SANITIZER_LINUX && (defined(__sparc__) || defined(__mips__))
23 # define SANITIZER_CAN_FAST_UNWIND 0
24 #elif SANITIZER_WINDOWS
25 # define SANITIZER_CAN_FAST_UNWIND 0
26 #elif SANITIZER_OPENBSD
27 # define SANITIZER_CAN_FAST_UNWIND 0
29 # define SANITIZER_CAN_FAST_UNWIND 1
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://github.com/google/sanitizers/issues/137
35 #if SANITIZER_MAC || SANITIZER_OPENBSD || SANITIZER_RTEMS
36 # define SANITIZER_CAN_SLOW_UNWIND 0
38 # define SANITIZER_CAN_SLOW_UNWIND 1
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.
51 StackTrace() : trace(nullptr), size(0), tag(0) {}
52 StackTrace(const uptr *trace, u32 size) : trace(trace), size(size), tag(0) {}
53 StackTrace(const uptr *trace, u32 size, u32 tag)
54 : trace(trace), size(size), tag(tag) {}
56 // Prints a symbolized stacktrace, followed by an empty line.
59 static bool WillUseFastUnwind(bool request_fast_unwind) {
60 if (!SANITIZER_CAN_FAST_UNWIND)
62 else if (!SANITIZER_CAN_SLOW_UNWIND)
64 return request_fast_unwind;
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,
74 // Performance-critical, must be in the header.
76 uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
78 // T32 (Thumb) branch instructions might be 16 or 32 bit long,
79 // so we return (pc-2) in that case in order to be safe.
80 // For A32 mode we return (pc-4) because all instructions are 32 bit long.
81 return (pc - 3) & (~1);
82 #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__aarch64__)
83 // PCs are always 4 byte aligned.
85 #elif defined(__sparc__) || defined(__mips__)
92 // StackTrace that owns the buffer used to store the addresses.
93 struct BufferedStackTrace : public StackTrace {
94 uptr trace_buffer[kStackTraceMax];
95 uptr top_frame_bp; // Optional bp of a top frame.
97 BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
99 void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
100 void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
101 uptr stack_bottom, bool request_fast_unwind);
104 *static_cast<StackTrace *>(this) = StackTrace(trace_buffer, 0);
109 void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
111 void SlowUnwindStack(uptr pc, u32 max_depth);
112 void SlowUnwindStackWithContext(uptr pc, void *context,
114 void PopStackFrames(uptr count);
115 uptr LocatePcInTrace(uptr pc);
117 BufferedStackTrace(const BufferedStackTrace &) = delete;
118 void operator=(const BufferedStackTrace &) = delete;
121 // Check if given pointer points into allocated stack area.
122 static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
123 return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr);
126 } // namespace __sanitizer
128 // Use this macro if you want to print stack trace with the caller
129 // of the current function in the top frame.
130 #define GET_CALLER_PC_BP_SP \
131 uptr bp = GET_CURRENT_FRAME(); \
132 uptr pc = GET_CALLER_PC(); \
134 uptr sp = (uptr)&local_stack
136 #define GET_CALLER_PC_BP \
137 uptr bp = GET_CURRENT_FRAME(); \
138 uptr pc = GET_CALLER_PC();
140 // Use this macro if you want to print stack trace with the current
141 // function in the top frame.
142 #define GET_CURRENT_PC_BP_SP \
143 uptr bp = GET_CURRENT_FRAME(); \
144 uptr pc = StackTrace::GetCurrentPc(); \
146 uptr sp = (uptr)&local_stack
149 #endif // SANITIZER_STACKTRACE_H