Home | History | Annotate | Line # | Download | only in sanitizer_common
sanitizer_stacktrace.h revision 1.3
      1 //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
      2 //
      3 // This file is distributed under the University of Illinois Open Source
      4 // License. See LICENSE.TXT for details.
      5 //
      6 //===----------------------------------------------------------------------===//
      7 //
      8 // This file is shared between AddressSanitizer and ThreadSanitizer
      9 // run-time libraries.
     10 //===----------------------------------------------------------------------===//
     11 #ifndef SANITIZER_STACKTRACE_H
     12 #define SANITIZER_STACKTRACE_H
     13 
     14 #include "sanitizer_internal_defs.h"
     15 
     16 namespace __sanitizer {
     17 
     18 static const uptr kStackTraceMax = 256;
     19 
     20 #if SANITIZER_LINUX && (defined(__aarch64__) || defined(__powerpc__) || \
     21                         defined(__powerpc64__) || defined(__sparc__) || \
     22                         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://code.google.com/p/address-sanitizer/issues/detail?id=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   uptr size;
     42 
     43   StackTrace() : trace(nullptr), size(0) {}
     44   StackTrace(const uptr *trace, uptr size) : trace(trace), size(size) {}
     45 
     46   // Prints a symbolized stacktrace, followed by an empty line.
     47   void Print() const;
     48 
     49   static bool WillUseFastUnwind(bool request_fast_unwind) {
     50     if (!SANITIZER_CAN_FAST_UNWIND)
     51       return false;
     52     else if (!SANITIZER_CAN_SLOW_UNWIND)
     53       return true;
     54     return request_fast_unwind;
     55   }
     56 
     57   static uptr GetCurrentPc();
     58   static uptr GetPreviousInstructionPc(uptr pc);
     59   typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer,
     60                                     int out_size);
     61 };
     62 
     63 // StackTrace that owns the buffer used to store the addresses.
     64 struct BufferedStackTrace : public StackTrace {
     65   uptr trace_buffer[kStackTraceMax];
     66   uptr top_frame_bp;  // Optional bp of a top frame.
     67 
     68   BufferedStackTrace() : StackTrace(trace_buffer, 0), top_frame_bp(0) {}
     69 
     70   void Init(const uptr *pcs, uptr cnt, uptr extra_top_pc = 0);
     71   void Unwind(uptr max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
     72               uptr stack_bottom, bool request_fast_unwind);
     73 
     74  private:
     75   void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
     76                        uptr max_depth);
     77   void SlowUnwindStack(uptr pc, uptr max_depth);
     78   void SlowUnwindStackWithContext(uptr pc, void *context,
     79                                   uptr max_depth);
     80   void PopStackFrames(uptr count);
     81   uptr LocatePcInTrace(uptr pc);
     82 
     83   BufferedStackTrace(const BufferedStackTrace &);
     84   void operator=(const BufferedStackTrace &);
     85 };
     86 
     87 }  // namespace __sanitizer
     88 
     89 // Use this macro if you want to print stack trace with the caller
     90 // of the current function in the top frame.
     91 #define GET_CALLER_PC_BP_SP \
     92   uptr bp = GET_CURRENT_FRAME();              \
     93   uptr pc = GET_CALLER_PC();                  \
     94   uptr local_stack;                           \
     95   uptr sp = (uptr)&local_stack
     96 
     97 #define GET_CALLER_PC_BP \
     98   uptr bp = GET_CURRENT_FRAME();              \
     99   uptr pc = GET_CALLER_PC();
    100 
    101 // Use this macro if you want to print stack trace with the current
    102 // function in the top frame.
    103 #define GET_CURRENT_PC_BP_SP \
    104   uptr bp = GET_CURRENT_FRAME();              \
    105   uptr pc = StackTrace::GetCurrentPc();   \
    106   uptr local_stack;                           \
    107   uptr sp = (uptr)&local_stack
    108 
    109 
    110 #endif  // SANITIZER_STACKTRACE_H
    111