1 //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries.
11 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_STACKTRACE_H
13 #define SANITIZER_STACKTRACE_H
15 #include "sanitizer_internal_defs.h"
17 namespace __sanitizer {
19 struct BufferedStackTrace;
21 static const u32 kStackTraceMax = 256;
23 #if SANITIZER_LINUX && defined(__mips__)
24 # define SANITIZER_CAN_FAST_UNWIND 0
25 #elif SANITIZER_WINDOWS
26 # define SANITIZER_CAN_FAST_UNWIND 0
27 #elif SANITIZER_OPENBSD
28 # define SANITIZER_CAN_FAST_UNWIND 0
30 # define SANITIZER_CAN_FAST_UNWIND 1
33 // Fast unwind is the only option on Mac for now; we will need to
34 // revisit this macro when slow unwind works on Mac, see
35 // https://github.com/google/sanitizers/issues/137
36 #if SANITIZER_MAC || SANITIZER_OPENBSD || SANITIZER_RTEMS
37 # define SANITIZER_CAN_SLOW_UNWIND 0
39 # define SANITIZER_CAN_SLOW_UNWIND 1
47 static const int TAG_UNKNOWN = 0;
48 static const int TAG_ALLOC = 1;
49 static const int TAG_DEALLOC = 2;
50 static const int TAG_CUSTOM = 100; // Tool specific tags start here.
52 StackTrace() : trace(nullptr), size(0), tag(0) {}
53 StackTrace(const uptr *trace, u32 size) : trace(trace), size(size), tag(0) {}
54 StackTrace(const uptr *trace, u32 size, u32 tag)
55 : trace(trace), size(size), tag(tag) {}
57 // Prints a symbolized stacktrace, followed by an empty line.
60 static bool WillUseFastUnwind(bool request_fast_unwind) {
61 if (!SANITIZER_CAN_FAST_UNWIND)
63 if (!SANITIZER_CAN_SLOW_UNWIND)
65 return request_fast_unwind;
68 static uptr GetCurrentPc();
69 static inline uptr GetPreviousInstructionPc(uptr pc);
70 static uptr GetNextInstructionPc(uptr pc);
71 typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer,
75 // Performance-critical, must be in the header.
77 uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
79 // T32 (Thumb) branch instructions might be 16 or 32 bit long,
80 // so we return (pc-2) in that case in order to be safe.
81 // For A32 mode we return (pc-4) because all instructions are 32 bit long.
82 return (pc - 3) & (~1);
83 #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__aarch64__)
84 // PCs are always 4 byte aligned.
86 #elif defined(__sparc__) || defined(__mips__)
93 // StackTrace that owns the buffer used to store the addresses.
94 struct BufferedStackTrace : public StackTrace {
95 uptr trace_buffer[kStackTraceMax];
96 uptr top_frame_bp; // Optional bp of a top frame.
98 BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
100 void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
102 // Get the stack trace with the given pc and bp.
103 // The pc will be in the position 0 of the resulting stack trace.
104 // The bp may refer to the current frame or to the caller's frame.
105 void Unwind(uptr pc, uptr bp, void *context, bool request_fast,
106 u32 max_depth = kStackTraceMax) {
107 top_frame_bp = (max_depth > 0) ? bp : 0;
108 // Small max_depth optimization
109 if (max_depth <= 1) {
111 trace_buffer[0] = pc;
115 UnwindImpl(pc, bp, context, request_fast, max_depth);
118 void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
119 uptr stack_bottom, bool request_fast_unwind);
122 *static_cast<StackTrace *>(this) = StackTrace(trace_buffer, 0);
127 // Every runtime defines its own implementation of this method
128 void UnwindImpl(uptr pc, uptr bp, void *context, bool request_fast,
131 // UnwindFast/Slow have platform-specific implementations
132 void UnwindFast(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
134 void UnwindSlow(uptr pc, u32 max_depth);
135 void UnwindSlow(uptr pc, void *context, u32 max_depth);
137 void PopStackFrames(uptr count);
138 uptr LocatePcInTrace(uptr pc);
140 BufferedStackTrace(const BufferedStackTrace &) = delete;
141 void operator=(const BufferedStackTrace &) = delete;
143 friend class FastUnwindTest;
146 // Check if given pointer points into allocated stack area.
147 static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
148 return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr);
151 } // namespace __sanitizer
153 // Use this macro if you want to print stack trace with the caller
154 // of the current function in the top frame.
155 #define GET_CALLER_PC_BP \
156 uptr bp = GET_CURRENT_FRAME(); \
157 uptr pc = GET_CALLER_PC();
159 #define GET_CALLER_PC_BP_SP \
162 uptr sp = (uptr)&local_stack
164 // Use this macro if you want to print stack trace with the current
165 // function in the top frame.
166 #define GET_CURRENT_PC_BP \
167 uptr bp = GET_CURRENT_FRAME(); \
168 uptr pc = StackTrace::GetCurrentPc()
170 #define GET_CURRENT_PC_BP_SP \
173 uptr sp = (uptr)&local_stack
176 #endif // SANITIZER_STACKTRACE_H