Add root verification when we try to mark an invalid object.

Now when we try to mark an object not contained by any spaces, We call verify
roots. This prints the root's vreg and method when it finds an invalid root.

Fixed a error in the total paused time statistic.

Change-Id: Id10e4097cce56bc54ee488de32183c18ba3f3780
diff --git a/src/gc/mark_sweep.cc b/src/gc/mark_sweep.cc
index e19e2c4..03bbb6a 100644
--- a/src/gc/mark_sweep.cc
+++ b/src/gc/mark_sweep.cc
@@ -35,6 +35,7 @@
 #include "space.h"
 #include "timing_logger.h"
 #include "thread.h"
+#include "thread_list.h"
 
 static const bool kUseMarkStackPrefetch = true;
 
@@ -111,8 +112,12 @@
       LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
       SpaceSetMap* large_objects = large_object_space->GetMarkObjects();
       if (!large_objects->Test(obj)) {
-        CHECK(large_object_space->Contains(obj)) << "Attempting to mark object " << obj
-                                                  << " not in large object space";
+        if (!large_object_space->Contains(obj)) {
+          LOG(ERROR) << "Tried to mark " << obj << " not contained by any spaces";
+          LOG(ERROR) << "Attempting see if it's a bad root";
+          VerifyRoots();
+          LOG(FATAL) << "Can't mark bad root";
+        }
         large_objects->Set(obj);
         // Don't need to check finger since large objects never have any object references.
       }
@@ -165,6 +170,29 @@
   mark_sweep->MarkObject0(root, true);
 }
 
+void MarkSweep::VerifyRootCallback(const Object* root, void* arg, size_t vreg,
+                                   const AbstractMethod* method) {
+  reinterpret_cast<MarkSweep*>(arg)->VerifyRoot(root, vreg, method);
+}
+
+void MarkSweep::VerifyRoot(const Object* root, size_t vreg, const AbstractMethod* method) {
+  // See if the root is on any space bitmap.
+  if (heap_->FindSpaceFromObject(root) == NULL) {
+    LargeObjectSpace* large_object_space = GetHeap()->GetLargeObjectsSpace();
+    if (large_object_space->Contains(root)) {
+      LOG(ERROR) << "Found invalid root: " << root;
+      LOG(ERROR) << "VReg / Shadow frame offset: " << vreg;
+      if (method != NULL) {
+        LOG(ERROR) << "In method " << PrettyMethod(method, true);
+      }
+    }
+  }
+}
+
+void MarkSweep::VerifyRoots() {
+  Runtime::Current()->GetThreadList()->VerifyRoots(VerifyRootCallback, this);
+}
+
 // Marks all objects in the root set.
 void MarkSweep::MarkRoots() {
   Runtime::Current()->VisitRoots(MarkObjectVisitor, this);