[PR67828] don't unswitch on default defs of non-parms
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_stacktrace.h
blob68cc2620db78f2a32d77744b2de11acc17cdb384
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 uptr kStackTraceMax = 256;
20 #if SANITIZER_LINUX && (defined(__aarch64__) || defined(__sparc__) || \
21 defined(__mips__))
22 # define SANITIZER_CAN_FAST_UNWIND 0
23 #elif SANITIZER_WINDOWS
24 # define SANITIZER_CAN_FAST_UNWIND 0
25 #else
26 # define SANITIZER_CAN_FAST_UNWIND 1
27 #endif
29 // Fast unwind is the only option on Mac for now; we will need to
30 // revisit this macro when slow unwind works on Mac, see
31 // https://code.google.com/p/address-sanitizer/issues/detail?id=137
32 #if SANITIZER_MAC
33 # define SANITIZER_CAN_SLOW_UNWIND 0
34 #else
35 # define SANITIZER_CAN_SLOW_UNWIND 1
36 #endif
38 struct StackTrace {
39 const uptr *trace;
40 uptr size;
42 StackTrace() : trace(nullptr), size(0) {}
43 StackTrace(const uptr *trace, uptr size) : trace(trace), size(size) {}
45 // Prints a symbolized stacktrace, followed by an empty line.
46 void Print() const;
48 static bool WillUseFastUnwind(bool request_fast_unwind) {
49 if (!SANITIZER_CAN_FAST_UNWIND)
50 return false;
51 else if (!SANITIZER_CAN_SLOW_UNWIND)
52 return true;
53 return request_fast_unwind;
56 static uptr GetCurrentPc();
57 static uptr GetPreviousInstructionPc(uptr pc);
58 typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer,
59 int out_size);
62 // StackTrace that owns the buffer used to store the addresses.
63 struct BufferedStackTrace : public StackTrace {
64 uptr trace_buffer[kStackTraceMax];
65 uptr top_frame_bp; // Optional bp of a top frame.
67 BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
69 void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
70 void Unwind(uptr max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
71 uptr stack_bottom, bool request_fast_unwind);
73 private:
74 void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
75 uptr max_depth);
76 void SlowUnwindStack(uptr pc, uptr max_depth);
77 void SlowUnwindStackWithContext(uptr pc, void *context,
78 uptr max_depth);
79 void PopStackFrames(uptr count);
80 uptr LocatePcInTrace(uptr pc);
82 BufferedStackTrace(const BufferedStackTrace &);
83 void operator=(const BufferedStackTrace &);
86 } // namespace __sanitizer
88 // Use this macro if you want to print stack trace with the caller
89 // of the current function in the top frame.
90 #define GET_CALLER_PC_BP_SP \
91 uptr bp = GET_CURRENT_FRAME(); \
92 uptr pc = GET_CALLER_PC(); \
93 uptr local_stack; \
94 uptr sp = (uptr)&local_stack
96 #define GET_CALLER_PC_BP \
97 uptr bp = GET_CURRENT_FRAME(); \
98 uptr pc = GET_CALLER_PC();
100 // Use this macro if you want to print stack trace with the current
101 // function in the top frame.
102 #define GET_CURRENT_PC_BP_SP \
103 uptr bp = GET_CURRENT_FRAME(); \
104 uptr pc = StackTrace::GetCurrentPc(); \
105 uptr local_stack; \
106 uptr sp = (uptr)&local_stack
109 #endif // SANITIZER_STACKTRACE_H