Fix JDWP class-only modifier.

Fix Dbg::MatchType which is used for class-only and exception-only modifiers.

Also fix crash happening when notifying an exception. A debugger may walk the
stack of the current thread so we need to keep the instrumentation stack frame
synced with the native stack.

Bug: 11856587
Change-Id: Ibf95f8a83ce9ee640abf945e498b42cc88ea92a0
diff --git a/runtime/debugger.cc b/runtime/debugger.cc
index 52a2141..e4b8a8a 100644
--- a/runtime/debugger.cc
+++ b/runtime/debugger.cc
@@ -452,6 +452,7 @@
 
 void Dbg::StopJdwp() {
   delete gJdwpState;
+  gJdwpState = NULL;
   delete gRegistry;
   gRegistry = NULL;
 }
@@ -1113,7 +1114,7 @@
   CHECK(c1 != NULL);
   mirror::Class* c2 = DecodeClass(class_id, status);
   CHECK(c2 != NULL);
-  return c1->IsAssignableFrom(c2);
+  return c2->IsAssignableFrom(c1);
 }
 
 static JDWP::FieldId ToFieldId(const mirror::ArtField* f)
diff --git a/runtime/stack.cc b/runtime/stack.cc
index a505383..4e3fb4a 100644
--- a/runtime/stack.cc
+++ b/runtime/stack.cc
@@ -259,6 +259,7 @@
 }
 
 instrumentation::InstrumentationStackFrame& StackVisitor::GetInstrumentationStackFrame(uint32_t depth) const {
+  CHECK_LT(depth, thread_->GetInstrumentationStack()->size());
   return thread_->GetInstrumentationStack()->at(depth);
 }
 
diff --git a/runtime/thread.cc b/runtime/thread.cc
index 1add507..1e2a742 100644
--- a/runtime/thread.cc
+++ b/runtime/thread.cc
@@ -1818,6 +1818,12 @@
     self_->EndAssertNoThreadSuspension(last_no_assert_suspension_cause_);
     // Do instrumentation events after allowing thread suspension again.
     instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
+    if (!is_deoptimization_) {
+      // The debugger may suspend this thread and walk its stack. Let's do this before popping
+      // instrumentation frames.
+      instrumentation->ExceptionCaughtEvent(self_, throw_location_, catch_method, handler_dex_pc_,
+                                            exception_);
+    }
     for (size_t i = 0; i < instrumentation_frames_to_pop_; ++i) {
       // We pop the instrumentation stack here so as not to corrupt it during the stack walk.
       if (i != instrumentation_frames_to_pop_ - 1 || self_->GetInstrumentationStack()->front().method_ != catch_method) {
@@ -1825,10 +1831,7 @@
         instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
       }
     }
-    if (!is_deoptimization_) {
-      instrumentation->ExceptionCaughtEvent(self_, throw_location_, catch_method, handler_dex_pc_,
-                                            exception_);
-    } else {
+    if (is_deoptimization_) {
       // TODO: proper return value.
       self_->SetDeoptimizationShadowFrame(top_shadow_frame_);
     }