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 struct StackTrace {
     23   typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer,
     24                                      int out_size);
     25   uptr size;
     26   uptr max_size;
     27   uptr trace[kStackTraceMax];
     28   static void PrintStack(const uptr *addr, uptr size,
     29                          bool symbolize, const char *strip_file_prefix,
     30                          SymbolizeCallback symbolize_callback);
     31   void CopyTo(uptr *dst, uptr dst_size) {
     32     for (uptr i = 0; i < size && i < dst_size; i++)
     33       dst[i] = trace[i];
     34     for (uptr i = size; i < dst_size; i++)
     35       dst[i] = 0;
     36   }
     37 
     38   void CopyFrom(uptr *src, uptr src_size) {
     39     size = src_size;
     40     if (size > kStackTraceMax) size = kStackTraceMax;
     41     for (uptr i = 0; i < size; i++) {
     42       trace[i] = src[i];
     43     }
     44   }
     45 
     46   void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom);
     47   void SlowUnwindStack(uptr pc, uptr max_depth);
     48 
     49   void PopStackFrames(uptr count);
     50 
     51   static uptr GetCurrentPc();
     52   static uptr GetPreviousInstructionPc(uptr pc);
     53 
     54   static uptr CompressStack(StackTrace *stack,
     55                             u32 *compressed, uptr size);
     56   static void UncompressStack(StackTrace *stack,
     57                               u32 *compressed, uptr size);
     58 };
     59 
     60 
     61 const char *StripPathPrefix(const char *filepath,
     62                             const char *strip_file_prefix);
     63 
     64 }  // namespace __sanitizer
     65 
     66 // Use this macro if you want to print stack trace with the caller
     67 // of the current function in the top frame.
     68 #define GET_CALLER_PC_BP_SP \
     69   uptr bp = GET_CURRENT_FRAME();              \
     70   uptr pc = GET_CALLER_PC();                  \
     71   uptr local_stack;                           \
     72   uptr sp = (uptr)&local_stack
     73 
     74 // Use this macro if you want to print stack trace with the current
     75 // function in the top frame.
     76 #define GET_CURRENT_PC_BP_SP \
     77   uptr bp = GET_CURRENT_FRAME();              \
     78   uptr pc = StackTrace::GetCurrentPc();   \
     79   uptr local_stack;                           \
     80   uptr sp = (uptr)&local_stack
     81 
     82 
     83 #endif  // SANITIZER_STACKTRACE_H
     84