[asan] more refactoring to move StackTrace to sanitizer_common

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@162752 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_linux.cc b/lib/asan/asan_linux.cc
index bbc7dad..5c52ddc 100644
--- a/lib/asan/asan_linux.cc
+++ b/lib/asan/asan_linux.cc
@@ -17,6 +17,7 @@
 #include "asan_internal.h"
 #include "asan_lock.h"
 #include "asan_thread.h"
+#include "asan_thread_registry.h"
 #include "sanitizer_common/sanitizer_libc.h"
 #include "sanitizer_common/sanitizer_procmaps.h"
 
@@ -131,15 +132,17 @@
   return UNWIND_CONTINUE;
 }
 
-void StackTrace::GetStackTrace(uptr max_s, uptr pc, uptr bp) {
-  size = 0;
-  trace[0] = pc;
+void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp) {
+  stack->size = 0;
+  stack->trace[0] = pc;
   if ((max_s) > 1) {
-    max_size = max_s;
+    stack->max_size = max_s;
 #ifdef __arm__
-    _Unwind_Backtrace(Unwind_Trace, this);
+    _Unwind_Backtrace(Unwind_Trace, stack);
 #else
-     FastUnwindStack(pc, bp);
+    if (!asan_inited) return;
+    if (AsanThread *t = asanThreadRegistry().GetCurrent())
+      stack->FastUnwindStack(pc, bp, t->stack_top(), t->stack_bottom());
 #endif
   }
 }
diff --git a/lib/asan/asan_mac.cc b/lib/asan/asan_mac.cc
index d7fc6c4..a96117d 100644
--- a/lib/asan/asan_mac.cc
+++ b/lib/asan/asan_mac.cc
@@ -152,12 +152,14 @@
   OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
 }
 
-void StackTrace::GetStackTrace(uptr max_s, uptr pc, uptr bp) {
-  size = 0;
-  trace[0] = pc;
+void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp) {
+  stack->size = 0;
+  stack->trace[0] = pc;
   if ((max_s) > 1) {
-    max_size = max_s;
-    FastUnwindStack(pc, bp);
+    stack->max_size = max_s;
+    if (!asan_inited) return;
+    if (AsanThread *t = asanThreadRegistry().GetCurrent())
+      stack->FastUnwindStack(pc, bp, t->stack_top(), t->stack_bottom());
   }
 }
 
diff --git a/lib/asan/asan_stack.cc b/lib/asan/asan_stack.cc
index 08fdb42..618fc32 100644
--- a/lib/asan/asan_stack.cc
+++ b/lib/asan/asan_stack.cc
@@ -11,12 +11,8 @@
 //
 // Code for ASan stack trace.
 //===----------------------------------------------------------------------===//
-#include "asan_interceptors.h"
 #include "asan_interface.h"
-#include "asan_lock.h"
 #include "asan_stack.h"
-#include "asan_thread.h"
-#include "asan_thread_registry.h"
 #include "sanitizer_common/sanitizer_procmaps.h"
 #include "sanitizer_common/sanitizer_symbolizer.h"
 
@@ -100,19 +96,15 @@
   return GET_CALLER_PC();
 }
 
-void StackTrace::FastUnwindStack(uptr pc, uptr bp) {
+void StackTrace::FastUnwindStack(uptr pc, uptr bp,
+                                 uptr stack_top, uptr stack_bottom) {
   CHECK(size == 0 && trace[0] == pc);
   size = 1;
-  if (!asan_inited) return;
-  AsanThread *t = asanThreadRegistry().GetCurrent();
-  if (!t) return;
   uptr *frame = (uptr*)bp;
   uptr *prev_frame = frame;
-  uptr *top = (uptr*)t->stack_top();
-  uptr *bottom = (uptr*)t->stack_bottom();
   while (frame >= prev_frame &&
-         frame < top - 2 &&
-         frame > bottom &&
+         frame < (uptr*)stack_top - 2 &&
+         frame > (uptr*)stack_bottom &&
          size < max_size) {
     uptr pc1 = frame[1];
     if (pc1 != pc) {
diff --git a/lib/asan/asan_stack.h b/lib/asan/asan_stack.h
index df1f246..0d483b9 100644
--- a/lib/asan/asan_stack.h
+++ b/lib/asan/asan_stack.h
@@ -43,9 +43,7 @@
     }
   }
 
-  void GetStackTrace(uptr max_s, uptr pc, uptr bp);
-
-  void FastUnwindStack(uptr pc, uptr bp);
+  void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom);
 
   static uptr GetCurrentPc();
 
@@ -55,6 +53,10 @@
                               u32 *compressed, uptr size);
 };
 
+void GetStackTrace(StackTrace *trace, uptr max_s, uptr pc, uptr bp);
+
+
+
 }  // namespace __asan
 
 // Use this macro if you want to print stack trace with the caller
@@ -79,7 +81,7 @@
 // fast_unwind is currently unused.
 #define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp)               \
   StackTrace stack;                                             \
-  stack.GetStackTrace(max_s, pc, bp)
+  GetStackTrace(&stack, max_s, pc, bp)
 
 // NOTE: A Rule of thumb is to retrieve stack trace in the interceptors
 // as early as possible (in functions exposed to the user), as we generally