Implement the Thread.sleep native method.

This makes returning TS_SLEEPING from JDWP simple and cheap, and
makes the stack dumps for sleeping threads more easily understood
by app developers (because there's no Object.wait magic going, and
the thread state is "Sleeping" rather than "TimedWaiting").

Also make Object.wait() a native method in its own right, so every call into
Monitor::Wait can explicitly pass the most appropriate ThreadState: kSleeping,
kWaiting, or kTimedWaiting.

Also add Thread.sleep and Object.wait(long, int) calls to the ThreadStress test.

Change-Id: I49adb45dbcd669eba7cf3def45e6cbfc461a3254
diff --git a/src/debugger.cc b/src/debugger.cc
index 158be0b..5240c6c 100644
--- a/src/debugger.cc
+++ b/src/debugger.cc
@@ -1424,6 +1424,7 @@
     case kTerminated:   *pThreadStatus = JDWP::TS_ZOMBIE;   break;
     case kRunnable:     *pThreadStatus = JDWP::TS_RUNNING;  break;
     case kTimedWaiting: *pThreadStatus = JDWP::TS_WAIT;     break;
+    case kSleeping:     *pThreadStatus = JDWP::TS_SLEEPING; break;
     case kBlocked:      *pThreadStatus = JDWP::TS_MONITOR;  break;
     case kWaiting:      *pThreadStatus = JDWP::TS_WAIT;     break;
     case kStarting:     *pThreadStatus = JDWP::TS_ZOMBIE;   break;
@@ -1442,34 +1443,6 @@
     // Don't add a 'default' here so the compiler can spot incompatible enum changes.
   }
 
-  if (thread->GetState() == kTimedWaiting) {
-    // Since Thread.sleep is implemented using Object.wait, see if Thread.sleep
-    // is on the stack and change state to TS_SLEEPING if it is.
-    struct SleepMethodVisitor : public StackVisitor {
-      SleepMethodVisitor(const ManagedStack* stack,
-                         const std::deque<InstrumentationStackFrame>* instrumentation_stack)
-          SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
-          : StackVisitor(stack, instrumentation_stack, NULL), found_(false) {}
-
-      virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
-        std::string name(PrettyMethod(GetMethod(), false));
-        if (name == "java.lang.Thread.sleep") {
-          found_ = true;
-          return false;
-        }
-        return true;
-      }
-
-      bool found_;
-    };
-
-    SleepMethodVisitor visitor(thread->GetManagedStack(), thread->GetInstrumentationStack());
-    visitor.WalkStack(false);
-    if (visitor.found_) {
-      *pThreadStatus = JDWP::TS_SLEEPING;
-    }
-  }
-
   *pSuspendStatus = (thread->IsSuspended() ? JDWP::SUSPEND_STATUS_SUSPENDED : JDWP::SUSPEND_STATUS_NOT_SUSPENDED);
 
   return true;