]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.h
MFV r337014:
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_stacktrace.h
1 //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_STACKTRACE_H
14 #define SANITIZER_STACKTRACE_H
15
16 #include "sanitizer_internal_defs.h"
17
18 namespace __sanitizer {
19
20 static const u32 kStackTraceMax = 256;
21
22 #if SANITIZER_LINUX &&  (defined(__sparc__) || defined(__mips__))
23 # define SANITIZER_CAN_FAST_UNWIND 0
24 #elif SANITIZER_WINDOWS
25 # define SANITIZER_CAN_FAST_UNWIND 0
26 #else
27 # define SANITIZER_CAN_FAST_UNWIND 1
28 #endif
29
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://github.com/google/sanitizers/issues/137
33 #if SANITIZER_MAC
34 # define SANITIZER_CAN_SLOW_UNWIND 0
35 #else
36 # define SANITIZER_CAN_SLOW_UNWIND 1
37 #endif
38
39 struct StackTrace {
40   const uptr *trace;
41   u32 size;
42   u32 tag;
43
44   static const int TAG_UNKNOWN = 0;
45   static const int TAG_ALLOC = 1;
46   static const int TAG_DEALLOC = 2;
47   static const int TAG_CUSTOM = 100; // Tool specific tags start here.
48
49   StackTrace() : trace(nullptr), size(0), tag(0) {}
50   StackTrace(const uptr *trace, u32 size) : trace(trace), size(size), tag(0) {}
51   StackTrace(const uptr *trace, u32 size, u32 tag)
52       : trace(trace), size(size), tag(tag) {}
53
54   // Prints a symbolized stacktrace, followed by an empty line.
55   void Print() const;
56
57   static bool WillUseFastUnwind(bool request_fast_unwind) {
58     if (!SANITIZER_CAN_FAST_UNWIND)
59       return false;
60     else if (!SANITIZER_CAN_SLOW_UNWIND)
61       return true;
62     return request_fast_unwind;
63   }
64
65   static uptr GetCurrentPc();
66   static inline uptr GetPreviousInstructionPc(uptr pc);
67   static uptr GetNextInstructionPc(uptr pc);
68   typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer,
69                                     int out_size);
70 };
71
72 // Performance-critical, must be in the header.
73 ALWAYS_INLINE
74 uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
75 #if defined(__arm__)
76   // Cancel Thumb bit.
77   pc = pc & (~1);
78 #endif
79 #if defined(__powerpc__) || defined(__powerpc64__)
80   // PCs are always 4 byte aligned.
81   return pc - 4;
82 #elif defined(__sparc__) || defined(__mips__)
83   return pc - 8;
84 #else
85   return pc - 1;
86 #endif
87 }
88
89 // StackTrace that owns the buffer used to store the addresses.
90 struct BufferedStackTrace : public StackTrace {
91   uptr trace_buffer[kStackTraceMax];
92   uptr top_frame_bp;  // Optional bp of a top frame.
93
94   BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
95
96   void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
97   void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
98               uptr stack_bottom, bool request_fast_unwind);
99
100   void Reset() {
101     *static_cast<StackTrace *>(this) = StackTrace(trace_buffer, 0);
102     top_frame_bp = 0;
103   }
104
105  private:
106   void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
107                        u32 max_depth);
108   void SlowUnwindStack(uptr pc, u32 max_depth);
109   void SlowUnwindStackWithContext(uptr pc, void *context,
110                                   u32 max_depth);
111   void PopStackFrames(uptr count);
112   uptr LocatePcInTrace(uptr pc);
113
114   BufferedStackTrace(const BufferedStackTrace &) = delete;
115   void operator=(const BufferedStackTrace &) = delete;
116 };
117
118 // Check if given pointer points into allocated stack area.
119 static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
120   return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr);
121 }
122
123 }  // namespace __sanitizer
124
125 // Use this macro if you want to print stack trace with the caller
126 // of the current function in the top frame.
127 #define GET_CALLER_PC_BP_SP \
128   uptr bp = GET_CURRENT_FRAME();              \
129   uptr pc = GET_CALLER_PC();                  \
130   uptr local_stack;                           \
131   uptr sp = (uptr)&local_stack
132
133 #define GET_CALLER_PC_BP \
134   uptr bp = GET_CURRENT_FRAME();              \
135   uptr pc = GET_CALLER_PC();
136
137 // Use this macro if you want to print stack trace with the current
138 // function in the top frame.
139 #define GET_CURRENT_PC_BP_SP \
140   uptr bp = GET_CURRENT_FRAME();              \
141   uptr pc = StackTrace::GetCurrentPc();   \
142   uptr local_stack;                           \
143   uptr sp = (uptr)&local_stack
144
145
146 #endif  // SANITIZER_STACKTRACE_H