Home | History | Annotate | 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 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 struct StackTrace {
     33   typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer,
     34                                      int out_size);
     35   uptr top_frame_bp;
     36   uptr size;
     37   uptr trace[kStackTraceMax];
     38 
     39   // Prints a symbolized stacktrace, followed by an empty line.
     40   static void PrintStack(const uptr *addr, uptr size);
     41   void Print() const {
     42     PrintStack(trace, size);
     43   }
     44 
     45   void CopyFrom(const uptr *src, uptr src_size) {
     46     top_frame_bp = 0;
     47     size = src_size;
     48     if (size > kStackTraceMax) size = kStackTraceMax;
     49     for (uptr i = 0; i < size; i++)
     50       trace[i] = src[i];
     51   }
     52 
     53   static bool WillUseFastUnwind(bool request_fast_unwind) {
     54     // Check if fast unwind is available. Fast unwind is the only option on Mac.
     55     // It is also the only option on FreeBSD as the slow unwinding that
     56     // leverages _Unwind_Backtrace() yields the call stack of the signal's
     57     // handler and not of the code that raised the signal (as it does on Linux).
     58     if (!SANITIZER_CAN_FAST_UNWIND)
     59       return false;
     60     else if (SANITIZER_MAC != 0 || SANITIZER_FREEBSD != 0)
     61       return true;
     62     return request_fast_unwind;
     63   }
     64 
     65   void Unwind(uptr max_depth, uptr pc, uptr bp, void *context, uptr stack_top,
     66               uptr stack_bottom, bool request_fast_unwind);
     67 
     68   static uptr GetCurrentPc();
     69   static uptr GetPreviousInstructionPc(uptr pc);
     70 
     71  private:
     72   void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
     73                        uptr max_depth);
     74   void SlowUnwindStack(uptr pc, uptr max_depth);
     75   void SlowUnwindStackWithContext(uptr pc, void *context,
     76                                   uptr max_depth);
     77   void PopStackFrames(uptr count);
     78   uptr LocatePcInTrace(uptr pc);
     79 };
     80 
     81 }  // namespace __sanitizer
     82 
     83 // Use this macro if you want to print stack trace with the caller
     84 // of the current function in the top frame.
     85 #define GET_CALLER_PC_BP_SP \
     86   uptr bp = GET_CURRENT_FRAME();              \
     87   uptr pc = GET_CALLER_PC();                  \
     88   uptr local_stack;                           \
     89   uptr sp = (uptr)&local_stack
     90 
     91 // Use this macro if you want to print stack trace with the current
     92 // function in the top frame.
     93 #define GET_CURRENT_PC_BP_SP \
     94   uptr bp = GET_CURRENT_FRAME();              \
     95   uptr pc = StackTrace::GetCurrentPc();   \
     96   uptr local_stack;                           \
     97   uptr sp = (uptr)&local_stack
     98 
     99 
    100 #endif  // SANITIZER_STACKTRACE_H
    101