* ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_stacktrace.h
blob31e99f6b9dbeba9e99bacfb4c91dd1a1d4a100e1
1 //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries.
10 //===----------------------------------------------------------------------===//
11 #ifndef SANITIZER_STACKTRACE_H
12 #define SANITIZER_STACKTRACE_H
14 #include "sanitizer_internal_defs.h"
16 namespace __sanitizer {
18 static const u32 kStackTraceMax = 256;
20 #if SANITIZER_LINUX && (defined(__sparc__) || defined(__mips__))
21 # define SANITIZER_CAN_FAST_UNWIND 0
22 #elif SANITIZER_WINDOWS
23 # define SANITIZER_CAN_FAST_UNWIND 0
24 #else
25 # define SANITIZER_CAN_FAST_UNWIND 1
26 #endif
28 // Fast unwind is the only option on Mac for now; we will need to
29 // revisit this macro when slow unwind works on Mac, see
30 // https://github.com/google/sanitizers/issues/137
31 #if SANITIZER_MAC
32 # define SANITIZER_CAN_SLOW_UNWIND 0
33 #else
34 # define SANITIZER_CAN_SLOW_UNWIND 1
35 #endif
37 struct StackTrace {
38 const uptr *trace;
39 u32 size;
40 u32 tag;
42 static const int TAG_UNKNOWN = 0;
43 static const int TAG_ALLOC = 1;
44 static const int TAG_DEALLOC = 2;
45 static const int TAG_CUSTOM = 100; // Tool specific tags start here.
47 StackTrace() : trace(nullptr), size(0), tag(0) {}
48 StackTrace(const uptr *trace, u32 size) : trace(trace), size(size), tag(0) {}
49 StackTrace(const uptr *trace, u32 size, u32 tag)
50 : trace(trace), size(size), tag(tag) {}
52 // Prints a symbolized stacktrace, followed by an empty line.
53 void Print() const;
55 static bool WillUseFastUnwind(bool request_fast_unwind) {
56 if (!SANITIZER_CAN_FAST_UNWIND)
57 return false;
58 else if (!SANITIZER_CAN_SLOW_UNWIND)
59 return true;
60 return request_fast_unwind;
63 static uptr GetCurrentPc();
64 static inline uptr GetPreviousInstructionPc(uptr pc);
65 static uptr GetNextInstructionPc(uptr pc);
66 typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer,
67 int out_size);
70 // Performance-critical, must be in the header.
71 ALWAYS_INLINE
72 uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
73 #if defined(__arm__)
74 // Cancel Thumb bit.
75 pc = pc & (~1);
76 #endif
77 #if defined(__powerpc__) || defined(__powerpc64__)
78 // PCs are always 4 byte aligned.
79 return pc - 4;
80 #elif defined(__sparc__) || defined(__mips__)
81 return pc - 8;
82 #else
83 return pc - 1;
84 #endif
87 // StackTrace that owns the buffer used to store the addresses.
88 struct BufferedStackTrace : public StackTrace {
89 uptr trace_buffer[kStackTraceMax];
90 uptr top_frame_bp; // Optional bp of a top frame.
92 BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
94 void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
95 void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
96 uptr stack_bottom, bool request_fast_unwind);
98 void Reset() {
99 *static_cast<StackTrace *>(this) = StackTrace(trace_buffer, 0);
100 top_frame_bp = 0;
103 private:
104 void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
105 u32 max_depth);
106 void SlowUnwindStack(uptr pc, u32 max_depth);
107 void SlowUnwindStackWithContext(uptr pc, void *context,
108 u32 max_depth);
109 void PopStackFrames(uptr count);
110 uptr LocatePcInTrace(uptr pc);
112 BufferedStackTrace(const BufferedStackTrace &) = delete;
113 void operator=(const BufferedStackTrace &) = delete;
116 // Check if given pointer points into allocated stack area.
117 static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
118 return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr);
121 } // namespace __sanitizer
123 // Use this macro if you want to print stack trace with the caller
124 // of the current function in the top frame.
125 #define GET_CALLER_PC_BP_SP \
126 uptr bp = GET_CURRENT_FRAME(); \
127 uptr pc = GET_CALLER_PC(); \
128 uptr local_stack; \
129 uptr sp = (uptr)&local_stack
131 #define GET_CALLER_PC_BP \
132 uptr bp = GET_CURRENT_FRAME(); \
133 uptr pc = GET_CALLER_PC();
135 // Use this macro if you want to print stack trace with the current
136 // function in the top frame.
137 #define GET_CURRENT_PC_BP_SP \
138 uptr bp = GET_CURRENT_FRAME(); \
139 uptr pc = StackTrace::GetCurrentPc(); \
140 uptr local_stack; \
141 uptr sp = (uptr)&local_stack
144 #endif // SANITIZER_STACKTRACE_H