sanitizer_stacktrace.h revision e74968cbb29c80073e4ff440555e35f3fbed2f20
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 18namespace __sanitizer { 19 20static const uptr kStackTraceMax = 256; 21 22#if SANITIZER_LINUX && (defined(__arm__) || \ 23 defined(__powerpc__) || defined(__powerpc64__) || \ 24 defined(__sparc__) || \ 25 defined(__mips__)) 26# define SANITIZER_CAN_FAST_UNWIND 0 27#elif SANITIZER_WINDOWS 28# define SANITIZER_CAN_FAST_UNWIND 0 29#else 30# define SANITIZER_CAN_FAST_UNWIND 1 31#endif 32 33struct StackTrace { 34 typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer, 35 int out_size); 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 SymbolizeCallback symbolize_callback = 0); 42 43 void CopyFrom(const uptr *src, uptr src_size) { 44 size = src_size; 45 if (size > kStackTraceMax) size = kStackTraceMax; 46 for (uptr i = 0; i < size; i++) 47 trace[i] = src[i]; 48 } 49 50 void Unwind(uptr max_depth, uptr pc, uptr bp, uptr stack_top, 51 uptr stack_bottom, bool fast); 52 // FIXME: Make FastUnwindStack and SlowUnwindStack private methods. 53 void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom, 54 uptr max_depth); 55 void SlowUnwindStack(uptr pc, uptr max_depth); 56 57 void PopStackFrames(uptr count); 58 59 static uptr GetCurrentPc(); 60 static uptr GetPreviousInstructionPc(uptr pc); 61}; 62 63} // namespace __sanitizer 64 65// Use this macro if you want to print stack trace with the caller 66// of the current function in the top frame. 67#define GET_CALLER_PC_BP_SP \ 68 uptr bp = GET_CURRENT_FRAME(); \ 69 uptr pc = GET_CALLER_PC(); \ 70 uptr local_stack; \ 71 uptr sp = (uptr)&local_stack 72 73// Use this macro if you want to print stack trace with the current 74// function in the top frame. 75#define GET_CURRENT_PC_BP_SP \ 76 uptr bp = GET_CURRENT_FRAME(); \ 77 uptr pc = StackTrace::GetCurrentPc(); \ 78 uptr local_stack; \ 79 uptr sp = (uptr)&local_stack 80 81 82#endif // SANITIZER_STACKTRACE_H 83