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
31 typedef bool (*SymbolizeCallback
)(const void *pc
, char *out_buffer
,
35 uptr trace
[kStackTraceMax
];
37 // Prints a symbolized stacktrace, followed by an empty line.
38 static void PrintStack(const uptr
*addr
, uptr size
);
40 PrintStack(trace
, size
);
43 void CopyFrom(const uptr
*src
, uptr src_size
) {
46 if (size
> kStackTraceMax
) size
= kStackTraceMax
;
47 for (uptr i
= 0; i
< size
; i
++)
51 static bool WillUseFastUnwind(bool request_fast_unwind
) {
52 // Check if fast unwind is available. Fast unwind is the only option on Mac.
53 if (!SANITIZER_CAN_FAST_UNWIND
)
55 else if (SANITIZER_MAC
)
57 return request_fast_unwind
;
60 void Unwind(uptr max_depth
, uptr pc
, uptr bp
, void *context
, uptr stack_top
,
61 uptr stack_bottom
, bool request_fast_unwind
);
63 static uptr
GetCurrentPc();
64 static uptr
GetPreviousInstructionPc(uptr pc
);
67 void FastUnwindStack(uptr pc
, uptr bp
, uptr stack_top
, uptr stack_bottom
,
69 void SlowUnwindStack(uptr pc
, uptr max_depth
);
70 void SlowUnwindStackWithContext(uptr pc
, void *context
,
72 void PopStackFrames(uptr count
);
73 uptr
LocatePcInTrace(uptr pc
);
76 } // namespace __sanitizer
78 // Use this macro if you want to print stack trace with the caller
79 // of the current function in the top frame.
80 #define GET_CALLER_PC_BP_SP \
81 uptr bp = GET_CURRENT_FRAME(); \
82 uptr pc = GET_CALLER_PC(); \
84 uptr sp = (uptr)&local_stack
86 // Use this macro if you want to print stack trace with the current
87 // function in the top frame.
88 #define GET_CURRENT_PC_BP_SP \
89 uptr bp = GET_CURRENT_FRAME(); \
90 uptr pc = StackTrace::GetCurrentPc(); \
92 uptr sp = (uptr)&local_stack
95 #endif // SANITIZER_STACKTRACE_H