1 //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer
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__) || \
22 # define SANITIZER_CAN_FAST_UNWIND 0
23 #elif SANITIZER_WINDOWS
24 # define SANITIZER_CAN_FAST_UNWIND 0
26 # define SANITIZER_CAN_FAST_UNWIND 1
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
33 # define SANITIZER_CAN_SLOW_UNWIND 0
35 # define SANITIZER_CAN_SLOW_UNWIND 1
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.
48 static bool WillUseFastUnwind(bool request_fast_unwind
) {
49 if (!SANITIZER_CAN_FAST_UNWIND
)
51 else if (!SANITIZER_CAN_SLOW_UNWIND
)
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
,
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
);
74 void FastUnwindStack(uptr pc
, uptr bp
, uptr stack_top
, uptr stack_bottom
,
76 void SlowUnwindStack(uptr pc
, uptr max_depth
);
77 void SlowUnwindStackWithContext(uptr pc
, void *context
,
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(); \
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(); \
106 uptr sp = (uptr)&local_stack
109 #endif // SANITIZER_STACKTRACE_H