Home | History | Annotate | Line # | Download | only in sanitizer_common
      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 defined(__sparc__) || (SANITIZER_LINUX && defined(__mips__))
     23 # define SANITIZER_CAN_FAST_UNWIND 0
     24 #elif SANITIZER_WINDOWS
     25 # define SANITIZER_CAN_FAST_UNWIND 0
     26 #elif SANITIZER_OPENBSD
     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://github.com/google/sanitizers/issues/137
     35 #if SANITIZER_MAC || SANITIZER_OPENBSD || SANITIZER_RTEMS
     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   u32 size;
     44   u32 tag;
     45 
     46   static const int TAG_UNKNOWN = 0;
     47   static const int TAG_ALLOC = 1;
     48   static const int TAG_DEALLOC = 2;
     49   static const int TAG_CUSTOM = 100; // Tool specific tags start here.
     50 
     51   StackTrace() : trace(nullptr), size(0), tag(0) {}
     52   StackTrace(const uptr *trace, u32 size) : trace(trace), size(size), tag(0) {}
     53   StackTrace(const uptr *trace, u32 size, u32 tag)
     54       : trace(trace), size(size), tag(tag) {}
     55 
     56   // Prints a symbolized stacktrace, followed by an empty line.
     57   void Print() const;
     58 
     59   static bool WillUseFastUnwind(bool request_fast_unwind) {
     60     if (!SANITIZER_CAN_FAST_UNWIND)
     61       return false;
     62     else if (!SANITIZER_CAN_SLOW_UNWIND)
     63       return true;
     64     return request_fast_unwind;
     65   }
     66 
     67   static uptr GetCurrentPc();
     68   static inline uptr GetPreviousInstructionPc(uptr pc);
     69   static uptr GetNextInstructionPc(uptr pc);
     70   typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer,
     71                                     int out_size);
     72 };
     73 
     74 // Performance-critical, must be in the header.
     75 ALWAYS_INLINE
     76 uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
     77 #if defined(__arm__)
     78   // T32 (Thumb) branch instructions might be 16 or 32 bit long,
     79   // so we return (pc-2) in that case in order to be safe.
     80   // For A32 mode we return (pc-4) because all instructions are 32 bit long.
     81   return (pc - 3) & (~1);
     82 #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__aarch64__)
     83   // PCs are always 4 byte aligned.
     84   return pc - 4;
     85 #elif defined(__sparc__) || defined(__mips__)
     86   return pc - 8;
     87 #else
     88   return pc - 1;
     89 #endif
     90 }
     91 
     92 // StackTrace that owns the buffer used to store the addresses.
     93 struct BufferedStackTrace : public StackTrace {
     94   uptr trace_buffer[kStackTraceMax];
     95   uptr top_frame_bp;  // Optional bp of a top frame.
     96 
     97   BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
     98 
     99   void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
    100   void Unwind(u32 max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
    101               uptr stack_bottom, bool request_fast_unwind);
    102 
    103   void Reset() {
    104     *static_cast<StackTrace *>(this) = StackTrace(trace_buffer, 0);
    105     top_frame_bp = 0;
    106   }
    107 
    108  private:
    109   void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
    110                        u32 max_depth);
    111   void SlowUnwindStack(uptr pc, u32 max_depth);
    112   void SlowUnwindStackWithContext(uptr pc, void *context,
    113                                   u32 max_depth);
    114   void PopStackFrames(uptr count);
    115   uptr LocatePcInTrace(uptr pc);
    116 
    117   BufferedStackTrace(const BufferedStackTrace &) = delete;
    118   void operator=(const BufferedStackTrace &) = delete;
    119 };
    120 
    121 // Check if given pointer points into allocated stack area.
    122 static inline bool IsValidFrame(uptr frame, uptr stack_top, uptr stack_bottom) {
    123   return frame > stack_bottom && frame < stack_top - 2 * sizeof (uhwptr);
    124 }
    125 
    126 }  // namespace __sanitizer
    127 
    128 // Use this macro if you want to print stack trace with the caller
    129 // of the current function in the top frame.
    130 #define GET_CALLER_PC_BP_SP \
    131   uptr bp = GET_CURRENT_FRAME();              \
    132   uptr pc = GET_CALLER_PC();                  \
    133   uptr local_stack;                           \
    134   uptr sp = (uptr)&local_stack
    135 
    136 #define GET_CALLER_PC_BP \
    137   uptr bp = GET_CURRENT_FRAME();              \
    138   uptr pc = GET_CALLER_PC();
    139 
    140 // Use this macro if you want to print stack trace with the current
    141 // function in the top frame.
    142 #define GET_CURRENT_PC_BP_SP \
    143   uptr bp = GET_CURRENT_FRAME();              \
    144   uptr pc = StackTrace::GetCurrentPc();   \
    145   uptr local_stack;                           \
    146   uptr sp = (uptr)&local_stack
    147 
    148 
    149 #endif  // SANITIZER_STACKTRACE_H
    150