Fix JDWP IsCollected command handling.

Update the handling of the JDWP IsCollected command for 'null' object id and
invalid object id.

We follow the RI where 'null' object id triggers an INVALID_OBEJCT error but
an invalid object id does not trigger error and return 'true' result.

Also fix handling of 'null' object id to avoid confusion with valid object id.
We used to rely on ObjectRegistry::Get returning NULL to detect null object id
(in which case we return an INVALID_OBJECT error). The issue is this method
may also return NULL for a valid object id if the corresponding object has
been collected. In this case, we must not return any JDWP error and the result
of the command should be 'true' meaning the object identified by the given id
has been collected.

Bug: 12221035
Change-Id: I013f3759bac7cf5b1e9ee3e8c0d1bb2a11f1b654
diff --git a/runtime/debugger.cc b/runtime/debugger.cc
index 4ea1366..2141997 100644
--- a/runtime/debugger.cc
+++ b/runtime/debugger.cc
@@ -847,14 +847,18 @@
 
 JDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
-  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
-  // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However
-  // the RI seems to ignore this and does not return any error in this case. Let's comply with
-  // JDWP specs here.
-  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
+  if (object_id == 0) {
+    // Null object id is invalid.
     return JDWP::ERR_INVALID_OBJECT;
   }
-  is_collected = gRegistry->IsCollected(object_id);
+  // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However
+  // the RI seems to ignore this and assume object has been collected.
+  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
+  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
+    is_collected = true;
+  } else {
+    is_collected = gRegistry->IsCollected(object_id);
+  }
   return JDWP::ERR_NONE;
 }