]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.h
Merge llvm 3.6.0rc1 from ^/vendor/llvm/dist, merge clang 3.6.0rc1 from
[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 uptr kStackTraceMax = 256;
21
22 #if SANITIZER_LINUX && (defined(__aarch64__) || defined(__powerpc__) || \
23                         defined(__powerpc64__) || defined(__sparc__) || \
24                         defined(__mips__))
25 # define SANITIZER_CAN_FAST_UNWIND 0
26 #elif SANITIZER_WINDOWS
27 # define SANITIZER_CAN_FAST_UNWIND 0
28 #else
29 # define SANITIZER_CAN_FAST_UNWIND 1
30 #endif
31
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
35 #if SANITIZER_MAC
36 # define SANITIZER_CAN_SLOW_UNWIND 0
37 #else
38 # define SANITIZER_CAN_SLOW_UNWIND 1
39 #endif
40
41 struct StackTrace {
42   const uptr *trace;
43   uptr size;
44
45   StackTrace() : trace(nullptr), size(0) {}
46   StackTrace(const uptr *trace, uptr size) : trace(trace), size(size) {}
47
48   // Prints a symbolized stacktrace, followed by an empty line.
49   void Print() const;
50
51   static bool WillUseFastUnwind(bool request_fast_unwind) {
52     if (!SANITIZER_CAN_FAST_UNWIND)
53       return false;
54     else if (!SANITIZER_CAN_SLOW_UNWIND)
55       return true;
56     return request_fast_unwind;
57   }
58
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,
63                                     int out_size);
64 };
65
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.
70
71   BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
72
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);
76
77  private:
78   void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
79                        uptr max_depth);
80   void SlowUnwindStack(uptr pc, uptr max_depth);
81   void SlowUnwindStackWithContext(uptr pc, void *context,
82                                   uptr max_depth);
83   void PopStackFrames(uptr count);
84   uptr LocatePcInTrace(uptr pc);
85
86   BufferedStackTrace(const BufferedStackTrace &);
87   void operator=(const BufferedStackTrace &);
88 };
89
90 }  // namespace __sanitizer
91
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();                  \
97   uptr local_stack;                           \
98   uptr sp = (uptr)&local_stack
99
100 #define GET_CALLER_PC_BP \
101   uptr bp = GET_CURRENT_FRAME();              \
102   uptr pc = GET_CALLER_PC();
103
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();   \
109   uptr local_stack;                           \
110   uptr sp = (uptr)&local_stack
111
112
113 #endif  // SANITIZER_STACKTRACE_H