1 //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_STACKTRACE_H
14 #define SANITIZER_STACKTRACE_H
16 #include "sanitizer_internal_defs.h"
18 namespace __sanitizer {
20 static const uptr kStackTraceMax = 256;
22 #if SANITIZER_LINUX && (defined(__aarch64__) || defined(__powerpc__) || \
23 defined(__powerpc64__) || defined(__sparc__) || \
25 # define SANITIZER_CAN_FAST_UNWIND 0
26 #elif SANITIZER_WINDOWS
27 # define SANITIZER_CAN_FAST_UNWIND 0
29 # define SANITIZER_CAN_FAST_UNWIND 1
32 // Fast unwind is the only option on Mac for now; we will need to
33 // revisit this macro when slow unwind works on Mac, see
34 // https://code.google.com/p/address-sanitizer/issues/detail?id=137
36 # define SANITIZER_CAN_SLOW_UNWIND 0
38 # define SANITIZER_CAN_SLOW_UNWIND 1
45 StackTrace() : trace(nullptr), size(0) {}
46 StackTrace(const uptr *trace, uptr size) : trace(trace), size(size) {}
48 // Prints a symbolized stacktrace, followed by an empty line.
51 static bool WillUseFastUnwind(bool request_fast_unwind) {
52 if (!SANITIZER_CAN_FAST_UNWIND)
54 else if (!SANITIZER_CAN_SLOW_UNWIND)
56 return request_fast_unwind;
59 static uptr GetCurrentPc();
60 static uptr GetPreviousInstructionPc(uptr pc);
61 static uptr GetNextInstructionPc(uptr pc);
62 typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer,
66 // StackTrace that owns the buffer used to store the addresses.
67 struct BufferedStackTrace : public StackTrace {
68 uptr trace_buffer[kStackTraceMax];
69 uptr top_frame_bp; // Optional bp of a top frame.
71 BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
73 void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
74 void Unwind(uptr max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
75 uptr stack_bottom, bool request_fast_unwind);
78 void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
80 void SlowUnwindStack(uptr pc, uptr max_depth);
81 void SlowUnwindStackWithContext(uptr pc, void *context,
83 void PopStackFrames(uptr count);
84 uptr LocatePcInTrace(uptr pc);
86 BufferedStackTrace(const BufferedStackTrace &);
87 void operator=(const BufferedStackTrace &);
90 } // namespace __sanitizer
92 // Use this macro if you want to print stack trace with the caller
93 // of the current function in the top frame.
94 #define GET_CALLER_PC_BP_SP \
95 uptr bp = GET_CURRENT_FRAME(); \
96 uptr pc = GET_CALLER_PC(); \
98 uptr sp = (uptr)&local_stack
100 #define GET_CALLER_PC_BP \
101 uptr bp = GET_CURRENT_FRAME(); \
102 uptr pc = GET_CALLER_PC();
104 // Use this macro if you want to print stack trace with the current
105 // function in the top frame.
106 #define GET_CURRENT_PC_BP_SP \
107 uptr bp = GET_CURRENT_FRAME(); \
108 uptr pc = StackTrace::GetCurrentPc(); \
110 uptr sp = (uptr)&local_stack
113 #endif // SANITIZER_STACKTRACE_H