Dump native stacks for all threads in native code.

art port of dalvik change 890ce010c4846deb82d3ac09b6d2ceb76e59fb67.

Bug: 7432159
Change-Id: Iea1cb9d60fee2ca197f056116836408675b076e6
diff --git a/src/thread.cc b/src/thread.cc
index ff0cd58..9c58b6d 100644
--- a/src/thread.cc
+++ b/src/thread.cc
@@ -847,9 +847,30 @@
   int frame_count;
 };
 
+static bool ShouldShowNativeStack(const Thread* thread) {
+  ThreadState state = thread->GetState();
+
+  // In native code somewhere in the VM (one of the kWaitingFor* states)? That's interesting.
+  if (state > kWaiting && state < kStarting) {
+    return true;
+  }
+
+  // In an Object.wait variant or Thread.sleep? That's not interesting.
+  if (state == kTimedWaiting || state == kSleeping || state == kWaiting) {
+    return false;
+  }
+
+  // In some other native method? That's interesting.
+  // We don't just check kNative because native methods will be in state kSuspended if they're
+  // calling back into the VM, or kBlocked if they're blocked on a monitor, or one of the
+  // thread-startup states if it's early enough in their life cycle (http://b/7432159).
+  mirror::AbstractMethod* current_method = thread->GetCurrentMethod();
+  return current_method != NULL && current_method->IsNative();
+}
+
 void Thread::DumpStack(std::ostream& os) const {
   // If we're currently in native code, dump that stack before dumping the managed stack.
-  if (GetState() == kNative) {
+  if (ShouldShowNativeStack(this)) {
     DumpKernelStack(os, GetTid(), "  kernel: ", false);
     DumpNativeStack(os, GetTid(), "  native: ", false);
   }