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(__powerpc__) || \
21 defined(__powerpc64__) || defined(__sparc__) || \
23 # define SANITIZER_CAN_FAST_UNWIND 0
24 #elif SANITIZER_WINDOWS
25 # define SANITIZER_CAN_FAST_UNWIND 0
27 # define SANITIZER_CAN_FAST_UNWIND 1
30 // Fast unwind is the only option on Mac for now; we will need to
31 // revisit this macro when slow unwind works on Mac, see
32 // https://code.google.com/p/address-sanitizer/issues/detail?id=137
34 # define SANITIZER_CAN_SLOW_UNWIND 0
36 # define SANITIZER_CAN_SLOW_UNWIND 1
43 StackTrace() : trace(nullptr), size(0) {}
44 StackTrace(const uptr
*trace
, uptr size
) : trace(trace
), size(size
) {}
46 // Prints a symbolized stacktrace, followed by an empty line.
49 static bool WillUseFastUnwind(bool request_fast_unwind
) {
50 if (!SANITIZER_CAN_FAST_UNWIND
)
52 else if (!SANITIZER_CAN_SLOW_UNWIND
)
54 return request_fast_unwind
;
57 static uptr
GetCurrentPc();
58 static uptr
GetPreviousInstructionPc(uptr pc
);
59 typedef bool (*SymbolizeCallback
)(const void *pc
, char *out_buffer
,
63 // StackTrace that owns the buffer used to store the addresses.
64 struct BufferedStackTrace
: public StackTrace
{
65 uptr trace_buffer
[kStackTraceMax
];
66 uptr top_frame_bp
; // Optional bp of a top frame.
68 BufferedStackTrace() : StackTrace(trace_buffer
, 0), top_frame_bp(0) {}
70 void Init(const uptr
*pcs
, uptr cnt
, uptr extra_top_pc
= 0);
71 void Unwind(uptr max_depth
, uptr pc
, uptr bp
, void *context
, uptr stack_top
,
72 uptr stack_bottom
, bool request_fast_unwind
);
75 void FastUnwindStack(uptr pc
, uptr bp
, uptr stack_top
, uptr stack_bottom
,
77 void SlowUnwindStack(uptr pc
, uptr max_depth
);
78 void SlowUnwindStackWithContext(uptr pc
, void *context
,
80 void PopStackFrames(uptr count
);
81 uptr
LocatePcInTrace(uptr pc
);
83 BufferedStackTrace(const BufferedStackTrace
&);
84 void operator=(const BufferedStackTrace
&);
87 } // namespace __sanitizer
89 // Use this macro if you want to print stack trace with the caller
90 // of the current function in the top frame.
91 #define GET_CALLER_PC_BP_SP \
92 uptr bp = GET_CURRENT_FRAME(); \
93 uptr pc = GET_CALLER_PC(); \
95 uptr sp = (uptr)&local_stack
97 #define GET_CALLER_PC_BP \
98 uptr bp = GET_CURRENT_FRAME(); \
99 uptr pc = GET_CALLER_PC();
101 // Use this macro if you want to print stack trace with the current
102 // function in the top frame.
103 #define GET_CURRENT_PC_BP_SP \
104 uptr bp = GET_CURRENT_FRAME(); \
105 uptr pc = StackTrace::GetCurrentPc(); \
107 uptr sp = (uptr)&local_stack
110 #endif // SANITIZER_STACKTRACE_H